1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/startuplock/CLIHandler.java Sat Jun 14 09:54:31 2008 +0200
1.3 @@ -0,0 +1,34 @@
1.4 +package org.apidesign.deadlock.startuplock;
1.5 +
1.6 +import java.io.DataOutputStream;
1.7 +import java.io.File;
1.8 +import java.io.FileOutputStream;
1.9 +import java.io.IOException;
1.10 +import java.net.ServerSocket;
1.11 +
1.12 +public final class CLIHandler {
1.13 + // BEGIN: cli.vision
1.14 + public static int start(File lockFile) throws IOException {
1.15 + if (lockFile.exists ()) {
1.16 + // read the port number and connect to it
1.17 + int alive = readPortNumber(lockFile);
1.18 + if (alive != -1) {
1.19 + // exit
1.20 + return alive;
1.21 + }
1.22 + }
1.23 + // otherwise try to create the file yourself
1.24 + lockFile.createNewFile();
1.25 + DataOutputStream os = new DataOutputStream(new FileOutputStream(lockFile));
1.26 + ServerSocket server = new ServerSocket();
1.27 + int p = server.getLocalPort();
1.28 + os.writeInt(p);
1.29 +
1.30 + return p;
1.31 + }
1.32 + // END: cli.vision
1.33 +
1.34 + private static int readPortNumber(File lockFile) throws IOException {
1.35 + return -1;
1.36 + }
1.37 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/startuplock/CLIHandlerBlocking.java Sat Jun 14 09:54:31 2008 +0200
2.3 @@ -0,0 +1,47 @@
2.4 +package org.apidesign.deadlock.startuplock;
2.5 +
2.6 +import java.io.DataOutputStream;
2.7 +import java.io.File;
2.8 +import java.io.FileOutputStream;
2.9 +import java.io.IOException;
2.10 +import java.net.ServerSocket;
2.11 +import java.util.logging.Level;
2.12 +import java.util.logging.Logger;
2.13 +
2.14 +public final class CLIHandlerBlocking {
2.15 + // BEGIN: cli.real
2.16 + public static int start(File lockFile) throws IOException {
2.17 + enterState(10);
2.18 + if (lockFile.exists ()) {
2.19 + // read the port number and connect to it
2.20 + enterState(11);
2.21 + int alive = readPortNumber(lockFile);
2.22 + if (alive != -1) {
2.23 + // exit
2.24 + return alive;
2.25 + }
2.26 + }
2.27 + // otherwise try to create the file yourself
2.28 + enterState(20);
2.29 + lockFile.createNewFile();
2.30 + DataOutputStream os = new DataOutputStream(new FileOutputStream(lockFile));
2.31 + ServerSocket server = new ServerSocket();
2.32 + enterState(21);
2.33 + int p = server.getLocalPort();
2.34 + enterState(22);
2.35 + os.writeInt(p);
2.36 + enterState(23);
2.37 +
2.38 + return p;
2.39 + }
2.40 + // END: cli.real
2.41 +
2.42 + private static Logger LOG = Logger.getLogger(CLIHandlerBlocking.class.getName());
2.43 + private static void enterState(int state) {
2.44 + LOG.log(Level.FINEST, "enterState {0}", state);
2.45 + }
2.46 +
2.47 + private static int readPortNumber(File lockFile) throws IOException {
2.48 + return -1;
2.49 + }
2.50 +}