1 package org.apache.fulcrum.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.sql.Connection;
21 import java.util.List;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.fulcrum.security.entity.Role;
26 import org.apache.fulcrum.security.spi.AbstractRoleManager;
27 import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
28 import org.apache.fulcrum.security.util.DataBackendException;
29 import org.apache.fulcrum.security.util.EntityExistsException;
30 import org.apache.fulcrum.security.util.RoleSet;
31 import org.apache.fulcrum.security.util.UnknownEntityException;
32 import org.apache.torque.NoRowsException;
33 import org.apache.torque.TooManyRowsException;
34 import org.apache.torque.TorqueException;
35 import org.apache.torque.util.Transaction;
36
37
38
39
40
41
42 public abstract class TorqueAbstractRoleManager extends AbstractRoleManager implements LazyLoadable
43 {
44
45
46 private static final long serialVersionUID = 4258137881250800204L;
47
48 private static final String LAZY_LOADING = "lazy";
49 protected Boolean lazyLoading = false;
50
51
52
53
54 @Override
55 public void configure(Configuration conf) throws ConfigurationException
56 {
57 super.configure( conf );
58 lazyLoading = conf.getAttributeAsBoolean( LAZY_LOADING, false);
59 getLogger().debug("setting lazyLoading: " + lazyLoading);
60 }
61
62
63
64
65
66
67
68
69
70
71
72 protected abstract <T extends Role> List<T> doSelectAllRoles(Connection con)
73 throws TorqueException;
74
75
76
77
78
79
80
81
82
83
84
85
86
87 protected abstract <T extends Role> T doSelectByName(String name, Connection con)
88 throws NoRowsException, TooManyRowsException, TorqueException;
89
90
91
92
93
94
95
96
97
98
99
100
101
102 protected abstract <T extends Role> T doSelectById(Integer id, Connection con)
103 throws NoRowsException, TooManyRowsException, TorqueException;
104
105
106
107
108
109
110
111
112
113
114
115 @Override
116 public synchronized void renameRole(Role role, String name) throws DataBackendException, UnknownEntityException
117 {
118 if (checkExists(role))
119 {
120 role.setName(name);
121
122 try
123 {
124 TorqueAbstractSecurityEntityg/apache/fulcrum/security/torque/security/TorqueAbstractSecurityEntity.html#TorqueAbstractSecurityEntity">TorqueAbstractSecurityEntity r = (TorqueAbstractSecurityEntity)role;
125 r.setNew(false);
126 r.save();
127 }
128 catch (Exception e)
129 {
130 throw new DataBackendException("Renaming Role '" + role.getName() + "' failed", e);
131 }
132 }
133 else
134 {
135 throw new UnknownEntityException("Unknown Role '" + role.getName() + "'");
136 }
137 }
138
139
140
141
142
143
144
145
146
147 @Override
148 protected synchronized <T extends Role> T persistNewRole(T role) throws DataBackendException
149 {
150 try
151 {
152 ((TorqueAbstractSecurityEntity)role).save();
153 }
154 catch (Exception e)
155 {
156 throw new DataBackendException("Adding Role '" + role.getName() + "' failed", e);
157 }
158
159 return role;
160 }
161
162
163
164
165
166
167
168
169
170 @Override
171 public synchronized void removeRole(Role role) throws DataBackendException, UnknownEntityException
172 {
173 if (checkExists(role))
174 {
175 try
176 {
177 ((TorqueAbstractSecurityEntity)role).delete();
178 }
179 catch (TorqueException e)
180 {
181 throw new DataBackendException("Removing Role '" + role.getName() + "' failed", e);
182 }
183 }
184 else
185 {
186 throw new UnknownEntityException("Unknown role '" + role.getName() + "'");
187 }
188 }
189
190
191
192
193
194
195
196
197
198 @Override
199 public boolean checkExists(String roleName) throws DataBackendException
200 {
201 boolean exists = false;
202
203 Connection con = null;
204
205 try
206 {
207 con = Transaction.begin();
208
209 doSelectByName(roleName, con);
210
211 Transaction.commit(con);
212 con = null;
213
214 exists = true;
215 }
216 catch (NoRowsException e)
217 {
218 exists = false;
219 }
220 catch (TooManyRowsException e)
221 {
222 throw new DataBackendException("Multiple roles with same name '" + roleName + "'");
223 }
224 catch (TorqueException e)
225 {
226 throw new DataBackendException("Error retrieving role information", e);
227 }
228 finally
229 {
230 if (con != null)
231 {
232 Transaction.safeRollback(con);
233 }
234 }
235
236 return exists;
237 }
238
239
240
241
242
243
244
245
246 @Override
247 public RoleSet getAllRoles() throws DataBackendException
248 {
249 RoleSet roleSet = new RoleSet();
250 Connection con = null;
251
252 try
253 {
254 con = Transaction.begin();
255
256 List<Role> roles = doSelectAllRoles(con);
257
258 for (Role role : roles)
259 {
260
261 ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con, getLazyLoading());
262
263 roleSet.add(role);
264 }
265
266 Transaction.commit(con);
267 con = null;
268 }
269 catch (TorqueException e)
270 {
271 throw new DataBackendException("Error retrieving role information", e);
272 }
273 finally
274 {
275 if (con != null)
276 {
277 Transaction.safeRollback(con);
278 }
279 }
280
281 return roleSet;
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295 @Override
296 public <T extends Role> T getRoleById(Object id) throws DataBackendException, UnknownEntityException
297 {
298 T role;
299
300 if (id != null && id instanceof Integer)
301 {
302 Connection con = null;
303
304 try
305 {
306 con = Transaction.begin();
307
308 role = doSelectById((Integer)id, con);
309
310
311 ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con, getLazyLoading());
312
313 Transaction.commit(con);
314 con = null;
315 }
316 catch (NoRowsException e)
317 {
318 throw new UnknownEntityException("Role with id '" + id + "' does not exist.", e);
319 }
320 catch (TorqueException e)
321 {
322 throw new DataBackendException("Error retrieving role information", e);
323 }
324 finally
325 {
326 if (con != null)
327 {
328 Transaction.safeRollback(con);
329 }
330 }
331 }
332 else
333 {
334 throw new UnknownEntityException("Invalid role id '" + id + "'");
335 }
336
337 return role;
338 }
339
340
341
342
343
344
345
346
347
348
349 @Override
350 public <T extends Role> T getRoleByName(String name) throws DataBackendException, UnknownEntityException
351 {
352 T role = null;
353 Connection con = null;
354
355 try
356 {
357 con = Transaction.begin();
358
359 role = doSelectByName(name, con);
360
361
362 ((TorqueAbstractSecurityEntity)role).retrieveAttachedObjects(con, getLazyLoading());
363
364 Transaction.commit(con);
365 con = null;
366 }
367 catch (NoRowsException e)
368 {
369 throw new UnknownEntityException("Could not find role" + name);
370 }
371 catch (TooManyRowsException e)
372 {
373 throw new DataBackendException("Multiple Roles with same name '" + name + "'");
374 }
375 catch (TorqueException e)
376 {
377 throw new DataBackendException("Error retrieving role information", e);
378 }
379 finally
380 {
381 if (con != null)
382 {
383 Transaction.safeRollback(con);
384 }
385 }
386
387 return role;
388 }
389
390
391
392
393
394 @Override
395 public Boolean getLazyLoading()
396 {
397 return lazyLoading;
398 }
399
400
401
402
403
404 @Override
405 public void setLazyLoading( Boolean lazyLoading )
406 {
407 this.lazyLoading = lazyLoading;
408 }
409 }