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