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 |
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); |