1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/startuplock/CLIHandlerBlocking.java Sat Jun 14 09:54:31 2008 +0200
1.3 @@ -0,0 +1,47 @@
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 +import java.util.logging.Level;
1.12 +import java.util.logging.Logger;
1.13 +
1.14 +public final class CLIHandlerBlocking {
1.15 + // BEGIN: cli.real
1.16 + public static int start(File lockFile) throws IOException {
1.17 + enterState(10);
1.18 + if (lockFile.exists ()) {
1.19 + // read the port number and connect to it
1.20 + enterState(11);
1.21 + int alive = readPortNumber(lockFile);
1.22 + if (alive != -1) {
1.23 + // exit
1.24 + return alive;
1.25 + }
1.26 + }
1.27 + // otherwise try to create the file yourself
1.28 + enterState(20);
1.29 + lockFile.createNewFile();
1.30 + DataOutputStream os = new DataOutputStream(new FileOutputStream(lockFile));
1.31 + ServerSocket server = new ServerSocket();
1.32 + enterState(21);
1.33 + int p = server.getLocalPort();
1.34 + enterState(22);
1.35 + os.writeInt(p);
1.36 + enterState(23);
1.37 +
1.38 + return p;
1.39 + }
1.40 + // END: cli.real
1.41 +
1.42 + private static Logger LOG = Logger.getLogger(CLIHandlerBlocking.class.getName());
1.43 + private static void enterState(int state) {
1.44 + LOG.log(Level.FINEST, "enterState {0}", state);
1.45 + }
1.46 +
1.47 + private static int readPortNumber(File lockFile) throws IOException {
1.48 + return -1;
1.49 + }
1.50 +}