[jboss-remoting-commits] JBoss Remoting SVN: r3832 - remoting2/branches/2.x/src/main/org/jboss/remoting/ident.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Tue Apr 1 23:49:26 EDT 2008


Author: ron.sigal at jboss.com
Date: 2008-04-01 23:49:26 -0400 (Tue, 01 Apr 2008)
New Revision: 3832

Modified:
   remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java
Log:
JBREM-934: Put InetAddress.getLocalHost(), System.getProperty(), File.exists(), File.canRead()), new FileInputStream(), File.createNewFile(), and System.setProperty() calls in AccessController.doPrivileged() calls.

Modified: remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java	2008-04-02 03:44:36 UTC (rev 3831)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java	2008-04-02 03:49:26 UTC (rev 3832)
@@ -22,6 +22,7 @@
 package org.jboss.remoting.ident;
 
 import org.jboss.remoting.network.NetworkRegistry;
+import org.jboss.remoting.util.SystemUtility;
 
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -228,11 +229,24 @@
          InetAddress localHost = null;
          try
          {
-            localHost = InetAddress.getLocalHost();
+            localHost = (InetAddress)AccessController.doPrivileged( new PrivilegedExceptionAction()
+            {
+               public Object run() throws IOException
+               {
+                  try
+                  {
+                     return InetAddress.getLocalHost();
+                  }
+                  catch (IOException e)
+                  {
+                     return InetAddress.getByName("127.0.0.1");
+                  }
+               }
+            });
          }
-         catch (IOException e)
+         catch (PrivilegedActionException e)
          {
-            localHost = InetAddress.getByName("127.0.0.1");
+            throw (IOException) e.getCause();
          }
          
          String serverid = null;
@@ -267,14 +281,7 @@
    private static final synchronized String createId(final MBeanServer server)
    {
       // we can set as a system property
-      String myid = (String)AccessController.doPrivileged( new PrivilegedAction()
-      {
-         public Object run()
-         {
-            return  System.getProperty("jboss.identity");
-         }
-      });
-      
+      String myid = SystemUtility.getSystemProperty("jboss.identity");
       if(myid != null)
       {
          return myid;
@@ -304,7 +311,7 @@
          
          if(dir != null)
          {
-            if(dir.exists() == false)
+            if(fileExists(dir) == false)
             {
                final File finalDir = dir;
                AccessController.doPrivileged( new PrivilegedAction()
@@ -342,7 +349,7 @@
          }
          
          final File dir = new File(fl);
-         if(dir.exists() == false)
+         if(fileExists(dir) == false)
          {
             AccessController.doPrivileged( new PrivilegedAction()
             {
@@ -356,12 +363,37 @@
          }
          file = new File(dir, "jboss.identity");
       }
-      if(file.exists() && file.canRead())
+
+      final File finalFile = file;
+      boolean canRead = ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
+                        {
+                           public Object run()
+                           {
+                              return new Boolean(finalFile.canRead());
+                           }
+                        })).booleanValue();
+
+      
+      if(fileExists(file) && canRead)
       {
          InputStream is = null;
          try
          {
-            is = new FileInputStream(file);
+            try
+            {
+               is = (FileInputStream) AccessController.doPrivileged( new PrivilegedExceptionAction()
+               {
+                  public Object run() throws Exception
+                  {
+                     return new FileInputStream(finalFile);
+                  }
+               });
+            }
+            catch (Exception e)
+            {
+               throw (Exception) e.getCause();
+            }
+            
             byte buf[] = new byte[800];
             int c = is.read(buf);
             id = new String(buf, 0, c);
@@ -390,11 +422,39 @@
          try
          {
             id = createUniqueID();
-            if(file.exists() == false)
+            if(fileExists(file) == false)
             {
-               file.createNewFile();
+               try
+               {
+                  AccessController.doPrivileged( new PrivilegedExceptionAction()
+                  {
+                     public Object run() throws Exception
+                     {
+                        finalFile.createNewFile();
+                        return null;
+                     }
+                  });
+               }
+               catch (Exception e)
+               {
+                  throw (Exception) e.getCause();
+               }
             }
-            out = new FileOutputStream(file);
+
+            try
+            {
+               out = (FileOutputStream) AccessController.doPrivileged( new PrivilegedExceptionAction()
+               {
+                  public Object run() throws Exception
+                  {
+                     return new FileOutputStream(finalFile);
+                  }
+               });
+            }
+            catch (Exception e)
+            {
+               throw (Exception) e.getCause();
+            }
             out.write(id.getBytes());
          }
          catch(Exception ex)
@@ -416,7 +476,23 @@
             }
          }
       }
-      System.setProperty("jboss.identity", id);
+      
+      try
+      {
+         final String finalId = id;
+         AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               System.setProperty("jboss.identity", finalId);
+               return null;
+            }
+         });
+      }
+      catch (Exception e)
+      {
+      }
+      
       return id;
    }
 
@@ -426,4 +502,22 @@
       // colons don't work in JMX
       return id.replace(':', 'x') + random.nextInt(1000);
    }
+   
+   private static boolean fileExists(final File file)
+   {
+      try
+      {
+         return ((Boolean)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return new Boolean(file.exists());
+            }
+         })).booleanValue();
+      }
+      catch (Exception e)
+      {
+         return false;
+      }
+   }
 }




More information about the jboss-remoting-commits mailing list