author | Jaroslav Tulach <jtulach@netbeans.org> |
Sat, 14 Jun 2008 09:58:11 +0200 | |
changeset 154 | 0fd5e9c500b9 |
parent 153 | b5cbb797ec0a |
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@154 | 22 |
DataOutputStream os = new DataOutputStream( |
jtulach@154 | 23 |
new FileOutputStream(lockFile) |
jtulach@154 | 24 |
); |
jtulach@106 | 25 |
ServerSocket server = new ServerSocket(); |
jtulach@106 | 26 |
int p = server.getLocalPort(); |
jtulach@106 | 27 |
os.writeInt(p); |
jtulach@106 | 28 |
|
jtulach@106 | 29 |
return p; |
jtulach@106 | 30 |
} |
jtulach@106 | 31 |
// END: cli.vision |
jtulach@106 | 32 |
|
jtulach@106 | 33 |
private static int readPortNumber(File lockFile) throws IOException { |
jtulach@106 | 34 |
return -1; |
jtulach@106 | 35 |
} |
jtulach@106 | 36 |
} |