View Javadoc
1   package org.apache.fulcrum.security;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.avalon.framework.activity.Initializable;
23  import org.apache.avalon.framework.logger.AbstractLogEnabled;
24  import org.apache.avalon.framework.service.ServiceException;
25  import org.apache.avalon.framework.service.ServiceManager;
26  import org.apache.avalon.framework.service.Serviceable;
27  import org.apache.avalon.framework.thread.ThreadSafe;
28  
29  /**
30   * This a basis implementation of the Fulcrum security service.
31   * 
32   * Provided functionality includes:
33   * <ul>
34   * <li>methods for retrieving different types of managers.
35   * <li>avalon lifecyle managers.
36   * </ul>
37   * 
38   * @author <a href="mailto:epugh@upstate.com">Eric PUgh</a>
39   * @version $Id$
40   */
41  public class BaseSecurityService extends AbstractLogEnabled implements SecurityService, Initializable, Serviceable, ThreadSafe
42  {
43      private ServiceManager manager = null;
44      // management of Groups/Role/Permissions
45  
46      // temporary storage of the classnames prior to initialization
47      String userClassName;
48      String groupClassName;
49      String permissionClassName;
50      String roleClassName;
51      String aclClassName;
52      /** The instance of UserManager the SecurityService uses */
53      protected UserManager userManager = null;
54      /** The instance of RoleManager the SecurityService uses */
55      protected RoleManager roleManager = null;
56      /** The instance of GroupManager the SecurityService uses */
57      protected GroupManager groupManager = null;
58      /** The instance of PermissionManager the SecurityService uses */
59      protected PermissionManager permissionManager = null;
60      /** The instance of ModelManager the SecurityService uses */
61      protected ModelManager modelManager = null;
62  
63      /**
64       * Returns the configured UserManager.
65       * 
66       * @return An UserManager object
67       */
68      public UserManager getUserManager()
69      {
70          if (userManager == null)
71          {
72              try
73              {
74                  userManager = (UserManager) manager.lookup(UserManager.ROLE);
75              }
76              catch (ServiceException ce)
77              {
78                  throw new RuntimeException(ce.getMessage(), ce);
79              }
80          }
81          return userManager;
82      }
83  
84      /**
85       * Returns the configured GroupManager.
86       * 
87       * @return An UserManager object
88       */
89      public GroupManager getGroupManager()
90      {
91          if (groupManager == null)
92          {
93              try
94              {
95                  groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
96              }
97              catch (ServiceException ce)
98              {
99                  throw new RuntimeException(ce.getMessage(), ce);
100             }
101         }
102         return groupManager;
103     }
104 
105     /**
106      * Returns the configured RoleManager.
107      * 
108      * @return An RoleManager object
109      */
110     public RoleManager getRoleManager()
111     {
112         if (roleManager == null)
113         {
114             try
115             {
116                 roleManager = (RoleManager) manager.lookup(RoleManager.ROLE);
117             }
118             catch (ServiceException ce)
119             {
120                 throw new RuntimeException(ce.getMessage(), ce);
121             }
122         }
123         return roleManager;
124     }
125 
126     /**
127      * Returns the configured PermissionManager.
128      * 
129      * @return An PermissionManager object
130      */
131     public PermissionManager getPermissionManager()
132     {
133         if (permissionManager == null)
134         {
135             try
136             {
137                 permissionManager = (PermissionManager) manager.lookup(PermissionManager.ROLE);
138             }
139             catch (ServiceException ce)
140             {
141                 throw new RuntimeException(ce.getMessage(), ce);
142             }
143         }
144         return permissionManager;
145     }
146 
147     /**
148      * Returns the configured ModelManager.
149      * 
150      * @return An ModelManager object
151      */
152     public ModelManager getModelManager()
153     {
154         if (modelManager == null)
155         {
156             try
157             {
158                 modelManager = (ModelManager) manager.lookup(ModelManager.ROLE);
159             }
160             catch (ServiceException ce)
161             {
162                 throw new RuntimeException(ce.getMessage(), ce);
163             }
164         }
165         return modelManager;
166     }
167 
168     /**
169      * Configure a new role Manager.
170      * 
171      * @param permissionManager
172      *            An PermissionManager object
173      */
174     // void setPermissionManager(PermissionManager permissionManager);
175 
176     /**
177      * Avalon Service lifecycle method
178      */
179     public void service(ServiceManager manager) throws ServiceException
180     {
181         this.manager = manager;
182     }
183 
184     /**
185      * Avalon Service lifecycle method Initializes the SecurityService, locating
186      * the appropriate UserManager
187      * 
188      * @throws Exception
189      *             A Problem occurred while initializing the User Manager.
190      */
191     public void initialize() throws Exception
192     {
193         userClassName = null;
194         groupClassName = null;
195         permissionClassName = null;
196         roleClassName = null;
197         aclClassName = null;
198     }
199 
200     /**
201      * Avalon Service lifecycle method
202      */
203     public void dispose()
204     {
205         manager.release(userManager);
206         manager.release(roleManager);
207         manager.release(groupManager);
208         manager.release(permissionManager);
209         manager.release(modelManager);
210         manager = null;
211     }
212 }