Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
   * JBoss, Home of Professional Open Source
   * Copyright 2005, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
  package org.jboss.remoting.util;
  
  import java.io.File;
  import java.net.Socket;
  import java.rmi.Remote;
  
  
  import  org.jboss.serial.io.JBossObjectInputStream;
  import  org.jboss.serial.io.JBossObjectOutputStream;

SecurityUtility provides a central point for making security sensitive calls. It is divided into five sections: 1. calls requiring FilePermissions 2. calls requiring MBeanPermissions 3. calls requiring PropertyPermissions 4. calls requiring RuntimePermissions 5. calls requiring SocketPermissions When the SecurityUtility class is loaded, it checks for two conditions: 1. there is no security manager 2. the system property Remoting.SKIP_ACCESS_CONTROL ("skipAccessControl") is set to true. If either condition is true, then every method in SecurityUtility will bypass its call to AccessController.doPrivileged().

Author(s):
Ron Sigal
Version:
$Revision: 1.1 $

Copyright Mar 31, 2008

  
  public class SecurityUtility
  {
     static boolean skipAccessControl;
     
    static
    {
       try
       {
           = ((Boolean)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                boolean b1 = System.getSecurityManager() == null;
                boolean b2 = Boolean.getBoolean(.);
 //               System.out.println("security manager: " + System.getSecurityManager());
                return new Boolean(b1 || b2);
             }
          })).booleanValue();
       }
       catch (PrivilegedActionException e)
       {
          e.getCause().printStackTrace();
       }
    }
    
    
    static public boolean skipAccessControl()
    {
       return ;
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////
    // FilePermission methods
    ///////////////////////////////////////////////////////////////////////////////////////
    
    static public File createTempFile(final String prefixfinal String suffixfinal boolean deleteOnExitthrows IOException
    {
       if ()
       {
          File file =  File.createTempFile(prefixsuffix);
          if (deleteOnExitfile.deleteOnExit();
          return file;
       }
       
       try
       {
          return (File)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws IOException
             {
                File file =  File.createTempFile(prefixsuffix);
                if (deleteOnExitfile.deleteOnExit();
                return file;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (IOExceptione.getCause();
       }
    }
    
    static public void deleteOnExit(final File file)
    {
       if (file == null)
          return;
       
       if ()
       {
          file.deleteOnExit();
          return;
       }
 
       AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             file.deleteOnExit();
             return null;
          }
       });
    }
    
    static public boolean fileExists(final File file)
    {
       if (file == null)
          return false;
       
       if ()
       {
          return file.exists();
       }
 
       return ((Boolean)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return new Boolean(file.exists());
          }
       })).booleanValue();
    }
 
    
    static public boolean mkdirs(final File dir)
    {
       if ()
       {
          return dir.mkdirs();
       }
       
       return ((Boolean) AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return new Boolean(dir.mkdirs());
          }
       })).booleanValue();
    }
    
    
    static public FileInputStream getFileInputStream(final File filethrows FileNotFoundException
    {
       if ()
       {
          return new FileInputStream(file);
       }
       
       try
       {
          return (FileInputStream)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws FileNotFoundException
             {
                return new FileInputStream(file);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (FileNotFoundExceptione.getCause();
       }
    }
    
    static public FileInputStream getFileInputStream(final String paththrows FileNotFoundException
    {
       if ()
       {
          return new FileInputStream(path);
       }
       
       try
       {
          return (FileInputStream)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws FileNotFoundException
             {
                return new FileInputStream(path);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (FileNotFoundExceptione.getCause();
       }
    }
    
    
    static public FileOutputStream getFileOutputStream(final File file)
    throws FileNotFoundException
    {
       if ()
       {
          return new FileOutputStream(file);
       }
       
       try
       {
          return (FileOutputStream)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws FileNotFoundException
             {
                return new FileOutputStream(file);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (FileNotFoundExceptione.getCause();
       }
    }
    
    
    static public FileOutputStream getFileOutputStream(final File filefinal boolean append)
    throws FileNotFoundException
    {
       if ()
       {
          return new FileOutputStream(fileappend);
       }
       
       try
       {
          return (FileOutputStream)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws FileNotFoundException
             {
                return new FileOutputStream(fileappend);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (FileNotFoundExceptione.getCause();
       }
    }
    
    
    static public boolean canRead(final File file)
    {
       if ()
       {
          return file.canRead();
       }
       
       return ((Boolean)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return new Boolean(file.canRead());
          }
       })).booleanValue();
    }
    
    
    static public boolean createNewFile(final File filethrows IOException
    {
       if ()
       {
          return file.createNewFile();
       }
       
       try
       {
          return ((Boolean)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return new Boolean(file.createNewFile());
             }
          })).booleanValue();
       }
       catch (Exception e)
       {
          throw (IOExceptione.getCause();
       }
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////
    // MBeanPermission methods
    ///////////////////////////////////////////////////////////////////////////////////////
    
    static public MBeanServer createMBeanServer() throws Exception
    {
       if ()
       {
          return MBeanServerFactory.createMBeanServer();
       }
       
       try
       {
          return (MBeanServer) AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return MBeanServerFactory.createMBeanServer();
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (Exceptione.getCause();
       }   
    }
    
    
    static public ArrayList findMBeanServer(final String agentId)
    {
       if ()
       {
          return MBeanServerFactory.findMBeanServer(agentId);
       }
       
       return (ArrayList)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return MBeanServerFactory.findMBeanServer(agentId);
          }
       });
    }
    
    
    static public Object getMBeanAttribute(final MBeanServer serverfinal ObjectName objectNamefinal String attribute)
    throws Exception
    {
       if ()
       {
          return server.getAttribute(objectNameattribute);
       }
       
       try
       {
          return AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return server.getAttribute(objectNameattribute);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (Exceptione.getCause();
       }  
    }
    
    
    static public MBeanServer getPlatformMBeanServer()
    {
       if ()
       {
          Class c = null;
          try
          {
             c = Class.forName("java.lang.management.ManagementFactory");
          }
          catch (Exception e)
          {
             ..println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
             return null;
          }
          Method m = c.getMethod("getPlatformMBeanServer"new Class[] {});
          MBeanServer s = (MBeanServerm.invoke(nullnew Object[] {});
          return s;
       }
       
       try
       {
          return (MBeanServer) AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run()
             {
                Class c = null;
                try
                {
                   c = Class.forName("java.lang.management.ManagementFactory");
                }
                catch (Exception e)
                {
                   ..println("Unable to access java.lang.management.ManagementFactory: must be using jdk 1.4");
                   return null;
                }
                Method m = c.getMethod("getPlatformMBeanServer"new Class[] {});
                MBeanServer s = (MBeanServerm.invoke(nullnew Object[] {});
                return s;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
         Throwable cause = e.getCause();
         if (cause instanceof NoSuchMethodException)
            throw (NoSuchMethodExceptioncause;
         else if (cause instanceof IllegalAccessException)
            throw (IllegalAccessExceptioncause;
         else
            throw (InvocationTargetExceptioncause;
       }  
    }
    
    
    static public boolean isInstanceOf(final MBeanServer serverfinal ObjectName objectNamefinal String className)
    {
       if ()
       {
          return server.isInstanceOf(objectNameclassName);
       }
       
       try
       {
          return ((Boolean)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return new Boolean(server.isInstanceOf(objectNameclassName));
             }
          })).booleanValue();
       }
       catch (PrivilegedActionException e)
       {
          throw (InstanceNotFoundExceptione.getCause();
       }
    }
    
    
    static public void registerMBean(final MBeanServer serverfinal Object ofinal ObjectName name)
    throws Exception
    {
       if ()
       {
          server.registerMBean(oname);
          return;
       }
       
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                server.registerMBean(oname);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (Exceptione.getCause();
       }
    }
      
    
    static public void unregisterMBean(final MBeanServer serverfinal ObjectName name)
    throws Exception
    {
       if ()
       {
          server.unregisterMBean(name);
          return;
       }
       
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                server.unregisterMBean(name);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (Exceptione.getCause();
       }
    }
    
 
    ///////////////////////////////////////////////////////////////////////////////////////
    // PropertyPermission methods
    ///////////////////////////////////////////////////////////////////////////////////////
    
    static public String getSystemProperty(final String namefinal String defaultValue)
    {
       if ()
          return System.getProperty(namedefaultValue);
          
       String value = null;
       try
       {
          value = (String)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return System.getProperty(namedefaultValue);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (RuntimeExceptione.getCause();
       }
       
       return value;
    }
    
    
    static public String getSystemProperty(final String name)
    {
       if ()
          return System.getProperty(name);
       
       String value = null;
       try
       {
          value = (String)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return System.getProperty(name);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (RuntimeExceptione.getCause();
       }
       
       return value;
    }
    
    
    static public void setSystemProperty(final String namefinal String value)
    {
       if ()
       {
          System.setProperty(namevalue);
          return;
       }
       
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return System.setProperty(namevalue);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (RuntimeExceptione.getCause();
       }
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////
    // RuntimePermission methods
    ///////////////////////////////////////////////////////////////////////////////////////
    
    static public RemotingClassLoader createRemotingClassLoader(final ClassLoader remotingClassLoader,
          final ClassLoader userClassLoader)
    {
       return createRemotingClassLoader(remotingClassLoaderuserClassLoadertrue);
    }
    static public RemotingClassLoader createRemotingClassLoader(final ClassLoader remotingClassLoader,
          final ClassLoader userClassLoaderfinal boolean parentFirstDelegation)
    {
       if ()
       {
          return new RemotingClassLoader(remotingClassLoaderuserClassLoaderparentFirstDelegation);
       }
 
       return (RemotingClassLoader)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return new RemotingClassLoader(remotingClassLoaderuserClassLoaderparentFirstDelegation);
          }
       });
    }
    
    
    static public Object forName(final String classNamethrows ClassNotFoundException
    {
       if ()
       {
          return Class.forName(className);
       }
       
       try
       {
          return  AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                return Class.forName(className);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (ClassNotFoundExceptione.getCause();
       }
    }
    
    
    static public ClassLoader getClassLoader(final Class c)
    {
       if ()
       {
          return c.getClassLoader();
       }
 
       return (ClassLoader)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return c.getClassLoader();
          }
       });
    }
    
    
    static public ClassLoader getContextClassLoader(final Thread thread)
    {
       if ()
       {
          return thread.getContextClassLoader();
       }
 
       return (ClassLoader) AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return thread.getContextClassLoader();
          }
       });
    }
    
    
    static public ClassLoader getSystemClassLoader()
    {
       if ()
       {
          return ClassLoader.getSystemClassLoader();
       }
 
       return (ClassLoader)AccessController.doPrivilegednew PrivilegedAction()
       {
          public Object run()
          {
             return ClassLoader.getSystemClassLoader();
          }
       });
    }
    
    
    static public Method getMethod(final Class cfinal String namefinal Class[] parameterTypes)
    throws NoSuchMethodException
    {
       if ()
       {
          return c.getMethod(nameparameterTypes);
       }
 
       try
       {
          return (Method) AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws NoSuchMethodException
             {
                return c.getMethod(nameparameterTypes);
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (NoSuchMethodExceptione.getCause();
       }
    }
    
    
    static public Method getDeclaredMethod(final Class cfinal String namefinal Class[] parameterTypes)
    throws NoSuchMethodException
    {
       if ()
       {
          Method m = c.getDeclaredMethod(nameparameterTypes);
          m.setAccessible(true);
          return m;
       }
 
       try
       {
          return (Method) AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws NoSuchMethodException
             {
                Method m = c.getDeclaredMethod(nameparameterTypes);
                m.setAccessible(true);
                return m;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (NoSuchMethodExceptione.getCause();
       }
    }
    
    static public Object readObject(final ObjectInputStream ois)
    {
       if ( || !(ois instanceof JBossObjectInputStream))
       {
          return ois.readObject();
       }
 
       try
       {
          return AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws IOExceptionClassNotFoundException
             {
                return ois.readObject();
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          Throwable cause = e.getCause();
          if (cause instanceof IOException)
             throw (IOExceptioncause;
          else if (cause instanceof ClassNotFoundException)
             throw (ClassNotFoundExceptioncause;
          else
             throw (RuntimeExceptioncause;
       }
    }
    
    static public void writeObject(final ObjectOutputStream oosfinal Object o)
    throws IOException
    {
       if ( || !(oos instanceof JBossObjectOutputStream))
       {
          oos.writeObject(o);
          return;
       }
 
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws IOException
             {
                oos.writeObject(o);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          Throwable cause = e.getCause();
          if (cause instanceof IOException)
             throw (IOExceptioncause;
          else
             throw (RuntimeExceptioncause;
       }
    }
    
    
    ///////////////////////////////////////////////////////////////////////////////////////
    // SocketPermission methods
    ///////////////////////////////////////////////////////////////////////////////////////
    
    static public Socket accept(final ServerSocket ssthrows IOException
    {
       if ()
       {
          return ss.accept();
       }
       
       try
       {
           return (Socket)AccessController.doPrivilegednew PrivilegedExceptionAction()
           {
              public Object run() throws Exception
              {
                  return ss.accept();
              }
           });
       }
       catch (PrivilegedActionException e)
       {
           throw (IOExceptione.getCause();
       }
    }
    
    
    static public void bind(final ServerSocket ssfinal SocketAddress address)
    throws IOException
    {
       if ()
       {
          ss.bind(address);
          return;
       }
 
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                ss.bind(address);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (IOExceptione.getCause();
       }
    }
 
 
    static public void bind(final ServerSocket ssfinal SocketAddress address,
                            final int backlogthrows IOException
    {
       if ()
       {
          ss.bind(addressbacklog);
          return;
       }
       
       try
       {
           AccessController.doPrivilegednew PrivilegedExceptionAction()
           {
              public Object run() throws Exception
              {
                 ss.bind(addressbacklog);
                 return null;
              }
           });
       }
       catch (PrivilegedActionException e)
       {
           throw (IOExceptione.getCause();
       }
    }
    
    
    static public void connect(final Socket socketfinal InetSocketAddress address)
    throws IOException
    {
       if ()
       {
          socket.connect(address);
          return;
       }
       
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                socket.connect(address);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (IOExceptione.getCause();
       }   
    }
    
    
    static public void connect(final Socket socketfinal InetSocketAddress addressfinal int timeout)
    throws IOException
    {
       if ()
       {
          socket.connect(addresstimeout);
          return;
       }
       
       try
       {
          AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                socket.connect(addresstimeout);
                return null;
             }
          });
       }
       catch (PrivilegedActionException e)
       {
          throw (IOExceptione.getCause();
       }   
    }
    
    
    static public void connect(final HttpURLConnection connthrows IOException
    {
       if ()
       {
          conn.connect();
          return;
       }
 
       try
      {
         AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws IOException
            {
               conn.connect();
               return null;
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   
   
   static public ServerSocket createServerSocket(final ServerSocketFactoryMBean ssfthrows IOException
   {
      if ()
      {
         return ssf.createServerSocket();
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws IOException
            {
               return ssf.createServerSocket();
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   
   
                                                 final int portthrows IOException
   {
      if ()
      {
         return ssf.createServerSocket(port);
      }
      
      try
      {
          return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                 return ssf.createServerSocket(port);
             }
          });
      }
      catch (PrivilegedActionException e)
      {
          throw (IOExceptione.getCause();
      }
   }
   
                                                 final int portfinal int backlog)
   throws IOException
   {
      if ()
      {
         return ssf.createServerSocket(portbacklog);
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return ssf.createServerSocket(portbacklog);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   
                                                 final int portfinal int backlog,
                                                 final InetAddress inetAddress)
   throws IOException
   {
      if ()
      {
         return ssf.createServerSocket(portbackloginetAddress);
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return ssf.createServerSocket(portbackloginetAddress);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   
   static public ServerSocket createServerSocket(final ServerSocketFactory ssfthrows IOException
   {
      if ()
      {
         return ssf.createServerSocket();
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws IOException
            {
               return ssf.createServerSocket();
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   
   
   static public ServerSocket createServerSocket(final ServerSocketFactory ssf,
                                                 final int portthrows IOException
   {
      if ()
      {
         return ssf.createServerSocket(port);
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return ssf.createServerSocket(port);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   static public ServerSocket createServerSocket(final ServerSocketFactory ssf,
                                                 final int portfinal int backlog)
   throws IOException
   {
      if ()
      {
         return ssf.createServerSocket(portbacklog);
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws Exception
            {
               return ssf.createServerSocket(portbacklog);
            }
         });
      }
      catch (PrivilegedActionException e)
      {
         throw (IOExceptione.getCause();
      }
   }
   static public ServerSocket createServerSocket(final ServerSocketFactory ssf,
                                                 final int portfinal int backlog,
                                                 final InetAddress inetAddress)
   throws IOException
   {
      if ()
      {
         return ssf.createServerSocket(portbackloginetAddress);
      }
      try
      {
         return (ServerSocket)AccessController.doPrivilegednew PrivilegedExceptionAction()
         {
            public Object run() throws