##### Coverage Report - org.apache.turbine.services.security.torque.PermissionPeerManager

Classes in this File | Line Coverage | Branch Coverage | Complexity
PermissionPeerManager |
0% | 0/181
0% | 0/42
| 3.6666666666666665;3,667

1 |  |
     package org.apache.turbine.services.security.torque;
2 |  |
3 |  |
     /*
4 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
5 |  |
      * or more contributor license agreements.  See the NOTICE file
6 |  |
      * distributed with this work for additional information
7 |  |
      * regarding copyright ownership.  The ASF licenses this file
8 |  |
      * to you under the Apache License, Version 2.0 (the
9 |  |
      * "License"); you may not use this file except in compliance
10 |  |
      * with the License.  You may obtain a copy of the License at
11 |  |
      *
12 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing,
15 |  |
      * software distributed under the License is distributed on an
16 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 |  |
      * KIND, either express or implied.  See the License for the
18 |  |
      * specific language governing permissions and limitations
19 |  |
      * under the License.
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 |  |
      * This class capsulates all direct Peer access for the Permission entities.
50 |  |
      * It allows the exchange of the default Turbine supplied TurbinePermissionPeer
51 |  |
      * class against a custom class.
52 |  |
      *
53 |  |
      * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
54 |  |
      * @version $Id: PermissionPeerManager.java 1096130 2011-04-23 10:37:19Z ludwig $
55 |  |
      *
56 |  |
      */
57 |  |
58 | 0 |
     public class PermissionPeerManager
59 |  |
         implements PermissionPeerManagerConstants
60 |  |
     {
61 |  |
         /** The class of the Peer the TorqueSecurityService uses */
62 | 0 |
         private static Class persistentPeerClass = null;
63 |  |
64 |  |
         /** The class name of the objects returned by the configured peer. */
65 | 0 |
         private static Class permissionObject = null;
66 |  |
67 |  |
         /** The name of the Table used for Permission Object queries  */
68 | 0 |
         private static String tableName = null;
69 |  |
70 |  |
         /** The name of the column used as "Name" Column */
71 | 0 |
         private static String nameColumn = null;
72 |  |
73 |  |
         /** The name of the column used as "Id" Column */
74 | 0 |
         private static String idColumn = null;
75 |  |
76 |  |
         /** The "Name" property descriptor */
77 | 0 |
         private static PropertyDescriptor namePropDesc = null;
78 |  |
79 |  |
         /** The "Id" property descriptor */
80 | 0 |
         private static PropertyDescriptor idPropDesc = null;
81 |  |
82 |  |
         /** Logging */
83 | 0 |
         static Log log = LogFactory.getLog(PermissionPeerManager.class);
84 |  |
85 |  |
         /**
86 |  |
          * Initializes the PermissionPeerManager, loading the class object for the
87 |  |
          * Peer used to retrieve Permission objects
88 |  |
          *
89 |  |
          * @param conf The configuration object used to configure the Manager
90 |  |
          *
91 |  |
          * @exception InitializationException A problem occured during initialization
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 |  |
                 // We have either an user configured Object class or we use the
112 |  |
                 // default as supplied by the Peer class
113 |  |
                 //
114 |  |
                 // Default from Peer, can be overridden
115 |  |
116 | 0 |
                 permissionObject = getPersistenceClass();
117 |  |
118 | 0 |
                 permissionObjectName = conf.getString(PERMISSION_CLASS_KEY,
119 |  |
                                             permissionObject.getName());
120 |  |
121 |  |
                 // Maybe the user set a new value...
122 | 0 |
                 permissionObject = Class.forName(permissionObjectName);
123 |  |
124 |  |
                 /* If any of the following Field queries fails, the permission
125 |  |
                  * subsystem is unusable. So check this right here at init time,
126 |  |
                  * which saves us much time and hassle if it fails...
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 |  |
          * Get the name of this table.
187 |  |
          *
188 |  |
          * @return A String with the name of the table.
189 |  |
          */
190 |  |
         public static String getTableName()
191 |  |
         {
192 | 0 |
             return tableName;
193 |  |
         }
194 |  |
195 |  |
         /**
196 |  |
          * Returns the fully qualified name of the Column to
197 |  |
          * use as the Name Column for a permission
198 |  |
          *
199 |  |
          * @return A String containing the column name
200 |  |
          */
201 |  |
         public static String getNameColumn()
202 |  |
         {
203 | 0 |
             return nameColumn;
204 |  |
         }
205 |  |
206 |  |
         /**
207 |  |
          * Returns the fully qualified name of the Column to
208 |  |
          * use as the Id Column for a permission
209 |  |
          *
210 |  |
          * @return A String containing the column id
211 |  |
          *
212 |  |
          */
213 |  |
         public static String getIdColumn()
214 |  |
         {
215 | 0 |
             return idColumn;
216 |  |
         }
217 |  |
218 |  |
         /**
219 |  |
          * Returns the full name of a column.
220 |  |
          *
221 |  |
          * @param name The column to fully qualify
222 |  |
          *
223 |  |
          * @return A String with the full name of the column.
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 |  |
          * Returns a new, empty object for the underlying peer.
236 |  |
          * Used to create a new underlying object
237 |  |
          *
238 |  |
          * @return A new object which is compatible to the Peer
239 |  |
          *         and can be used as a User object
240 |  |
          *
241 |  |
          */
242 |  |
243 |  |
         public static Persistent newPersistentInstance()
244 |  |
         {
245 | 0 |
             Persistent obj = null;
246 |  |
247 | 0 |
             if(permissionObject == null)
248 |  |
             {
249 |  |
                 // This can happen if the Turbine wants to determine the
250 |  |
                 // name of the anonymous user before the security service
251 |  |
                 // has been initialized. In this case, the Peer Manager
252 |  |
                 // has not yet been inited and the permissionObject is still
253 |  |
                 // null. Return null in this case.
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 |  |
          * Checks if a Permission is defined in the system. The name
272 |  |
          * is used as query criteria.
273 |  |
          *
274 |  |
          * @param permission The Permission to be checked.
275 |  |
          * @return <code>true</code> if given Permission exists in the system.
276 |  |
          * @throws DataBackendException when more than one Permission with
277 |  |
          *         the same name exists.
278 |  |
          * @throws Exception A generic exception.
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 |  |
          * Retrieves/assembles a PermissionSet
301 |  |
          *
302 |  |
          * @param criteria The criteria to use.
303 |  |
          * @return A PermissionSet.
304 |  |
          * @exception Exception A generic Exception.
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 |  |
          * Retrieves a set of Permissions associated with a particular Role.
321 |  |
          *
322 |  |
          * @param role The role to query permissions of.
323 |  |
          * @return A set of permissions associated with the Role.
324 |  |
          * @exception Exception A generic Exception.
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 |  |
          * Pass in two Vector's of Permission Objects.  It will return a
341 |  |
          * new Vector with the difference of the two Vectors: C = (A - B).
342 |  |
          *
343 |  |
          * @param some Vector B in C = (A - B).
344 |  |
          * @param all Vector A in C = (A - B).
345 |  |
          * @return Vector C in C = (A - B).
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 |  |
          * WARNING! Do not read on if you have a weak stomach. What follows here
371 |  |
          * are some abominations thanks to the braindead static peers of Torque
372 |  |
          * and the rigidity of Java....
373 |  |
          *
374 |  |
          * ========================================================================
375 |  |
          *
376 |  |
          */
377 |  |
378 |  |
         /**
379 |  |
          * Calls buildCriteria(Permission permission) in
380 |  |
          * the configured PermissionPeer. If you get a
381 |  |
          * ClassCastException in this routine, you put a
382 |  |
          * Permission object into this method which
383 |  |
          * can't be cast into an object for the
384 |  |
          * TorqueSecurityService. This is a configuration error most of
385 |  |
          * the time.
386 |  |
          *
387 |  |
          * @param permission An object which implements
388 |  |
          *                 the Permission interface
389 |  |
          *
390 |  |
          * @return A criteria for the supplied permission object
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 |  |
          * Invokes doUpdate(Criteria c) on the configured Peer Object
417 |  |
          *
418 |  |
          * @param criteria  A Criteria Object
419 |  |
          *
420 |  |
          * @exception TorqueException A problem occured.
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 |  |
          * Invokes doInsert(Criteria c) on the configured Peer Object
443 |  |
          *
444 |  |
          * @param criteria  A Criteria Object
445 |  |
          *
446 |  |
          * @exception TorqueException A problem occured.
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 |  |
          * Invokes doSelect(Criteria c) on the configured Peer Object
469 |  |
          *
470 |  |
          * @param criteria  A Criteria Object
471 |  |
          *
472 |  |
          * @return A List of Permission Objects selected by the Criteria
473 |  |
          *
474 |  |
          * @exception TorqueException A problem occured.
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 |  |
             // Wrap the returned Objects into TorquePermissions.
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 |  |
          * Invokes doDelete(Criteria c) on the configured Peer Object
512 |  |
          *
513 |  |
          * @param criteria  A Criteria Object
514 |  |
          *
515 |  |
          * @exception TorqueException A problem occured.
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 |  |
          * Invokes setName(String s) on the supplied base object
537 |  |
          *
538 |  |
          * @param obj The object to use for setting the name
539 |  |
          * @param name The Name to set
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 |  |
          * Invokes getName() on the supplied base object
567 |  |
          *
568 |  |
          * @param obj The object to use for getting the name
569 |  |
          *
570 |  |
          * @return A string containing the name
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 |  |
          * Invokes setId(int n) on the supplied base object
602 |  |
          *
603 |  |
          * @param obj The object to use for setting the name
604 |  |
          * @param id The new Id
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 |  |
          * Invokes getId() on the supplied base object
632 |  |
          *
633 |  |
          * @param obj The object to use for getting the id
634 |  |
          *
635 |  |
          * @return The Id of this object
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 |  |
          * Returns the Class of the configured Object class
667 |  |
          * from the peer
668 |  |
          *
669 |  |
          * @return The class of the objects returned by the configured peer
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 |  |
          * Returns a new, configured Permission Object with
695 |  |
          * a supplied Persistent object at its core
696 |  |
          *
697 |  |
          * @param p The persistent object
698 |  |
          *
699 |  |
          * @return a new, configured Permission Object
700 |  |
          *
701 |  |
          * @exception Exception Could not create a new Object
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 |  |