author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:54:31 +0200 | |
changeset 106 | a773f1bc5ba1 |
child 133 | 50bf1b976c0d |
permissions | -rw-r--r-- |
jtulach@106 | 1 |
package org.apidesign.deadlock.startuplock; |
jtulach@106 | 2 |
|
jtulach@106 | 3 |
import java.io.DataOutputStream; |
jtulach@106 | 4 |
import java.io.File; |
jtulach@106 | 5 |
import java.io.FileOutputStream; |
jtulach@106 | 6 |
import java.io.IOException; |
jtulach@106 | 7 |
import java.net.ServerSocket; |
jtulach@106 | 8 |
|
jtulach@106 | 9 |
public final class CLIHandler { |
jtulach@106 | 10 |
// BEGIN: cli.vision |
jtulach@106 | 11 |
public static int start(File lockFile) throws IOException { |
jtulach@106 | 12 |
if (lockFile.exists ()) { |
jtulach@106 | 13 |
// read the port number and connect to it |
jtulach@106 | 14 |
int alive = readPortNumber(lockFile); |
jtulach@106 | 15 |
if (alive != -1) { |
jtulach@106 | 16 |
// exit |
jtulach@106 | 17 |
return alive; |
jtulach@106 | 18 |
} |
jtulach@106 | 19 |
} |
jtulach@106 | 20 |
// otherwise try to create the file yourself |
jtulach@106 | 21 |
lockFile.createNewFile(); |
jtulach@106 | 22 |
DataOutputStream os = new DataOutputStream(new FileOutputStream(lockFile)); |
jtulach@106 | 23 |
ServerSocket server = new ServerSocket(); |
jtulach@106 | 24 |
int p = server.getLocalPort(); |
jtulach@106 | 25 |
os.writeInt(p); |
jtulach@106 | 26 |
|
jtulach@106 | 27 |
return p; |
jtulach@106 | 28 |
} |
jtulach@106 | 29 |
// END: cli.vision |
jtulach@106 | 30 |
|
jtulach@106 | 31 |
private static int readPortNumber(File lockFile) throws IOException { |
jtulach@106 | 32 |
return -1; |
jtulach@106 | 33 |
} |
jtulach@106 | 34 |
} |