001package org.apache.fulcrum.security;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.avalon.framework.thread.ThreadSafe;
028
029/**
030 * This a basis implementation of the Fulcrum security service.
031 * 
032 * Provided functionality includes:
033 * <ul>
034 * <li>methods for retrieving different types of managers.
035 * <li>avalon lifecyle managers.
036 * </ul>
037 * 
038 * @author <a href="mailto:epugh@upstate.com">Eric PUgh</a>
039 * @version $Id$
040 */
041public class BaseSecurityService extends AbstractLogEnabled implements SecurityService, Initializable, Serviceable, ThreadSafe
042{
043    private ServiceManager manager = null;
044    // management of Groups/Role/Permissions
045
046    // temporary storage of the classnames prior to initialization
047    String userClassName;
048    String groupClassName;
049    String permissionClassName;
050    String roleClassName;
051    String aclClassName;
052    /** The instance of UserManager the SecurityService uses */
053    protected UserManager userManager = null;
054    /** The instance of RoleManager the SecurityService uses */
055    protected RoleManager roleManager = null;
056    /** The instance of GroupManager the SecurityService uses */
057    protected GroupManager groupManager = null;
058    /** The instance of PermissionManager the SecurityService uses */
059    protected PermissionManager permissionManager = null;
060    /** The instance of ModelManager the SecurityService uses */
061    protected ModelManager modelManager = null;
062
063    /**
064     * Returns the configured UserManager.
065     * 
066     * @return An UserManager object
067     */
068    public UserManager getUserManager()
069    {
070        if (userManager == null)
071        {
072            try
073            {
074                userManager = (UserManager) manager.lookup(UserManager.ROLE);
075            }
076            catch (ServiceException ce)
077            {
078                throw new RuntimeException(ce.getMessage(), ce);
079            }
080        }
081        return userManager;
082    }
083
084    /**
085     * Returns the configured GroupManager.
086     * 
087     * @return An UserManager object
088     */
089    public GroupManager getGroupManager()
090    {
091        if (groupManager == null)
092        {
093            try
094            {
095                groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
096            }
097            catch (ServiceException ce)
098            {
099                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}