KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > security > Ejb3PolicyConfigurationFactory


1 /*
2  * JBoss, the OpenSource J2EE WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.security;
8
9 import java.security.SecurityPermission JavaDoc;
10 import java.security.AccessController JavaDoc;
11 import java.security.PrivilegedExceptionAction JavaDoc;
12 import java.security.PrivilegedActionException JavaDoc;
13
14 import javax.security.jacc.PolicyContextException JavaDoc;
15 import javax.security.jacc.PolicyConfiguration JavaDoc;
16 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
17
18 import org.jboss.logging.Logger;
19
20 /**
21  * @author Scott.Stark@jboss.org
22  * @author Ron Monzillo, Gary Ellison (javadoc)
23  * @version $Revision: 39451 $
24  */

25 public abstract class Ejb3PolicyConfigurationFactory
26 {
27    private static final Logger log = Logger.getLogger(Ejb3PolicyConfigurationFactory.class);
28    
29    /** The standard name of the system property specifying the JACC
30     PolicyConfigurationFactory implementation class name.
31     */

32    private static final String JavaDoc FACTORY_PROP =
33       "javax.security.jacc.PolicyConfigurationFactory.provider";
34    /** The default PolicyConfigurationFactory implementation */
35    private static final String JavaDoc DEFAULT_FACTORY_NAME =
36       "org.jboss.security.jacc.JBossPolicyConfigurationFactory";
37    /** The loaded PolicyConfigurationFactory provider */
38    private static PolicyConfigurationFactory JavaDoc factory;
39
40    /** This static method uses the javax.security.jacc.PolicyConfigurationFactory.provider
41     * system property to create a provider factory implementation. The provider
42     * class must provide a public no-arg ctor.
43     *
44     * @return the PolicyConfigurationFactory singleton
45     * @throws SecurityException - when the caller does not have a
46     * SecurityPermission(setPolicy) permission.
47     * @throws ClassNotFoundException - when the class named by the system
48     * property could not be found or because the value of the system
49     * property is null.
50     * @throws PolicyContextException - if the PolicyConfigurationFactory ctor
51     * throws an exception other than those in the getPolicyConfigurationFactory
52     * method signature. The exception will be encapsulated in a
53     * PolicyContextException as its cause.
54     */

55    public static PolicyConfigurationFactory JavaDoc getPolicyConfigurationFactory()
56       throws ClassNotFoundException JavaDoc, PolicyContextException JavaDoc
57    {
58       
59       // Validate the caller permission
60
SecurityManager JavaDoc sm = System.getSecurityManager();
61       if (sm != null)
62          sm.checkPermission(new SecurityPermission JavaDoc("setPolicy"));
63
64       if (factory == null)
65       {
66          String JavaDoc factoryName = null;
67          Class JavaDoc clazz = null;
68          try
69          {
70             LoadAction action = new LoadAction();
71             try
72             {
73                clazz = (Class JavaDoc) AccessController.doPrivileged(action);
74                factoryName = action.getName();
75             }
76             catch (PrivilegedActionException JavaDoc ex)
77             {
78                ex.printStackTrace();
79                factoryName = action.getName();
80                Exception JavaDoc e = ex.getException();
81                if (e instanceof ClassNotFoundException JavaDoc)
82                   throw (ClassNotFoundException JavaDoc) e;
83                else
84                   throw new PolicyContextException JavaDoc("Failure during load of class: "+action.getName(), e);
85             }
86             
87             factory = (PolicyConfigurationFactory JavaDoc) clazz.newInstance();
88          }
89          catch (ClassNotFoundException JavaDoc e)
90          {
91             String JavaDoc msg = "Failed to find PolicyConfigurationFactory : " + factoryName;
92             throw new ClassNotFoundException JavaDoc(msg, e);
93          }
94          catch (IllegalAccessException JavaDoc e)
95          {
96             String JavaDoc msg = "Unable to access class : " + factoryName;
97             throw new PolicyContextException JavaDoc(msg, e);
98          }
99          catch (InstantiationException JavaDoc e)
100          {
101             String JavaDoc msg = "Failed to create instance of: " + factoryName;
102             throw new PolicyContextException JavaDoc(msg, e);
103          }
104          catch (ClassCastException JavaDoc e)
105          {
106             StringBuffer JavaDoc msg = new StringBuffer JavaDoc(factoryName + " Is not a PolicyConfigurationFactory, ");
107             msg.append("PCF.class.CL: "+Ejb3PolicyConfigurationFactory.class.getClassLoader());
108             msg.append("\nPCF.class.CS: "+Ejb3PolicyConfigurationFactory.class.getProtectionDomain().getCodeSource());
109             msg.append("\nPCF.class.hash: "+System.identityHashCode(Ejb3PolicyConfigurationFactory.class));
110             msg.append("\nclazz.CL: "+clazz.getClassLoader());
111             msg.append("\nclazz.CS: "+clazz.getProtectionDomain().getCodeSource());
112             msg.append("\nclazz.super.CL: "+clazz.getSuperclass().getClassLoader());
113             msg.append("\nclazz.super.CS: "+clazz.getSuperclass().getProtectionDomain().getCodeSource());
114             msg.append("\nclazz.super.hash: "+System.identityHashCode(clazz.getSuperclass()));
115             ClassCastException JavaDoc cce = new ClassCastException JavaDoc(msg.toString());
116             cce.initCause(e);
117          }
118       }
119       return factory;
120    }
121
122    /** This method is used to obtain an instance of the provider specific class
123     * that implements the PolicyConfiguration interface that corresponds to the
124     * identified policy context within the provider. The methods of the
125     * PolicyConfiguration interface are used to define the policy statements of
126     * the identified policy context.
127     *
128     * If at the time of the call, the identified policy context does not exist
129     * in the provider, then the policy context will be created in the provider
130     * and the Object that implements the context's PolicyConfiguration Interface
131     * will be returned. If the state of the identified context is "deleted" or
132     * "inService" it will be transitioned to the "open" state as a result of the
133     * call. The states in the lifecycle of a policy context are defined by the
134     * PolicyConfiguration interface.
135     *
136     * For a given value of policy context identifier, this method must always
137     * return the same instance of PolicyConfiguration and there must be at most
138     * one actual instance of a PolicyConfiguration with a given policy context
139     * identifier (during a process context).
140     *
141     * To preserve the invariant that there be at most one PolicyConfiguration
142     * object for a given policy context, it may be necessary for this method to
143     * be thread safe.
144     *
145     * @param contextID - the policy context ID indicates which
146     * PolicyConfiguration to return. This must not be null.
147     * @param remove - A boolean flag that establishes whether or not the policy
148     * statements of an existing policy context are to be removed before its
149     * PolicyConfiguration object is returned. If the value passed to this
150     * parameter is true, the policy statements of an existing policy context
151     * will be removed. If the value is false, they will not be removed.
152     * @return a PolicyConfiguration instance
153     * @throws PolicyContextException
154     */

155    public abstract PolicyConfiguration JavaDoc getPolicyConfiguration(String JavaDoc contextID,
156       boolean remove)
157       throws PolicyContextException JavaDoc;
158
159    /** This method determines if the identified policy context exists with state
160     * "inService" in the Policy provider associated with the factory.
161     *
162     * @param contextID - the context ID for selecting the policy
163     * @return true if the identified policy context exists within the provider
164     * and its state is "inService", false otherwise.
165     * @throws PolicyContextException
166     */

167    public abstract boolean inService(String JavaDoc contextID)
168       throws PolicyContextException JavaDoc;
169
170    /** A PrivilegedExceptionAction that looks up the class name identified
171     * by the javax.security.jacc.PolicyConfigurationFactory.provider system
172     * property and loads the class using the thread context class loader.
173     */

174    private static class LoadAction implements PrivilegedExceptionAction JavaDoc
175    {
176       private String JavaDoc name;
177       public String JavaDoc getName()
178       {
179          return name;
180       }
181       public Object JavaDoc run()
182          throws Exception JavaDoc
183       {
184          name = System.getProperty(FACTORY_PROP);
185          if( name == null )
186          {
187             // Use the default factory impl
188
name = DEFAULT_FACTORY_NAME;
189          }
190          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
191          Class JavaDoc factoryClass = loader.loadClass(name);
192          return factoryClass;
193       }
194    }
195 }
196
Popular Tags