KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > provider > PolicyConfigurationFactoryImpl


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 com.sun.enterprise.security.provider;
25
26 import javax.security.jacc.*;
27
28 import java.util.logging.*;
29 import com.sun.logging.LogDomains;
30
31 import java.util.*;
32 import java.io.File JavaDoc;
33
34 /**
35  * Implementation of jacc PolicyConfigurationFactory class
36  * @author Harpreet Singh
37  * @author Ron Monzillo
38  * @version
39  */

40 public class PolicyConfigurationFactoryImpl
41     extends PolicyConfigurationFactory{
42
43     // Table of ContextId->PolicyConfiguration
44
static Map polConfTable = new HashMap();
45
46     private static Logger logger =
47     Logger.getLogger(LogDomains.SECURITY_LOGGER);
48
49     public PolicyConfigurationFactoryImpl(){
50     }
51
52    /**
53     * This method is used to obtain an instance of the provider specific
54     * class that implements the PolicyConfiguration interface that
55     * corresponds to the identified policy context within the provider.
56     * The methods of the PolicyConfiguration interface are used to
57     * define the policy statements of the identified policy context.
58     * <P>
59     * If at the time of the call, the identified policy context does not
60     * exist in the provider, then the policy context will be created
61     * in the provider and the Object that implements the context's
62     * PolicyConfiguration Interface will be returned. If the state of the
63     * identified context is "deleted" or "inService" it will be transitioned to
64     * the "open" state as a result of the call. The states in the lifecycle
65     * of a policy context are defined by the PolicyConfiguration interface.
66     * <P>
67     * For a given value of policy context identifier, this method
68     * must always return the same instance of PolicyConfiguration
69     * and there must be at most one actual instance of a
70     * PolicyConfiguration with a given policy context identifier
71     * (during a process context).
72     * <P>
73     * To preserve the invariant that there be at most one
74     * PolicyConfiguration object for a given policy context,
75     * it may be necessary for this method to be thread safe.
76     * <P>
77     * @param contextID A String identifying the policy context whose
78     * PolicyConfiguration interface is to be returned. The value passed to
79     * this parameter must not be null.
80     * <P>
81     * @param remove A boolean value that establishes whether or not the
82     * policy statements of an existing policy context are to be
83     * removed before its PolicyConfiguration object is returned. If the value
84     * passed to this parameter is true, the policy statements of
85     * an existing policy context will be removed. If the value is false,
86     * they will not be removed.
87     *
88     * @return an Object that implements the PolicyConfiguration
89     * Interface matched to the Policy provider and corresponding to the
90     * identified policy context.
91     *
92     * @throws java.lang.SecurityException
93     * when called by an AccessControlContext that has not been
94     * granted the "setPolicy" SecurityPermission.
95     *
96     * @throws javax.security.jacc.PolicyContextException
97     * if the implementation throws a checked exception that has not been
98     * accounted for by the getPolicyConfiguration method signature.
99     * The exception thrown
100     * by the implementation class will be encapsulated (during construction)
101     * in the thrown PolicyContextException.
102     */

103     public PolicyConfiguration getPolicyConfiguration(String JavaDoc contextId, boolean remove)
104     throws PolicyContextException {
105
106     PolicyConfigurationImpl.checkSetPolicyPermission();
107     
108     if(logger.isLoggable(Level.FINE)){
109         logger.fine("JACC Policy Provider: Getting PolicyConfiguration object with id = "+ contextId);
110     }
111
112     PolicyConfiguration pc = (PolicyConfiguration)polConfTable.get(contextId);
113
114     // if the pc is not in the table, see if it was copied into the
115
// filesystem (e.g. by the DAS)
116
if (pc == null){
117         pc = getPolicyConfigurationImplFromDirectory(contextId,true,remove);
118         if (pc == null) {
119         pc = new PolicyConfigurationImpl(contextId);
120         polConfTable.put(contextId,pc);
121         }
122     } else {
123         // return the policy configuration to the open state, value of
124
// remove will determine if statements are removed
125
((PolicyConfigurationImpl) pc).initialize(true,remove,false);
126     }
127     return pc;
128     }
129
130    /**
131     * This method determines if the identified policy context
132     * exists with state "inService" in the Policy provider
133     * associated with the factory.
134     * <P>
135     * @param contextID A string identifying a policy context
136     *
137     * @return true if the identified policy context exists within the
138     * provider and its state is "inService", false otherwise.
139     *
140     * @throws java.lang.SecurityException
141     * when called by an AccessControlContext that has not been
142     * granted the "setPolicy" SecurityPermission.
143     *
144     * @throws javax.security.jacc.PolicyContextException
145     * if the implementation throws a checked exception that has not been
146     * accounted for by the inService method signature. The exception thrown
147     * by the implementation class will be encapsulated (during construction)
148     * in the thrown PolicyContextException.
149     */

150     public boolean inService(String JavaDoc contextID) throws PolicyContextException{
151     PolicyConfigurationImpl.checkSetPolicyPermission();
152     PolicyConfiguration pc = (PolicyConfiguration)polConfTable.get(contextID);
153
154     // if the pc is not in the table, see if it was copied into the
155
// filesystem (e.g. by the DAS)
156
if (pc == null) {
157         pc = getPolicyConfigurationImplFromDirectory(contextID,false,false);
158     }
159     return pc == null ? false : pc.inService();
160     }
161
162     // finds pc copied into the filesystem (by DAS) after the repository was
163
// initialized. Will only open pc if remove is true (otherwise pc will
164
// remain in service);
165

166     private static PolicyConfigurationImpl
167     getPolicyConfigurationImplFromDirectory(String JavaDoc contextId, boolean open, boolean remove){
168     PolicyConfigurationImpl pci = null;
169     File JavaDoc f = new File JavaDoc(PolicyConfigurationImpl.getContextDirectoryName(contextId));
170     if (f.exists()) {
171         pci = new PolicyConfigurationImpl(f,open,remove);
172         if (pci != null) {
173         polConfTable.put(contextId,pci);
174         }
175     }
176     return pci;
177     }
178
179     // The following package protected methods are needed to support the
180
// PolicyCongigurationImpl class.
181

182     protected static Collection getPolicyConfigurationImpls() {
183     return polConfTable.values();
184     }
185
186     protected static PolicyConfigurationImpl
187     putPolicyConfigurationImpl(String JavaDoc contextID, PolicyConfigurationImpl pc) {
188     return (PolicyConfigurationImpl) polConfTable.put(contextID,pc);
189     }
190
191     // does not reopen PC
192
protected static PolicyConfigurationImpl getPolicyConfigurationImpl(String JavaDoc contextId) {
193     PolicyConfigurationImpl pci =
194         (PolicyConfigurationImpl)polConfTable.get(contextId);
195     if (pci == null) {
196         // check if pc was copied into the filesystem after the repository
197
// was initialized (do not open pc or remove policy statements).
198
pci = getPolicyConfigurationImplFromDirectory(contextId,false,false);
199         if (pci == null) {
200         logger.log(Level.WARNING,"pc.unknown_policy_context",
201                new Object JavaDoc[]{contextId});
202         }
203     }
204     return pci;
205     }
206
207 }
208
Popular Tags