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.commons.lang3.StringUtils;
22 import org.apache.fulcrum.security.RoleManager;
23 import org.apache.fulcrum.security.entity.Role;
24 import org.apache.fulcrum.security.util.DataBackendException;
25 import org.apache.fulcrum.security.util.EntityExistsException;
26 import org.apache.fulcrum.security.util.UnknownEntityException;
27
28 /**
29 *
30 * This implementation keeps all objects in memory. This is mostly meant to help
31 * with testing and prototyping of ideas.
32 *
33 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
34 * @version $Id$
35 */
36 public abstract class AbstractRoleManager extends AbstractEntityManager implements RoleManager
37 {
38 /** default serial id */
39 private static final long serialVersionUID = 1L;
40
41 /**
42 * @param <T> role type
43 * @param role to persist
44 * @return the Role object
45 * @throws DataBackendException if fail to connect to datasource
46 */
47 protected abstract <T extends Role> T persistNewRole(T role) throws DataBackendException;
48
49 /**
50 * Construct a blank Role object.
51 *
52 * This method calls getRoleClass, and then creates a new object using the
53 * default constructor.
54 *
55 * @param <T> role type
56 * @return an object implementing Role interface.
57 * @throws DataBackendException
58 * if the object could not be instantiated.
59 */
60 @Override
61 public <T extends Role> T getRoleInstance() throws DataBackendException
62 {
63 try
64 {
65 @SuppressWarnings("unchecked")
66 T role = (T) Class.forName(getClassName()).newInstance();
67 return role;
68 }
69 catch (Exception e)
70 {
71 throw new DataBackendException("Problem creating instance of class " + getClassName(), e);
72 }
73 }
74
75 /**
76 * Construct a blank Role object.
77 *
78 * This method calls getRoleClass, and then creates a new object using the
79 * default constructor.
80 *
81 * @param <T> type of role
82 *
83 * @param roleName
84 * The name of the role.
85 *
86 * @return an object implementing Role interface.
87 *
88 * @throws DataBackendException
89 * if the object could not be instantiated.
90 */
91 @Override
92 public <T extends Role> T getRoleInstance(String roleName) throws DataBackendException
93 {
94 T role = getRoleInstance();
95 role.setName(roleName);
96 return role;
97 }
98
99 /**
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 }