KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > security > jacc > PolicyConfigurationFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package javax.security.jacc;
25
26
27 import java.security.AccessController JavaDoc;
28 import java.security.AccessControlException JavaDoc;
29 import java.security.PrivilegedExceptionAction JavaDoc;
30 import java.security.PrivilegedActionException JavaDoc;
31 import java.security.SecurityPermission JavaDoc;
32
33 import javax.security.jacc.PolicyConfiguration JavaDoc;
34
35 import javax.security.jacc.PolicyContextException JavaDoc;
36
37 /**
38  * Abstract factory and finder class for obtaining
39  * the instance of the class that implements the PolicyConfigurationFactory
40  * of a provider. The factory will be used to instantiate PolicyConfiguration
41  * objects that will be used by the deployment tools of the container
42  * to create and manage policy contexts within the Policy Provider.
43  * <P>
44  * Implementation classes must have a public no argument constructor that
45  * may be used to create an operational instance of the factory implementation
46  * class.
47  *
48  * @see java.security.Permission
49  * @see javax.security.jacc.PolicyConfiguration
50  * @see javax.security.jacc.PolicyContextException
51  *
52  * @author Ron Monzillo
53  * @author Gary Ellison
54  * @author Harpreet Singh
55  */

56
57 public abstract class PolicyConfigurationFactory
58 {
59     private static String JavaDoc FACTORY_NAME =
60     "javax.security.jacc.PolicyConfigurationFactory.provider";
61     
62     private static PolicyConfigurationFactory JavaDoc pcFactory;
63
64    /**
65     * This static method uses a system property to find and instantiate
66     * (via a public constructor) a provider specific factory implementation
67     * class. The name of the provider specific factory implementation class is
68     * obtained from the value of the system property,
69     * <P><code><Pre>
70     * javax.security.jacc.PolicyConfigurationFactory.provider.
71     * </Pre></code><P>
72     *
73     * @return the singleton instance of the provider specific
74     * PolicyConfigurationFactory implementation class.
75     *
76     * @throws java.lang.SecurityException
77     * when called by an AccessControlContext that has not been
78     * granted the "setPolicy" SecurityPermission.
79     *
80     * @throws java.lang.ClassNotFoundException
81     * when the class named by the system property could not be found
82     * including because the value of the system property has not be set.
83     *
84     * @throws javax.security.jacc.PolicyContextException
85     * if the implementation throws a checked exception that has not been
86     * accounted for by the getPolicyConfigurationFactory method signature.
87     * The exception thrown
88     * by the implementation class will be encapsulated (during construction)
89     * in the thrown PolicyContextException
90     */

91
92     public static PolicyConfigurationFactory JavaDoc getPolicyConfigurationFactory()
93         throws java.lang.ClassNotFoundException JavaDoc,
94         javax.security.jacc.PolicyContextException JavaDoc
95     {
96
97       SecurityManager JavaDoc sm = System.getSecurityManager();
98       if (sm != null) sm.checkPermission(new
99                  java.security.SecurityPermission JavaDoc("setPolicy"));
100       if(pcFactory != null)
101       return pcFactory;
102     
103       String JavaDoc msg;
104       final String JavaDoc classname[] = { null };
105
106       try {
107
108           Class JavaDoc clazz = null;
109           
110           if (sm != null){
111               try{
112                 clazz = (Class JavaDoc)AccessController.doPrivileged
113             (new PrivilegedExceptionAction JavaDoc(){
114                       public Object JavaDoc run() throws java.lang.Exception JavaDoc{
115               
116               classname[0] = System.getProperty(FACTORY_NAME);
117               
118               if(classname[0] == null){
119                   String JavaDoc msg = new String JavaDoc
120                   ("JACC:Error PolicyConfigurationFactory : property not set : "+ FACTORY_NAME);
121                   throw new ClassNotFoundException JavaDoc(msg);
122               }
123     
124               return Class.forName(classname[0],true,
125                   Thread.currentThread().getContextClassLoader());
126                       }
127                   });
128               } catch (PrivilegedActionException JavaDoc ex){
129                   Exception JavaDoc e = ex.getException() ;
130                   if ( e instanceof java.lang.ClassNotFoundException JavaDoc){
131                         throw (java.lang.ClassNotFoundException JavaDoc)e;
132                   } else if ( e instanceof java.lang.InstantiationException JavaDoc){
133                         throw (java.lang.InstantiationException JavaDoc)e;
134                   } else if ( e instanceof java.lang.IllegalAccessException JavaDoc){
135                         throw (java.lang.IllegalAccessException JavaDoc)e;
136                   }
137               }
138           } else {
139           classname[0] = System.getProperty(FACTORY_NAME);
140           
141           if (classname[0] == null){
142           msg = new String JavaDoc
143               ("JACC:Error PolicyConfigurationFactory : property not set : "+ FACTORY_NAME);
144           throw new ClassNotFoundException JavaDoc(msg);
145           }
146
147           clazz = Class.forName(classname[0],true,
148                     Thread.currentThread().getContextClassLoader());
149           }
150
151           Object JavaDoc factory = clazz.newInstance();
152
153           pcFactory = (PolicyConfigurationFactory JavaDoc) factory;
154
155       } catch(java.lang.ClassNotFoundException JavaDoc cnfe){
156       msg = new String JavaDoc
157           ("JACC:Error PolicyConfigurationFactory : cannot find class : "
158            + classname[0]);
159       throw new ClassNotFoundException JavaDoc(msg,cnfe);
160       } catch(java.lang.IllegalAccessException JavaDoc iae){
161       msg = new String JavaDoc
162           ("JACC:Error PolicyConfigurationFactory : cannot access class : "
163            + classname[0]);
164       throw new PolicyContextException JavaDoc(msg,iae);
165       } catch(java.lang.InstantiationException JavaDoc ie){
166       msg = new String JavaDoc
167           ("JACC:Error PolicyConfigurationFactory : cannot instantiate : "
168            + classname[0]);
169       throw new PolicyContextException JavaDoc(msg,ie);
170       } catch(java.lang.ClassCastException JavaDoc cce){
171       msg = new String JavaDoc
172           ("JACC:Error PolicyConfigurationFactory : class not PolicyConfigurationFactory : "+ classname[0]);
173       throw new ClassCastException JavaDoc(msg);
174       }
175
176       return pcFactory;
177       
178     }
179     
180    /**
181     * This method is used to obtain an instance of the provider specific
182     * class that implements the PolicyConfiguration interface that
183     * corresponds to the identified policy context within the provider.
184     * The methods of the PolicyConfiguration interface are used to
185     * define the policy statements of the identified policy context.
186     * <P>
187     * If at the time of the call, the identified policy context does not
188     * exist in the provider, then the policy context will be created
189     * in the provider and the Object that implements the context's
190     * PolicyConfiguration Interface will be returned. If the state of the
191     * identified context is "deleted" or "inService" it will be transitioned to
192     * the "open" state as a result of the call. The states in the lifecycle
193     * of a policy context are defined by the PolicyConfiguration interface.
194     * <P>
195     * For a given value of policy context identifier, this method
196     * must always return the same instance of PolicyConfiguration
197     * and there must be at most one actual instance of a
198     * PolicyConfiguration with a given policy context identifier
199     * (during a process context).
200     * <P>
201     * To preserve the invariant that there be at most one
202     * PolicyConfiguration object for a given policy context,
203     * it may be necessary for this method to be thread safe.
204     * <P>
205     * @param contextID A String identifying the policy context whose
206     * PolicyConfiguration interface is to be returned. The value passed to
207     * this parameter must not be null.
208     * <P>
209     * @param remove A boolean value that establishes whether or not the
210     * policy statements of an existing policy context are to be
211     * removed before its PolicyConfiguration object is returned. If the value
212     * passed to this parameter is true, the policy statements of
213     * an existing policy context will be removed. If the value is false,
214     * they will not be removed.
215     *
216     * @return an Object that implements the PolicyConfiguration
217     * Interface matched to the Policy provider and corresponding to the
218     * identified policy context.
219     *
220     * @throws java.lang.SecurityException
221     * when called by an AccessControlContext that has not been
222     * granted the "setPolicy" SecurityPermission.
223     *
224     * @throws javax.security.jacc.PolicyContextException
225     * if the implementation throws a checked exception that has not been
226     * accounted for by the getPolicyConfiguration method signature.
227     * The exception thrown
228     * by the implementation class will be encapsulated (during construction)
229     * in the thrown PolicyContextException.
230     */

231
232     public abstract PolicyConfiguration JavaDoc
233     getPolicyConfiguration(String JavaDoc contextID, boolean remove)
234         throws javax.security.jacc.PolicyContextException JavaDoc;
235
236    /**
237     * This method determines if the identified policy context
238     * exists with state "inService" in the Policy provider
239     * associated with the factory.
240     * <P>
241     * @param contextID A string identifying a policy context
242     *
243     * @return true if the identified policy context exists within the
244     * provider and its state is "inService", false otherwise.
245     *
246     * @throws java.lang.SecurityException
247     * when called by an AccessControlContext that has not been
248     * granted the "setPolicy" SecurityPermission.
249     *
250     * @throws javax.security.jacc.PolicyContextException
251     * if the implementation throws a checked exception that has not been
252     * accounted for by the inService method signature. The exception thrown
253     * by the implementation class will be encapsulated (during construction)
254     * in the thrown PolicyContextException.
255     */

256
257     public abstract boolean inService(String JavaDoc contextID)
258         throws javax.security.jacc.PolicyContextException JavaDoc;
259
260 }
261
Popular Tags