%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.fulcrum.security.memory.MemoryRoleManagerImpl |
|
|
1 | package org.apache.fulcrum.security.memory; |
|
2 | /* |
|
3 | * Copyright 2001-2004 The Apache Software Foundation |
|
4 | * |
|
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 | * you may not use this file except in compliance with the License. |
|
7 | * You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | import java.util.ArrayList; |
|
18 | import java.util.List; |
|
19 | ||
20 | import org.apache.commons.logging.Log; |
|
21 | import org.apache.commons.logging.LogFactory; |
|
22 | import org.apache.fulcrum.security.entity.Role; |
|
23 | import org.apache.fulcrum.security.spi.AbstractRoleManager; |
|
24 | import org.apache.fulcrum.security.util.DataBackendException; |
|
25 | import org.apache.fulcrum.security.util.EntityExistsException; |
|
26 | import org.apache.fulcrum.security.util.RoleSet; |
|
27 | import org.apache.fulcrum.security.util.UnknownEntityException; |
|
28 | ||
29 | /** |
|
30 | * |
|
31 | * This implementation keeps all objects in memory. This is mostly meant to help |
|
32 | * with testing and prototyping of ideas. |
|
33 | * |
|
34 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
|
35 | * @version $Id: MemoryRoleManagerImpl.java 223060 2004-07-07 16:51:27Z epugh $ |
|
36 | */ |
|
37 | 276 | public class MemoryRoleManagerImpl extends AbstractRoleManager |
38 | { |
|
39 | /** Logging */ |
|
40 | 24 | private static Log log = LogFactory.getLog(MemoryRoleManagerImpl.class); |
41 | /** List to store all our roles in */ |
|
42 | 24 | private static List roles = new ArrayList(); |
43 | /** Our Unique ID counter */ |
|
44 | 24 | private static int uniqueId = 0; |
45 | ||
46 | /** |
|
47 | * Renames an existing Role. |
|
48 | * |
|
49 | * @param role The object describing the role to be renamed. |
|
50 | * @param name the new name for the role. |
|
51 | * @throws DataBackendException if there was an error accessing the data |
|
52 | * backend. |
|
53 | * @throws UnknownEntityException if the role does not exist. |
|
54 | */ |
|
55 | public synchronized void renameRole(Role role, String name) |
|
56 | throws DataBackendException, UnknownEntityException |
|
57 | { |
|
58 | 3 | boolean roleExists = false; |
59 | try |
|
60 | { |
|
61 | 3 | roleExists = checkExists(role); |
62 | 3 | if (roleExists) |
63 | { |
|
64 | 3 | roles.remove(role); |
65 | 3 | role.setName(name); |
66 | 3 | roles.add(role); |
67 | 3 | return; |
68 | } |
|
69 | } |
|
70 | 0 | catch (Exception e) |
71 | { |
|
72 | 0 | throw new DataBackendException("renameRole(Role,String)", e); |
73 | } |
|
74 | finally |
|
75 | { |
|
76 | } |
|
77 | 0 | throw new UnknownEntityException("Unknown role '" + role + "'"); |
78 | } |
|
79 | ||
80 | /** |
|
81 | * Determines if the <code>Role</code> exists in the security system. |
|
82 | * |
|
83 | * @param permission a <code>String</code> value |
|
84 | * @return true if the role exists in the system, false otherwise |
|
85 | * @throws DataBackendException when more than one Role with the same name exists. |
|
86 | * @throws Exception A generic exception. |
|
87 | */ |
|
88 | public boolean checkExists(String roleName) |
|
89 | { |
|
90 | 516 | return MemoryHelper.checkExists(roles,roleName); |
91 | } |
|
92 | /** |
|
93 | * Retrieves all roles defined in the system. |
|
94 | * |
|
95 | * @return the names of all roles defined in the system. |
|
96 | * @throws DataBackendException if there was an error accessing the |
|
97 | * data backend. |
|
98 | */ |
|
99 | public RoleSet getAllRoles() throws DataBackendException |
|
100 | { |
|
101 | 255 | return new RoleSet(roles); |
102 | } |
|
103 | ||
104 | /** |
|
105 | * Creates a new role with specified attributes. |
|
106 | * |
|
107 | * @param role the object describing the role to be created. |
|
108 | * @return a new Role object that has id set up properly. |
|
109 | * @throws DataBackendException if there was an error accessing the data |
|
110 | * backend. |
|
111 | * @throws EntityExistsException if the role already exists. |
|
112 | */ |
|
113 | protected synchronized Role persistNewRole(Role role) |
|
114 | throws DataBackendException |
|
115 | { |
|
116 | ||
117 | 177 | role.setId(MemoryHelper.getUniqueId()); |
118 | 177 | roles.add(role); |
119 | // add the role to system-wide cache |
|
120 | 177 | getAllRoles().add(role); |
121 | // return the object with correct id |
|
122 | 177 | return role; |
123 | ||
124 | } |
|
125 | ||
126 | /** |
|
127 | * Removes a Role from the system. |
|
128 | * |
|
129 | * @param role The object describing the role to be removed. |
|
130 | * @throws DataBackendException if there was an error accessing the data |
|
131 | * backend. |
|
132 | * @throws UnknownEntityException if the role does not exist. |
|
133 | */ |
|
134 | public synchronized void removeRole(Role role) |
|
135 | throws DataBackendException, UnknownEntityException |
|
136 | { |
|
137 | 3 | boolean roleExists = false; |
138 | try |
|
139 | { |
|
140 | 3 | roleExists = checkExists(role); |
141 | 3 | if (roleExists) |
142 | { |
|
143 | 3 | roles.remove(role); |
144 | 3 | getAllRoles().remove(role); |
145 | 3 | return; |
146 | } |
|
147 | } |
|
148 | 0 | catch (Exception e) |
149 | { |
|
150 | 0 | throw new DataBackendException("removeRole(Role)", e); |
151 | } |
|
152 | finally |
|
153 | { |
|
154 | } |
|
155 | 0 | throw new UnknownEntityException("Unknown role '" + role + "'"); |
156 | } |
|
157 | ||
158 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |