KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > jacc > provider > JPolicyConfigurationFactory


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JPolicyConfigurationFactory.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.security.jacc.provider;
27
28 import java.security.SecurityPermission JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.security.jacc.PolicyConfiguration JavaDoc;
33 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
34 import javax.security.jacc.PolicyContextException JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38
39 /**
40  * Defines the PolicyConfigurationFactory implementation class of JACC.
41  * @author Florent Benoit
42  */

43 public class JPolicyConfigurationFactory extends PolicyConfigurationFactory JavaDoc {
44
45     /**
46      * List of PolicyConfiguration objects. Manage all configurations available
47      */

48     private Map JavaDoc<String JavaDoc, PolicyConfiguration JavaDoc> policyConfigurations = null;
49
50     /**
51      * Logger.
52      */

53     private static JLog logger = JLogFactory.getLog(JPolicyConfigurationFactory.class);
54
55     /**
56      * Constructor.
57      */

58     public JPolicyConfigurationFactory() {
59         policyConfigurations = new HashMap JavaDoc<String JavaDoc, PolicyConfiguration JavaDoc>();
60
61     }
62
63     /**
64      * This method is used to obtain an instance of the provider specific class
65      * that implements the PolicyConfiguration interface that corresponds to the
66      * identified policy context within the provider.
67      * @param contextID A String identifying the policy context whose
68      * PolicyConfiguration interface is to be returned. The value passed
69      * to this parameter must not be null.
70      * @param remove A boolean value that establishes whether or not the policy
71      * statements of an existing policy context are to be removed before
72      * its PolicyConfiguration object is returned. If the value passed to
73      * this parameter is true, the policy statements of an existing
74      * policy context will be removed. If the value is false, they will
75      * not be removed.
76      * @return an Object that implements the PolicyConfiguration Interface
77      * matched to the Policy provider and corresponding to the
78      * identified policy context.
79      * @throws SecurityException when called by an AccessControlContext that has
80      * not been granted the "setPolicy" SecurityPermission.
81      * @throws PolicyContextException if the implementation throws a checked
82      * exception that has not been accounted for by the
83      * getPolicyConfiguration method signature. The exception thrown by
84      * the implementation class will be encapsulated (during
85      * construction) in the thrown PolicyContextException.
86      */

87     @Override JavaDoc
88     public PolicyConfiguration JavaDoc getPolicyConfiguration(final String JavaDoc contextID, final boolean remove)
89             throws PolicyContextException JavaDoc, SecurityException JavaDoc {
90
91         // Section 3.3 - Check permissions
92
checkSetPolicy();
93
94         // Get in cache
95
PolicyConfiguration JavaDoc policyConfiguration = getInternalPolicyConfiguration(contextID);
96
97         // Is there an existing configuration ?
98
if (policyConfiguration != null) {
99             // Need to be removed ?
100
if (remove) {
101                 // Delete permissions
102
policyConfiguration.delete();
103                 ((JPolicyConfiguration) policyConfiguration).resetState();
104             }
105             // return cache
106
return policyConfiguration;
107         }
108
109         // No previous PolicyConfiguration for the specific contextID
110
// need to build a new PolicyConfiguration
111
policyConfiguration = new JPolicyConfiguration(contextID);
112
113         // Add in cache for future use and return it.
114
policyConfigurations.put(contextID, policyConfiguration);
115
116         return policyConfiguration;
117
118     }
119
120     /**
121      * This method is used to check if there the PolicyConfiguration is in cache
122      * and return it if it is in the cache.
123      * @param contextID A String identifying the policy context whose
124      * PolicyConfiguration interface is to be returned. The value passed
125      * to this parameter must not be null.
126      * @return an Object that implements the PolicyConfiguration Interface
127      * matched to the Policy provider and corresponding to the
128      * identified policy context.
129      */

130     private synchronized PolicyConfiguration JavaDoc getInternalPolicyConfiguration(final String JavaDoc contextID) {
131         // Get in cache
132
return policyConfigurations.get(contextID);
133     }
134
135     /**
136      * This method determines if the identified policy context exists with state
137      * "inService" in the Policy provider associated with the factory.
138      * @param contextID A string identifying a policy context
139      * @return true if the identified policy context exists within the provider
140      * and its state is "inService", false otherwise.
141      * @throws SecurityException when called by an AccessControlContext that has
142      * not been granted the "setPolicy" SecurityPermission.
143      * @throws PolicyContextException if the implementation throws a checked
144      * exception that has not been accounted for by the inService method
145      * signature. The exception thrown by the implementation class will
146      * be encapsulated (during construction) in the thrown
147      * PolicyContextException.
148      */

149     @Override JavaDoc
150     public boolean inService(final String JavaDoc contextID) throws PolicyContextException JavaDoc, SecurityException JavaDoc {
151
152         // Section 3.3 - Check permissions
153
logger.debug("Check setpolicy...");
154         checkSetPolicy();
155
156         // Context exists ?
157
if (policyConfigurations.containsKey(contextID)) {
158             logger.debug("Existing config for contextID ''{0}'', gets internal config...", contextID);
159             return getInternalPolicyConfiguration(contextID).inService();
160         }
161         // false otherwise (see javaDoc)
162
logger.debug("Config for contextID ''{0}'' not found, return false", contextID);
163         return false;
164     }
165
166     /**
167      * Method which check setPolicy access Section 3.3.<br/>
168      * getPolicyConfiguration and inService must throw a SecurityException when
169      * called by an AccessControlContext that has not been granted the
170      * "setPolicy" SecurityPermission
171      * @throws SecurityException when called by an AccessControlContext that has
172      * not been granted the "setPolicy" SecurityPermission.
173      */

174     private void checkSetPolicy() throws SecurityException JavaDoc {
175         SecurityManager JavaDoc securityManager = System.getSecurityManager();
176         if (securityManager != null) {
177             securityManager.checkPermission(new SecurityPermission JavaDoc("setPolicy"));
178         }
179     }
180
181 }
182
Popular Tags