1 package org.apache.fulcrum.security.spi;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.apache.fulcrum.security.PermissionManager;
23 import org.apache.fulcrum.security.entity.Permission;
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
31
32
33
34
35
36 public abstract class AbstractPermissionManager extends AbstractEntityManager implements PermissionManager
37 {
38
39
40
41 private static final long serialVersionUID = 1L;
42
43 protected abstract <T extends Permission> T persistNewPermission(T permission) throws DataBackendException;
44
45
46
47
48
49
50
51
52
53
54
55 @Override
56 public <T extends Permission> T getPermissionInstance() throws DataBackendException
57 {
58 try
59 {
60 @SuppressWarnings("unchecked")
61 T permission = (T) Class.forName(getClassName()).newInstance();
62 return permission;
63 }
64 catch (Exception e)
65 {
66 throw new DataBackendException("Failed to instantiate a Permission implementation object", e);
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 @Override
84 public <T extends Permission> T getPermissionInstance(String permName) throws DataBackendException
85 {
86 T perm = getPermissionInstance();
87 perm.setName(permName);
88 return perm;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102 @Override
103 public <T extends Permission> T getPermissionByName(String name) throws DataBackendException, UnknownEntityException
104 {
105 @SuppressWarnings("unchecked")
106 T permission = (T) getAllPermissions().getByName(name);
107 if (permission == null)
108 {
109 throw new UnknownEntityException("The specified permission does not exist");
110 }
111 return permission;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 @Override
128 public <T extends Permission> T getPermissionById(Object id) throws DataBackendException, UnknownEntityException
129 {
130 @SuppressWarnings("unchecked")
131 T permission = (T) getAllPermissions().getById(id);
132 if (permission == null)
133 {
134 throw new UnknownEntityException("The specified permission does not exist");
135 }
136 return permission;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public synchronized <T extends Permission> T addPermission(T permission) throws DataBackendException, EntityExistsException
152 {
153 boolean permissionExists = false;
154 if (StringUtils.isEmpty(permission.getName()))
155 {
156 throw new DataBackendException("Could not create a permission with empty name!");
157 }
158
159
160
161
162 try
163 {
164 permissionExists = checkExists(permission);
165 if (!permissionExists)
166 {
167 return persistNewPermission(permission);
168 }
169 }
170 catch (Exception e)
171 {
172 throw new DataBackendException("addPermission(Permission) failed", e);
173 }
174
175
176 throw new EntityExistsException("Permission '" + permission + "' already exists");
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 @Override
191 public boolean checkExists(Permission permission) throws DataBackendException
192 {
193 return checkExists(permission.getName());
194 }
195
196 }