[jboss-remoting-commits] JBoss Remoting SVN: r5002 - 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 14 06:14:29 EDT 2009


Author: ron.sigal at jboss.com
Date: 2009-04-14 06:14:29 -0400 (Tue, 14 Apr 2009)
New Revision: 5002

Modified:
   remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java
Log:
JBREM-1116: Eliminated dependence on SecurityUtility.

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	2009-04-14 10:13:51 UTC (rev 5001)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/ident/Identity.java	2009-04-14 10:14:29 UTC (rev 5002)
@@ -28,12 +28,14 @@
 import javax.management.ObjectName;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Serializable;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.rmi.dgc.VMID;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
@@ -60,7 +62,7 @@
    
    static
    {
-      _domain = SecurityUtility.getSystemProperty("jboss.identity.domain", DEFAULT_DOMAIN);
+      _domain = getSystemProperty("jboss.identity.domain", DEFAULT_DOMAIN);
    }
    
    private static transient Map identities = new WeakHashMap(2);
@@ -100,7 +102,7 @@
          }
          ident.calcHashCode();
       }
-      SecurityUtility.setSystemProperty("jboss.identity.domain", domain);
+      setSystemProperty("jboss.identity.domain", domain);
       _domain = domain;
       NetworkRegistry.getInstance().changeDomain(domain);
    }
@@ -213,9 +215,9 @@
       }
       try
       {
-         InetAddress localHost = SecurityUtility.getLocalHost();
+         InetAddress localHost = getLocalHost();
          ObjectName objectName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
-         String serverid = (String) SecurityUtility.getMBeanAttribute(server, objectName, "MBeanServerId");
+         String serverid = (String) getMBeanAttribute(server, objectName, "MBeanServerId");
          Identity identity = new Identity(localHost, createId(server), serverid);
          identities.put(server, identity);
          return identity;
@@ -232,7 +234,7 @@
    private static final synchronized String createId(final MBeanServer server)
    {
       // we can set as a system property
-      String myid = SecurityUtility.getSystemProperty("jboss.identity");
+      String myid = getSystemProperty("jboss.identity");
       if(myid != null)
       {
          return myid;
@@ -243,12 +245,12 @@
       {
          // FIRST TRY THE JBOSS guy to determine our data directory
          final ObjectName obj = new ObjectName("jboss.system:type=ServerConfig");
-         File dir = (File) SecurityUtility.getMBeanAttribute(server, obj, "ServerDataDir");
+         File dir = (File) getMBeanAttribute(server, obj, "ServerDataDir");
          if(dir != null)
          {
-            if(SecurityUtility.fileExists(dir) == false)
+            if(fileExists(dir) == false)
             {
-               SecurityUtility.mkdirs(dir);
+               mkdirs(dir);
             }
             file = new File(dir, "jboss.identity");
          }
@@ -259,23 +261,23 @@
       if(file == null)
       {
          // we may not have that mbean, which is OK
-         String fl = SecurityUtility.getSystemProperty("jboss.identity.dir", ".");
+         String fl = getSystemProperty("jboss.identity.dir", ".");
          
          File dir = new File(fl);
-         if(SecurityUtility.fileExists(dir) == false)
+         if(fileExists(dir) == false)
          {
-            SecurityUtility.mkdirs(dir);
+            mkdirs(dir);
          }
          file = new File(dir, "jboss.identity");
       }
 
-      boolean canRead = SecurityUtility.canRead(file);
-      if(SecurityUtility.fileExists(file) && canRead)
+      boolean canRead = canRead(file);
+      if(fileExists(file) && canRead)
       {
          InputStream is = null;
          try
          {
-            is = SecurityUtility.getFileInputStream(file);
+            is = getFileInputStream(file);
             byte buf[] = new byte[800];
             int c = is.read(buf);
             id = new String(buf, 0, c);
@@ -304,12 +306,12 @@
          try
          {
             id = createUniqueID();
-            if(SecurityUtility.fileExists(file) == false)
+            if(fileExists(file) == false)
             {
-               SecurityUtility.createNewFile(file);
+               createNewFile(file);
             }
 
-            out = SecurityUtility.getFileOutputStream(file);
+            out = getFileOutputStream(file);
             out.write(id.getBytes());
          }
          catch(Exception ex)
@@ -332,7 +334,7 @@
          }
       }
       
-      SecurityUtility.setSystemProperty("jboss.identity", id);
+      setSystemProperty("jboss.identity", id);
       
       return id;
    }
@@ -343,4 +345,258 @@
       // colons don't work in JMX
       return id.replace(':', 'x') + random.nextInt(1000);
    }
+   
+   static private boolean fileExists(final File file)
+   {
+      if (file == null)
+         return false;
+      
+      if (SecurityUtility.skipAccessControl())
+      {
+         return file.exists();
+      }
+
+      return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
+      {
+         public Object run()
+         {
+            return new Boolean(file.exists());
+         }
+      })).booleanValue();
+   }
+   
+   static private boolean mkdirs(final File dir)
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return dir.mkdirs();
+      }
+      
+      return ((Boolean) AccessController.doPrivileged( new PrivilegedAction()
+      {
+         public Object run()
+         {
+            return new Boolean(dir.mkdirs());
+         }
+      })).booleanValue();
+   }
+   
+   static private FileInputStream getFileInputStream(final File file) throws FileNotFoundException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return new FileInputStream(file);
+      }
+      
+      try
+      {
+         return (FileInputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws FileNotFoundException
+            {
+               return new FileInputStream(file);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (FileNotFoundException) e.getCause();
+      }
+   }
+   
+   static private FileOutputStream getFileOutputStream(final File file)
+   throws FileNotFoundException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return new FileOutputStream(file);
+      }
+      
+      try
+      {
+         return (FileOutputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws FileNotFoundException
+            {
+               return new FileOutputStream(file);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (FileNotFoundException) e.getCause();
+      }
+   }
+   
+   static private boolean canRead(final File file)
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return file.canRead();
+      }
+      
+      return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
+      {
+         public Object run()
+         {
+            return new Boolean(file.canRead());
+         }
+      })).booleanValue();
+   }
+   
+   static private boolean createNewFile(final File file) throws IOException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return file.createNewFile();
+      }
+      
+      try
+      {
+         return ((Boolean)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return new Boolean(file.createNewFile());
+            }
+         })).booleanValue();
+      }
+      catch (Exception e)
+      {
+         throw (IOException) e.getCause();
+      }
+   }
+   
+   static private Object getMBeanAttribute(final MBeanServer server, final ObjectName objectName, final String attribute)
+   throws Exception
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return server.getAttribute(objectName, attribute);
+      }
+      
+      try
+      {
+         return AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return server.getAttribute(objectName, attribute);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (Exception) e.getCause();
+      }  
+   }
+   
+   static private String getSystemProperty(final String name, final String defaultValue)
+   {
+      if (SecurityUtility.skipAccessControl())
+         return System.getProperty(name, defaultValue);
+         
+      String value = null;
+      try
+      {
+         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return System.getProperty(name, defaultValue);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (RuntimeException) e.getCause();
+      }
+      
+      return value;
+   }
+   
+   static private String getSystemProperty(final String name)
+   {
+      if (SecurityUtility.skipAccessControl())
+         return System.getProperty(name);
+      
+      String value = null;
+      try
+      {
+         value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return System.getProperty(name);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (RuntimeException) e.getCause();
+      }
+      
+      return value;
+   }
+   
+   static private void setSystemProperty(final String name, final String value)
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         System.setProperty(name, value);
+         return;
+      }
+      
+      try
+      {
+         AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws Exception
+            {
+               return System.setProperty(name, value);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (RuntimeException) e.getCause();
+      }
+   }
+   
+   static private InetAddress getLocalHost() throws UnknownHostException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         try
+         {
+            return InetAddress.getLocalHost();
+         }
+         catch (IOException e)
+         {
+            return InetAddress.getByName("127.0.0.1");
+         }
+      }
+
+      try
+      {
+         return (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 (PrivilegedActionException e)
+      {
+         throw (UnknownHostException) e.getCause();
+      }
+   }
 }




More information about the jboss-remoting-commits mailing list