8148498: The sjavac client should never create a port file
authoralundblad
Mon, 29 Feb 2016 19:07:05 +0100
changeset 36163 3d0d0a06fe92
parent 36162 e5c4e9ab3cdd
child 36164 8ced04acb5e0
8148498: The sjavac client should never create a port file Summary: Sjavac client now avoids creating a port file. Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/ClientMain.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/SjavacClient.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/PortFile.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/ClientMain.java	Mon Feb 29 13:37:29 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/ClientMain.java	Mon Feb 29 19:07:05 2016 +0100
@@ -70,24 +70,15 @@
 
         // Prepare sjavac object
         boolean useServer = options.getServerConf() != null;
-        Sjavac sjavac;
-        // Create an sjavac implementation to be used for compilation
-        if (useServer) {
-            try {
-                sjavac = new SjavacClient(options);
-            } catch (PortFileInaccessibleException e) {
-                Log.error("Port file inaccessible.");
-                return -1;
-            }
-        } else {
-            sjavac = new SjavacImpl();
-        }
+        Sjavac sjavac = useServer ? new SjavacClient(options) : new SjavacImpl();
 
+        // Perform compilation
         int rc = sjavac.compile(args);
 
         // If sjavac is running in the foreground we should shut it down at this point
-        if (!useServer)
+        if (!useServer) {
             sjavac.shutdown();
+        }
 
         return rc;
     }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/SjavacClient.java	Mon Feb 29 13:37:29 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/client/SjavacClient.java	Mon Feb 29 19:07:05 2016 +0100
@@ -88,7 +88,7 @@
     // Store the server conf settings here.
     private final String settings;
 
-    public SjavacClient(Options options) throws PortFileInaccessibleException {
+    public SjavacClient(Options options) {
         String tmpServerConf = options.getServerConf();
         String serverConf = (tmpServerConf!=null)? tmpServerConf : "";
         String tmpId = Util.extractStringOption("id", serverConf);
@@ -98,12 +98,7 @@
                                         .toAbsolutePath()
                                         .toString();
         String portfileName = Util.extractStringOption("portfile", serverConf, defaultPortfile);
-        try {
-            portFile = SjavacServer.getPortFile(portfileName);
-        } catch (PortFileInaccessibleException e) {
-            Log.error("Port file inaccessable: " + e);
-            throw e;
-        }
+        portFile = SjavacServer.getPortFile(portfileName);
         sjavacForkCmd = Util.extractStringOption("sjavac", serverConf, "sjavac");
         int poolsize = Util.extractIntOption("poolsize", serverConf);
         keepalive = Util.extractIntOption("keepalive", serverConf, 120);
@@ -154,6 +149,9 @@
                     result = Integer.parseInt(content);
                 }
             }
+        } catch (PortFileInaccessibleException e) {
+            Log.error("Port file inaccessible.");
+            result = CompilationSubResult.ERROR_FATAL;
         } catch (IOException ioe) {
             Log.error("IOException caught during compilation: " + ioe.getMessage());
             Log.debug(ioe);
@@ -204,13 +202,15 @@
     private void makeSureServerIsRunning(PortFile portFile)
             throws IOException, InterruptedException {
 
-        portFile.lock();
-        portFile.getValues();
-        portFile.unlock();
+        if (portFile.exists()) {
+            portFile.lock();
+            portFile.getValues();
+            portFile.unlock();
 
-        if (portFile.containsPortInfo()) {
-            // Server seems to already be running
-            return;
+            if (portFile.containsPortInfo()) {
+                // Server seems to already be running
+                return;
+            }
         }
 
         // Fork a new server and wait for it to start
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/PortFile.java	Mon Feb 29 13:37:29 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/PortFile.java	Mon Feb 29 19:07:05 2016 +0100
@@ -81,10 +81,15 @@
      * Create a new portfile.
      * @param fn is the path to the file.
      */
-    public PortFile(String fn) throws PortFileInaccessibleException {
+    public PortFile(String fn) {
         filename = fn;
         file = new File(filename);
         stopFile = new File(filename+".stop");
+        containsPortInfo = false;
+        lock = null;
+    }
+
+    private void initializeChannel() throws PortFileInaccessibleException {
         try {
             rwfile = new RandomAccessFile(file, "rw");
         } catch (FileNotFoundException e) {
@@ -94,14 +99,15 @@
         // The rwfile should only be readable by the owner of the process
         // and no other! How do we do that on a RandomAccessFile?
         channel = rwfile.getChannel();
-        containsPortInfo = false;
-        lock = null;
     }
 
     /**
      * Lock the port file.
      */
     public void lock() throws IOException, InterruptedException {
+        if (channel == null) {
+            initializeChannel();
+        }
         lockSem.acquire();
         lock = channel.lock();
     }
@@ -204,8 +210,8 @@
         if (stopFile.exists()) {
             try {
                 stopFile.delete();
-            } catch (Exception e)
-            {}
+            } catch (Exception e) {
+            }
             return true;
         }
         return false;
@@ -215,7 +221,9 @@
      * Unlock the port file.
      */
     public void unlock() throws IOException {
-        Assert.check(lock != null);
+        if (lock == null) {
+            return;
+        }
         lock.release();
         lock = null;
         lockSem.release();
@@ -230,9 +238,11 @@
         long timeout = startTime + getServerStartupTimeoutSeconds() * 1000;
         while (true) {
             Log.debug("Looking for valid port file values...");
-            lock();
-            getValues();
-            unlock();
+            if (exists()) {
+                lock();
+                getValues();
+                unlock();
+            }
             if (containsPortInfo) {
                 Log.debug("Valid port file values found after " + (System.currentTimeMillis() - startTime) + " ms");
                 return;
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java	Mon Feb 29 13:37:29 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/server/SjavacServer.java	Mon Feb 29 19:07:05 2016 +0100
@@ -107,8 +107,7 @@
     /**
      * Acquire the port file. Synchronized since several threads inside an smart javac wrapper client acquires the same port file at the same time.
      */
-    public static synchronized PortFile getPortFile(String filename)
-            throws PortFileInaccessibleException {
+    public static synchronized PortFile getPortFile(String filename) {
         if (allPortFiles == null) {
             allPortFiles = new HashMap<>();
         }