jtulach@106: package org.apidesign.deadlock.startuplock; jtulach@106: jtulach@106: import java.io.DataOutputStream; jtulach@106: import java.io.File; jtulach@106: import java.io.FileOutputStream; jtulach@106: import java.io.IOException; jtulach@106: import java.net.ServerSocket; jtulach@106: import java.util.logging.Level; jtulach@106: import java.util.logging.Logger; jtulach@106: jtulach@106: public final class CLIHandlerBlocking { jtulach@106: // BEGIN: cli.real jtulach@106: public static int start(File lockFile) throws IOException { jtulach@106: enterState(10); jtulach@106: if (lockFile.exists ()) { jtulach@106: // read the port number and connect to it jtulach@106: enterState(11); jtulach@106: int alive = readPortNumber(lockFile); jtulach@106: if (alive != -1) { jtulach@106: // exit jtulach@106: return alive; jtulach@106: } jtulach@106: } jtulach@106: // otherwise try to create the file yourself jtulach@106: enterState(20); jtulach@106: lockFile.createNewFile(); jtulach@106: DataOutputStream os = new DataOutputStream(new FileOutputStream(lockFile)); jtulach@106: ServerSocket server = new ServerSocket(); jtulach@106: enterState(21); jtulach@106: int p = server.getLocalPort(); jtulach@106: enterState(22); jtulach@106: os.writeInt(p); jtulach@106: enterState(23); jtulach@106: jtulach@106: return p; jtulach@106: } jtulach@106: // END: cli.real jtulach@106: jtulach@106: private static Logger LOG = Logger.getLogger(CLIHandlerBlocking.class.getName()); jtulach@106: private static void enterState(int state) { jtulach@106: LOG.log(Level.FINEST, "enterState {0}", state); jtulach@106: } jtulach@106: jtulach@106: private static int readPortNumber(File lockFile) throws IOException { jtulach@106: return -1; jtulach@106: } jtulach@106: }