[jboss-svn-commits] JBL Code SVN: r29845 - in labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats: internal/arjuna/utils and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Oct 28 06:02:52 EDT 2009


Author: jhalliday
Date: 2009-10-28 06:02:52 -0400 (Wed, 28 Oct 2009)
New Revision: 29845

Modified:
   labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/Utility.java
   labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/internal/arjuna/utils/SocketProcessId.java
Log:
Streamline host and process id getter methods for better concurrency and performance. JBTM-635


Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/Utility.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/Utility.java	2009-10-28 09:53:45 UTC (rev 29844)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/utils/Utility.java	2009-10-28 10:02:52 UTC (rev 29845)
@@ -169,8 +169,15 @@
      *         separated for general availability.
      * @since JTS 2.1.
      */
+    public static long[] hostInetAddr() throws UnknownHostException {
+        if(myAddr == null) {
+            calculateHostInetAddr();
+        }
 
-    public static synchronized long[] hostInetAddr () throws UnknownHostException
+        return myAddr;
+    }
+
+    private static synchronized void calculateHostInetAddr () throws UnknownHostException
     {
         /*
          * Calculate only once.
@@ -245,8 +252,6 @@
                 myAddr[1] = (myAddr[1] << 8) | l;
             }               
         }
-
-        return myAddr;
     }
 
     /**
@@ -358,15 +363,22 @@
      * @return a Uid representing this process.
      * @since JTS 2.1.
      */
-
-    public static final synchronized Uid getProcessUid ()
+    public static Uid getProcessUid ()
     {
-        if (processUid == null)
-            processUid = new Uid();
+        if (processUid == null) {
+            initProcessUid();
+        }
 
         return processUid;
     }
 
+    private static synchronized void initProcessUid() {
+        // not done from a static initializer because Uid ctor calls back into this class.
+        if(processUid == null) {
+            processUid = new Uid();
+        }
+    }
+
     public static final boolean isWindows ()
     {
         String os = System.getProperty("os.name");
@@ -387,27 +399,28 @@
      *          [com.arjuna.ats.arjuna.utils.Utility_1] -
      *          Utility.getDefaultProcess - failed with
      */
-    public static final Process getDefaultProcess ()
+    private static synchronized void initDefaultProcess ()
     {
-        try
+        if(processHandle == null)
         {
-            Class c = Thread.currentThread().getContextClassLoader().loadClass( arjPropertyManager.getCoreEnvironmentBean().getProcessImplementation());
+            try
+            {
+                Class c = Thread.currentThread().getContextClassLoader().loadClass( arjPropertyManager.getCoreEnvironmentBean().getProcessImplementation());
 
-            return (Process) c.newInstance();
+                processHandle = (Process) c.newInstance();
+            }
+            catch (Exception e)
+            {
+                tsLogger.arjLoggerI18N.warn("Utility_1", e);
+            }
         }
-        catch (Exception e)
-        {
-            tsLogger.arjLoggerI18N.warn("Utility_1", e);
-
-            return null;
-        }
     }
 
     private static final Process getProcess ()
     {
         if (processHandle == null)
         {
-            processHandle = getDefaultProcess();
+            initDefaultProcess();
         }
 
         return processHandle;
@@ -419,11 +432,11 @@
         }
     }
 
-    private static long[] myAddr = null;
+    private static volatile long[] myAddr = null;
 
     private static Uid processUid = null;
 
-    private static Process processHandle = null;
+    private static volatile Process processHandle = null;
 
     private static final String hexStart = "0x";
 

Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/internal/arjuna/utils/SocketProcessId.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/internal/arjuna/utils/SocketProcessId.java	2009-10-28 09:53:45 UTC (rev 29844)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/internal/arjuna/utils/SocketProcessId.java	2009-10-28 10:02:52 UTC (rev 29845)
@@ -52,56 +52,47 @@
 
 public class SocketProcessId implements com.arjuna.ats.arjuna.utils.Process
 {
-
     /**
-     * @return the process id. This had better be unique between processes
-     * on the same machine. If not we're in trouble!
-     *
-     * @message com.arjuna.ats.internal.arjuna.utils.SocketProcessId_1 [com.arjuna.ats.internal.arjuna.utils.SocketProcessId_1]- Invalid port specified
      * @message com.arjuna.ats.internal.arjuna.utils.SocketProcessId_2 [com.arjuna.ats.internal.arjuna.utils.SocketProcessId_2] - SocketProcessId.getpid could not get unique port.
-     * @message com.arjuna.ats.internal.arjuna.utils.SocketProcessId_3 [com.arjuna.ats.internal.arjuna.utils.SocketProcessId_3]- Invalid value for SocketProcessIdMaxPorts specified {0}
      */
-
-    public int getpid ()
+    public SocketProcessId()
     {
-	synchronized (SocketProcessId._lock)
-	{
-	    if (_thePort == 0)
-	    {
-		if (_theSocket == null)
-		{
-            int port = arjPropertyManager.getCoreEnvironmentBean().getSocketProcessIdPort();
+        int port = arjPropertyManager.getCoreEnvironmentBean().getSocketProcessIdPort();
+        int maxPorts = arjPropertyManager.getCoreEnvironmentBean().getSocketProcessIdMaxPorts();
 
-            int maxPorts = arjPropertyManager.getCoreEnvironmentBean().getSocketProcessIdMaxPorts();
+        int maxPort;
 
-            int maxPort;
+        if (maxPorts <= 1)
+        {
+            maxPort = port;
+        }
+        else if (Utility.MAX_PORT - maxPorts < port)
+        {
+            maxPort = Utility.MAX_PORT;
+        }
+        else
+        {
+            maxPort = port + maxPorts;
+        }
 
-            if (maxPorts <= 1)
-            {
-                maxPort = port;
-            }
-            else if (Utility.MAX_PORT - maxPorts < port)
-            {
-                maxPort = Utility.MAX_PORT;
-            }
-            else
-            {
-                maxPort = port + maxPorts;
-            }
+        do {
+            _theSocket = createSocket(port);
+        } while (_theSocket == null && ++port < maxPort);
 
-            do {
-                _theSocket = createSocket(port);
-            } while (_theSocket == null && ++port < maxPort);
+        _thePort = ((_theSocket == null) ? -1 : _theSocket.getLocalPort());
 
-	    	_thePort = ((_theSocket == null) ? -1 : _theSocket.getLocalPort());
-		}
-	    }
-	}
+        if (_thePort == -1) {
+            throw new FatalError(tsLogger.log_mesg.getString("com.arjuna.ats.internal.arjuna.utils.SocketProcessId_2"));
+        }
+    }
 
-	if (_thePort == -1)
-	    throw new FatalError(tsLogger.log_mesg.getString("com.arjuna.ats.internal.arjuna.utils.SocketProcessId_2"));
-
-	return _thePort;
+    /**
+     * @return the process id. This had better be unique between processes
+     * on the same machine. If not we're in trouble!
+     */
+    public int getpid ()
+    {
+    	return _thePort;
     }
 
     private static ServerSocket createSocket(int port)
@@ -116,22 +107,6 @@
         }
     }
 
-    public static ServerSocket getSocket ()
-    {
-	synchronized (SocketProcessId._lock)
-	{
-	    return _theSocket;
-	}
-    }
-
-    private static int          _thePort = 0;
-    private static ServerSocket _theSocket = null;
-    private static final Object       _lock = new Object();
-
-    /**
-     * Default port is any free port.
-     */
-
-    private static final int _defaultPort = 0 ;
-
+    private final int _thePort;
+    private ServerSocket _theSocket;
 }



More information about the jboss-svn-commits mailing list