KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > security > jacc > JPolicyConfigurationFactory


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@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  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JPolicyConfigurationFactory.java,v 1.3 2004/04/09 09:58:27 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.security.jacc;
28
29
30 import java.security.SecurityPermission JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.security.jacc.PolicyConfiguration JavaDoc;
35 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
36 import javax.security.jacc.PolicyContextException JavaDoc;
37
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40
41 import org.objectweb.jonas_lib.I18n;
42
43 import org.objectweb.common.TraceCore;
44
45 /**
46  * Defines the PolicyConfigurationFactory implementation class of JACC
47  * @author Florent Benoit
48  */

49 public class JPolicyConfigurationFactory extends PolicyConfigurationFactory JavaDoc {
50
51     /**
52      * Name of the Factory
53      */

54     public static final String JavaDoc FACTORY_NAME = "org.objectweb.jonas_lib.security.jacc.JPolicyConfigurationFactory";
55
56     /**
57      * I18n
58      */

59     private static I18n i18n = I18n.getInstance(JPolicyConfigurationFactory.class);
60
61     /**
62      * List of PolicyConfiguration objects
63      * Manage all configurations available
64      */

65     private Map JavaDoc policyConfigurations = null;
66
67     /**
68      * Logger
69      */

70     private static Logger logger = null;
71
72     /**
73      * Constructor
74      */

75     public JPolicyConfigurationFactory() {
76         policyConfigurations = new HashMap JavaDoc();
77         logger = TraceCore.sec;
78
79     }
80
81     /**
82      * This method is used to obtain an instance of the provider specific
83      * class that implements the PolicyConfiguration interface that corresponds
84      * to the identified policy context within the provider.
85      * @param contextID A String identifying the policy context whose
86      * PolicyConfiguration interface is to be returned. The value passed
87      * to this parameter must not be null.
88      * @param remove A boolean value that establishes whether or not the policy
89      * statements of an existing policy context are to be removed before
90      * its PolicyConfiguration object is returned. If the value passed to
91      * this parameter is true, the policy statements of an existing
92      * policy context will be removed. If the value is false,
93      * they will not be removed.
94      * @return an Object that implements the PolicyConfiguration Interface
95      * matched to the Policy provider and corresponding to the
96      * identified policy context.
97      * @throws SecurityException when called by an AccessControlContext that
98      * has not been granted the "setPolicy" SecurityPermission.
99      * @throws PolicyContextException if the implementation throws a checked
100      * exception that has not been accounted for by the
101      * getPolicyConfiguration method signature. The exception thrown
102      * by the implementation class will be encapsulated
103      * (during construction) in the thrown PolicyContextException.
104      */

105     public PolicyConfiguration JavaDoc getPolicyConfiguration(String JavaDoc contextID, boolean remove) throws PolicyContextException JavaDoc, SecurityException JavaDoc {
106
107         // Section 3.3 - Check permissions
108
checkSetPolicy();
109
110
111         // Get in cache
112
PolicyConfiguration JavaDoc policyConfiguration = getInternalPolicyConfiguration(contextID);
113
114         // Is there an existing configuration ?
115
if (policyConfiguration != null) {
116             // Need to be removed ?
117
if (remove) {
118                 // Delete permissions
119
policyConfiguration.delete();
120                 ((JPolicyConfiguration) policyConfiguration).resetState();
121             }
122             // return cache
123
return policyConfiguration;
124         } else { //No previous PolicyConfiguration for the specific contextID
125
// need to build a new PolicyConfiguration
126
policyConfiguration = new JPolicyConfiguration(contextID);
127         }
128
129         // Add in cache and return it.
130
policyConfigurations.put(contextID, policyConfiguration);
131
132         return policyConfiguration;
133
134     }
135
136
137     /**
138      * This method is used to check if there the PolicyConfiguration is in cache
139      * and return it if it is in the cache.
140      * @param contextID A String identifying the policy context whose
141      * PolicyConfiguration interface is to be returned. The value passed
142      * to this parameter must not be null.
143      * @return an Object that implements the PolicyConfiguration Interface
144      * matched to the Policy provider and corresponding to the
145      * identified policy context.
146      */

147     private synchronized PolicyConfiguration JavaDoc getInternalPolicyConfiguration(String JavaDoc contextID) {
148         // Get in cache
149
return (PolicyConfiguration JavaDoc) policyConfigurations.get(contextID);
150     }
151
152
153     /**
154      * This method determines if the identified policy context exists
155      * with state "inService" in the Policy provider associated with
156      * the factory.
157      * @param contextID A string identifying a policy context
158      * @return true if the identified policy context exists within
159      * the provider and its state is "inService", false otherwise.
160      * @throws SecurityException when called by an AccessControlContext
161      * that has not been granted the "setPolicy" SecurityPermission.
162      * @throws PolicyContextException if the implementation throws a checked
163      * exception that has not been accounted for by the inService
164      * method signature. The exception thrown by the implementation
165      * class will be encapsulated (during construction) in the thrown
166      * PolicyContextException.
167      */

168     public boolean inService(String JavaDoc contextID) throws PolicyContextException JavaDoc, SecurityException JavaDoc {
169
170         if (logger.isLoggable(BasicLevel.DEBUG)) {
171             logger.log(BasicLevel.DEBUG, "Check setpolicy...");
172         }
173
174         // Section 3.3 - Check permissions
175
checkSetPolicy();
176
177         if (logger.isLoggable(BasicLevel.DEBUG)) {
178             logger.log(BasicLevel.DEBUG, "Context exist ?");
179         }
180
181         // Context exists ?
182
if (policyConfigurations.containsKey(contextID)) {
183             if (logger.isLoggable(BasicLevel.DEBUG)) {
184                 logger.log(BasicLevel.DEBUG, "Look internal config...");
185             }
186             return getInternalPolicyConfiguration(contextID).inService();
187         } else {
188             // false otherwise (see javaDoc)
189
if (logger.isLoggable(BasicLevel.DEBUG)) {
190                 logger.log(BasicLevel.DEBUG, "Policy configuration not found, return false");
191             }
192             return false;
193         }
194     }
195
196     /**
197      * Method which check setPolicy access
198      * Section 3.3 : getPolicyConfiguration and inService must throw a
199      * SecurityException when called by an AccessControlContext that has
200      * not been granted the "setPolicy" SecurityPermission
201      * @throws SecurityException when called by an AccessControlContext that
202      * has not been granted the "setPolicy" SecurityPermission.
203      */

204     private void checkSetPolicy() throws SecurityException JavaDoc {
205         SecurityManager JavaDoc securityManager = System.getSecurityManager();
206         if (securityManager != null) {
207             securityManager.checkPermission(new SecurityPermission JavaDoc("setPolicy"));
208         }
209     }
210
211
212 }
213
Popular Tags