src/share/classes/com/sun/tools/sjavac/server/PortFile.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1504
22e417cdddee
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

ohrstrom@1504 1 /*
ksrini@2227 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
ohrstrom@1504 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohrstrom@1504 4 *
ohrstrom@1504 5 * This code is free software; you can redistribute it and/or modify it
ohrstrom@1504 6 * under the terms of the GNU General Public License version 2 only, as
ohrstrom@1504 7 * published by the Free Software Foundation. Oracle designates this
ohrstrom@1504 8 * particular file as subject to the "Classpath" exception as provided
ohrstrom@1504 9 * by Oracle in the LICENSE file that accompanied this code.
ohrstrom@1504 10 *
ohrstrom@1504 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohrstrom@1504 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohrstrom@1504 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohrstrom@1504 14 * version 2 for more details (a copy is included in the LICENSE file that
ohrstrom@1504 15 * accompanied this code).
ohrstrom@1504 16 *
ohrstrom@1504 17 * You should have received a copy of the GNU General Public License version
ohrstrom@1504 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohrstrom@1504 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohrstrom@1504 20 *
ohrstrom@1504 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohrstrom@1504 22 * or visit www.oracle.com if you need additional information or have any
ohrstrom@1504 23 * questions.
ohrstrom@1504 24 */
ohrstrom@1504 25
ohrstrom@1504 26 package com.sun.tools.sjavac.server;
ohrstrom@1504 27
ohrstrom@1504 28 import java.io.File;
ohrstrom@1504 29 import java.io.FileNotFoundException;
ohrstrom@1504 30 import java.io.IOException;
ohrstrom@1504 31 import java.io.RandomAccessFile;
ohrstrom@1504 32 import java.nio.channels.ClosedChannelException;
ohrstrom@1504 33 import java.nio.channels.FileChannel;
ohrstrom@1504 34 import java.nio.channels.FileLock;
ohrstrom@1504 35 import java.nio.channels.FileLockInterruptionException;
ohrstrom@1504 36 import com.sun.tools.sjavac.Log;
ohrstrom@1504 37
ohrstrom@1504 38 /**
ohrstrom@1504 39 * The PortFile class mediates access to a short binary file containing the tcp/ip port (for the localhost)
ohrstrom@1504 40 * and a cookie necessary for the server answering on that port. The file can be locked using file system
ohrstrom@1504 41 * primitives to avoid race conditions when several javac clients are started at the same. Note that file
ohrstrom@1504 42 * system locking is not always supported on a all operating systems and/or file systems.
ohrstrom@1504 43 *
ohrstrom@1504 44 * <p><b>This is NOT part of any supported API.
ohrstrom@1504 45 * If you write code that depends on this, you do so at your own
ohrstrom@1504 46 * risk. This code and its internal interfaces are subject to change
ohrstrom@1504 47 * or deletion without notice.</b></p>
ohrstrom@1504 48 */
ohrstrom@1504 49 class PortFile {
ohrstrom@1504 50
ohrstrom@1504 51 // Port file format:
ohrstrom@1504 52 // byte ordering: high byte first = big endian
ohrstrom@1504 53 // Magic nr, 4 byte int, first in file.
ohrstrom@1504 54 private final static int magicNr = 0x1174;
ohrstrom@1504 55 // Followed by a 4 byte int, with the port nr.
ohrstrom@1504 56 // Followed by a 8 byte long, with cookie nr.
ohrstrom@1504 57
ohrstrom@1504 58 private String filename;
ohrstrom@1504 59 private File file;
ohrstrom@1504 60 private File stopFile;
ohrstrom@1504 61 private RandomAccessFile rwfile;
ohrstrom@1504 62 private FileChannel channel;
ohrstrom@1504 63 private FileLock lock;
ohrstrom@1504 64
ohrstrom@1504 65 private boolean containsPortInfo;
ohrstrom@1504 66 private int serverPort;
ohrstrom@1504 67 private long serverCookie;
ohrstrom@1504 68 private int myServerPort;
ohrstrom@1504 69 private long myServerCookie;
ohrstrom@1504 70
ohrstrom@1504 71 /**
ohrstrom@1504 72 * Create a new portfile.
ohrstrom@1504 73 * @param filename is the path to the file.
ohrstrom@1504 74 */
ohrstrom@1504 75 public PortFile(String fn) throws FileNotFoundException
ohrstrom@1504 76 {
ohrstrom@1504 77 filename = fn;
ohrstrom@1504 78 file = new File(filename);
ohrstrom@1504 79 stopFile = new File(filename+".stop");
ohrstrom@1504 80 rwfile = new RandomAccessFile(file, "rw");
ohrstrom@1504 81 // The rwfile should only be readable by the owner of the process
ohrstrom@1504 82 // and no other! How do we do that on a RandomAccessFile?
ohrstrom@1504 83 channel = rwfile.getChannel();
ohrstrom@1504 84 containsPortInfo = false;
ohrstrom@1504 85 lock = null;
ohrstrom@1504 86 }
ohrstrom@1504 87
ohrstrom@1504 88 /**
ohrstrom@1504 89 * Lock the port file.
ohrstrom@1504 90 */
ohrstrom@1504 91 void lock() throws IOException {
ohrstrom@1504 92 lock = channel.lock();
ohrstrom@1504 93 }
ohrstrom@1504 94
ohrstrom@1504 95 /**
ohrstrom@1504 96 * Read the values from the port file in the file system.
ohrstrom@1504 97 * Expects the port file to be locked.
ohrstrom@1504 98 */
ohrstrom@1504 99 public void getValues() {
ohrstrom@1504 100 containsPortInfo = false;
ohrstrom@1504 101 if (lock == null) {
ohrstrom@1504 102 // Not locked, remain ignorant about port file contents.
ohrstrom@1504 103 return;
ohrstrom@1504 104 }
ohrstrom@1504 105 try {
ohrstrom@1504 106 if (rwfile.length()>0) {
ohrstrom@1504 107 rwfile.seek(0);
ohrstrom@1504 108 int nr = rwfile.readInt();
ohrstrom@1504 109 serverPort = rwfile.readInt();
ohrstrom@1504 110 serverCookie = rwfile.readLong();
ohrstrom@1504 111
ohrstrom@1504 112 if (nr == magicNr) {
ohrstrom@1504 113 containsPortInfo = true;
ohrstrom@1504 114 } else {
ohrstrom@1504 115 containsPortInfo = false;
ohrstrom@1504 116 }
ohrstrom@1504 117 }
ohrstrom@1504 118 } catch (Exception e) {
ohrstrom@1504 119 containsPortInfo = false;
ohrstrom@1504 120 }
ohrstrom@1504 121 }
ohrstrom@1504 122
ohrstrom@1504 123 /**
ohrstrom@1504 124 * Did the locking and getValues succeed?
ohrstrom@1504 125 */
ohrstrom@1504 126 public boolean containsPortInfo() {
ohrstrom@1504 127 return containsPortInfo;
ohrstrom@1504 128 }
ohrstrom@1504 129
ohrstrom@1504 130 /**
ohrstrom@1504 131 * If so, then we can acquire the tcp/ip port on localhost.
ohrstrom@1504 132 */
ohrstrom@1504 133 public int getPort() {
ohrstrom@1504 134 assert(containsPortInfo);
ohrstrom@1504 135 return serverPort;
ohrstrom@1504 136 }
ohrstrom@1504 137
ohrstrom@1504 138 /**
ohrstrom@1504 139 * If so, then we can acquire the server cookie.
ohrstrom@1504 140 */
ohrstrom@1504 141 public long getCookie() {
ohrstrom@1504 142 assert(containsPortInfo);
ohrstrom@1504 143 return serverCookie;
ohrstrom@1504 144 }
ohrstrom@1504 145
ohrstrom@1504 146 /**
ohrstrom@1504 147 * Store the values into the locked port file.
ohrstrom@1504 148 */
ohrstrom@1504 149 public void setValues(int port, long cookie) throws IOException {
ohrstrom@1504 150 assert(lock != null);
ohrstrom@1504 151 rwfile.seek(0);
ohrstrom@1504 152 // Write the magic nr that identifes a port file.
ohrstrom@1504 153 rwfile.writeInt(magicNr);
ohrstrom@1504 154 rwfile.writeInt(port);
ohrstrom@1504 155 rwfile.writeLong(cookie);
ohrstrom@1504 156 myServerPort = port;
ohrstrom@1504 157 myServerCookie = cookie;
ohrstrom@1504 158 }
ohrstrom@1504 159
ohrstrom@1504 160 /**
ohrstrom@1504 161 * Delete the port file.
ohrstrom@1504 162 */
ohrstrom@1504 163 public void delete() throws IOException {
ohrstrom@1504 164 // Access to file must be closed before deleting.
ohrstrom@1504 165 rwfile.close();
ohrstrom@1504 166 // Now delete.
ohrstrom@1504 167 file.delete();
ohrstrom@1504 168 }
ohrstrom@1504 169
ohrstrom@1504 170 /**
ohrstrom@1504 171 * Is the port file still there?
ohrstrom@1504 172 */
ohrstrom@1504 173 public boolean exists() throws IOException {
ohrstrom@1504 174 return file.exists();
ohrstrom@1504 175 }
ohrstrom@1504 176
ohrstrom@1504 177 /**
ohrstrom@1504 178 * Is a stop file there?
ohrstrom@1504 179 */
ohrstrom@1504 180 public boolean markedForStop() throws IOException {
ohrstrom@1504 181 if (stopFile.exists()) {
ohrstrom@1504 182 try {
ohrstrom@1504 183 stopFile.delete();
ohrstrom@1504 184 } catch (Exception e)
ohrstrom@1504 185 {}
ohrstrom@1504 186 return true;
ohrstrom@1504 187 }
ohrstrom@1504 188 return false;
ohrstrom@1504 189 }
ohrstrom@1504 190
ohrstrom@1504 191 /**
ohrstrom@1504 192 * Unlock the port file.
ohrstrom@1504 193 */
ohrstrom@1504 194 public void unlock() throws IOException {
ohrstrom@1504 195 assert(lock != null);
ohrstrom@1504 196 lock.release();
ohrstrom@1504 197 lock = null;
ohrstrom@1504 198 }
ohrstrom@1504 199
ohrstrom@1504 200 /**
ohrstrom@1504 201 * Wait for the port file to contain values that look valid.
ohrstrom@1504 202 * Return true, if a-ok, false if the valid values did not materialize within 5 seconds.
ohrstrom@1504 203 */
ohrstrom@1504 204 public synchronized boolean waitForValidValues() throws IOException, FileNotFoundException {
ohrstrom@1504 205 for (int tries = 0; tries < 50; tries++) {
ohrstrom@1504 206 lock();
ohrstrom@1504 207 getValues();
ohrstrom@1504 208 unlock();
ohrstrom@1504 209 if (containsPortInfo) {
ohrstrom@1504 210 Log.debug("Found valid values in port file after waiting "+(tries*100)+"ms");
ohrstrom@1504 211 return true;
ohrstrom@1504 212 }
ohrstrom@1504 213 try {
ohrstrom@1504 214 Thread.sleep(100);
ohrstrom@1504 215 } catch (InterruptedException e)
ohrstrom@1504 216 {}
ohrstrom@1504 217 }
ohrstrom@1504 218 Log.debug("Gave up waiting for valid values in port file");
ohrstrom@1504 219 return false;
ohrstrom@1504 220 }
ohrstrom@1504 221
ohrstrom@1504 222 /**
ohrstrom@1504 223 * Check if the portfile still contains my values, assuming that I am the server.
ohrstrom@1504 224 */
ohrstrom@1504 225 public synchronized boolean stillMyValues() throws IOException, FileNotFoundException {
ohrstrom@1504 226 for (;;) {
ohrstrom@1504 227 try {
ohrstrom@1504 228 lock();
ohrstrom@1504 229 getValues();
ohrstrom@1504 230 unlock();
ohrstrom@1504 231 if (containsPortInfo) {
ohrstrom@1504 232 if (serverPort == myServerPort &&
ohrstrom@1504 233 serverCookie == myServerCookie) {
ohrstrom@1504 234 // Everything is ok.
ohrstrom@1504 235 return true;
ohrstrom@1504 236 }
ohrstrom@1504 237 // Someone has overwritten the port file.
ohrstrom@1504 238 // Probably another javac server, lets quit.
ohrstrom@1504 239 return false;
ohrstrom@1504 240 }
ohrstrom@1504 241 // Something else is wrong with the portfile. Lets quit.
ohrstrom@1504 242 return false;
ohrstrom@1504 243 } catch (FileLockInterruptionException e) {
ohrstrom@1504 244 continue;
ohrstrom@1504 245 }
ohrstrom@1504 246 catch (ClosedChannelException e) {
ohrstrom@1504 247 // The channel has been closed since sjavac is exiting.
ohrstrom@1504 248 return false;
ohrstrom@1504 249 }
ohrstrom@1504 250 }
ohrstrom@1504 251 }
ohrstrom@1504 252
ohrstrom@1504 253 /**
ohrstrom@1504 254 * Return the name of the port file.
ohrstrom@1504 255 */
ohrstrom@1504 256 public String getFilename() {
ohrstrom@1504 257 return filename;
ohrstrom@1504 258 }
ohrstrom@1504 259 }

mercurial