001package org.apache.fulcrum.security.spi;
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 */
021import org.apache.commons.lang3.StringUtils;
022import org.apache.fulcrum.security.RoleManager;
023import org.apache.fulcrum.security.entity.Role;
024import org.apache.fulcrum.security.util.DataBackendException;
025import org.apache.fulcrum.security.util.EntityExistsException;
026import org.apache.fulcrum.security.util.UnknownEntityException;
027
028/**
029 *
030 * This implementation keeps all objects in memory. This is mostly meant to help
031 * with testing and prototyping of ideas.
032 *
033 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
034 * @version $Id$
035 */
036public abstract class AbstractRoleManager extends AbstractEntityManager implements RoleManager
037{
038    /** default serial id */
039        private static final long serialVersionUID = 1L;
040
041        /**
042         * @param <T> role type
043         * @param role to persist
044         * @return the Role object
045         * @throws DataBackendException if fail to connect to datasource
046         */
047        protected abstract <T extends Role> T persistNewRole(T role) throws DataBackendException;
048
049    /**
050     * Construct a blank Role object.
051     *
052     * This method calls getRoleClass, and then creates a new object using the
053     * default constructor.
054     *
055     * @param <T> role type
056     * @return an object implementing Role interface.
057     * @throws DataBackendException
058     *             if the object could not be instantiated.
059     */
060    @Override
061        public <T extends Role> T getRoleInstance() throws DataBackendException
062    {
063        try
064        {
065            @SuppressWarnings("unchecked")
066                        T role = (T) Class.forName(getClassName()).newInstance();
067            return role;
068        }
069        catch (Exception e)
070        {
071            throw new DataBackendException("Problem creating instance of class " + getClassName(), e);
072        }
073    }
074
075    /**
076     * Construct a blank Role object.
077     *
078     * This method calls getRoleClass, and then creates a new object using the
079     * default constructor.
080     * 
081     * @param <T> type of role 
082     *
083     * @param roleName
084     *            The name of the role.
085     *
086     * @return an object implementing Role interface.
087     *
088     * @throws DataBackendException
089     *             if the object could not be instantiated.
090     */
091    @Override
092        public <T extends Role> T getRoleInstance(String roleName) throws DataBackendException
093    {
094        T role = getRoleInstance();
095        role.setName(roleName);
096        return role;
097    }
098
099    /**
100     * Retrieve a Role object with specified name.
101     *
102     * @see org.apache.fulcrum.security.RoleManager#getRoleByName(java.lang.String)
103     * 
104     * @param <T> type of role
105     *
106     * @param name
107     *            the name of the Role.
108     * @return an object representing the Role with specified name.
109     * @throws DataBackendException
110     *             if there was an error accessing the data backend.
111     * @throws UnknownEntityException
112     *             if the role does not exist.
113     * 
114     */
115    @Override
116        public <T extends Role> T getRoleByName(String name) throws DataBackendException, UnknownEntityException
117    {
118        @SuppressWarnings("unchecked")
119                T role = (T) getAllRoles().getByName(name);
120        if (role == null)
121        {
122            throw new UnknownEntityException("The specified role does not exist");
123        }
124        return role;
125    }
126
127    /**
128     * Retrieve a Role object with specified Id.
129     * 
130     * @param <T> the role type
131     *
132     * @param id
133     *            the ID of the Role.
134     *
135     * @return an object representing the Role with specified name.
136     *
137     * @throws UnknownEntityException
138     *             if the permission does not exist in the database.
139     * @throws DataBackendException
140     *             if there is a problem accessing the storage.
141     */
142    @Override
143        public <T extends Role> T getRoleById(Object id) throws DataBackendException, UnknownEntityException
144    {
145        @SuppressWarnings("unchecked")
146                T role = (T) getAllRoles().getById(id);
147        if (role == null)
148        {
149            throw new UnknownEntityException("The specified role does not exist");
150        }
151        return role;
152    }
153
154    /**
155     * Creates a new role with specified attributes.
156     * 
157     * @param <T> role type
158     *
159     * @param role
160     *            the object describing the role to be created.
161     * @return a new Role object that has id set up properly.
162     * @throws DataBackendException
163     *             if there was an error accessing the data backend.
164     * @throws EntityExistsException
165     *             if the role already exists.
166     */
167    @Override
168        public synchronized <T extends Role> T addRole(T role) throws DataBackendException, EntityExistsException
169    {
170        boolean roleExists = false;
171        if (StringUtils.isEmpty(role.getName()))
172        {
173            throw new DataBackendException("Could not create a role with empty name!");
174        }
175//        if (role.getId() == null)
176//        {
177//            throw new DataBackendException("Could not create a role with an id of null!");
178//        }
179        try
180        {
181            roleExists = checkExists(role);
182            if (!roleExists)
183            {
184                return persistNewRole(role);
185            }
186        }
187        catch (Exception e)
188        {
189            throw new DataBackendException("addRole(Role) failed", e);
190        }
191
192        // the only way we could get here without return/throw triggered
193        // is that the roleExists was true.
194        throw new EntityExistsException("Role '" + role + "' already exists");
195    }
196
197    /**
198     * Check whether a specified role exists.
199     *
200     * The name is used for looking up the role
201     *
202     * @param role
203     *            The role to be checked.
204     * @return true if the specified role exists
205     * @throws DataBackendException
206     *             if there was an error accessing the data backend.
207     */
208    @Override
209        public boolean checkExists(Role role) throws DataBackendException
210    {
211        return checkExists(role.getName());
212    }
213}