View Javadoc
1   package org.apache.fulcrum.security.spi;
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  import org.apache.avalon.framework.activity.Disposable;
22  import org.apache.avalon.framework.logger.AbstractLogEnabled;
23  import org.apache.avalon.framework.service.ServiceException;
24  import org.apache.avalon.framework.service.ServiceManager;
25  import org.apache.avalon.framework.service.Serviceable;
26  import org.apache.avalon.framework.thread.ThreadSafe;
27  import org.apache.fulcrum.security.GroupManager;
28  import org.apache.fulcrum.security.PermissionManager;
29  import org.apache.fulcrum.security.RoleManager;
30  import org.apache.fulcrum.security.UserManager;
31  import org.apache.fulcrum.security.util.DataBackendException;
32  
33  /**
34   * 
35   * This abstract implementation provides most of the functionality that a
36   * manager will need.
37   * 
38   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
39   * @version $Id$
40   */
41  public abstract class AbstractManager extends AbstractLogEnabled implements Serviceable, Disposable, ThreadSafe
42  {
43      boolean composed = false;
44  
45      protected ServiceManager manager = null;
46      private PermissionManager permissionManager;
47      private RoleManager roleManager;
48      private GroupManager groupManager;
49      private UserManager userManager;
50  
51      /**
52       * @return the service manager
53       */
54      protected ServiceManager getServiceManager()
55      {
56          return manager;
57      }
58  
59      /**
60       * @return the user manager
61       * @throws DataBackendException if fail to connect to datasource
62       */
63      protected UserManager getUserManager() throws DataBackendException
64      {
65          if (userManager == null)
66          {
67              try
68              {
69                  userManager = (UserManager) manager.lookup(UserManager.ROLE);
70  
71              }
72              catch (ServiceException ce)
73              {
74                  throw new DataBackendException(ce.getMessage(), ce);
75              }
76          }
77          return userManager;
78      }
79  
80      /**
81       * @return the permission manager
82       * @throws DataBackendException if fail to connect to datasource
83       */
84      protected PermissionManager getPermissionManager() throws DataBackendException
85      {
86          if (permissionManager == null)
87          {
88              try
89              {
90                  permissionManager = (PermissionManager) manager.lookup(PermissionManager.ROLE);
91  
92              }
93              catch (ServiceException ce)
94              {
95                  throw new DataBackendException(ce.getMessage(), ce);
96              }
97          }
98          return permissionManager;
99      }
100 
101     /**
102      * @return the role manager
103      * @throws DataBackendException if fail to connect to datasource
104      */
105     protected RoleManager getRoleManager() throws DataBackendException
106     {
107         if (roleManager == null)
108         {
109             try
110             {
111                 roleManager = (RoleManager) manager.lookup(RoleManager.ROLE);
112 
113             }
114             catch (ServiceException ce)
115             {
116                 throw new DataBackendException(ce.getMessage(), ce);
117             }
118         }
119         return roleManager;
120     }
121 
122     /**
123      * @return the group manager
124      * @throws DataBackendException if fail to connect to datasource
125      */
126     protected GroupManager getGroupManager() throws DataBackendException
127     {
128         if (groupManager == null)
129         {
130             try
131             {
132                 groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
133 
134             }
135             catch (ServiceException ce)
136             {
137                 throw new DataBackendException(ce.getMessage(), ce);
138             }
139         }
140         return groupManager;
141     }
142 
143     /**
144      * Avalon Service lifecycle method
145      * @throws ServiceException if fail to connect
146      */
147     @Override
148 	public void service(ServiceManager manager) throws ServiceException
149     {
150         this.manager = manager;
151 
152     }
153 
154     @Override
155 	public void dispose()
156     {
157         release(roleManager);
158         release(permissionManager);
159         release(groupManager);
160         release(userManager);
161         manager = null;
162     }
163 
164     /**
165      * @param obj the object to release
166      */
167     protected void release(Object obj)
168     {
169         if (obj != null)
170         {
171             manager.release(obj);
172         }
173     }
174 
175     /**
176      * @param lookup the object to resolve
177      * @return the actual object
178      * @throws RuntimeException exception if fails to find the manager
179      */
180     protected Object resolve(String lookup) throws RuntimeException
181     {
182         Object component = null;
183         {
184             try
185             {
186                 component = manager.lookup(lookup);
187             }
188             catch (ServiceException ce)
189             {
190                 throw new RuntimeException(ce.getMessage(), ce);
191             }
192         }
193         return component;
194     }
195 
196 }