001package org.apache.fulcrum.security.memory;
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 java.util.ArrayList;
022import java.util.List;
023
024import org.apache.fulcrum.security.entity.Role;
025import org.apache.fulcrum.security.spi.AbstractRoleManager;
026import org.apache.fulcrum.security.util.DataBackendException;
027import org.apache.fulcrum.security.util.EntityExistsException;
028import org.apache.fulcrum.security.util.RoleSet;
029import org.apache.fulcrum.security.util.UnknownEntityException;
030
031/**
032 *
033 * This implementation keeps all objects in memory. This is mostly meant to help
034 * with testing and prototyping of ideas.
035 *
036 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
037 * @version $Id$
038 */
039public class MemoryRoleManagerImpl extends AbstractRoleManager
040{
041    /** List to store all our roles in */
042    private static List<Role> roles = new ArrayList<Role>();
043    
044    public MemoryRoleManagerImpl() {
045        // reset
046        roles.clear();
047    }
048
049    /** Our Unique ID counter */
050    // private static int uniqueId = 0;
051
052    /**
053     * Renames an existing Role.
054     *
055     * @param role
056     *            The object describing the role to be renamed.
057     * @param name
058     *            the new name for the role.
059     * @throws DataBackendException
060     *             if there was an error accessing the data backend.
061     * @throws UnknownEntityException
062     *             if the role does not exist.
063     */
064    public synchronized void renameRole(Role role, String name) throws DataBackendException, UnknownEntityException
065    {
066        boolean roleExists = false;
067        try
068        {
069            roleExists = checkExists(role);
070            if (roleExists)
071            {
072                roles.remove(role);
073                role.setName(name);
074                roles.add(role);
075                return;
076            }
077        }
078        catch (DataBackendException e)
079        {
080            throw new DataBackendException("renameRole(Role,String)", e);
081        }
082
083        throw new UnknownEntityException("Unknown role '" + role + "'");
084    }
085
086    /**
087     * Determines if the <code>Role</code> exists in the security system.
088     *
089     * @param roleName
090     *            a <code>String</code> value
091     * @return true if the role exists in the system, false otherwise
092     */
093    public boolean checkExists(String roleName)
094    {
095        return MemoryHelper.checkExists(roles, roleName);
096    }
097
098    /**
099     * Retrieves all roles defined in the system.
100     *
101     * @return the names of all roles defined in the system.
102     * @throws DataBackendException
103     *             if there was an error accessing the data backend.
104     */
105    public RoleSet getAllRoles() throws DataBackendException
106    {
107        return new RoleSet(roles);
108    }
109
110    /**
111     * Creates a new role with specified attributes.
112     *
113     * @param role
114     *            the object describing the role to be created.
115     * @return a new Role object that has id set up properly.
116     * @throws DataBackendException
117     *             if there was an error accessing the data backend.
118     */
119    @Override
120    protected synchronized <T extends Role> T persistNewRole(T role) throws DataBackendException
121    {
122        role.setId(MemoryHelper.getUniqueId());
123        roles.add(role);
124        // add the role to system-wide cache
125        getAllRoles().add(role);
126        // return the object with correct id
127        return role;
128    }
129
130    /**
131     * Removes a Role from the system.
132     *
133     * @param role
134     *            The object describing the role to be removed.
135     * @throws DataBackendException
136     *             if there was an error accessing the data backend.
137     * @throws UnknownEntityException
138     *             if the role does not exist.
139     */
140    public synchronized void removeRole(Role role) throws DataBackendException, UnknownEntityException
141    {
142        boolean roleExists = false;
143        try
144        {
145            roleExists = checkExists(role);
146            if (roleExists)
147            {
148                roles.remove(role);
149                getAllRoles().remove(role);
150                return;
151            }
152        }
153        catch (DataBackendException e)
154        {
155            throw new DataBackendException("removeRole(Role)", e);
156        }
157
158        throw new UnknownEntityException("Unknown role '" + role + "'");
159    }
160}