langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/PortFile.java
changeset 26107 a4a156a33c94
parent 26104 34de7e01b3c1
parent 25874 83c19f00452c
child 27383 716ed9a6d607
equal deleted inserted replaced
25876:d06a6d3c66c0 26107:a4a156a33c94
    31 import java.io.RandomAccessFile;
    31 import java.io.RandomAccessFile;
    32 import java.nio.channels.ClosedChannelException;
    32 import java.nio.channels.ClosedChannelException;
    33 import java.nio.channels.FileChannel;
    33 import java.nio.channels.FileChannel;
    34 import java.nio.channels.FileLock;
    34 import java.nio.channels.FileLock;
    35 import java.nio.channels.FileLockInterruptionException;
    35 import java.nio.channels.FileLockInterruptionException;
       
    36 import com.sun.tools.javac.util.Assert;
    36 import com.sun.tools.sjavac.Log;
    37 import com.sun.tools.sjavac.Log;
    37 
    38 
    38 /**
    39 /**
    39  * The PortFile class mediates access to a short binary file containing the tcp/ip port (for the localhost)
    40  * The PortFile class mediates access to a short binary file containing the tcp/ip port (for the localhost)
    40  * and a cookie necessary for the server answering on that port. The file can be locked using file system
    41  * and a cookie necessary for the server answering on that port. The file can be locked using file system
    41  * primitives to avoid race conditions when several javac clients are started at the same. Note that file
    42  * primitives to avoid race conditions when several javac clients are started at the same. Note that file
    42  * system locking is not always supported on a all operating systems and/or file systems.
    43  * system locking is not always supported on a all operating systems and/or file systems.
    43  *
    44  *
    44  * <p><b>This is NOT part of any supported API.
    45  *  <p><b>This is NOT part of any supported API.
    45  * If you write code that depends on this, you do so at your own
    46  *  If you write code that depends on this, you do so at your own risk.
    46  * risk.  This code and its internal interfaces are subject to change
    47  *  This code and its internal interfaces are subject to change or
    47  * or deletion without notice.</b></p>
    48  *  deletion without notice.</b>
    48  */
    49  */
    49 class PortFile {
    50 public class PortFile {
    50 
    51 
    51     // Port file format:
    52     // Port file format:
    52     // byte ordering: high byte first = big endian
    53     // byte ordering: high byte first = big endian
    53     // Magic nr, 4 byte int, first in file.
    54     // Magic nr, 4 byte int, first in file.
    54     private final static int magicNr = 0x1174;
    55     private final static int magicNr = 0x1174;
    70 
    71 
    71     /**
    72     /**
    72      * Create a new portfile.
    73      * Create a new portfile.
    73      * @param filename is the path to the file.
    74      * @param filename is the path to the file.
    74      */
    75      */
    75     public PortFile(String fn) throws FileNotFoundException
    76     public PortFile(String fn) throws FileNotFoundException {
    76     {
       
    77         filename = fn;
    77         filename = fn;
    78         file = new File(filename);
    78         file = new File(filename);
    79         stopFile = new File(filename+".stop");
    79         stopFile = new File(filename+".stop");
    80         rwfile = new RandomAccessFile(file, "rw");
    80         rwfile = new RandomAccessFile(file, "rw");
    81         // The rwfile should only be readable by the owner of the process
    81         // The rwfile should only be readable by the owner of the process
    86     }
    86     }
    87 
    87 
    88     /**
    88     /**
    89      * Lock the port file.
    89      * Lock the port file.
    90      */
    90      */
    91     void lock() throws IOException {
    91     public void lock() throws IOException {
    92         lock = channel.lock();
    92         lock = channel.lock();
    93     }
    93     }
    94 
    94 
    95     /**
    95     /**
    96      * Read the values from the port file in the file system.
    96      * Read the values from the port file in the file system.
   113                     containsPortInfo = true;
   113                     containsPortInfo = true;
   114                 } else {
   114                 } else {
   115                     containsPortInfo = false;
   115                     containsPortInfo = false;
   116                 }
   116                 }
   117             }
   117             }
   118         } catch (Exception e) {
   118         } catch (IOException e) {
   119             containsPortInfo = false;
   119             containsPortInfo = false;
   120         }
   120         }
   121     }
   121     }
   122 
   122 
   123     /**
   123     /**
   129 
   129 
   130     /**
   130     /**
   131      * If so, then we can acquire the tcp/ip port on localhost.
   131      * If so, then we can acquire the tcp/ip port on localhost.
   132      */
   132      */
   133     public int getPort() {
   133     public int getPort() {
   134         assert(containsPortInfo);
   134         Assert.check(containsPortInfo);
   135         return serverPort;
   135         return serverPort;
   136     }
   136     }
   137 
   137 
   138     /**
   138     /**
   139      * If so, then we can acquire the server cookie.
   139      * If so, then we can acquire the server cookie.
   140      */
   140      */
   141     public long getCookie() {
   141     public long getCookie() {
   142         assert(containsPortInfo);
   142         Assert.check(containsPortInfo);
   143         return serverCookie;
   143         return serverCookie;
   144     }
   144     }
   145 
   145 
   146     /**
   146     /**
   147      * Store the values into the locked port file.
   147      * Store the values into the locked port file.
   148      */
   148      */
   149     public void setValues(int port, long cookie) throws IOException {
   149     public void setValues(int port, long cookie) throws IOException {
   150         assert(lock != null);
   150         Assert.check(lock != null);
   151         rwfile.seek(0);
   151         rwfile.seek(0);
   152         // Write the magic nr that identifes a port file.
   152         // Write the magic nr that identifes a port file.
   153         rwfile.writeInt(magicNr);
   153         rwfile.writeInt(magicNr);
   154         rwfile.writeInt(port);
   154         rwfile.writeInt(port);
   155         rwfile.writeLong(cookie);
   155         rwfile.writeLong(cookie);
   190 
   190 
   191     /**
   191     /**
   192      * Unlock the port file.
   192      * Unlock the port file.
   193      */
   193      */
   194     public void unlock() throws IOException {
   194     public void unlock() throws IOException {
   195         assert(lock != null);
   195         Assert.check(lock != null);
   196         lock.release();
   196         lock.release();
   197         lock = null;
   197         lock = null;
   198     }
   198     }
   199 
   199 
   200     /**
   200     /**