[jboss-remoting-commits] JBoss Remoting SVN: r4999 - remoting2/branches/2.x/src/main/org/jboss/remoting/detection/jndi.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Tue Apr 14 06:13:00 EDT 2009


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

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

Modified: remoting2/branches/2.x/src/main/org/jboss/remoting/detection/jndi/JNDIDetector.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/detection/jndi/JNDIDetector.java	2009-04-14 10:12:27 UTC (rev 4998)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/detection/jndi/JNDIDetector.java	2009-04-14 10:13:00 UTC (rev 4999)
@@ -39,9 +39,14 @@
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 
+import java.io.IOException;
 import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Map;
 import java.util.Properties;
 
@@ -277,7 +282,7 @@
          cleanDetectionCount++;
          boolean cleanDetect = cleanDetectionCount > detectionNumber;
          String bindName = "";
-         NamingEnumeration enumeration = SecurityUtility.listBindings(context, bindName);
+         NamingEnumeration enumeration = listBindings(context, bindName);
          while(enumeration.hasMore())
          {
             Binding binding = (Binding) enumeration.next();
@@ -414,7 +419,7 @@
       {
          try
          {
-            SecurityUtility.rebind(context, sId, msg);
+            rebind(context, sId, msg);
             log.info("Added " + sId + " to registry.");
          }
          catch(NameAlreadyBoundException nabex)
@@ -447,7 +452,7 @@
                namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
                namingBean = namingBeanImplClass.newInstance();
                Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
-               SecurityUtility.setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
+               setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                startMethod.invoke(namingBean, new Object[] {});
             }
             catch (Exception e)
@@ -455,7 +460,7 @@
                log.debug("Cannot find NamingBeanImpl: must be running jdk 1.4");
             }
             
-            host = SecurityUtility.getLocalHostName();
+            host = getLocalHostName();
             port = PortUtil.findFreePort(host);
 
             log.info("Remoting JNDI detector starting JNDI server instance since none where specified via configuration.");
@@ -502,18 +507,18 @@
       InitialContext initialContext = new InitialContext(env);
       try
       {
-         context = SecurityUtility.initialContextLookup(initialContext, subContextName);
+         context = initialContextLookup(initialContext, subContextName);
       }
       catch(NamingException e)
       {
          try
          {
-            context = SecurityUtility.createSubcontext(initialContext, subContextName);
+            context = createSubcontext(initialContext, subContextName);
          }
          catch(NameAlreadyBoundException e1)
          {
             log.debug("The sub context " + subContextName + " was created before we could.");
-            context = SecurityUtility.initialContextLookup(initialContext, subContextName);
+            context = initialContextLookup(initialContext, subContextName);
          }
       }
    }
@@ -545,6 +550,224 @@
       {
          log.trace("unregistering detector " + sId);
       }
-      SecurityUtility.unbind(context, sId);
+      unbind(context, sId);
    }
+   
+   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();
+      }
+   }
+   
+   static private String getLocalHostName() throws UnknownHostException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return getLocalHost().getHostName();
+      }
+
+      try
+      {
+         return (String) AccessController.doPrivileged( new PrivilegedExceptionAction()
+         {
+            public Object run() throws IOException
+            {
+               InetAddress address = null;
+               try
+               {
+                  address = InetAddress.getLocalHost();
+               }
+               catch (IOException e)
+               {
+                  address = InetAddress.getByName("127.0.0.1");
+               }
+               
+               return address.getHostName();
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (UnknownHostException) e.getCause();
+      }
+   }
+   
+   static private Context createSubcontext(final InitialContext initialContext, final String subContextName)
+   throws NamingException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return initialContext.createSubcontext(subContextName);
+      }
+
+      try
+      {
+         return (Context) AccessController.doPrivileged( new PrivilegedExceptionAction() 
+         {
+            public Object run() throws NamingException
+            {
+               return initialContext.createSubcontext(subContextName);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (NamingException) e.getCause();
+      }
+   }
+   
+   static private Context initialContextLookup(final InitialContext initialContext, final String subContextName)
+   throws NamingException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return (Context) initialContext.lookup(subContextName);
+      }
+
+      try
+      {
+         return (Context) AccessController.doPrivileged( new PrivilegedExceptionAction() 
+         {
+            public Object run() throws NamingException
+            {
+               return initialContext.lookup(subContextName);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (NamingException) e.getCause();
+      }
+   }
+   
+   static private NamingEnumeration listBindings(final Context context, final String bindName)
+   throws NamingException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         return context.listBindings(bindName);
+      }
+
+      try
+      {
+         return (NamingEnumeration) AccessController.doPrivileged( new PrivilegedExceptionAction() 
+         {
+            public Object run() throws NamingException
+            {
+               return context.listBindings(bindName);
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (NamingException) e.getCause();
+      }
+   }
+   
+   static private void rebind(final Context context, final String name, final Object object)
+   throws NamingException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         context.rebind(name, object);
+         return;
+      }
+
+      try
+      {
+         AccessController.doPrivileged( new PrivilegedExceptionAction() 
+         {
+            public Object run() throws NamingException
+            {
+               context.rebind(name, object);
+               return null;
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (NamingException) e.getCause();
+      }
+   }
+   
+   static private void unbind(final Context context, final String name)
+   throws NamingException
+   {
+      if (SecurityUtility.skipAccessControl())
+      {
+         context.unbind(name);
+         return;
+      }
+
+      try
+      {
+         AccessController.doPrivileged( new PrivilegedExceptionAction() 
+         {
+            public Object run() throws NamingException
+            {
+               context.unbind(name);
+               return null;
+            }
+         });
+      }
+      catch (PrivilegedActionException e)
+      {
+         throw (NamingException) e.getCause();
+      }
+   }
 }




More information about the jboss-remoting-commits mailing list