samples/deadlock/src/org/apidesign/deadlock/startuplock/CLIHandler.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:11 +0200
changeset 154 0fd5e9c500b9
parent 153 b5cbb797ec0a
permissions -rw-r--r--
Merge: Geertjan's changs up to 2000
     1 package org.apidesign.deadlock.startuplock;
     2 
     3 import java.io.DataOutputStream;
     4 import java.io.File;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.net.ServerSocket;
     8 
     9 public final class CLIHandler {
    10     // BEGIN: cli.vision
    11     public static int start(File lockFile) throws IOException {
    12         if (lockFile.exists ()) {
    13             // read the port number and connect to it
    14             int alive = readPortNumber(lockFile);
    15             if (alive != -1) {
    16                 // exit
    17                 return alive;
    18             }
    19         }
    20         // otherwise try to create the file yourself
    21         lockFile.createNewFile();
    22         DataOutputStream os = new DataOutputStream(
    23             new FileOutputStream(lockFile)
    24         );
    25         ServerSocket server = new ServerSocket();
    26         int p = server.getLocalPort();
    27         os.writeInt(p);
    28         
    29         return p;
    30     }
    31     // END: cli.vision
    32 
    33     private static int readPortNumber(File lockFile) throws IOException {
    34         return -1;
    35     }
    36 }