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