KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > security > SecurityCacheImpl


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.jetspeed.services.security;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletConfig JavaDoc;
25
26 import org.apache.jetspeed.om.security.Group;
27 import org.apache.jetspeed.om.security.Permission;
28 import org.apache.jetspeed.om.security.Role;
29 import org.apache.jetspeed.services.JetspeedSecurity;
30 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31 import org.apache.jetspeed.services.logging.JetspeedLogger;
32 import org.apache.turbine.services.InitializationException;
33 import org.apache.turbine.services.TurbineBaseService;
34 import org.apache.turbine.services.TurbineServices;
35
36 /**
37  * The Security Cache Service caches roles and permissions (ACLs)
38  *
39  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
40  * @author <a HREF="mailto:morciuch@apache.org">Mark Orciuch</a>
41  * @version $Id: SecurityCacheImpl.java,v 1.10 2004/02/23 03:58:11 jford Exp $
42  */

43
44
45 public class SecurityCacheImpl extends TurbineBaseService
46                                 implements SecurityCacheService
47 {
48     /**
49      * Static initialization of the logger for this class
50      */

51     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(SecurityCacheImpl.class.getName());
52     
53     protected Map JavaDoc acls = new HashMap JavaDoc();
54     protected Map JavaDoc perms = new HashMap JavaDoc();
55
56    /*
57     * Utility method for accessing the service
58     * implementation
59     *
60     * @return a SecurityCacheService implementation instance
61     */

62    protected static SecurityCacheService getService()
63    {
64        return (SecurityCacheService)TurbineServices
65        .getInstance().getService(SecurityCacheService.SERVICE_NAME);
66    }
67
68    /*
69     * Load the security cache for the given user's roles and permissions.
70     *
71     * @param JetspeedUser the user to cache all role and permission information for.
72     */

73     public void load(String JavaDoc username)
74         throws JetspeedSecurityException
75     {
76         CachedAcl acl = new CachedAcl(username);
77         acl.setRoles(JetspeedSecurity.getRoles(username));
78         acls.put(username, acl);
79         if (perms.size() == 0)
80         {
81             loadRolePermissions();
82         }
83     }
84
85     public void unload(String JavaDoc username)
86     {
87         acls.remove(username);
88     }
89
90     public Role getRole(String JavaDoc roleName)
91     {
92         return (Role)perms.get(roleName);
93     }
94
95     public Role getRole(String JavaDoc username, String JavaDoc roleName)
96     {
97         CachedAcl acl = (CachedAcl)acls.get(username);
98         if (acl == null)
99         {
100             return null;
101         }
102         return acl.getRole(roleName);
103     }
104     
105     public Role getRole(String JavaDoc username, String JavaDoc roleName, String JavaDoc groupName)
106     {
107         CachedAcl acl = (CachedAcl) acls.get(username);
108         if (acl == null)
109         {
110             return null;
111         }
112         return acl.getRole(roleName, groupName);
113     }
114     
115     public void addRole(Role role)
116     {
117         if (!perms.containsKey(role.getName()))
118         {
119             perms.put(role.getName(), new HashMap JavaDoc());
120         }
121     }
122
123     public void addRole(String JavaDoc username, Role role)
124     {
125         CachedAcl acl = (CachedAcl)acls.get(username);
126         if (null != acl)
127         {
128             acl.addRole(role);
129         }
130         if (!perms.containsKey(role.getName()))
131         {
132             perms.put(role.getName(), new HashMap JavaDoc());
133         }
134     }
135
136     public void addRole(String JavaDoc username, Role role, Group group)
137     {
138         CachedAcl acl = (CachedAcl) acls.get(username);
139         if (null != acl)
140         {
141             acl.addRole(role, group);
142         }
143         if (!perms.containsKey(role.getName()))
144         {
145             perms.put(role.getName(), new HashMap JavaDoc());
146         }
147     }
148
149     public boolean hasRole(String JavaDoc username, String JavaDoc roleName)
150     {
151         return hasRole(username, roleName, GroupManagement.DEFAULT_GROUP_NAME);
152     }
153
154     public boolean hasRole(String JavaDoc username, String JavaDoc roleName, String JavaDoc groupName)
155     {
156         CachedAcl acl = (CachedAcl) acls.get(username);
157         if (null != acl)
158         {
159             return acl.hasRole(roleName, groupName);
160         }
161         return false;
162     }
163
164     public void removeRole(String JavaDoc username, String JavaDoc roleName)
165     {
166         removeRole(username, roleName, GroupManagement.DEFAULT_GROUP_NAME);
167     }
168
169     public void removeRole(String JavaDoc username, String JavaDoc roleName, String JavaDoc groupName)
170     {
171         CachedAcl acl = (CachedAcl) acls.get(username);
172         if (null != acl)
173         {
174             acl.removeRole(roleName, groupName);
175         }
176         // TODO: Why do this?
177
perms.remove(roleName);
178     }
179
180     public CachedAcl getAcl(String JavaDoc username)
181     {
182         return (CachedAcl)acls.get(username);
183     }
184
185
186     public Iterator JavaDoc getRoles(String JavaDoc username)
187     {
188         CachedAcl acl = (CachedAcl)acls.get(username);
189         if (null != acl)
190         {
191             return acl.getRoles();
192         }
193         return null;
194     }
195
196     public Permission getPermission(String JavaDoc roleName, String JavaDoc permissionName)
197     {
198         Map JavaDoc map = (Map JavaDoc)perms.get(roleName);
199         if (null != map)
200         {
201             return (Permission)map.get(permissionName);
202         }
203         return null;
204     }
205     
206     public void addPermission(String JavaDoc roleName, Permission permission)
207     {
208         Map JavaDoc map = (Map JavaDoc)perms.get(roleName);
209         if (null != map)
210         {
211             map.put(permission.getName(), permission);
212         }
213     }
214
215     public boolean hasPermission(String JavaDoc roleName, String JavaDoc permissionName)
216     {
217         Map JavaDoc map = (Map JavaDoc)perms.get(roleName);
218         if (null != map)
219         {
220             return map.containsKey(permissionName);
221         }
222         return false;
223     }
224
225     public void removePermission(String JavaDoc roleName, String JavaDoc permissionName)
226     {
227         Map JavaDoc map = (Map JavaDoc)perms.get(roleName);
228         if (null != map)
229         {
230             map.remove(permissionName);
231         }
232     }
233
234     public Iterator JavaDoc getPermissions(String JavaDoc roleName)
235     {
236         Map JavaDoc map = (Map JavaDoc)perms.get(roleName);
237         if (map != null)
238         {
239             return map.values().iterator();
240         }
241         return null;
242     }
243
244
245     public void removeAllRoles(String JavaDoc rolename)
246     {
247         Iterator JavaDoc iterator = acls.values().iterator();
248         while (iterator.hasNext())
249         {
250             CachedAcl acl = (CachedAcl)iterator.next();
251             acl.removeRole(rolename);
252         }
253         perms.remove(rolename);
254     }
255
256     public void removeAllPermissions(String JavaDoc permissionName)
257     {
258         Iterator JavaDoc iterator = perms.values().iterator();
259         while (iterator.hasNext())
260         {
261             Map JavaDoc map = (Map JavaDoc)iterator.next();
262             map.remove(permissionName);
263         }
264     }
265
266     public void loadRolePermissions()
267     {
268         try
269         {
270             Iterator JavaDoc roles = JetspeedSecurity.getRoles();
271             while (roles.hasNext())
272             {
273                 Role role = (Role)roles.next();
274                 Map JavaDoc map = new HashMap JavaDoc();
275                 Iterator JavaDoc prms = JetspeedSecurity.getPermissions(role.getName());
276                 while (prms.hasNext())
277                 {
278                     Permission perm = (Permission)prms.next();
279                     map.put(perm.getName(), perm);
280                 }
281                 perms.put(role.getName(), map);
282             }
283         }
284         catch (JetspeedSecurityException e)
285         {
286             logger.error("Exception", e);
287         }
288     }
289     ///////////////////////////////////////////////////////////////////////////
290
// Service Init
291
///////////////////////////////////////////////////////////////////////////
292

293
294     /**
295      * This is the early initialization method called by the
296      * Turbine <code>Service</code> framework
297      * @param conf The <code>ServletConfig</code>
298      * @exception throws a <code>InitializationException</code> if the service
299      * fails to initialize
300      */

301     public synchronized void init(ServletConfig JavaDoc conf)
302         throws InitializationException
303     {
304         if (getInit()) return;
305
306         super.init(conf);
307
308         setInit(true);
309      }
310
311 }
312
313
314
Popular Tags