Home » geronimo-2.2-source-release » org.apache.geronimo.security.util » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.security.util;
   19   
   20   import java.lang.reflect.Constructor;
   21   import java.lang.reflect.InvocationTargetException;
   22   import java.lang.reflect.Method;
   23   import java.security.AccessController;
   24   import java.security.PrivilegedActionException;
   25   import java.security.PrivilegedExceptionAction;
   26   import java.security.Principal;
   27   
   28   import javax.security.auth.x500.X500Principal;
   29   import javax.security.jacc.PolicyContext;
   30   import javax.security.jacc.PolicyContextException;
   31   import javax.security.jacc.PolicyContextHandler;
   32   
   33   import org.apache.geronimo.common.DeploymentException;
   34   import org.apache.geronimo.security.DomainPrincipal;
   35   import org.apache.geronimo.security.PrimaryDomainPrincipal;
   36   import org.apache.geronimo.security.PrimaryPrincipal;
   37   import org.apache.geronimo.security.PrimaryRealmPrincipal;
   38   import org.apache.geronimo.security.RealmPrincipal;
   39   import org.apache.geronimo.security.deploy.PrincipalInfo;
   40   import org.slf4j.LoggerFactory;
   41   import org.slf4j.Logger;
   42   
   43   
   44   /**
   45    * A collection of utility functions that assist with the configuration of
   46    * <code>PolicyConfiguration</code>s.
   47    *
   48    * @version $Rev: 772462 $ $Date: 2009-05-06 15:57:58 -0700 (Wed, 06 May 2009) $
   49    * @see javax.security.jacc.PolicyConfiguration
   50    * @see "JSR 115" Java Authorization Contract for Containers
   51    */
   52   public class ConfigurationUtil {
   53       private static final Logger log = LoggerFactory.getLogger(ConfigurationUtil.class);
   54   
   55       /**
   56        * Create an X500Principal from a deployment description.
   57        *
   58        * @param name the distinguished name of the principal
   59        * @return an X500Principal from a deployment description
   60        */
   61       public static X500Principal generateX500Principal(String name) {
   62           return new X500Principal(name);
   63       }
   64   
   65       /**
   66        * Create a Principal from a deployment description.
   67        *
   68        * @param principalInfo the deployment description of the principal to be created.
   69        * @param classLoader
   70        * @return a RealmPrincipal from a deployment description
   71        */
   72       public static Principal generatePrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) {
   73           return generatePrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
   74       }
   75   
   76       public static Principal generatePrincipal(final String className, final String principalName, final ClassLoader classLoader) {
   77           try {
   78               return AccessController.doPrivileged(new PrivilegedExceptionAction<Principal>() {
   79                   public Principal run() throws Exception {
   80                       Class<Principal> clazz = (Class<Principal>) classLoader.loadClass(className);
   81                       try {
   82                           Constructor<Principal> constructor = clazz.getDeclaredConstructor(new Class[]{String.class});
   83                           return constructor.newInstance(new Object[]{principalName});
   84                       } catch (NoSuchMethodException e) {
   85                           Constructor<Principal>[] constructors = (Constructor<Principal>[])clazz.getDeclaredConstructors();
   86                           for (Constructor<Principal> constructor: constructors) {
   87                               Class<?>[] paramTypes = constructor.getParameterTypes();
   88                               if (paramTypes.length == 0) {
   89                                   Principal p = constructor.newInstance();
   90                                   Method m = clazz.getMethod("setName", String.class);
   91                                   m.invoke(p, principalName);
   92                                   return p;
   93                               }
   94                               if (paramTypes[0] == String.class) {
   95                                   Object[] params = new Object[paramTypes.length];
   96                                   params[0] = principalName;
   97                                   return constructor.newInstance(params);
   98                               }
   99                           }
  100                           throw new RuntimeException("Could not construct principal of class: " + className);
  101                       }
  102                   }
  103               });
  104           } catch (PrivilegedActionException e) {
  105               e.printStackTrace();
  106               if (e.getException() != null) {
  107                   log.info("PrivilegedActionException containing", e.getException());
  108               } else {
  109                   log.info("PrivilegedActionException", e);
  110               }
  111               return null;
  112           }
  113       }
  114   
  115       /**
  116        * Create a RealmPrincipal from a deployment description.
  117        *
  118        * @param principalInfo the deployment description of the principal to be created.
  119        * @param classLoader
  120        * @return a RealmPrincipal from a deployment description
  121        */
  122       public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) {
  123           return generateRealmPrincipal(realm, loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
  124       }
  125   
  126       public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final String className, final String principalName,
  127                                                           ClassLoader classLoader)
  128       {
  129           return new RealmPrincipal(realm, loginDomain, generatePrincipal(className, principalName, classLoader));
  130       }
  131   
  132       /**
  133        * Create a DomainPrincipal from a deployment description.
  134        *
  135        * @param principalInfo the deployment description of the principal to be created.
  136        * @param classLoader
  137        * @return a RealmPrincipal from a deployment description
  138        */
  139       public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) {
  140           return generateDomainPrincipal(loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
  141       }
  142   
  143       public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final String className, final String principalName, ClassLoader classLoader) {
  144           return new DomainPrincipal(loginDomain, generatePrincipal(className, principalName, classLoader));
  145       }
  146   
  147       /**
  148        * Create a RealmPrincipal from a deployment description.
  149        *
  150        * @param principalInfo the deployment description of the principal to be created.
  151        * @param classLoader
  152        * @return a PrimaryRealmPrincipal from a deployment description
  153        */
  154       public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException {
  155           return generatePrimaryRealmPrincipal(realm, domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
  156       }
  157   
  158       public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final String className, final String principalName,
  159                                                                         final ClassLoader classLoader) throws DeploymentException
  160       {
  161           try {
  162               return (PrimaryRealmPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() {
  163                   public Object run() throws Exception {
  164                       Principal p = null;
  165                       Class clazz = classLoader.loadClass(className);
  166                       Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class});
  167                       p = (Principal) constructor.newInstance(new Object[]{principalName});
  168   
  169                       return new PrimaryRealmPrincipal(realm, domain, p);
  170                   }
  171               });
  172           } catch (PrivilegedActionException pae) {
  173               throw new DeploymentException("Unable to create realm principal", pae.getException());
  174           }
  175       }
  176   
  177       /**
  178        * Create a DomainPrincipal from a deployment description.
  179        *
  180        * @param principalInfo the deployment description of the principal to be created.
  181        * @param classLoader
  182        * @return a PrimaryDomainPrincipal from a deployment description
  183        */
  184       public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException {
  185           return generatePrimaryDomainPrincipal(domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
  186       }
  187   
  188       public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final String className, final String principalName,
  189                                                                           final ClassLoader classLoader) throws DeploymentException
  190       {
  191           try {
  192               return (PrimaryDomainPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() {
  193                   public Object run() throws Exception {
  194                       Principal p = null;
  195                       Class clazz = classLoader.loadClass(className);
  196                       Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class});
  197                       p = (Principal) constructor.newInstance(new Object[]{principalName});
  198   
  199                       return new PrimaryDomainPrincipal(domain, p);
  200                   }
  201               });
  202           } catch (PrivilegedActionException pae) {
  203               throw new DeploymentException("Unable to create domain principal", pae.getException());
  204           }
  205       }
  206   
  207       /**
  208        * Create a Principal from a deployment description.
  209        *
  210        * @param principalInfo the deployment description of the principal to be created.
  211        * @param classLoader
  212        * @return a Principal from a deployment description
  213        */
  214       public static PrimaryPrincipal generatePrimaryPrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException {
  215           return generatePrimaryPrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader);
  216       }
  217   
  218       public static PrimaryPrincipal generatePrimaryPrincipal(final String className, final String principalName, final ClassLoader classLoader) throws DeploymentException {
  219           try {
  220               return (PrimaryPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() {
  221                   public Object run() throws Exception {
  222                       Principal p = null;
  223                       Class clazz = classLoader.loadClass(className);
  224                       Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class});
  225                       p = (Principal) constructor.newInstance(new Object[]{principalName});
  226   
  227                       return new PrimaryPrincipal(p);
  228                   }
  229               });
  230           } catch (PrivilegedActionException pae) {
  231               throw new DeploymentException("Unable to create principal", pae.getException());
  232           }
  233       }
  234   
  235       /**
  236        * A simple helper method to register PolicyContextHandlers
  237        *
  238        * @param handler an object that implements the <code>PolicyContextHandler</code>
  239        *                interface. The value of this parameter must not be null.
  240        * @param replace this boolean value defines the behavior of this method
  241        *                if, when it is called, a <code>PolicyContextHandler</code> has already
  242        *                been registered to handle the same key. In that case, and if the value
  243        *                of this argument is true, the existing handler is replaced with the
  244        *                argument handler. If the value of this parameter is false the existing
  245        *                registration is preserved and an exception is thrown.
  246        */
  247       public static void registerPolicyContextHandler(PolicyContextHandler handler, boolean replace) throws PolicyContextException {
  248           String[] keys = handler.getKeys();
  249   
  250           for (int i = 0; i < keys.length; i++) {
  251               PolicyContext.registerHandler(keys[i], handler, replace);
  252           }
  253       }
  254   
  255   
  256   }

Home » geronimo-2.2-source-release » org.apache.geronimo.security.util » [javadoc | source]