View Javadoc

1   package org.apache.turbine.services.security.torque;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.beans.PropertyDescriptor;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.configuration.Configuration;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import org.apache.torque.TorqueException;
31  import org.apache.torque.om.Persistent;
32  import org.apache.torque.util.BasePeer;
33  import org.apache.torque.util.Criteria;
34  
35  import org.apache.turbine.om.security.User;
36  import org.apache.turbine.services.InitializationException;
37  import org.apache.turbine.services.security.TurbineSecurity;
38  import org.apache.turbine.util.security.DataBackendException;
39  
40  /***
41   * This class capsulates all direct Peer access for the User entities.
42   * It allows the exchange of the default Turbine supplied TurbineUserPeer
43   * class against a custom class.
44   *
45   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
46   * @version $Id: UserPeerManager.java 264152 2005-08-29 14:50:22Z henning $
47   */
48  
49  public class UserPeerManager
50      implements UserPeerManagerConstants
51  {
52      /*** Serial UID */
53      private static final long serialVersionUID = 6943046259921811593L;
54  
55      /*** The class of the Peer the TorqueSecurityService uses */
56      private static Class userPeerClass = null;
57  
58      /*** The class name of the objects returned by the configured peer. */
59      private static Class userObject = null;
60  
61      /*** The name of the Table used for Group Object queries  */
62      private static String tableName = null;
63  
64      /*** The name of the column used as "Name" Column */
65      private static String nameColumn = null;
66  
67      /*** The name of the column used as "Id" Column */
68      private static String idColumn = null;
69  
70      /*** The name of the column used as "Password" Column */
71      private static String passwordColumn = null;
72  
73      /*** The name of the column used as "First name" Column */
74      private static String firstNameColumn = null;
75  
76      /*** The name of the column used as "Last name" Column */
77      private static String lastNameColumn = null;
78  
79      /*** The name of the column used as "Email" Column */
80      private static String emailColumn = null;
81  
82      /*** The name of the column used as "Confirm" Column */
83      private static String confirmColumn = null;
84  
85      /*** The name of the column used as "create date" Column */
86      private static String createDateColumn = null;
87  
88      /*** The name of the column used as "last login" Column */
89      private static String lastLoginColumn = null;
90  
91      /*** The name of the column used as "objectdata" Column */
92      private static String objectdataColumn = null;
93  
94      /*** The "Name" property descriptor */
95      private static PropertyDescriptor namePropDesc = null;
96  
97      /*** The "Id" property descriptor */
98      private static PropertyDescriptor idPropDesc = null;
99  
100     /*** The "Password" property descriptor */
101     private static PropertyDescriptor passwordPropDesc = null;
102 
103     /*** The "First name" property descriptor */
104     private static PropertyDescriptor firstNamePropDesc = null;
105 
106     /*** The "Last name" property descriptor */
107     private static PropertyDescriptor lastNamePropDesc = null;
108 
109     /*** The "Email" property descriptor */
110     private static PropertyDescriptor emailPropDesc = null;
111 
112     /*** The "Confirm" property descriptor */
113     private static PropertyDescriptor confirmPropDesc = null;
114 
115     /*** The "create date" property descriptor */
116     private static PropertyDescriptor createDatePropDesc = null;
117 
118     /*** The "last login" property descriptor */
119     private static PropertyDescriptor lastLoginPropDesc = null;
120 
121     /*** The "objectdata" property descriptor */
122     private static PropertyDescriptor objectdataPropDesc = null;
123 
124     /*** Logging */
125     static Log log = LogFactory.getLog(UserPeerManager.class);
126 
127     /***
128      * Initializes the UserPeerManager, loading the class object for the
129      * Peer used to retrieve User objects
130      *
131      * @param conf The configuration object used to configure the Manager
132      *
133      * @exception InitializationException A problem occured during
134      *            initialization
135      */
136 
137     public static void init(Configuration conf)
138         throws InitializationException
139     {
140         String userPeerClassName = conf.getString(USER_PEER_CLASS_KEY,
141                                                   USER_PEER_CLASS_DEFAULT);
142         String userObjectName = null;
143 
144         try
145         {
146             userPeerClass = Class.forName(userPeerClassName);
147 
148             tableName  =
149               (String) userPeerClass.getField("TABLE_NAME").get(null);
150 
151             //
152             // We have either an user configured Object class or we use the
153             // default as supplied by the Peer class
154             //
155 
156             // Default from Peer, can be overridden
157 
158             userObject = getPersistenceClass();
159 
160             userObjectName = conf.getString(USER_CLASS_KEY,
161                     userObject.getName());
162 
163             // Maybe the user set a new value...
164             userObject = Class.forName(userObjectName);
165 
166             /* If any of the following Field queries fails, the user
167              * subsystem is unusable. So check this right here at init time,
168              * which saves us much time and hassle if it fails...
169              */
170 
171             nameColumn = (String) userPeerClass.getField(
172                 conf.getString(USER_NAME_COLUMN_KEY,
173                                USER_NAME_COLUMN_DEFAULT)
174                 ).get(null);
175 
176             idColumn = (String) userPeerClass.getField(
177                 conf.getString(USER_ID_COLUMN_KEY,
178                                USER_ID_COLUMN_DEFAULT)
179                 ).get(null);
180 
181             passwordColumn = (String) userPeerClass.getField(
182                 conf.getString(USER_PASSWORD_COLUMN_KEY,
183                                USER_PASSWORD_COLUMN_DEFAULT)
184                 ).get(null);
185 
186             firstNameColumn  = (String) userPeerClass.getField(
187                 conf.getString(USER_FIRST_NAME_COLUMN_KEY,
188                                USER_FIRST_NAME_COLUMN_DEFAULT)
189                 ).get(null);
190 
191             lastNameColumn = (String) userPeerClass.getField(
192                 conf.getString(USER_LAST_NAME_COLUMN_KEY,
193                                USER_LAST_NAME_COLUMN_DEFAULT)
194                 ).get(null);
195 
196             emailColumn = (String) userPeerClass.getField(
197                 conf.getString(USER_EMAIL_COLUMN_KEY,
198                                USER_EMAIL_COLUMN_DEFAULT)
199                 ).get(null);
200 
201             confirmColumn    = (String) userPeerClass.getField(
202                 conf.getString(USER_CONFIRM_COLUMN_KEY,
203                                USER_CONFIRM_COLUMN_DEFAULT)
204                 ).get(null);
205 
206             createDateColumn = (String) userPeerClass.getField(
207                 conf.getString(USER_CREATE_COLUMN_KEY,
208                                USER_CREATE_COLUMN_DEFAULT)
209                 ).get(null);
210 
211             lastLoginColumn = (String) userPeerClass.getField(
212                 conf.getString(USER_LAST_LOGIN_COLUMN_KEY,
213                                USER_LAST_LOGIN_COLUMN_DEFAULT)
214                 ).get(null);
215 
216             objectdataColumn = (String) userPeerClass.getField(
217                 conf.getString(USER_OBJECTDATA_COLUMN_KEY,
218                                USER_OBJECTDATA_COLUMN_DEFAULT)
219                 ).get(null);
220 
221             namePropDesc =
222                 new PropertyDescriptor(conf.getString(
223                                            USER_NAME_PROPERTY_KEY,
224                                            USER_NAME_PROPERTY_DEFAULT),
225                                        userObject);
226 
227             idPropDesc =
228                 new PropertyDescriptor(conf.getString(
229                                            USER_ID_PROPERTY_KEY,
230                                            USER_ID_PROPERTY_DEFAULT),
231                                        userObject);
232 
233             passwordPropDesc =
234                 new PropertyDescriptor(conf.getString(
235                                            USER_PASSWORD_PROPERTY_KEY,
236                                            USER_PASSWORD_PROPERTY_DEFAULT),
237                                        userObject);
238 
239             firstNamePropDesc =
240                 new PropertyDescriptor(conf.getString(
241                                            USER_FIRST_NAME_PROPERTY_KEY,
242                                            USER_FIRST_NAME_PROPERTY_DEFAULT),
243                                        userObject);
244 
245             lastNamePropDesc   =
246                 new PropertyDescriptor(conf.getString(
247                                            USER_LAST_NAME_PROPERTY_KEY,
248                                            USER_LAST_NAME_PROPERTY_DEFAULT),
249                                        userObject);
250 
251             emailPropDesc =
252                 new PropertyDescriptor(conf.getString(
253                                            USER_EMAIL_PROPERTY_KEY,
254                                            USER_EMAIL_PROPERTY_DEFAULT),
255                                        userObject);
256 
257             confirmPropDesc =
258                 new PropertyDescriptor(conf.getString(
259                                            USER_CONFIRM_PROPERTY_KEY,
260                                            USER_CONFIRM_PROPERTY_DEFAULT),
261                                        userObject);
262 
263             createDatePropDesc =
264                 new PropertyDescriptor(conf.getString(
265                                            USER_CREATE_PROPERTY_KEY,
266                                            USER_CREATE_PROPERTY_DEFAULT),
267                                        userObject);
268 
269             lastLoginPropDesc  =
270                 new PropertyDescriptor(conf.getString(
271                                            USER_LAST_LOGIN_PROPERTY_KEY,
272                                            USER_LAST_LOGIN_PROPERTY_DEFAULT),
273                                        userObject);
274 
275             objectdataPropDesc =
276                 new PropertyDescriptor(conf.getString(
277                                            USER_OBJECTDATA_PROPERTY_KEY,
278                                            USER_OBJECTDATA_PROPERTY_DEFAULT),
279                                        userObject);
280         }
281         catch (Exception e)
282         {
283             if (userPeerClassName == null || userPeerClass == null)
284             {
285                 throw new InitializationException(
286                     "Could not find UserPeer class ("
287                     + userPeerClassName + ")", e);
288             }
289             if (tableName == null)
290             {
291                 throw new InitializationException(
292                     "Failed to get the table name from the Peer object", e);
293             }
294 
295             if (userObject == null || userObjectName == null)
296             {
297                 throw new InitializationException(
298                     "Failed to get the object type from the Peer object", e);
299             }
300 
301 
302             if (nameColumn == null || namePropDesc == null)
303             {
304                 throw new InitializationException(
305                     "UserPeer " + userPeerClassName
306                     + " has no name column information!", e);
307             }
308             if (idColumn == null || idPropDesc == null)
309             {
310                 throw new InitializationException(
311                     "UserPeer " + userPeerClassName
312                     + " has no id column information!", e);
313             }
314             if (passwordColumn == null || passwordPropDesc == null)
315             {
316                 throw new InitializationException(
317                     "UserPeer " + userPeerClassName
318                     + " has no password column information!", e);
319             }
320             if (firstNameColumn == null || firstNamePropDesc == null)
321             {
322                 throw new InitializationException(
323                     "UserPeer " + userPeerClassName
324                     + " has no firstName column information!", e);
325             }
326             if (lastNameColumn == null || lastNamePropDesc == null)
327             {
328                 throw new InitializationException(
329                     "UserPeer " + userPeerClassName
330                     + " has no lastName column information!", e);
331             }
332             if (emailColumn == null || emailPropDesc == null)
333             {
334                 throw new InitializationException(
335                     "UserPeer " + userPeerClassName
336                     + " has no email column information!", e);
337             }
338             if (confirmColumn == null || confirmPropDesc == null)
339             {
340                 throw new InitializationException(
341                     "UserPeer " + userPeerClassName
342                     + " has no confirm column information!", e);
343             }
344             if (createDateColumn == null || createDatePropDesc == null)
345             {
346                 throw new InitializationException(
347                     "UserPeer " + userPeerClassName
348                     + " has no createDate column information!", e);
349             }
350             if (lastLoginColumn == null || lastLoginPropDesc == null)
351             {
352                 throw new InitializationException(
353                     "UserPeer " + userPeerClassName
354                     + " has no lastLogin column information!", e);
355             }
356             if (objectdataColumn == null || objectdataPropDesc == null)
357             {
358                 throw new InitializationException(
359                     "UserPeer " + userPeerClassName
360                     + " has no objectdata column information!", e);
361             }
362         }
363     }
364 
365     /***
366      * Get the name of this table.
367      *
368      * @return A String with the name of the table.
369      */
370     public static String getTableName()
371     {
372         return tableName;
373     }
374 
375     /***
376      * Returns the fully qualified name of the Column to
377      * use as the Name Column for a group
378      *
379      * @return A String containing the column name
380      */
381     public static String getNameColumn()
382     {
383         return nameColumn;
384     }
385 
386     /***
387      * Returns the fully qualified name of the Column to
388      * use as the Id Column for a group
389      *
390      * @return A String containing the column id
391      */
392     public static String getIdColumn()
393     {
394         return idColumn;
395     }
396 
397     /***
398      * Returns the fully qualified name of the Column to
399      * use as the Password Column for a role
400      *
401      * @return A String containing the column name
402      */
403     public static String getPasswordColumn()
404     {
405         return passwordColumn;
406     }
407 
408     /***
409      * Returns the fully qualified name of the Column to
410      * use as the FirstName Column for a role
411      *
412      * @return A String containing the column name
413      */
414     public static String getFirstNameColumn()
415     {
416         return firstNameColumn;
417     }
418 
419     /***
420      * Returns the fully qualified name of the Column to
421      * use as the LastName Column for a role
422      *
423      * @return A String containing the column name
424      */
425     public static String getLastNameColumn()
426     {
427         return lastNameColumn;
428     }
429 
430     /***
431      * Returns the fully qualified name of the Column to
432      * use as the Email Column for a role
433      *
434      * @return A String containing the column name
435      */
436     public static String getEmailColumn()
437     {
438         return emailColumn;
439     }
440 
441     /***
442      * Returns the fully qualified name of the Column to
443      * use as the Confirm Column for a role
444      *
445      * @return A String containing the column name
446      */
447     public static String getConfirmColumn()
448     {
449         return confirmColumn;
450     }
451 
452     /***
453      * Returns the fully qualified name of the Column to
454      * use as the CreateDate Column for a role
455      *
456      * @return A String containing the column name
457      */
458     public static String getCreateDateColumn()
459     {
460         return createDateColumn;
461     }
462 
463     /***
464      * Returns the fully qualified name of the Column to
465      * use as the LastLogin Column for a role
466      *
467      * @return A String containing the column name
468      */
469     public static String getLastLoginColumn()
470     {
471         return lastLoginColumn;
472     }
473 
474     /***
475      * Returns the fully qualified name of the Column to
476      * use as the objectdata Column for a role
477      *
478      * @return A String containing the column name
479      */
480     public static String getObjectdataColumn()
481     {
482         return objectdataColumn;
483     }
484 
485     /***
486      * Returns the full name of a column.
487      *
488      * @param name The column to fully qualify
489      *
490      * @return A String with the full name of the column.
491      */
492     public static String getColumnName(String name)
493     {
494         StringBuffer sb = new StringBuffer();
495         sb.append(getTableName());
496         sb.append(".");
497         sb.append(name);
498         return sb.toString();
499     }
500 
501     /***
502      * Returns the full name of a column.
503      *
504      * @param name The column to fully qualify
505      *
506      * @return A String with the full name of the column.
507      * @deprecated use getColumnName(String name)
508      */
509     public String getFullColumnName(String name)
510     {
511         return getColumnName(name);
512     }
513 
514 
515     /***
516      * Returns a new, empty object for the underlying peer.
517      * Used to create a new underlying object
518      *
519      * @return A new object which is compatible to the Peer
520      *         and can be used as a User object
521      *
522      */
523 
524     public static Persistent newPersistentInstance()
525     {
526         Persistent obj = null;
527 
528         if (userObject == null)
529         {
530             // This can happen if the Turbine wants to determine the
531             // name of the anonymous user before the security service
532             // has been initialized. In this case, the Peer Manager
533             // has not yet been inited and the userObject is still
534             // null. Return null in this case.
535             //
536             return obj;
537         }
538 
539         try
540         {
541             obj = (Persistent) userObject.newInstance();
542         }
543         catch (Exception e)
544         {
545             log.error("Could not instantiate a user object", e);
546             obj = null;
547         }
548         return obj;
549     }
550 
551     /***
552      * Checks if a User is defined in the system. The name
553      * is used as query criteria.
554      *
555      * @param user The User to be checked.
556      * @return <code>true</code> if given User exists in the system.
557      * @throws DataBackendException when more than one User with
558      *         the same name exists.
559      * @throws Exception A generic exception.
560      */
561     public static boolean checkExists(User user)
562         throws DataBackendException, Exception
563     {
564         Criteria criteria = new Criteria();
565 
566         criteria.addSelectColumn(getIdColumn());
567 
568         criteria.add(getNameColumn(), user.getName());
569 
570         List results = BasePeer.doSelect(criteria);
571 
572         if (results.size() > 1)
573         {
574             throw new DataBackendException("Multiple users named '" +
575                                            user.getName() + "' exist!");
576         }
577         return (results.size() == 1);
578     }
579 
580     /***
581      * Returns a List of all User objects.
582      *
583      * @return A List with all users in the system.
584      * @exception Exception A generic exception.
585      */
586     public static List selectAllUsers()
587         throws Exception
588     {
589         Criteria criteria = new Criteria();
590         criteria.addAscendingOrderByColumn(getLastNameColumn());
591         criteria.addAscendingOrderByColumn(getFirstNameColumn());
592         criteria.setIgnoreCase(true);
593         return doSelect(criteria);
594     }
595 
596     /***
597      * Returns a List of all confirmed User objects.
598      *
599      * @return A List with all confirmed users in the system.
600      * @exception Exception A generic exception.
601      */
602     public static List selectAllConfirmedUsers()
603         throws Exception
604     {
605         Criteria criteria = new Criteria();
606 
607         criteria.add (getConfirmColumn(), User.CONFIRM_DATA);
608         criteria.addAscendingOrderByColumn(getLastNameColumn());
609         criteria.addAscendingOrderByColumn(getFirstNameColumn());
610         criteria.setIgnoreCase(true);
611         return doSelect(criteria);
612     }
613 
614     /*
615      * ========================================================================
616      *
617      * WARNING! Do not read on if you have a weak stomach. What follows here
618      * are some abominations thanks to the braindead static peers of Torque
619      * and the rigidity of Java....
620      *
621      * ========================================================================
622      *
623      */
624 
625     /***
626      * Calls buildCriteria(User user) in the configured UserPeer. If you get
627      * a ClassCastException in this routine, you put a User object into this
628      * method which can't be cast into an object for the TorqueSecurityService. This is a
629      * configuration error most of the time.
630      *
631      * @param user An object which implements the User interface
632      *
633      * @return A criteria for the supplied user object
634      */
635 
636     public static Criteria buildCriteria(User user)
637     {
638         Criteria crit;
639 
640         try
641         {
642             Class[] clazz = new Class[] { userObject };
643             Object[] params =
644                 new Object[] { ((TorqueUser) user).getPersistentObj() };
645 
646             crit =  (Criteria) userPeerClass
647                 .getMethod("buildCriteria", clazz)
648                 .invoke(null, params);
649         }
650         catch (Exception e)
651         {
652             crit = null;
653         }
654 
655         return crit;
656     }
657 
658     /***
659      * Invokes doUpdate(Criteria c) on the configured Peer Object
660      *
661      * @param criteria  A Criteria Object
662      *
663      * @exception TorqueException A problem occured.
664      */
665 
666     public static void doUpdate(Criteria criteria)
667         throws TorqueException
668     {
669         try
670         {
671             Class[] clazz = new Class[] { Criteria.class };
672             Object[] params = new Object[] { criteria };
673 
674             userPeerClass
675                 .getMethod("doUpdate", clazz)
676                 .invoke(null, params);
677         }
678         catch (Exception e)
679         {
680             throw new TorqueException("doUpdate failed", e);
681         }
682     }
683 
684     /***
685      * Invokes doInsert(Criteria c) on the configured Peer Object
686      *
687      * @param criteria  A Criteria Object
688      *
689      * @exception TorqueException A problem occured.
690      */
691 
692     public static void doInsert(Criteria criteria)
693         throws TorqueException
694     {
695         try
696         {
697             Class[] clazz = new Class[] { Criteria.class };
698             Object[] params = new Object[] { criteria };
699 
700             userPeerClass
701                 .getMethod("doInsert", clazz)
702                 .invoke(null, params);
703         }
704         catch (Exception e)
705         {
706             throw new TorqueException("doInsert failed", e);
707         }
708     }
709 
710     /***
711      * Invokes doSelect(Criteria c) on the configured Peer Object
712      *
713      * @param criteria  A Criteria Object
714      *
715      * @return A List of User Objects selected by the Criteria
716      *
717      * @exception TorqueException A problem occured.
718      */
719     public static List doSelect(Criteria criteria)
720         throws TorqueException
721     {
722         List list;
723 
724         try
725         {
726             Class[] clazz =
727                 new Class[] { Criteria.class };
728             Object[] params = new Object[] { criteria };
729 
730             list = (List) userPeerClass
731                 .getMethod("doSelect", clazz)
732                 .invoke(null, params);
733         }
734         catch (Exception e)
735         {
736             throw new TorqueException("doSelect failed", e);
737         }
738         List newList = new ArrayList(list.size());
739 
740         //
741         // Wrap the returned Objects into TorqueUsers.
742         //
743         for (Iterator it = list.iterator(); it.hasNext(); )
744         {
745             User u = getNewUser((Persistent) it.next());
746             newList.add(u);
747         }
748 
749         return newList;
750     }
751 
752     /***
753      * Invokes doDelete(Criteria c) on the configured Peer Object
754      *
755      * @param criteria  A Criteria Object
756      *
757      * @exception TorqueException A problem occured.
758      */
759     public static void doDelete(Criteria criteria)
760         throws TorqueException
761     {
762         try
763         {
764             Class[] clazz = new Class[] { Criteria.class };
765             Object[] params = new Object[] { criteria };
766 
767             userPeerClass
768                 .getMethod("doDelete", clazz)
769                 .invoke(null, params);
770         }
771         catch (Exception e)
772         {
773             throw new TorqueException("doDelete failed", e);
774         }
775     }
776 
777     /***
778      * Invokes setName(String s) on the supplied base object
779      *
780      * @param obj The object to use for setting the name
781      * @param name The Name to set
782      */
783     public static void setUserName(Persistent obj, String name)
784     {
785         if (obj == null)
786         {
787             return;
788         }
789 
790         try
791         {
792             Object[] params = new Object[] { name };
793             namePropDesc.getWriteMethod().invoke(obj, params);
794         }
795         catch (ClassCastException cce)
796         {
797             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
798             log.error(msg);
799             throw new RuntimeException(msg);
800         }
801         catch (Exception e)
802         {
803             log.error(e, e);
804         }
805     }
806 
807     /***
808      * Invokes getName() on the supplied base object
809      *
810      * @param obj The object to use for getting the name
811      *
812      * @return A string containing the name
813      *
814      * @deprecated use getName(obj)
815      */
816     public static String getUserName(Persistent obj)
817     {
818         return getName(obj);
819     }
820 
821     /***
822      * Invokes getName() on the supplied base object
823      *
824      * @param obj The object to use for getting the name
825      *
826      * @return A string containing the name
827      */
828     public static String getName(Persistent obj)
829     {
830         String name = null;
831 
832         if (obj == null)
833         {
834             return null;
835         }
836 
837         try
838         {
839             name = (String) namePropDesc
840                 .getReadMethod()
841                 .invoke(obj, new Object[] {});
842         }
843         catch (ClassCastException cce)
844         {
845             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
846             log.error(msg);
847             throw new RuntimeException(msg);
848         }
849         catch (Exception e)
850         {
851             log.error(e, e);
852         }
853         return name;
854     }
855 
856     /***
857      * Invokes setPassword(String s) on the supplied base object
858      *
859      * @param obj The object to use for setting the password
860      * @param password The Password to set
861      */
862     public static void setUserPassword(Persistent obj, String password)
863     {
864         if (obj == null)
865         {
866             return;
867         }
868 
869         try
870         {
871             Object[] params = new Object[] { password };
872             passwordPropDesc.getWriteMethod().invoke(obj, params);
873         }
874         catch (ClassCastException cce)
875         {
876             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
877             log.error(msg);
878             throw new RuntimeException(msg);
879         }
880         catch (Exception e)
881         {
882             log.error(e, e);
883         }
884     }
885 
886     /***
887      * Invokes getPassword() on the supplied base object
888      *
889      * @param obj The object to use for getting the password
890      *
891      * @return A string containing the password
892      */
893     public static String getUserPassword(Persistent obj)
894     {
895         String password = null;
896 
897         if (obj == null)
898         {
899             return null;
900         }
901 
902         try
903         {
904             password = (String) passwordPropDesc
905                 .getReadMethod()
906                 .invoke(obj, new Object[] {});
907         }
908         catch (ClassCastException cce)
909         {
910             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
911             log.error(msg);
912             throw new RuntimeException(msg);
913         }
914         catch (Exception e)
915         {
916             log.error(e, e);
917         }
918         return password;
919     }
920 
921     /***
922      * Invokes setFirstName(String s) on the supplied base object
923      *
924      * @param obj The object to use for setting the first name
925      * @param firstName The first name to set
926      */
927     public static void setUserFirstName(Persistent obj, String firstName)
928     {
929         if (obj == null)
930         {
931             return;
932         }
933 
934         try
935         {
936             Object[] params = new Object[] { firstName };
937             firstNamePropDesc.getWriteMethod().invoke(obj, params);
938         }
939         catch (ClassCastException cce)
940         {
941             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
942             log.error(msg);
943             throw new RuntimeException(msg);
944         }
945         catch (Exception e)
946         {
947             log.error(e, e);
948         }
949     }
950 
951     /***
952      * Invokes getFirstName() on the supplied base object
953      *
954      * @param obj The object to use for getting the first name
955      *
956      * @return A string containing the first name
957      */
958     public static String getUserFirstName(Persistent obj)
959     {
960         String firstName = null;
961 
962         if (obj == null)
963         {
964             return null;
965         }
966 
967         try
968         {
969             firstName = (String) firstNamePropDesc
970                 .getReadMethod()
971                 .invoke(obj, new Object[] {});
972         }
973         catch (ClassCastException cce)
974         {
975             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
976             log.error(msg);
977             throw new RuntimeException(msg);
978         }
979         catch (Exception e)
980         {
981             log.error(e, e);
982         }
983         return firstName;
984     }
985 
986     /***
987      * Invokes setLastName(String s) on the supplied base object
988      *
989      * @param obj The object to use for setting the last name
990      * @param lastName The Last Name to set
991      */
992     public static void setUserLastName(Persistent obj, String lastName)
993     {
994         if (obj == null)
995         {
996             return;
997         }
998 
999         try
1000         {
1001             Object[] params = new Object[] { lastName };
1002             lastNamePropDesc.getWriteMethod().invoke(obj, params);
1003         }
1004         catch (ClassCastException cce)
1005         {
1006             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1007             log.error(msg);
1008             throw new RuntimeException(msg);
1009         }
1010         catch (Exception e)
1011         {
1012             log.error(e, e);
1013         }
1014     }
1015 
1016     /***
1017      * Invokes getLastName() on the supplied base object
1018      *
1019      * @param obj The object to use for getting the last name
1020      *
1021      * @return A string containing the last name
1022      */
1023     public static String getUserLastName(Persistent obj)
1024     {
1025         String lastName = null;
1026 
1027         if (obj == null)
1028         {
1029             return null;
1030         }
1031 
1032         try
1033         {
1034             lastName = (String) lastNamePropDesc
1035                 .getReadMethod()
1036                 .invoke(obj, new Object[] {});
1037         }
1038         catch (ClassCastException cce)
1039         {
1040             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1041             log.error(msg);
1042             throw new RuntimeException(msg);
1043         }
1044         catch (Exception e)
1045         {
1046             log.error(e, e);
1047         }
1048         return lastName;
1049     }
1050 
1051     /***
1052      * Invokes setEmail(String s) on the supplied base object
1053      *
1054      * @param obj The object to use for setting the email
1055      * @param email The Email to set
1056      */
1057     public static void setUserEmail(Persistent obj, String email)
1058     {
1059         if (obj == null)
1060         {
1061             return;
1062         }
1063 
1064         try
1065         {
1066             Object[] params = new Object[] { email };
1067             emailPropDesc.getWriteMethod().invoke(obj, params);
1068         }
1069         catch (ClassCastException cce)
1070         {
1071             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1072             log.error(msg);
1073             throw new RuntimeException(msg);
1074         }
1075         catch (Exception e)
1076         {
1077             log.error(e, e);
1078         }
1079     }
1080 
1081     /***
1082      * Invokes getEmail() on the supplied base object
1083      *
1084      * @param obj The object to use for getting the email
1085      *
1086      * @return A string containing the email
1087      */
1088     public static String getUserEmail(Persistent obj)
1089     {
1090         String email = null;
1091 
1092         if (obj == null)
1093         {
1094             return null;
1095         }
1096 
1097         try
1098         {
1099             email = (String) emailPropDesc
1100                 .getReadMethod()
1101                 .invoke(obj, new Object[] {});
1102         }
1103         catch (ClassCastException cce)
1104         {
1105             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1106             log.error(msg);
1107             throw new RuntimeException(msg);
1108         }
1109         catch (Exception e)
1110         {
1111             log.error(e, e);
1112         }
1113         return email;
1114     }
1115 
1116     /***
1117      * Invokes setConfirmed(String s) on the supplied base object
1118      *
1119      * @param obj The object to use for setting the confirm value
1120      * @param confirm The confirm value to set
1121      */
1122     public static void setUserConfirmed(Persistent obj, String confirm)
1123     {
1124         if (obj == null)
1125         {
1126             return;
1127         }
1128 
1129         try
1130         {
1131             Object[] params = new Object[] { confirm };
1132             confirmPropDesc.getWriteMethod().invoke(obj, params);
1133         }
1134         catch (ClassCastException cce)
1135         {
1136             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1137             log.error(msg);
1138             throw new RuntimeException(msg);
1139         }
1140         catch (Exception e)
1141         {
1142             log.error(e, e);
1143         }
1144     }
1145 
1146     /***
1147      * Invokes getConfirmed() on the supplied base object
1148      *
1149      * @param obj The object to use for getting the confirm value
1150      *
1151      * @return A string containing the confirm value
1152      */
1153     public static String getUserConfirmed(Persistent obj)
1154     {
1155         String confirm = null;
1156 
1157         if (obj == null)
1158         {
1159             return null;
1160         }
1161 
1162         try
1163         {
1164             confirm = (String) confirmPropDesc
1165                 .getReadMethod()
1166                 .invoke(obj, new Object[] {});
1167         }
1168         catch (ClassCastException cce)
1169         {
1170             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1171             log.error(msg);
1172             throw new RuntimeException(msg);
1173         }
1174         catch (Exception e)
1175         {
1176             log.error(e, e);
1177         }
1178         return confirm;
1179     }
1180 
1181     /***
1182      * Invokes setCreateDate(java.util.Date date) on the supplied base object
1183      *
1184      * @param obj The object to use for setting the create date
1185      * @param createDate The create date to set
1186      */
1187     public static void setUserCreateDate(Persistent obj, java.util.Date createDate)
1188     {
1189         if (obj == null)
1190         {
1191             return;
1192         }
1193 
1194         try
1195         {
1196             Object[] params = new Object[] { createDate };
1197             createDatePropDesc.getWriteMethod().invoke(obj, params);
1198         }
1199         catch (ClassCastException cce)
1200         {
1201             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1202             log.error(msg);
1203             throw new RuntimeException(msg);
1204         }
1205         catch (Exception e)
1206         {
1207             log.error(e, e);
1208         }
1209     }
1210 
1211     /***
1212      * Invokes getCreateDate() on the supplied base object
1213      *
1214      * @param obj The object to use for getting the create date
1215      *
1216      * @return A string containing the create date
1217      */
1218     public static java.util.Date getUserCreateDate(Persistent obj)
1219     {
1220         java.util.Date createDate = null;
1221 
1222         if (obj == null)
1223         {
1224             return null;
1225         }
1226 
1227         try
1228         {
1229             createDate = (java.util.Date) createDatePropDesc
1230                 .getReadMethod()
1231                 .invoke(obj, new Object[] {});
1232         }
1233         catch (ClassCastException cce)
1234         {
1235             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1236             log.error(msg);
1237             throw new RuntimeException(msg);
1238         }
1239         catch (Exception e)
1240         {
1241             log.error(e, e);
1242         }
1243         return createDate;
1244     }
1245 
1246     /***
1247      * Invokes setLastLogin(java.util.Date date) on the supplied base object
1248      *
1249      * @param obj The object to use for setting the last login daet
1250      * @param lastLogin The last login date to set
1251      */
1252     public static void setUserLastLogin(Persistent obj, java.util.Date lastLogin)
1253     {
1254         if (obj == null)
1255         {
1256             return;
1257         }
1258 
1259         try
1260         {
1261             Object[] params = new Object[] { lastLogin };
1262             lastLoginPropDesc.getWriteMethod().invoke(obj, params);
1263         }
1264         catch (ClassCastException cce)
1265         {
1266             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1267             log.error(msg);
1268             throw new RuntimeException(msg);
1269         }
1270         catch (Exception e)
1271         {
1272             log.error(e, e);
1273         }
1274     }
1275 
1276     /***
1277      * Invokes getLastLogin() on the supplied base object
1278      *
1279      * @param obj The object to use for getting the last login date
1280      *
1281      * @return A string containing the last login date
1282      */
1283     public static java.util.Date getUserLastLogin(Persistent obj)
1284     {
1285         java.util.Date lastLogin = null;
1286 
1287         if (obj == null)
1288         {
1289             return null;
1290         }
1291 
1292         try
1293         {
1294             lastLogin = (java.util.Date) lastLoginPropDesc
1295                 .getReadMethod()
1296                 .invoke(obj, new Object[] {});
1297         }
1298         catch (ClassCastException cce)
1299         {
1300             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1301             log.error(msg);
1302             throw new RuntimeException(msg);
1303         }
1304         catch (Exception e)
1305         {
1306             log.error(e, e);
1307         }
1308         return lastLogin;
1309     }
1310 
1311     /***
1312      * Invokes setObjectdata(byte [] date) on the supplied base object
1313      *
1314      * @param obj The object to use for setting the last login daet
1315      * @param objectdata The objectdata to use
1316      */
1317     public static void setUserObjectdata(Persistent obj, byte [] objectdata)
1318     {
1319         if (obj == null)
1320         {
1321             return;
1322         }
1323 
1324         try
1325         {
1326             Object[] params = new Object[] { objectdata };
1327             objectdataPropDesc.getWriteMethod().invoke(obj, params);
1328         }
1329         catch (ClassCastException cce)
1330         {
1331             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1332             log.error(msg);
1333             throw new RuntimeException(msg);
1334         }
1335         catch (Exception e)
1336         {
1337             log.error(e, e);
1338         }
1339     }
1340 
1341     /***
1342      * Invokes getObjectdata() on the supplied base object
1343      *
1344      * @param obj The object to use for getting the last login date
1345      *
1346      * @return A string containing the last login date
1347      */
1348     public static byte [] getUserObjectdata(Persistent obj)
1349     {
1350         byte [] objectdata = null;
1351 
1352         if (obj == null)
1353         {
1354             return null;
1355         }
1356 
1357         try
1358         {
1359             objectdata = (byte []) objectdataPropDesc
1360                 .getReadMethod()
1361                 .invoke(obj, new Object[] {});
1362         }
1363         catch (ClassCastException cce)
1364         {
1365             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1366             log.error(msg);
1367             throw new RuntimeException(msg);
1368         }
1369         catch (Exception e)
1370         {
1371             log.error(e, e);
1372         }
1373         return objectdata;
1374     }
1375 
1376     /***
1377      * Invokes setId(int n) on the supplied base object
1378      *
1379      * @param obj The object to use for setting the name
1380      * @param id The new Id
1381      */
1382     public static void setId(Persistent obj, int id)
1383     {
1384         if (obj == null)
1385         {
1386             return;
1387         }
1388 
1389         try
1390         {
1391             Object[] params = new Object[] { Integer.TYPE };
1392             idPropDesc.getWriteMethod().invoke(obj, params);
1393         }
1394         catch (ClassCastException cce)
1395         {
1396             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1397             log.error(msg);
1398             throw new RuntimeException(msg);
1399         }
1400         catch (Exception e)
1401         {
1402             log.error(e, e);
1403         }
1404     }
1405 
1406     /***
1407      * Invokes getId() on the supplied base object
1408      *
1409      * @param obj The object to use for getting the id
1410      *
1411      * @return The Id of this object
1412      */
1413     public static Integer getIdAsObj(Persistent obj)
1414     {
1415         Integer id = null;
1416 
1417         if (obj == null)
1418         {
1419             return new Integer(0);
1420         }
1421 
1422         try
1423         {
1424             id = (Integer) idPropDesc
1425                 .getReadMethod()
1426                 .invoke(obj, new Object[] {});
1427         }
1428         catch (ClassCastException cce)
1429         {
1430             String msg = obj.getClass().getName() + " does not seem to be an User Object!";
1431             log.error(msg);
1432             throw new RuntimeException(msg);
1433         }
1434         catch (Exception e)
1435         {
1436             log.error(e, e);
1437         }
1438         return id;
1439     }
1440 
1441     /***
1442      * Returns the Class of the configured Object class
1443      * from the peer
1444      *
1445      * @return The class of the objects returned by the configured peer
1446      *
1447      */
1448 
1449     private static Class getPersistenceClass()
1450     {
1451         Class persistenceClass = null;
1452 
1453         try
1454         {
1455             Object[] params = new Object[0];
1456 
1457             persistenceClass =  (Class) userPeerClass
1458                 .getMethod("getOMClass", null)
1459                 .invoke(null, params);
1460         }
1461         catch (Exception e)
1462         {
1463             persistenceClass = null;
1464         }
1465 
1466         return persistenceClass;
1467     }
1468 
1469     /***
1470      * Returns a new, configured User Object with
1471      * a supplied Persistent object at its core
1472      *
1473      * @param p The persistent object
1474      *
1475      * @return a new, configured User Object
1476      *
1477      * @exception Exception Could not create a new Object
1478      *
1479      */
1480 
1481     public static User getNewUser(Persistent p)
1482     {
1483         User u = null;
1484         try
1485         {
1486             Class userWrapperClass = TurbineSecurity.getUserClass();
1487 
1488             Class [] clazz = new Class [] { Persistent.class };
1489             Object [] params = new Object [] { p };
1490 
1491             u = (User) userWrapperClass
1492                 .getConstructor(clazz)
1493                 .newInstance(params);
1494         }
1495         catch (Exception e)
1496         {
1497             log.error("Could not instantiate a new user from supplied persistent: ", e);
1498         }
1499 
1500         return u;
1501     }
1502 }
1503 
1504