samples/deadlock/src/org/apidesign/deadlock/startuplock/CLIHandler.java
changeset 106 a773f1bc5ba1
child 133 50bf1b976c0d
     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 +}