KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > PermissionCacheFactory


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 package com.sun.enterprise.security;
24
25 import java.security.AccessController JavaDoc;
26 import java.security.CodeSource JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.security.Policy JavaDoc;
29 import java.security.PrivilegedExceptionAction JavaDoc;
30 import java.security.PrivilegedActionException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import javax.security.jacc.PolicyContext JavaDoc;
37 import com.sun.enterprise.J2EESecurityManager;
38
39 import com.sun.logging.LogDomains;
40
41 /**
42  * This class is the factory for creating and managing PermissionCache.
43  * @author Shing Wai Chan
44  */

45
46 public class PermissionCacheFactory {
47
48     private static HashMap JavaDoc cacheMap = new HashMap JavaDoc();
49     private static int factoryKey = 0;
50     private static boolean supportsReuse = false;
51
52     private static Permission JavaDoc[] protoPerms = {
53     new java.net.SocketPermission JavaDoc("localhost","connect"),
54     new java.util.PropertyPermission JavaDoc("x","read")
55     };
56  
57     private static PermissionCache securityManagerCache =
58     createSecurityManagerCache();
59
60     static {
61         try {
62         // make a call to policy.refresh() to see if the provider
63
// calls the supportsReuse callback (see resetCaches below).
64
// which will set supportsReuse to true (to enable caching).
65
Policy JavaDoc policy = Policy.getPolicy();
66         if (policy != null) {
67         policy.refresh();
68         }
69         } catch(Exception JavaDoc pe) {
70         }
71     }
72
73     /**
74      * Reserve the next Cache Key for subsequent registration.
75      * @return the key as an Integer object.
76      */

77     private static Integer JavaDoc getNextKey() {
78    
79     Integer JavaDoc key = new Integer JavaDoc(factoryKey++);
80    
81     while (cacheMap.get(key) != null) {
82         key = new Integer JavaDoc(factoryKey++);
83     }
84     
85     return key;
86     }
87
88     private static synchronized PermissionCache createSecurityManagerCache() {
89
90         Integer JavaDoc key = getNextKey();
91
92     PermissionCache cache =
93
94         new PermissionCache(key, null, null, protoPerms, null);
95
96         return registerPermissionCache(cache);
97     }
98
99     /**
100      * Create a PermissionCache object.
101      * If the corresponding object exists, then it will overwrite the
102      * previous one.
103      * @param pcID - a string identifying the policy context and which must
104      * be set when getPermissions is called (internally).
105      * This value may be null, in which case the permisions of
106      * the default policy context will be cached.
107      * @param codesource - the codesource argument to be used in the call
108      * to getPermissions. This value may be null.
109      * @param perms - an array of Permission objects identifying the
110      * permission types that will be managed by the cache.
111      * This value may be null, in which case all permissions
112      * obtained by the getPermissions call will be cached.
113      * @param name - a string corresponding to a value returned by
114      * Permission.getName() only permissions whose getName()
115      * value matches the name parameter will be included
116      * in the cache. This value may be null, in which case
117      * permission name dos not factor into the
118      * permission caching.
119      */

120     public static synchronized PermissionCache
121     createPermissionCache(String JavaDoc pcID,
122         CodeSource JavaDoc codesource, Permission JavaDoc[] perms, String JavaDoc name) {
123
124         if (!supportsReuse) {
125             return null;
126         }
127
128         Integer JavaDoc key = getNextKey();
129
130     PermissionCache cache =
131         new PermissionCache(key, pcID, codesource, perms, name);
132
133         return registerPermissionCache(cache);
134     }
135
136     /**
137      * Create a PermissionCache object.
138      * If the corresponding object exists, then it will overwrite the
139      * previous one.
140      * @param pcID - a string identifying the policy context and which must
141      * be set when getPermissions is called (internally).
142      * This value may be null, in which case the permisions of
143      * the default policy context will be cached.
144      * @param codesource - the codesource argument to be used in the call
145      * to getPermissions. This value may be null.
146      * @param clazz - a class object identifying the
147      * permission type that will be managed by the cache.
148      * This value may be null, in which case all permissions
149      * obtained by the getPermissions call will be cached.
150      * @param name - a string corresponding to a value returned by
151      * Permission.getName() only permissions whose getName()
152      * value matches the name parameter will be included
153      * in the cache. This value may be null, in which case
154      * permission name dos not factor into the
155      * permission caching.
156      */

157     public static synchronized PermissionCache
158     createPermissionCache(String JavaDoc pcID,
159         CodeSource JavaDoc codesource, Class JavaDoc clazz, String JavaDoc name) {
160   
161     if (!supportsReuse) {
162         return null;
163     }
164   
165     Integer JavaDoc key = getNextKey();
166  
167     PermissionCache cache =
168         new PermissionCache(key, pcID, codesource, clazz, name);
169     
170     return registerPermissionCache(cache);
171     }
172  
173      /**
174       * Register a PermissionCache object with the factory. If an object is
175       * already registered at the key, it will be overidden.
176       * @param cache a cache with an internal key value.
177       * @return the cache object
178       */

179     private static PermissionCache
180     registerPermissionCache(PermissionCache cache) {
181     cacheMap.put(cache.getFactoryKey(),cache);
182     
183     return cache;
184     }
185
186     public static synchronized PermissionCache
187     removePermissionCache(PermissionCache cache) {
188   
189     PermissionCache rvalue = null;
190   
191     if (cache != null) {
192         
193         Object JavaDoc value = cacheMap.remove(cache.getFactoryKey());
194   
195         if (value != null && value instanceof PermissionCache) {
196         rvalue = (PermissionCache) value;
197         rvalue.reset();
198         }
199     }
200     return rvalue;
201     }
202
203     /**
204      * This resets all caches inside the factory.
205      */

206     public static synchronized void resetCaches() {
207
208     supportsReuse = true;
209
210     SecurityManager JavaDoc sm = System.getSecurityManager();
211     if (sm != null && sm instanceof J2EESecurityManager) {
212         if (!((J2EESecurityManager)sm).cacheEnabled()) {
213         ((J2EESecurityManager)sm).enablePermissionCache
214             (securityManagerCache);
215         }
216     }
217
218     Iterator JavaDoc iter = cacheMap.values().iterator();
219     while (iter.hasNext()) {
220         Object JavaDoc cache = iter.next();
221         if (cache instanceof PermissionCache) {
222         ((PermissionCache) cache).reset();
223         }
224     }
225     }
226 }
227
Popular Tags