KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > security > torque > UserPeerManager


1 package org.apache.turbine.services.security.torque;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
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,v 1.5.2.2 2004/05/20 03:06:50 seade Exp $
47  */

48
49 public class UserPeerManager
50     implements UserPeerManagerConstants
51 {
52     /** The class of the Peer the TorqueSecurityService uses */
53     private static Class JavaDoc userPeerClass = null;
54
55     /** The class name of the objects returned by the configured peer. */
56     private static Class JavaDoc userObject = null;
57
58     /** The name of the Table used for Group Object queries */
59     private static String JavaDoc tableName = null;
60
61     /** The name of the column used as "Name" Column */
62     private static String JavaDoc nameColumn = null;
63
64     /** The name of the column used as "Id" Column */
65     private static String JavaDoc idColumn = null;
66
67     /** The name of the column used as "Password" Column */
68     private static String JavaDoc passwordColumn = null;
69
70     /** The name of the column used as "First name" Column */
71     private static String JavaDoc firstNameColumn = null;
72
73     /** The name of the column used as "Last name" Column */
74     private static String JavaDoc lastNameColumn = null;
75
76     /** The name of the column used as "Email" Column */
77     private static String JavaDoc emailColumn = null;
78
79     /** The name of the column used as "Confirm" Column */
80     private static String JavaDoc confirmColumn = null;
81
82     /** The name of the column used as "create date" Column */
83     private static String JavaDoc createDateColumn = null;
84
85     /** The name of the column used as "last login" Column */
86     private static String JavaDoc lastLoginColumn = null;
87
88     /** The name of the column used as "objectdata" Column */
89     private static String JavaDoc objectdataColumn = null;
90
91     /** The "Name" property descriptor */
92     private static PropertyDescriptor JavaDoc namePropDesc = null;
93
94     /** The "Id" property descriptor */
95     private static PropertyDescriptor JavaDoc idPropDesc = null;
96
97     /** The "Password" property descriptor */
98     private static PropertyDescriptor JavaDoc passwordPropDesc = null;
99
100     /** The "First name" property descriptor */
101     private static PropertyDescriptor JavaDoc firstNamePropDesc = null;
102
103     /** The "Last name" property descriptor */
104     private static PropertyDescriptor JavaDoc lastNamePropDesc = null;
105
106     /** The "Email" property descriptor */
107     private static PropertyDescriptor JavaDoc emailPropDesc = null;
108
109     /** The "Confirm" property descriptor */
110     private static PropertyDescriptor JavaDoc confirmPropDesc = null;
111
112     /** The "create date" property descriptor */
113     private static PropertyDescriptor JavaDoc createDatePropDesc = null;
114
115     /** The "last login" property descriptor */
116     private static PropertyDescriptor JavaDoc lastLoginPropDesc = null;
117
118     /** The "objectdata" property descriptor */
119     private static PropertyDescriptor JavaDoc objectdataPropDesc = null;
120
121     /** Logging */
122     static Log log = LogFactory.getLog(UserPeerManager.class);
123
124     /**
125      * Initializes the UserPeerManager, loading the class object for the
126      * Peer used to retrieve User objects
127      *
128      * @param conf The configuration object used to configure the Manager
129      *
130      * @exception InitializationException A problem occured during
131      * initialization
132      */

133
134     public static void init(Configuration conf)
135         throws InitializationException
136     {
137         String JavaDoc userPeerClassName = conf.getString(USER_PEER_CLASS_KEY,
138                                                   USER_PEER_CLASS_DEFAULT);
139         String JavaDoc userObjectName = null;
140
141         try
142         {
143             userPeerClass = Class.forName(userPeerClassName);
144
145             tableName =
146               (String JavaDoc) userPeerClass.getField("TABLE_NAME").get(null);
147
148             //
149
// We have either an user configured Object class or we use the
150
// default as supplied by the Peer class
151
//
152

153             // Default from Peer, can be overridden
154

155             userObject = getPersistenceClass();
156
157             userObjectName = conf.getString(USER_CLASS_KEY,
158                     userObject.getName());
159
160             // Maybe the user set a new value...
161
userObject = Class.forName(userObjectName);
162
163             /* If any of the following Field queries fails, the user
164              * subsystem is unusable. So check this right here at init time,
165              * which saves us much time and hassle if it fails...
166              */

167
168             nameColumn = (String JavaDoc) userPeerClass.getField(
169                 conf.getString(USER_NAME_COLUMN_KEY,
170                                USER_NAME_COLUMN_DEFAULT)
171                 ).get(null);
172
173             idColumn = (String JavaDoc) userPeerClass.getField(
174                 conf.getString(USER_ID_COLUMN_KEY,
175                                USER_ID_COLUMN_DEFAULT)
176                 ).get(null);
177
178             passwordColumn = (String JavaDoc) userPeerClass.getField(
179                 conf.getString(USER_PASSWORD_COLUMN_KEY,
180                                USER_PASSWORD_COLUMN_DEFAULT)
181                 ).get(null);
182
183             firstNameColumn = (String JavaDoc) userPeerClass.getField(
184                 conf.getString(USER_FIRST_NAME_COLUMN_KEY,
185                                USER_FIRST_NAME_COLUMN_DEFAULT)
186                 ).get(null);
187
188             lastNameColumn = (String JavaDoc) userPeerClass.getField(
189                 conf.getString(USER_LAST_NAME_COLUMN_KEY,
190                                USER_LAST_NAME_COLUMN_DEFAULT)
191                 ).get(null);
192
193             emailColumn = (String JavaDoc) userPeerClass.getField(
194                 conf.getString(USER_EMAIL_COLUMN_KEY,
195                                USER_EMAIL_COLUMN_DEFAULT)
196                 ).get(null);
197
198             confirmColumn = (String JavaDoc) userPeerClass.getField(
199                 conf.getString(USER_CONFIRM_COLUMN_KEY,
200                                USER_CONFIRM_COLUMN_DEFAULT)
201                 ).get(null);
202
203             createDateColumn = (String JavaDoc) userPeerClass.getField(
204                 conf.getString(USER_CREATE_COLUMN_KEY,
205                                USER_CREATE_COLUMN_DEFAULT)
206                 ).get(null);
207
208             lastLoginColumn = (String JavaDoc) userPeerClass.getField(
209                 conf.getString(USER_LAST_LOGIN_COLUMN_KEY,
210                                USER_LAST_LOGIN_COLUMN_DEFAULT)
211                 ).get(null);
212
213             objectdataColumn = (String JavaDoc) userPeerClass.getField(
214                 conf.getString(USER_OBJECTDATA_COLUMN_KEY,
215                                USER_OBJECTDATA_COLUMN_DEFAULT)
216                 ).get(null);
217
218             namePropDesc =
219                 new PropertyDescriptor JavaDoc(conf.getString(
220                                            USER_NAME_PROPERTY_KEY,
221                                            USER_NAME_PROPERTY_DEFAULT),
222                                        userObject);
223
224             idPropDesc =
225                 new PropertyDescriptor JavaDoc(conf.getString(
226                                            USER_ID_PROPERTY_KEY,
227                                            USER_ID_PROPERTY_DEFAULT),
228                                        userObject);
229
230             passwordPropDesc =
231                 new PropertyDescriptor JavaDoc(conf.getString(
232                                            USER_PASSWORD_PROPERTY_KEY,
233                                            USER_PASSWORD_PROPERTY_DEFAULT),
234                                        userObject);
235
236             firstNamePropDesc =
237                 new PropertyDescriptor JavaDoc(conf.getString(
238                                            USER_FIRST_NAME_PROPERTY_KEY,
239                                            USER_FIRST_NAME_PROPERTY_DEFAULT),
240                                        userObject);
241
242             lastNamePropDesc =
243                 new PropertyDescriptor JavaDoc(conf.getString(
244                                            USER_LAST_NAME_PROPERTY_KEY,
245                                            USER_LAST_NAME_PROPERTY_DEFAULT),
246                                        userObject);
247
248             emailPropDesc =
249                 new PropertyDescriptor JavaDoc(conf.getString(
250                                            USER_EMAIL_PROPERTY_KEY,
251                                            USER_EMAIL_PROPERTY_DEFAULT),
252                                        userObject);
253
254             confirmPropDesc =
255                 new PropertyDescriptor JavaDoc(conf.getString(
256                                            USER_CONFIRM_PROPERTY_KEY,
257                                            USER_CONFIRM_PROPERTY_DEFAULT),
258                                        userObject);
259
260             createDatePropDesc =
261                 new PropertyDescriptor JavaDoc(conf.getString(
262                                            USER_CREATE_PROPERTY_KEY,
263                                            USER_CREATE_PROPERTY_DEFAULT),
264                                        userObject);
265
266             lastLoginPropDesc =
267                 new PropertyDescriptor JavaDoc(conf.getString(
268                                            USER_LAST_LOGIN_PROPERTY_KEY,
269                                            USER_LAST_LOGIN_PROPERTY_DEFAULT),
270                                        userObject);
271
272             objectdataPropDesc =
273                 new PropertyDescriptor JavaDoc(conf.getString(
274                                            USER_OBJECTDATA_PROPERTY_KEY,
275                                            USER_OBJECTDATA_PROPERTY_DEFAULT),
276                                        userObject);
277         }
278         catch (Exception JavaDoc e)
279         {
280             if (userPeerClassName == null || userPeerClass == null)
281             {
282                 throw new InitializationException(
283                     "Could not find UserPeer class ("
284                     + userPeerClassName + ")", e);
285             }
286             if (tableName == null)
287             {
288                 throw new InitializationException(
289                     "Failed to get the table name from the Peer object", e);
290             }
291
292             if (userObject == null || userObjectName == null)
293             {
294                 throw new InitializationException(
295                     "Failed to get the object type from the Peer object", e);
296             }
297
298
299             if (nameColumn == null || namePropDesc == null)
300             {
301                 throw new InitializationException(
302                     "UserPeer " + userPeerClassName
303                     + " has no name column information!", e);
304             }
305             if (idColumn == null || idPropDesc == null)
306             {
307                 throw new InitializationException(
308                     "UserPeer " + userPeerClassName
309                     + " has no id column information!", e);
310             }
311             if (passwordColumn == null || passwordPropDesc == null)
312             {
313                 throw new InitializationException(
314                     "UserPeer " + userPeerClassName
315                     + " has no password column information!", e);
316             }
317             if (firstNameColumn == null || firstNamePropDesc == null)
318             {
319                 throw new InitializationException(
320                     "UserPeer " + userPeerClassName
321                     + " has no firstName column information!", e);
322             }
323             if (lastNameColumn == null || lastNamePropDesc == null)
324             {
325                 throw new InitializationException(
326                     "UserPeer " + userPeerClassName
327                     + " has no lastName column information!", e);
328             }
329             if (emailColumn == null || emailPropDesc == null)
330             {
331                 throw new InitializationException(
332                     "UserPeer " + userPeerClassName
333                     + " has no email column information!", e);
334             }
335             if (confirmColumn == null || confirmPropDesc == null)
336             {
337                 throw new InitializationException(
338                     "UserPeer " + userPeerClassName
339                     + " has no confirm column information!", e);
340             }
341             if (createDateColumn == null || createDatePropDesc == null)
342             {
343                 throw new InitializationException(
344                     "UserPeer " + userPeerClassName
345                     + " has no createDate column information!", e);
346             }
347             if (lastLoginColumn == null || lastLoginPropDesc == null)
348             {
349                 throw new InitializationException(
350                     "UserPeer " + userPeerClassName
351                     + " has no lastLogin column information!", e);
352             }
353             if (objectdataColumn == null || objectdataPropDesc == null)
354             {
355                 throw new InitializationException(
356                     "UserPeer " + userPeerClassName
357                     + " has no objectdata column information!", e);
358             }
359         }
360     }
361
362     /**
363      * Get the name of this table.
364      *
365      * @return A String with the name of the table.
366      */

367     public static String JavaDoc getTableName()
368     {
369         return tableName;
370     }
371
372     /**
373      * Returns the fully qualified name of the Column to
374      * use as the Name Column for a group
375      *
376      * @return A String containing the column name
377      */

378     public static String JavaDoc getNameColumn()
379     {
380         return nameColumn;
381     }
382
383     /**
384      * Returns the fully qualified name of the Column to
385      * use as the Id Column for a group
386      *
387      * @return A String containing the column id
388      */

389     public static String JavaDoc getIdColumn()
390     {
391         return idColumn;
392     }
393
394     /**
395      * Returns the fully qualified name of the Column to
396      * use as the Password Column for a role
397      *
398      * @return A String containing the column name
399      */

400     public static String JavaDoc getPasswordColumn()
401     {
402         return passwordColumn;
403     }
404
405     /**
406      * Returns the fully qualified name of the Column to
407      * use as the FirstName Column for a role
408      *
409      * @return A String containing the column name
410      */

411     public static String JavaDoc getFirstNameColumn()
412     {
413         return firstNameColumn;
414     }
415
416     /**
417      * Returns the fully qualified name of the Column to
418      * use as the LastName Column for a role
419      *
420      * @return A String containing the column name
421      */

422     public static String JavaDoc getLastNameColumn()
423     {
424         return lastNameColumn;
425     }
426
427     /**
428      * Returns the fully qualified name of the Column to
429      * use as the Email Column for a role
430      *
431      * @return A String containing the column name
432      */

433     public static String JavaDoc getEmailColumn()
434     {
435         return emailColumn;
436     }
437
438     /**
439      * Returns the fully qualified name of the Column to
440      * use as the Confirm Column for a role
441      *
442      * @return A String containing the column name
443      */

444     public static String JavaDoc getConfirmColumn()
445     {
446         return confirmColumn;
447     }
448
449     /**
450      * Returns the fully qualified name of the Column to
451      * use as the CreateDate Column for a role
452      *
453      * @return A String containing the column name
454      */

455     public static String JavaDoc getCreateDateColumn()
456     {
457         return createDateColumn;
458     }
459
460     /**
461      * Returns the fully qualified name of the Column to
462      * use as the LastLogin Column for a role
463      *
464      * @return A String containing the column name
465      */

466     public static String JavaDoc getLastLoginColumn()
467     {
468         return lastLoginColumn;
469     }
470
471     /**
472      * Returns the fully qualified name of the Column to
473      * use as the objectdata Column for a role
474      *
475      * @return A String containing the column name
476      */

477     public static String JavaDoc getObjectdataColumn()
478     {
479         return objectdataColumn;
480     }
481
482     /**
483      * Returns the full name of a column.
484      *
485      * @param name The column to fully qualify
486      *
487      * @return A String with the full name of the column.
488      */

489     public static String JavaDoc getColumnName(String JavaDoc name)
490     {
491         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
492         sb.append(getTableName());
493         sb.append(".");
494         sb.append(name);
495         return sb.toString();
496     }
497
498     /**
499      * Returns the full name of a column.
500      *
501      * @param name The column to fully qualify
502      *
503      * @return A String with the full name of the column.
504      * @deprecated use getColumnName(String name)
505      */

506     public String JavaDoc getFullColumnName(String JavaDoc name)
507     {
508         return getColumnName(name);
509     }
510
511
512     /**
513      * Returns a new, empty object for the underlying peer.
514      * Used to create a new underlying object
515      *
516      * @return A new object which is compatible to the Peer
517      * and can be used as a User object
518      *
519      */

520
521     public static Persistent newPersistentInstance()
522     {
523         Persistent obj = null;
524
525         if(userObject == null)
526         {
527             // This can happen if the Turbine wants to determine the
528
// name of the anonymous user before the security service
529
// has been initialized. In this case, the Peer Manager
530
// has not yet been inited and the userObject is still
531
// null. Return null in this case.
532
//
533
return obj;
534         }
535
536         try
537         {
538             obj = (Persistent) userObject.newInstance();
539         }
540         catch (Exception JavaDoc e)
541         {
542             log.error("Could not instantiate a user object", e);
543             obj = null;
544         }
545         return obj;
546     }
547
548     /**
549      * Checks if a User is defined in the system. The name
550      * is used as query criteria.
551      *
552      * @param user The User to be checked.
553      * @return <code>true</code> if given User exists in the system.
554      * @throws DataBackendException when more than one User with
555      * the same name exists.
556      * @throws Exception A generic exception.
557      */

558     public static boolean checkExists(User user)
559         throws DataBackendException, Exception JavaDoc
560     {
561         Criteria criteria = new Criteria();
562
563         criteria.addSelectColumn(getIdColumn());
564
565         criteria.add(getNameColumn(), user.getName());
566
567         List JavaDoc results = BasePeer.doSelect(criteria);
568
569         if (results.size() > 1)
570         {
571             throw new DataBackendException("Multiple users named '" +
572                                            user.getName() + "' exist!");
573         }
574         return (results.size() == 1);
575     }
576
577     /**
578      * Returns a List of all User objects.
579      *
580      * @return A List with all users in the system.
581      * @exception Exception A generic exception.
582      */

583     public static List JavaDoc selectAllUsers()
584         throws Exception JavaDoc
585     {
586         Criteria criteria = new Criteria();
587         criteria.addAscendingOrderByColumn(getLastNameColumn());
588         criteria.addAscendingOrderByColumn(getFirstNameColumn());
589         criteria.setIgnoreCase(true);
590         return doSelect(criteria);
591     }
592
593     /**
594      * Returns a List of all confirmed User objects.
595      *
596      * @return A List with all confirmed users in the system.
597      * @exception Exception A generic exception.
598      */

599     public static List JavaDoc selectAllConfirmedUsers()
600         throws Exception JavaDoc
601     {
602         Criteria criteria = new Criteria();
603
604         criteria.add (getConfirmColumn(), User.CONFIRM_DATA);
605         criteria.addAscendingOrderByColumn(getLastNameColumn());
606         criteria.addAscendingOrderByColumn(getFirstNameColumn());
607         criteria.setIgnoreCase(true);
608         return doSelect(criteria);
609     }
610
611     /*
612      * ========================================================================
613      *
614      * WARNING! Do not read on if you have a weak stomach. What follows here
615      * are some abominations thanks to the braindead static peers of Torque
616      * and the rigidity of Java....
617      *
618      * ========================================================================
619      *
620      */

621
622     /**
623      * Calls buildCriteria(User user) in the configured UserPeer. If you get
624      * a ClassCastException in this routine, you put a User object into this
625      * method which can't be cast into an object for the TorqueSecurityService. This is a
626      * configuration error most of the time.
627      *
628      * @param user An object which implements the User interface
629      *
630      * @return A criteria for the supplied user object
631      */

632
633     public static Criteria buildCriteria(User user)
634     {
635         Criteria crit;
636
637         try
638         {
639             Class JavaDoc[] clazz = new Class JavaDoc[] { userObject };
640             Object JavaDoc[] params =
641                 new Object JavaDoc[] { ((TorqueUser) user).getPersistentObj() };
642
643             crit = (Criteria) userPeerClass
644                 .getMethod("buildCriteria", clazz)
645                 .invoke(null, params);
646         }
647         catch (Exception JavaDoc e)
648         {
649             crit = null;
650         }
651
652         return crit;
653     }
654
655     /**
656      * Invokes doUpdate(Criteria c) on the configured Peer Object
657      *
658      * @param criteria A Criteria Object
659      *
660      * @exception TorqueException A problem occured.
661      */

662
663     public static void doUpdate(Criteria criteria)
664         throws TorqueException
665     {
666         try
667         {
668             Class JavaDoc[] clazz = new Class JavaDoc[] { Criteria.class };
669             Object JavaDoc[] params = new Object JavaDoc[] { criteria };
670
671             userPeerClass
672                 .getMethod("doUpdate", clazz)
673                 .invoke(null, params);
674         }
675         catch (Exception JavaDoc e)
676         {
677             throw new TorqueException("doUpdate failed", e);
678         }
679     }
680
681     /**
682      * Invokes doInsert(Criteria c) on the configured Peer Object
683      *
684      * @param criteria A Criteria Object
685      *
686      * @exception TorqueException A problem occured.
687      */

688
689     public static void doInsert(Criteria criteria)
690         throws TorqueException
691     {
692         try
693         {
694             Class JavaDoc[] clazz = new Class JavaDoc[] { Criteria.class };
695             Object JavaDoc[] params = new Object JavaDoc[] { criteria };
696
697             userPeerClass
698                 .getMethod("doInsert", clazz)
699                 .invoke(null, params);
700         }
701         catch (Exception JavaDoc e)
702         {
703             throw new TorqueException("doInsert failed", e);
704         }
705     }
706
707     /**
708      * Invokes doSelect(Criteria c) on the configured Peer Object
709      *
710      * @param criteria A Criteria Object
711      *
712      * @return A List of User Objects selected by the Criteria
713      *
714      * @exception TorqueException A problem occured.
715      */

716     public static List JavaDoc doSelect(Criteria criteria)
717         throws TorqueException
718     {
719         List JavaDoc list;
720
721         try
722         {
723             Class JavaDoc[] clazz =
724                 new Class JavaDoc[] { Criteria.class };
725             Object JavaDoc[] params = new Object JavaDoc[] { criteria };
726
727             list = (List JavaDoc) userPeerClass
728                 .getMethod("doSelect", clazz)
729                 .invoke(null, params);
730         }
731         catch (Exception JavaDoc e)
732         {
733             throw new TorqueException("doSelect failed", e);
734         }
735         List JavaDoc newList = new ArrayList JavaDoc(list.size());
736
737         //
738
// Wrap the returned Objects into TorqueUsers.
739
//
740
for (Iterator JavaDoc it = list.iterator(); it.hasNext(); )
741         {
742             User u = getNewUser((Persistent) it.next());
743             newList.add(u);
744         }
745
746         return newList;
747     }
748
749     /**
750      * Invokes doDelete(Criteria c) on the configured Peer Object
751      *
752      * @param criteria A Criteria Object
753      *
754      * @exception TorqueException A problem occured.
755      */

756     public static void doDelete(Criteria criteria)
757         throws TorqueException
758     {
759         try
760         {
761             Class JavaDoc[] clazz = new Class JavaDoc[] { Criteria.class };
762             Object JavaDoc[] params = new Object JavaDoc[] { criteria };
763
764             userPeerClass
765                 .getMethod("doDelete", clazz)
766                 .invoke(null, params);
767         }
768         catch (Exception JavaDoc e)
769         {
770             throw new TorqueException("doDelete failed", e);
771         }
772     }
773
774     /**
775      * Invokes setName(String s) on the supplied base object
776      *
777      * @param obj The object to use for setting the name
778      * @param name The Name to set
779      */

780     public static void setUserName(Persistent obj, String JavaDoc name)
781     {
782         if(obj == null)
783         {
784             return;
785         }
786
787         try
788         {
789             Object JavaDoc[] params = new Object JavaDoc[] { name };
790             namePropDesc.getWriteMethod().invoke(obj, params);
791         }
792         catch (ClassCastException JavaDoc cce)
793         {
794             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
795             log.error(msg);
796             throw new RuntimeException JavaDoc(msg);
797         }
798         catch (Exception JavaDoc e)
799         {
800             log.error(e, e);
801         }
802     }
803
804     /**
805      * Invokes getName() on the supplied base object
806      *
807      * @param obj The object to use for getting the name
808      *
809      * @return A string containing the name
810      *
811      * @deprecated use getName(obj)
812      */

813     public static String JavaDoc getUserName(Persistent obj)
814     {
815         return getName(obj);
816     }
817
818     /**
819      * Invokes getName() on the supplied base object
820      *
821      * @param obj The object to use for getting the name
822      *
823      * @return A string containing the name
824      */

825     public static String JavaDoc getName(Persistent obj)
826     {
827         String JavaDoc name = null;
828
829         if(obj == null)
830         {
831             return null;
832         }
833
834         try
835         {
836             name = (String JavaDoc) namePropDesc
837                 .getReadMethod()
838                 .invoke(obj, new Object JavaDoc[] {});
839         }
840         catch (ClassCastException JavaDoc cce)
841         {
842             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
843             log.error(msg);
844             throw new RuntimeException JavaDoc(msg);
845         }
846         catch (Exception JavaDoc e)
847         {
848             log.error(e, e);
849         }
850         return name;
851     }
852
853     /**
854      * Invokes setPassword(String s) on the supplied base object
855      *
856      * @param obj The object to use for setting the password
857      * @param password The Password to set
858      */

859     public static void setUserPassword(Persistent obj, String JavaDoc password)
860     {
861         if(obj == null)
862         {
863             return;
864         }
865
866         try
867         {
868             Object JavaDoc[] params = new Object JavaDoc[] { password };
869             passwordPropDesc.getWriteMethod().invoke(obj, params);
870         }
871         catch (ClassCastException JavaDoc cce)
872         {
873             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
874             log.error(msg);
875             throw new RuntimeException JavaDoc(msg);
876         }
877         catch (Exception JavaDoc e)
878         {
879             log.error(e, e);
880         }
881     }
882
883     /**
884      * Invokes getPassword() on the supplied base object
885      *
886      * @param obj The object to use for getting the password
887      *
888      * @return A string containing the password
889      */

890     public static String JavaDoc getUserPassword(Persistent obj)
891     {
892         String JavaDoc password = null;
893
894         if(obj == null)
895         {
896             return null;
897         }
898
899         try
900         {
901             password = (String JavaDoc) passwordPropDesc
902                 .getReadMethod()
903                 .invoke(obj, new Object JavaDoc[] {});
904         }
905         catch (ClassCastException JavaDoc cce)
906         {
907             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
908             log.error(msg);
909             throw new RuntimeException JavaDoc(msg);
910         }
911         catch (Exception JavaDoc e)
912         {
913             log.error(e, e);
914         }
915         return password;
916     }
917
918     /**
919      * Invokes setFirstName(String s) on the supplied base object
920      *
921      * @param obj The object to use for setting the first name
922      * @param firstName The first name to set
923      */

924     public static void setUserFirstName(Persistent obj, String JavaDoc firstName)
925     {
926         if(obj == null)
927         {
928             return;
929         }
930
931         try
932         {
933             Object JavaDoc[] params = new Object JavaDoc[] { firstName };
934             firstNamePropDesc.getWriteMethod().invoke(obj, params);
935         }
936         catch (ClassCastException JavaDoc cce)
937         {
938             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
939             log.error(msg);
940             throw new RuntimeException JavaDoc(msg);
941         }
942         catch (Exception JavaDoc e)
943         {
944             log.error(e, e);
945         }
946     }
947
948     /**
949      * Invokes getFirstName() on the supplied base object
950      *
951      * @param obj The object to use for getting the first name
952      *
953      * @return A string containing the first name
954      */

955     public static String JavaDoc getUserFirstName(Persistent obj)
956     {
957         String JavaDoc firstName = null;
958
959         if(obj == null)
960         {
961             return null;
962         }
963
964         try
965         {
966             firstName = (String JavaDoc) firstNamePropDesc
967                 .getReadMethod()
968                 .invoke(obj, new Object JavaDoc[] {});
969         }
970         catch (ClassCastException JavaDoc cce)
971         {
972             String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
973             log.error(msg);
974             throw new RuntimeException JavaDoc(msg);
975         }
976         catch (Exception JavaDoc e)
977         {
978             log.error(e, e);
979         }
980         return firstName;
981     }
982
983     /**
984      * Invokes setLastName(String s) on the supplied base object
985      *
986      * @param obj The object to use for setting the last name
987      * @param lastName The Last Name to set
988      */

989     public static void setUserLastName(Persistent obj, String JavaDoc lastName)
990     {
991         if(obj == null)
992         {
993             return;
994         }
995
996         try
997         {
998             Object JavaDoc[] params = new Object JavaDoc[] { lastName };
999             lastNamePropDesc.getWriteMethod().invoke(obj, params);
1000        }
1001        catch (ClassCastException JavaDoc cce)
1002        {
1003            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1004            log.error(msg);
1005            throw new RuntimeException JavaDoc(msg);
1006        }
1007        catch (Exception JavaDoc e)
1008        {
1009            log.error(e, e);
1010        }
1011    }
1012
1013    /**
1014     * Invokes getLastName() on the supplied base object
1015     *
1016     * @param obj The object to use for getting the last name
1017     *
1018     * @return A string containing the last name
1019     */

1020    public static String JavaDoc getUserLastName(Persistent obj)
1021    {
1022        String JavaDoc lastName = null;
1023
1024        if(obj == null)
1025        {
1026            return null;
1027        }
1028
1029        try
1030        {
1031            lastName = (String JavaDoc) lastNamePropDesc
1032                .getReadMethod()
1033                .invoke(obj, new Object JavaDoc[] {});
1034        }
1035        catch (ClassCastException JavaDoc cce)
1036        {
1037            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1038            log.error(msg);
1039            throw new RuntimeException JavaDoc(msg);
1040        }
1041        catch (Exception JavaDoc e)
1042        {
1043            log.error(e, e);
1044        }
1045        return lastName;
1046    }
1047
1048    /**
1049     * Invokes setEmail(String s) on the supplied base object
1050     *
1051     * @param obj The object to use for setting the email
1052     * @param email The Email to set
1053     */

1054    public static void setUserEmail(Persistent obj, String JavaDoc email)
1055    {
1056        if(obj == null)
1057        {
1058            return;
1059        }
1060
1061        try
1062        {
1063            Object JavaDoc[] params = new Object JavaDoc[] { email };
1064            emailPropDesc.getWriteMethod().invoke(obj, params);
1065        }
1066        catch (ClassCastException JavaDoc cce)
1067        {
1068            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1069            log.error(msg);
1070            throw new RuntimeException JavaDoc(msg);
1071        }
1072        catch (Exception JavaDoc e)
1073        {
1074            log.error(e, e);
1075        }
1076    }
1077
1078    /**
1079     * Invokes getEmail() on the supplied base object
1080     *
1081     * @param obj The object to use for getting the email
1082     *
1083     * @return A string containing the email
1084     */

1085    public static String JavaDoc getUserEmail(Persistent obj)
1086    {
1087        String JavaDoc email = null;
1088
1089        if(obj == null)
1090        {
1091            return null;
1092        }
1093
1094        try
1095        {
1096            email = (String JavaDoc) emailPropDesc
1097                .getReadMethod()
1098                .invoke(obj, new Object JavaDoc[] {});
1099        }
1100        catch (ClassCastException JavaDoc cce)
1101        {
1102            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1103            log.error(msg);
1104            throw new RuntimeException JavaDoc(msg);
1105        }
1106        catch (Exception JavaDoc e)
1107        {
1108            log.error(e, e);
1109        }
1110        return email;
1111    }
1112
1113    /**
1114     * Invokes setConfirmed(String s) on the supplied base object
1115     *
1116     * @param obj The object to use for setting the confirm value
1117     * @param confirm The confirm value to set
1118     */

1119    public static void setUserConfirmed(Persistent obj, String JavaDoc confirm)
1120    {
1121        if(obj == null)
1122        {
1123            return;
1124        }
1125
1126        try
1127        {
1128            Object JavaDoc[] params = new Object JavaDoc[] { confirm };
1129            confirmPropDesc.getWriteMethod().invoke(obj, params);
1130        }
1131        catch (ClassCastException JavaDoc cce)
1132        {
1133            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1134            log.error(msg);
1135            throw new RuntimeException JavaDoc(msg);
1136        }
1137        catch (Exception JavaDoc e)
1138        {
1139            log.error(e, e);
1140        }
1141    }
1142
1143    /**
1144     * Invokes getConfirmed() on the supplied base object
1145     *
1146     * @param obj The object to use for getting the confirm value
1147     *
1148     * @return A string containing the confirm value
1149     */

1150    public static String JavaDoc getUserConfirmed(Persistent obj)
1151    {
1152        String JavaDoc confirm = null;
1153
1154        if(obj == null)
1155        {
1156            return null;
1157        }
1158
1159        try
1160        {
1161            confirm = (String JavaDoc) confirmPropDesc
1162                .getReadMethod()
1163                .invoke(obj, new Object JavaDoc[] {});
1164        }
1165        catch (ClassCastException JavaDoc cce)
1166        {
1167            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1168            log.error(msg);
1169            throw new RuntimeException JavaDoc(msg);
1170        }
1171        catch (Exception JavaDoc e)
1172        {
1173            log.error(e, e);
1174        }
1175        return confirm;
1176    }
1177
1178    /**
1179     * Invokes setCreateDate(java.util.Date date) on the supplied base object
1180     *
1181     * @param obj The object to use for setting the create date
1182     * @param createDate The create date to set
1183     */

1184    public static void setUserCreateDate(Persistent obj, java.util.Date JavaDoc createDate)
1185    {
1186        if(obj == null)
1187        {
1188            return;
1189        }
1190
1191        try
1192        {
1193            Object JavaDoc[] params = new Object JavaDoc[] { createDate };
1194            createDatePropDesc.getWriteMethod().invoke(obj, params);
1195        }
1196        catch (ClassCastException JavaDoc cce)
1197        {
1198            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1199            log.error(msg);
1200            throw new RuntimeException JavaDoc(msg);
1201        }
1202        catch (Exception JavaDoc e)
1203        {
1204            log.error(e, e);
1205        }
1206    }
1207
1208    /**
1209     * Invokes getCreateDate() on the supplied base object
1210     *
1211     * @param obj The object to use for getting the create date
1212     *
1213     * @return A string containing the create date
1214     */

1215    public static java.util.Date JavaDoc getUserCreateDate(Persistent obj)
1216    {
1217        java.util.Date JavaDoc createDate = null;
1218
1219        if(obj == null)
1220        {
1221            return null;
1222        }
1223
1224        try
1225        {
1226            createDate = (java.util.Date JavaDoc) createDatePropDesc
1227                .getReadMethod()
1228                .invoke(obj, new Object JavaDoc[] {});
1229        }
1230        catch (ClassCastException JavaDoc cce)
1231        {
1232            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1233            log.error(msg);
1234            throw new RuntimeException JavaDoc(msg);
1235        }
1236        catch (Exception JavaDoc e)
1237        {
1238            log.error(e, e);
1239        }
1240        return createDate;
1241    }
1242
1243    /**
1244     * Invokes setLastLogin(java.util.Date date) on the supplied base object
1245     *
1246     * @param obj The object to use for setting the last login daet
1247     * @param lastLogin The last login date to set
1248     */

1249    public static void setUserLastLogin(Persistent obj, java.util.Date JavaDoc lastLogin)
1250    {
1251        if(obj == null)
1252        {
1253            return;
1254        }
1255
1256        try
1257        {
1258            Object JavaDoc[] params = new Object JavaDoc[] { lastLogin };
1259            lastLoginPropDesc.getWriteMethod().invoke(obj, params);
1260        }
1261        catch (ClassCastException JavaDoc cce)
1262        {
1263            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1264            log.error(msg);
1265            throw new RuntimeException JavaDoc(msg);
1266        }
1267        catch (Exception JavaDoc e)
1268        {
1269            log.error(e, e);
1270        }
1271    }
1272
1273    /**
1274     * Invokes getLastLogin() on the supplied base object
1275     *
1276     * @param obj The object to use for getting the last login date
1277     *
1278     * @return A string containing the last login date
1279     */

1280    public static java.util.Date JavaDoc getUserLastLogin(Persistent obj)
1281    {
1282        java.util.Date JavaDoc lastLogin = null;
1283
1284        if(obj == null)
1285        {
1286            return null;
1287        }
1288
1289        try
1290        {
1291            lastLogin = (java.util.Date JavaDoc) lastLoginPropDesc
1292                .getReadMethod()
1293                .invoke(obj, new Object JavaDoc[] {});
1294        }
1295        catch (ClassCastException JavaDoc cce)
1296        {
1297            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1298            log.error(msg);
1299            throw new RuntimeException JavaDoc(msg);
1300        }
1301        catch (Exception JavaDoc e)
1302        {
1303            log.error(e, e);
1304        }
1305        return lastLogin;
1306    }
1307
1308    /**
1309     * Invokes setObjectdata(byte [] date) on the supplied base object
1310     *
1311     * @param obj The object to use for setting the last login daet
1312     * @param objectdata The objectdata to use
1313     */

1314    public static void setUserObjectdata(Persistent obj, byte [] objectdata)
1315    {
1316        if(obj == null)
1317        {
1318            return;
1319        }
1320
1321        try
1322        {
1323            Object JavaDoc[] params = new Object JavaDoc[] { objectdata };
1324            objectdataPropDesc.getWriteMethod().invoke(obj, params);
1325        }
1326        catch (ClassCastException JavaDoc cce)
1327        {
1328            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1329            log.error(msg);
1330            throw new RuntimeException JavaDoc(msg);
1331        }
1332        catch (Exception JavaDoc e)
1333        {
1334            log.error(e, e);
1335        }
1336    }
1337
1338    /**
1339     * Invokes getObjectdata() on the supplied base object
1340     *
1341     * @param obj The object to use for getting the last login date
1342     *
1343     * @return A string containing the last login date
1344     */

1345    public static byte [] getUserObjectdata(Persistent obj)
1346    {
1347        byte [] objectdata = null;
1348
1349        if(obj == null)
1350        {
1351            return null;
1352        }
1353
1354        try
1355        {
1356            objectdata = (byte []) objectdataPropDesc
1357                .getReadMethod()
1358                .invoke(obj, new Object JavaDoc[] {});
1359        }
1360        catch (ClassCastException JavaDoc cce)
1361        {
1362            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1363            log.error(msg);
1364            throw new RuntimeException JavaDoc(msg);
1365        }
1366        catch (Exception JavaDoc e)
1367        {
1368            log.error(e, e);
1369        }
1370        return objectdata;
1371    }
1372
1373    /**
1374     * Invokes setId(int n) on the supplied base object
1375     *
1376     * @param obj The object to use for setting the name
1377     * @param id The new Id
1378     */

1379    public static void setId(Persistent obj, int id)
1380    {
1381        if(obj == null)
1382        {
1383            return;
1384        }
1385
1386        try
1387        {
1388            Object JavaDoc[] params = new Object JavaDoc[] { Integer.TYPE };
1389            idPropDesc.getWriteMethod().invoke(obj, params);
1390        }
1391        catch (ClassCastException JavaDoc cce)
1392        {
1393            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1394            log.error(msg);
1395            throw new RuntimeException JavaDoc(msg);
1396        }
1397        catch (Exception JavaDoc e)
1398        {
1399            log.error(e, e);
1400        }
1401    }
1402
1403    /**
1404     * Invokes getId() on the supplied base object
1405     *
1406     * @param obj The object to use for getting the id
1407     *
1408     * @return The Id of this object
1409     */

1410    public static Integer JavaDoc getIdAsObj(Persistent obj)
1411    {
1412        Integer JavaDoc id = null;
1413
1414        if(obj == null)
1415        {
1416            return new Integer JavaDoc(0);
1417        }
1418
1419        try
1420        {
1421            id = (Integer JavaDoc) idPropDesc
1422                .getReadMethod()
1423                .invoke(obj, new Object JavaDoc[] {});
1424        }
1425        catch (ClassCastException JavaDoc cce)
1426        {
1427            String JavaDoc msg = obj.getClass().getName() + " does not seem to be an User Object!";
1428            log.error(msg);
1429            throw new RuntimeException JavaDoc(msg);
1430        }
1431        catch (Exception JavaDoc e)
1432        {
1433            log.error(e, e);
1434        }
1435        return id;
1436    }
1437
1438    /**
1439     * Returns the Class of the configured Object class
1440     * from the peer
1441     *
1442     * @return The class of the objects returned by the configured peer
1443     *
1444     */

1445
1446    private static Class JavaDoc getPersistenceClass()
1447    {
1448        Class JavaDoc persistenceClass = null;
1449
1450        try
1451        {
1452            Object JavaDoc[] params = new Object JavaDoc[0];
1453
1454            persistenceClass = (Class JavaDoc) userPeerClass
1455                .getMethod("getOMClass", null)
1456                .invoke(null, params);
1457        }
1458        catch (Exception JavaDoc e)
1459        {
1460            persistenceClass = null;
1461        }
1462
1463        return persistenceClass;
1464    }
1465
1466    /**
1467     * Returns a new, configured User Object with
1468     * a supplied Persistent object at its core
1469     *
1470     * @param p The persistent object
1471     *
1472     * @return a new, configured User Object
1473     *
1474     * @exception Exception Could not create a new Object
1475     *
1476     */

1477
1478    public static User getNewUser(Persistent p)
1479    {
1480        User u = null;
1481        try
1482        {
1483            Class JavaDoc userWrapperClass = TurbineSecurity.getUserClass();
1484
1485            Class JavaDoc [] clazz = new Class JavaDoc [] { Persistent.class };
1486            Object JavaDoc [] params = new Object JavaDoc [] { p };
1487
1488            u = (User) userWrapperClass
1489                .getConstructor(clazz)
1490                .newInstance(params);
1491        }
1492        catch (Exception JavaDoc e)
1493        {
1494            log.error("Could not instantiate a new user from supplied persistent: ", e);
1495        }
1496
1497        return u;
1498    }
1499}
1500
1501
1502
Popular Tags