View Javadoc

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;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.servlet.ServletConfig;
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 acls = new HashMap();
54      protected Map perms = new HashMap();
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 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 username)
86      {
87          acls.remove(username);
88      }
89  
90      public Role getRole(String roleName)
91      {
92          return (Role)perms.get(roleName);
93      }
94  
95      public Role getRole(String username, String 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 username, String roleName, String 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());
120         }        
121     }
122 
123     public void addRole(String 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());
133         }        
134     }
135 
136 	public void addRole(String 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());
146 		}
147 	}
148 
149 	public boolean hasRole(String username, String roleName)
150 	{
151 		return hasRole(username, roleName, GroupManagement.DEFAULT_GROUP_NAME);
152 	}
153 
154 	public boolean hasRole(String username, String roleName, String 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 username, String roleName)
165 	{
166 		removeRole(username, roleName, GroupManagement.DEFAULT_GROUP_NAME);
167 	}
168 
169 	public void removeRole(String username, String roleName, String 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 username)
181     {
182         return (CachedAcl)acls.get(username);
183     }
184 
185 
186     public Iterator getRoles(String 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 roleName, String permissionName)
197     {        
198         Map map = (Map)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 roleName, Permission permission)
207     {
208         Map map = (Map)perms.get(roleName);
209         if (null != map)
210         {
211             map.put(permission.getName(), permission);
212         }
213     }
214 
215     public boolean hasPermission(String roleName, String permissionName)
216     {
217         Map map = (Map)perms.get(roleName);
218         if (null != map)
219         {
220             return map.containsKey(permissionName);
221         }
222         return false;
223     }
224 
225     public void removePermission(String roleName, String permissionName)
226     {
227         Map map = (Map)perms.get(roleName);
228         if (null != map)
229         {
230             map.remove(permissionName);
231         }
232     }
233 
234     public Iterator getPermissions(String roleName)
235     {
236         Map map = (Map)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 rolename)
246     {
247         Iterator 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 permissionName)
257     {
258         Iterator iterator = perms.values().iterator();
259         while (iterator.hasNext())
260         {
261             Map map = (Map)iterator.next();
262             map.remove(permissionName);
263         }
264     }
265 
266     public void loadRolePermissions()
267     {
268         try
269         {
270             Iterator roles = JetspeedSecurity.getRoles();
271             while (roles.hasNext())
272             {
273                 Role role = (Role)roles.next();
274                 Map map = new HashMap();
275                 Iterator 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 conf) 
302         throws InitializationException 
303     {
304         if (getInit()) return;
305 
306         super.init(conf);
307 
308         setInit(true);
309      }
310 
311 }
312 
313