1 | |
package org.apache.turbine.services.security.torque; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.beans.PropertyDescriptor; |
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Enumeration; |
27 | |
import java.util.List; |
28 | |
import java.util.Vector; |
29 | |
|
30 | |
import org.apache.commons.configuration.Configuration; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
|
35 | |
import org.apache.turbine.om.security.Permission; |
36 | |
import org.apache.turbine.om.security.Role; |
37 | |
import org.apache.turbine.services.InitializationException; |
38 | |
import org.apache.turbine.services.security.TurbineSecurity; |
39 | |
import org.apache.turbine.services.security.torque.om.TurbineRolePermissionPeer; |
40 | |
import org.apache.turbine.util.security.DataBackendException; |
41 | |
import org.apache.turbine.util.security.PermissionSet; |
42 | |
|
43 | |
import org.apache.torque.TorqueException; |
44 | |
import org.apache.torque.om.Persistent; |
45 | |
import org.apache.torque.util.BasePeer; |
46 | |
import org.apache.torque.util.Criteria; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public class PermissionPeerManager |
59 | |
implements PermissionPeerManagerConstants |
60 | |
{ |
61 | |
|
62 | 0 | private static Class persistentPeerClass = null; |
63 | |
|
64 | |
|
65 | 0 | private static Class permissionObject = null; |
66 | |
|
67 | |
|
68 | 0 | private static String tableName = null; |
69 | |
|
70 | |
|
71 | 0 | private static String nameColumn = null; |
72 | |
|
73 | |
|
74 | 0 | private static String idColumn = null; |
75 | |
|
76 | |
|
77 | 0 | private static PropertyDescriptor namePropDesc = null; |
78 | |
|
79 | |
|
80 | 0 | private static PropertyDescriptor idPropDesc = null; |
81 | |
|
82 | |
|
83 | 0 | static Log log = LogFactory.getLog(PermissionPeerManager.class); |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static void init(Configuration conf) |
95 | |
throws InitializationException |
96 | |
{ |
97 | 0 | String persistentPeerClassName = |
98 | |
conf.getString(PERMISSION_PEER_CLASS_KEY, |
99 | |
PERMISSION_PEER_CLASS_DEFAULT); |
100 | |
|
101 | 0 | String permissionObjectName = null; |
102 | |
|
103 | |
try |
104 | |
{ |
105 | 0 | persistentPeerClass = Class.forName(persistentPeerClassName); |
106 | |
|
107 | 0 | tableName = |
108 | |
(String) persistentPeerClass.getField("TABLE_NAME").get(null); |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | 0 | permissionObject = getPersistenceClass(); |
117 | |
|
118 | 0 | permissionObjectName = conf.getString(PERMISSION_CLASS_KEY, |
119 | |
permissionObject.getName()); |
120 | |
|
121 | |
|
122 | 0 | permissionObject = Class.forName(permissionObjectName); |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | 0 | nameColumn = (String) persistentPeerClass.getField( |
130 | |
conf.getString(PERMISSION_NAME_COLUMN_KEY, |
131 | |
PERMISSION_NAME_COLUMN_DEFAULT) |
132 | |
).get(null); |
133 | |
|
134 | 0 | idColumn = (String) persistentPeerClass.getField( |
135 | |
conf.getString(PERMISSION_ID_COLUMN_KEY, |
136 | |
PERMISSION_ID_COLUMN_DEFAULT) |
137 | |
).get(null); |
138 | |
|
139 | 0 | namePropDesc = new PropertyDescriptor( |
140 | |
conf.getString(PERMISSION_NAME_PROPERTY_KEY, |
141 | |
PERMISSION_NAME_PROPERTY_DEFAULT), |
142 | |
permissionObject); |
143 | |
|
144 | 0 | idPropDesc = new PropertyDescriptor( |
145 | |
conf.getString(PERMISSION_ID_PROPERTY_KEY, |
146 | |
PERMISSION_ID_PROPERTY_DEFAULT), |
147 | |
permissionObject); |
148 | |
} |
149 | 0 | catch (Exception e) |
150 | |
{ |
151 | 0 | if (persistentPeerClassName == null || persistentPeerClass == null) |
152 | |
{ |
153 | 0 | throw new InitializationException( |
154 | |
"Could not find PermissionPeer class (" |
155 | |
+ persistentPeerClassName + ")", e); |
156 | |
} |
157 | 0 | if (tableName == null) |
158 | |
{ |
159 | 0 | throw new InitializationException( |
160 | |
"Failed to get the table name from the Peer object", e); |
161 | |
} |
162 | |
|
163 | 0 | if (permissionObject == null || permissionObjectName == null) |
164 | |
{ |
165 | 0 | throw new InitializationException( |
166 | |
"Failed to get the object type from the Peer object", e); |
167 | |
} |
168 | |
|
169 | |
|
170 | 0 | if (nameColumn == null || namePropDesc == null) |
171 | |
{ |
172 | 0 | throw new InitializationException( |
173 | |
"PermissionPeer " + persistentPeerClassName + |
174 | |
" has no name column information!", e); |
175 | |
} |
176 | 0 | if (idColumn == null || idPropDesc == null) |
177 | |
{ |
178 | 0 | throw new InitializationException( |
179 | |
"PermissionPeer " + persistentPeerClassName + |
180 | |
" has no id column information!", e); |
181 | |
} |
182 | 0 | } |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public static String getTableName() |
191 | |
{ |
192 | 0 | return tableName; |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public static String getNameColumn() |
202 | |
{ |
203 | 0 | return nameColumn; |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public static String getIdColumn() |
214 | |
{ |
215 | 0 | return idColumn; |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public static String getColumnName(String name) |
226 | |
{ |
227 | 0 | StringBuffer sb = new StringBuffer(); |
228 | 0 | sb.append(getTableName()); |
229 | 0 | sb.append("."); |
230 | 0 | sb.append(name); |
231 | 0 | return sb.toString(); |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public static Persistent newPersistentInstance() |
244 | |
{ |
245 | 0 | Persistent obj = null; |
246 | |
|
247 | 0 | if(permissionObject == null) |
248 | |
{ |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | 0 | return obj; |
256 | |
} |
257 | |
|
258 | |
try |
259 | |
{ |
260 | 0 | obj = (Persistent) permissionObject.newInstance(); |
261 | |
} |
262 | 0 | catch (Exception e) |
263 | |
{ |
264 | 0 | log.error("Could not instantiate a permission object", e); |
265 | 0 | obj = null; |
266 | 0 | } |
267 | 0 | return obj; |
268 | |
} |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public static boolean checkExists(Permission permission) |
281 | |
throws DataBackendException, Exception |
282 | |
{ |
283 | 0 | Criteria criteria = new Criteria(); |
284 | |
|
285 | 0 | criteria.addSelectColumn(getIdColumn()); |
286 | |
|
287 | 0 | criteria.add(getNameColumn(), permission.getName()); |
288 | |
|
289 | 0 | List results = BasePeer.doSelect(criteria); |
290 | |
|
291 | 0 | if (results.size() > 1) |
292 | |
{ |
293 | 0 | throw new DataBackendException("Multiple permissions named '" + |
294 | |
permission.getName() + "' exist!"); |
295 | |
} |
296 | 0 | return (results.size() == 1); |
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public static PermissionSet retrieveSet(Criteria criteria) |
307 | |
throws Exception |
308 | |
{ |
309 | 0 | List results = doSelect(criteria); |
310 | 0 | PermissionSet ps = new PermissionSet(); |
311 | |
|
312 | 0 | for(Iterator it = results.iterator(); it.hasNext(); ) |
313 | |
{ |
314 | 0 | ps.add((Permission) it.next()); |
315 | |
} |
316 | 0 | return ps; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
public static PermissionSet retrieveSet(Role role) |
327 | |
throws Exception |
328 | |
{ |
329 | 0 | Criteria criteria = new Criteria(); |
330 | 0 | criteria.add(TurbineRolePermissionPeer.ROLE_ID, |
331 | |
((Persistent) role).getPrimaryKey()); |
332 | |
|
333 | 0 | criteria.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, |
334 | |
getIdColumn()); |
335 | |
|
336 | 0 | return retrieveSet(criteria); |
337 | |
} |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public static final Vector getDifference(Vector some, Vector all) |
348 | |
{ |
349 | 0 | Vector clone = (Vector) all.clone(); |
350 | 0 | for (Enumeration e = some.elements() ; e.hasMoreElements() ;) |
351 | |
{ |
352 | 0 | Permission tmp = (Permission) e.nextElement(); |
353 | 0 | for (Enumeration f = clone.elements() ; f.hasMoreElements() ;) |
354 | |
{ |
355 | 0 | Permission tmp2 = (Permission) f.nextElement(); |
356 | 0 | if (((Persistent) tmp).getPrimaryKey() == |
357 | |
((Persistent) tmp2).getPrimaryKey()) |
358 | |
{ |
359 | 0 | clone.removeElement(tmp2); |
360 | 0 | break; |
361 | |
} |
362 | 0 | } |
363 | 0 | } |
364 | 0 | return clone; |
365 | |
} |
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
public static Criteria buildCriteria(Permission permission) |
394 | |
{ |
395 | |
Criteria crit; |
396 | |
|
397 | |
try |
398 | |
{ |
399 | 0 | Class[] clazz = new Class[] { permissionObject }; |
400 | 0 | Object[] params = |
401 | |
new Object[] { ((TorquePermission) permission).getPersistentObj() }; |
402 | |
|
403 | 0 | crit = (Criteria) persistentPeerClass |
404 | |
.getMethod("buildCriteria", clazz) |
405 | |
.invoke(null, params); |
406 | |
} |
407 | 0 | catch (Exception e) |
408 | |
{ |
409 | 0 | crit = null; |
410 | 0 | } |
411 | |
|
412 | 0 | return crit; |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
public static void doUpdate(Criteria criteria) |
424 | |
throws TorqueException |
425 | |
{ |
426 | |
try |
427 | |
{ |
428 | 0 | Class[] clazz = new Class[] { Criteria.class }; |
429 | 0 | Object[] params = new Object[] { criteria }; |
430 | |
|
431 | 0 | persistentPeerClass |
432 | |
.getMethod("doUpdate", clazz) |
433 | |
.invoke(null, params); |
434 | |
} |
435 | 0 | catch (Exception e) |
436 | |
{ |
437 | 0 | throw new TorqueException("doUpdate failed", e); |
438 | 0 | } |
439 | 0 | } |
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public static void doInsert(Criteria criteria) |
450 | |
throws TorqueException |
451 | |
{ |
452 | |
try |
453 | |
{ |
454 | 0 | Class[] clazz = new Class[] { Criteria.class }; |
455 | 0 | Object[] params = new Object[] { criteria }; |
456 | |
|
457 | 0 | persistentPeerClass |
458 | |
.getMethod("doInsert", clazz) |
459 | |
.invoke(null, params); |
460 | |
} |
461 | 0 | catch (Exception e) |
462 | |
{ |
463 | 0 | throw new TorqueException("doInsert failed", e); |
464 | 0 | } |
465 | 0 | } |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
public static List doSelect(Criteria criteria) |
477 | |
throws TorqueException |
478 | |
{ |
479 | |
List list; |
480 | |
|
481 | |
try |
482 | |
{ |
483 | 0 | Class[] clazz = |
484 | |
new Class[] { Criteria.class }; |
485 | 0 | Object[] params = new Object[] { criteria }; |
486 | |
|
487 | 0 | list = (List) persistentPeerClass |
488 | |
.getMethod("doSelect", clazz) |
489 | |
.invoke(null, params); |
490 | |
} |
491 | 0 | catch (Exception e) |
492 | |
{ |
493 | 0 | throw new TorqueException("doSelect failed", e); |
494 | 0 | } |
495 | |
|
496 | 0 | List newList = new ArrayList(list.size()); |
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | 0 | for (Iterator it = list.iterator(); it.hasNext(); ) |
502 | |
{ |
503 | 0 | Permission p = getNewPermission((Persistent) it.next()); |
504 | 0 | newList.add(p); |
505 | 0 | } |
506 | |
|
507 | 0 | return newList; |
508 | |
} |
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
public static void doDelete(Criteria criteria) |
518 | |
throws TorqueException |
519 | |
{ |
520 | |
try |
521 | |
{ |
522 | 0 | Class[] clazz = new Class[] { Criteria.class }; |
523 | 0 | Object[] params = new Object[] { criteria }; |
524 | |
|
525 | 0 | persistentPeerClass |
526 | |
.getMethod("doDelete", clazz) |
527 | |
.invoke(null, params); |
528 | |
} |
529 | 0 | catch (Exception e) |
530 | |
{ |
531 | 0 | throw new TorqueException("doDelete failed", e); |
532 | 0 | } |
533 | 0 | } |
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
public static void setPermissionName(Persistent obj, String name) |
542 | |
{ |
543 | 0 | if(obj == null) |
544 | |
{ |
545 | 0 | return; |
546 | |
} |
547 | |
|
548 | |
try |
549 | |
{ |
550 | 0 | Object[] params = new Object[] { name }; |
551 | 0 | namePropDesc.getWriteMethod().invoke(obj, params); |
552 | |
} |
553 | 0 | catch (ClassCastException cce) |
554 | |
{ |
555 | 0 | String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; |
556 | 0 | log.error(msg); |
557 | 0 | throw new RuntimeException(msg); |
558 | |
} |
559 | 0 | catch (Exception e) |
560 | |
{ |
561 | 0 | log.error(e, e); |
562 | 0 | } |
563 | 0 | } |
564 | |
|
565 | |
|
566 | |
|
567 | |
|
568 | |
|
569 | |
|
570 | |
|
571 | |
|
572 | |
public static String getPermissionName(Persistent obj) |
573 | |
{ |
574 | 0 | String name = null; |
575 | |
|
576 | 0 | if(obj == null) |
577 | |
{ |
578 | 0 | return null; |
579 | |
} |
580 | |
|
581 | |
try |
582 | |
{ |
583 | 0 | name = (String) namePropDesc |
584 | |
.getReadMethod() |
585 | |
.invoke(obj, new Object[] {}); |
586 | |
} |
587 | 0 | catch (ClassCastException cce) |
588 | |
{ |
589 | 0 | String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; |
590 | 0 | log.error(msg); |
591 | 0 | throw new RuntimeException(msg); |
592 | |
} |
593 | 0 | catch (Exception e) |
594 | |
{ |
595 | 0 | log.error(e, e); |
596 | 0 | } |
597 | 0 | return name; |
598 | |
} |
599 | |
|
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
public static void setId(Persistent obj, int id) |
607 | |
{ |
608 | 0 | if(obj == null) |
609 | |
{ |
610 | 0 | return; |
611 | |
} |
612 | |
|
613 | |
try |
614 | |
{ |
615 | 0 | Object[] params = new Object[] { Integer.TYPE }; |
616 | 0 | idPropDesc.getWriteMethod().invoke(obj, params); |
617 | |
} |
618 | 0 | catch (ClassCastException cce) |
619 | |
{ |
620 | 0 | String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; |
621 | 0 | log.error(msg); |
622 | 0 | throw new RuntimeException(msg); |
623 | |
} |
624 | 0 | catch (Exception e) |
625 | |
{ |
626 | 0 | log.error(e, e); |
627 | 0 | } |
628 | 0 | } |
629 | |
|
630 | |
|
631 | |
|
632 | |
|
633 | |
|
634 | |
|
635 | |
|
636 | |
|
637 | |
public static Integer getIdAsObj(Persistent obj) |
638 | |
{ |
639 | 0 | Integer id = null; |
640 | |
|
641 | 0 | if(obj == null) |
642 | |
{ |
643 | 0 | return new Integer(0); |
644 | |
} |
645 | |
|
646 | |
try |
647 | |
{ |
648 | 0 | id = (Integer) idPropDesc |
649 | |
.getReadMethod() |
650 | |
.invoke(obj, new Object[] {}); |
651 | |
} |
652 | 0 | catch (ClassCastException cce) |
653 | |
{ |
654 | 0 | String msg = obj.getClass().getName() + " does not seem to be a Permission Object!"; |
655 | 0 | log.error(msg); |
656 | 0 | throw new RuntimeException(msg); |
657 | |
} |
658 | 0 | catch (Exception e) |
659 | |
{ |
660 | 0 | log.error(e, e); |
661 | 0 | } |
662 | 0 | return id; |
663 | |
} |
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
private static Class getPersistenceClass() |
674 | |
{ |
675 | 0 | Class persistenceClass = null; |
676 | |
|
677 | |
try |
678 | |
{ |
679 | 0 | Object[] params = new Object[0]; |
680 | |
|
681 | 0 | persistenceClass = (Class) persistentPeerClass |
682 | |
.getMethod("getOMClass", (Class[])null) |
683 | |
.invoke(null, params); |
684 | |
} |
685 | 0 | catch (Exception e) |
686 | |
{ |
687 | 0 | persistenceClass = null; |
688 | 0 | } |
689 | |
|
690 | 0 | return persistenceClass; |
691 | |
} |
692 | |
|
693 | |
|
694 | |
|
695 | |
|
696 | |
|
697 | |
|
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
|
703 | |
|
704 | |
|
705 | |
public static Permission getNewPermission(Persistent p) |
706 | |
{ |
707 | 0 | Permission perm = null; |
708 | |
try |
709 | |
{ |
710 | 0 | Class permissionWrapperClass = TurbineSecurity.getPermissionClass(); |
711 | |
|
712 | 0 | Class [] clazz = new Class [] { Persistent.class }; |
713 | 0 | Object [] params = new Object [] { p }; |
714 | |
|
715 | 0 | perm = (Permission) permissionWrapperClass |
716 | |
.getConstructor(clazz) |
717 | |
.newInstance(params); |
718 | |
} |
719 | 0 | catch (Exception e) |
720 | |
{ |
721 | 0 | log.error("Could not instantiate a new permission from supplied persistent: ", e); |
722 | 0 | } |
723 | |
|
724 | 0 | return perm; |
725 | |
} |
726 | |
} |
727 | |
|