%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.fulcrum.security.memory.MemoryPermissionManagerImpl |
|
|
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.Permission; |
|
23 | import org.apache.fulcrum.security.spi.AbstractPermissionManager; |
|
24 | import org.apache.fulcrum.security.util.DataBackendException; |
|
25 | import org.apache.fulcrum.security.util.EntityExistsException; |
|
26 | import org.apache.fulcrum.security.util.PermissionSet; |
|
27 | import org.apache.fulcrum.security.util.UnknownEntityException; |
|
28 | ||
29 | /** |
|
30 | * This implementation keeps all objects in memory. This is mostly meant to help with testing and |
|
31 | * prototyping of ideas. |
|
32 | * |
|
33 | * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> |
|
34 | * @version $Id: MemoryPermissionManagerImpl.java 223060 2004-07-07 16:51:27Z epugh $ |
|
35 | */ |
|
36 | 276 | public class MemoryPermissionManagerImpl extends AbstractPermissionManager |
37 | { |
|
38 | /** Logging */ |
|
39 | 24 | private static Log log = |
40 | 24 | LogFactory.getLog(MemoryPermissionManagerImpl.class); |
41 | 24 | private static List permissions = new ArrayList(); |
42 | /** Our Unique ID counter */ |
|
43 | 24 | private static int uniqueId = 0; |
44 | ||
45 | ||
46 | /** |
|
47 | * Retrieves all permissions defined in the system. |
|
48 | * |
|
49 | * @return the names of all permissions defined in the system. |
|
50 | * @throws DataBackendException if there was an error accessing the data backend. |
|
51 | */ |
|
52 | public PermissionSet getAllPermissions() throws DataBackendException |
|
53 | { |
|
54 | 33 | return new PermissionSet(permissions); |
55 | } |
|
56 | /** |
|
57 | * Renames an existing Permission. |
|
58 | * |
|
59 | * @param permission The object describing the permission to be renamed. |
|
60 | * @param name the new name for the permission. |
|
61 | * @throws DataBackendException if there was an error accessing the data backend. |
|
62 | * @throws UnknownEntityException if the permission does not exist. |
|
63 | */ |
|
64 | public synchronized void renamePermission( |
|
65 | Permission permission, |
|
66 | String name) |
|
67 | throws DataBackendException, UnknownEntityException |
|
68 | { |
|
69 | 3 | boolean permissionExists = false; |
70 | try |
|
71 | { |
|
72 | 3 | permissionExists = checkExists(permission); |
73 | 3 | if (permissionExists) |
74 | { |
|
75 | 3 | permissions.remove(permission); |
76 | 3 | permission.setName(name); |
77 | 3 | permissions.add(permission); |
78 | 3 | return; |
79 | } |
|
80 | } |
|
81 | 0 | catch (Exception e) |
82 | { |
|
83 | 0 | throw new DataBackendException( |
84 | 0 | "renamePermission(Permission,name)", |
85 | 0 | e); |
86 | } |
|
87 | finally |
|
88 | { |
|
89 | } |
|
90 | 0 | throw new UnknownEntityException( |
91 | 0 | "Unknown permission '" + permission + "'"); |
92 | } |
|
93 | ||
94 | /** |
|
95 | * Determines if the <code>Permission</code> exists in the security system. |
|
96 | * |
|
97 | * @param permission a <code>String</code> value |
|
98 | * @return true if the permission exists in the system, false otherwise |
|
99 | * @throws DataBackendException when more than one Permission with the same name exists. |
|
100 | * @throws Exception A generic exception. |
|
101 | */ |
|
102 | public boolean checkExists(String permissionName) |
|
103 | throws DataBackendException |
|
104 | { |
|
105 | 324 | return MemoryHelper.checkExists(permissions,permissionName); |
106 | } |
|
107 | ||
108 | /** |
|
109 | * Removes a Permission from the system. |
|
110 | * |
|
111 | * @param permission The object describing the permission to be removed. |
|
112 | * @throws DataBackendException if there was an error accessing the data backend. |
|
113 | * @throws UnknownEntityException if the permission does not exist. |
|
114 | */ |
|
115 | public synchronized void removePermission(Permission permission) |
|
116 | throws DataBackendException, UnknownEntityException |
|
117 | { |
|
118 | 3 | boolean permissionExists = false; |
119 | try |
|
120 | { |
|
121 | 3 | permissionExists = checkExists(permission); |
122 | 3 | if (permissionExists) |
123 | { |
|
124 | 3 | permissions.remove(permission); |
125 | } |
|
126 | else |
|
127 | { |
|
128 | 0 | throw new UnknownEntityException( |
129 | 0 | "Unknown permission '" + permission + "'"); |
130 | } |
|
131 | } |
|
132 | 0 | catch (Exception e) |
133 | { |
|
134 | 0 | throw new DataBackendException("removePermission(Permission)", e); |
135 | } |
|
136 | finally |
|
137 | { |
|
138 | } |
|
139 | 3 | } |
140 | /** |
|
141 | * Creates a new permission with specified attributes. |
|
142 | * |
|
143 | * @param permission the object describing the permission to be created. |
|
144 | * @return a new Permission object that has id set up properly. |
|
145 | * @throws DataBackendException if there was an error accessing the data backend. |
|
146 | * @throws EntityExistsException if the permission already exists. |
|
147 | */ |
|
148 | protected synchronized Permission persistNewPermission(Permission permission) |
|
149 | throws DataBackendException |
|
150 | { |
|
151 | ||
152 | 132 | permission.setId(MemoryHelper.getUniqueId()); |
153 | 132 | permissions.add(permission); |
154 | 132 | return permission; |
155 | ||
156 | } |
|
157 | ||
158 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |