KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > JahiaUserManagerRoutingService


1 package org.jahia.services.usermanager;
2
3 import org.jahia.data.JahiaDOMObject;
4 import org.jahia.exceptions.JahiaException;
5 import org.jahia.exceptions.JahiaInitializationException;
6 import org.jahia.settings.SettingsBean;
7 import org.jahia.utils.InsertionSortedMap;
8 import org.w3c.dom.*;
9
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.lang.reflect.Method JavaDoc;
16 import java.util.*;
17
18 /**
19  * <p>Title: Manages routing of user management processes to the corresponding
20  * provider.</p>
21  * <p>Description: This service is the heart of the "routing" process of the
22  * user manager. It is also a configurable system where regexps are using to
23  * define the routing process. These regexps are taken in a specific order, as
24  * to give priority to one provider or another, such as : </p>
25  * <p>Copyright: Copyright (c) 2002</p>
26  * <p>Company: Jahia Ltd</p>
27  *
28  * @author Serge Huber
29  * @version 3.0
30  */

31
32 public class JahiaUserManagerRoutingService extends JahiaUserManagerService {
33
34     private static org.apache.log4j.Logger logger =
35             org.apache.log4j.Logger.getLogger (JahiaUserManagerRoutingService.class);
36
37     private static final int ROUTE_CALL_ONE = 0;
38     private static final int ROUTE_CALL_ALL = 1;
39     private static final int ROUTE_CALL_ALL_UNTIL_SUCCESS = 2;
40
41     /**
42      * @todo FIXME we had to hardcode this reference, so when we change the
43      * name or location of the JahiaUser class we must update this too :(
44      */

45     private static final String JavaDoc JAHIAUSER_INTERFACE_NAME = "org.jahia.services.usermanager.JahiaUser";
46     private static final String JavaDoc XML_ROOT_TAG = "router-config";
47
48     private static final String JavaDoc XML_PROVIDER_ROOT_TAG = "provider";
49     private static final String JavaDoc XML_PROVIDER_KEY_TAG = "key";
50     private static final String JavaDoc XML_PROVIDER_CLASS_TAG = "class";
51     private static final String JavaDoc XML_PROVIDER_TITLE_TAG = "title";
52     private static final String JavaDoc XML_PROVIDER_DESCRIPTION_TAG = "description";
53     private static final String JavaDoc XML_PROVIDER_ISDEFAULT_TAG = "is-default";
54     private static final String JavaDoc XML_PROVIDER_ISREADONLY_TAG = "is-readonly";
55     private static final String JavaDoc XML_PROVIDER_PRIORITY_TAG = "priority";
56
57     private static final String JavaDoc XML_CRITERIA_ROOT_TAG = "criteria";
58     private static final String JavaDoc XML_CRITERIA_KEY_TAG = "key";
59     private static final String JavaDoc XML_CRITERIA_DESCRIPTION_TAG = "description";
60     private static final String JavaDoc XML_CRITERIA_CONDITION_TAG = "condition";
61     private static final String JavaDoc XML_CRITERIA_DESTINATION_TAG = "provider-destination";
62     private static final String JavaDoc XML_CRITERIA_CONDITION_PROPNAME_ATTR = "propertyName";
63     private static final String JavaDoc XML_CRITERIA_CONDITION_PROPVALUE_ATTR = "propertyValue";
64
65     /**
66      * EP: 2004/28/06
67      * Static constants to retrieve definitions of servers in the xml files
68      */

69     private static final String JavaDoc XML_SERVER_ROOT_TAG = "server";
70     private static final String JavaDoc XML_SERVER_URL_TAG = "url";
71     private static final String JavaDoc XML_SERVER_USERNAME_TAG = "username";
72     private static final String JavaDoc XML_SERVER_PASSWORD_TAG = "password";
73     private static final String JavaDoc XML_SERVER_FACTORY_TAG = "factory";
74     private static final String JavaDoc XML_SERVER_AUTH_MODE_TAG = "authentication-mode";
75     private static final String JavaDoc XML_SERVER_DESTINATION_TAG = "provider-destination";
76     private static final String JavaDoc XML_SERVER_PRIORITY_TAG = "priority";
77     private static final String JavaDoc XML_SERVER_MAX_RECONN_TAG = "max-reconnection";
78     private static final String JavaDoc XML_SERVER_REFERRAL = "referral";
79
80     private Map serversTable = null;
81     // end EP
82

83     private static final String JavaDoc CONFIG_PATH = File.separator + "services" +
84             File.separator + "usermanager";
85     private static final String JavaDoc CONFIG_FILE_NAME = "router-config.xml";
86
87     private Vector criteriaList = null;
88     private Map providersTable = null;
89     private SortedSet sortedProviders = null;
90     private JahiaUserManagerProvider defaultProviderInstance = null;
91     static private JahiaUserManagerRoutingService mInstance = null;
92
93     private Class JavaDoc stringClass = null;
94     private Class JavaDoc propertiesClass = null;
95
96
97     /**
98      * Create an new instance of the User Manager Service if the instance do not
99      * exist, or return the existing instance.
100      *
101      * @return Return the instance of the User Manager Service.
102      */

103     public static JahiaUserManagerRoutingService getInstance () {
104         if (mInstance == null) {
105             mInstance = new JahiaUserManagerRoutingService ();
106         }
107         return mInstance;
108     }
109
110     private JahiaUserManagerRoutingService () {
111
112         criteriaList = new Vector ();
113         providersTable = new HashMap ();
114         
115         // EP: 2004/28/06
116
serversTable = new HashMap ();
117
118         sortedProviders = new TreeSet (new Comparator () {
119             public int compare (Object JavaDoc o1, Object JavaDoc o2) {
120                 return ((UserManagerProviderBean) o1).getPriority () - ((UserManagerProviderBean) o2).getPriority ();
121             }
122         });
123
124         // the following default classes are used to solve the problem of
125
// null parameters being passed and us being unable to do .getClass()
126
// calls on the real objects. Sad that Java doesn't offer another way
127
// to do this.
128

129         // for JahiaUser we cannot do this properly so we will just manually
130
// add the test inside the methods that need one. (The problem being that
131
// JahiaUser is an abstract class :( )
132

133         // for Integer we don't have to do it since we have Integer.TYPE that's
134
// already done for us...
135

136         stringClass = (new String JavaDoc ()).getClass ();
137         propertiesClass = (new Properties ()).getClass ();
138
139     }
140
141     /**
142      * Returns a vector of UserManagerProviderBean object describing the
143      * available user management providers
144      *
145      * @return result a Vector of UserManagerProviderBean objects that describe
146      * the providers. This will never be null but may be empty if no providers
147      * are available.
148      */

149     public Vector getProviderList () {
150         Vector result = new Vector (sortedProviders);
151 // Iterator providerIter = providersTable.keySet().iterator();
152
// while (providerIter.hasNext()) {
153
// String curProviderKey = (String) providerIter.next();
154
// UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get(curProviderKey);
155
// result.add(curProvider);
156
// }
157

158         return result;
159     }
160
161     public TreeSet getServerList (String JavaDoc name) {
162         return (TreeSet)serversTable.get(name);
163     }
164     
165     public void init (SettingsBean jSettings)
166             throws JahiaInitializationException {
167
168         try {
169             loadConfiguration (jSettings.getJahiaEtcDiskPath () + JahiaUserManagerRoutingService.CONFIG_PATH +
170                     File.separator + JahiaUserManagerRoutingService.CONFIG_FILE_NAME);
171         } catch (JahiaException je) {
172             logger.error (
173                     "Error while trying to load configuration from " +
174                     jSettings.getJahiaEtcDiskPath () + JahiaUserManagerRoutingService.CONFIG_PATH +
175                     File.separator + JahiaUserManagerRoutingService.CONFIG_FILE_NAME,
176                     je);
177             throw new JahiaInitializationException ("Error while trying to load configuration from " +
178                     jSettings.getJahiaEtcDiskPath () + JahiaUserManagerRoutingService.CONFIG_PATH +
179                     File.separator + JahiaUserManagerRoutingService.CONFIG_FILE_NAME +
180                     " Exception:" + je.getMessage (), je);
181         }
182
183         Iterator providerIter = providersTable.keySet ().iterator ();
184         while (providerIter.hasNext ()) {
185             String JavaDoc curProviderKey = (String JavaDoc) providerIter.next ();
186             UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get (
187                     curProviderKey);
188             curProvider.getInstance ().init (jSettings);
189         }
190
191     }
192
193
194     public JahiaUser createUser (String JavaDoc name,
195                                  String JavaDoc password,
196                                  String JavaDoc userKey,
197                                  int siteID,
198                                  Properties properties) {
199         JahiaUser user = null;
200         Class JavaDoc[] parameterTypes = new Class JavaDoc[5];
201         parameterTypes[0] = stringClass;
202         parameterTypes[1] = stringClass;
203         parameterTypes[2] = stringClass;
204         parameterTypes[3] = Integer.TYPE;
205         parameterTypes[4] = propertiesClass;
206         Object JavaDoc[] args = new Object JavaDoc[5];
207         args[0] = name;
208         args[1] = password;
209         args[2] = userKey;
210         args[3] = new Integer JavaDoc (siteID);
211         args[4] = properties;
212         Object JavaDoc result = null;
213         try {
214             result = routeCall ("createUser",
215                     parameterTypes,
216                     properties,
217                     ROUTE_CALL_ONE,
218                     null,
219                     false,
220                     null,
221                     args);
222         } catch (Throwable JavaDoc t) {
223             t.printStackTrace ();
224             return null;
225         }
226         user = (JahiaUser) result;
227         return user;
228     }
229
230     public boolean deleteUser (JahiaUser user) {
231         if (user == null) {
232             return false;
233         }
234         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
235
236         // all this code just to get the Class of the interfaces, Java sucks here :(
237
Class JavaDoc[] interfaces = user.getClass ().getInterfaces ();
238         Class JavaDoc jahiaUserClass = null;
239         for (int i = 0; i < interfaces.length; i++) {
240             logger.debug ("Interface[" + i + "].getName()=[" + interfaces[i].getName () + "]");
241             if (JAHIAUSER_INTERFACE_NAME.equals (interfaces[i].getName ())) {
242                 jahiaUserClass = interfaces[i];
243             }
244         }
245         if (jahiaUserClass == null) {
246             logger.debug (
247                     "Cannot find interface JahiaUSer in passed user variables !! Aborting deleteUser call ! ");
248             return false;
249         }
250
251         parameterTypes[0] = jahiaUserClass;
252         Object JavaDoc[] args = new Object JavaDoc[1];
253         args[0] = user;
254         Object JavaDoc result = null;
255         try {
256             result = routeCall ("deleteUser",
257                     parameterTypes,
258                     user.getProperties (),
259                     ROUTE_CALL_ONE,
260                     null,
261                     false,
262                     null,
263                     args);
264         } catch (Throwable JavaDoc t) {
265             t.printStackTrace ();
266             return false;
267         }
268         Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
269         return resultBool.booleanValue ();
270     }
271
272     public int getNbUsers ()
273             throws org.jahia.exceptions.JahiaException {
274         Object JavaDoc result = null;
275         try {
276             result = routeCall ("getNbUsers",
277                     null,
278                     null,
279                     ROUTE_CALL_ALL,
280                     null,
281                     false,
282                     null,
283                     null);
284         } catch (Throwable JavaDoc t) {
285             t.printStackTrace ();
286             return -1;
287         }
288         Vector resultVector = (Vector) result;
289         Enumeration resultEnum = resultVector.elements ();
290         int nbUsers = 0;
291         while (resultEnum.hasMoreElements ()) {
292             Integer JavaDoc curResult = (Integer JavaDoc) resultEnum.nextElement ();
293             nbUsers += curResult.intValue ();
294         }
295         return nbUsers;
296     }
297
298     public int getNbUsers (int siteID)
299             throws org.jahia.exceptions.JahiaException {
300         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
301         parameterTypes[0] = Integer.TYPE;
302         Object JavaDoc[] args = new Object JavaDoc[1];
303         args[0] = new Integer JavaDoc (siteID);
304         Object JavaDoc result = null;
305         try {
306             result = routeCall ("getNbUsers",
307                     parameterTypes,
308                     null,
309                     ROUTE_CALL_ALL,
310                     null,
311                     false,
312                     null,
313                     args);
314         } catch (Throwable JavaDoc t) {
315             t.printStackTrace ();
316             return -1;
317         }
318         Vector resultVector = (Vector) result;
319         Enumeration resultEnum = resultVector.elements ();
320         int nbUsers = 0;
321         while (resultEnum.hasMoreElements ()) {
322             Integer JavaDoc curResult = (Integer JavaDoc) resultEnum.nextElement ();
323             nbUsers += curResult.intValue ();
324         }
325         return nbUsers;
326     }
327
328     public String JavaDoc getUniqueKey () {
329         Object JavaDoc result = null;
330         try {
331             result = routeCall ("getUniqueKey",
332                     null,
333                     null,
334                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
335                     null,
336                     false,
337                     null,
338                     null);
339         } catch (Throwable JavaDoc t) {
340             t.printStackTrace ();
341             return null;
342         }
343         return (String JavaDoc) result;
344     }
345
346     public Vector getUserList () {
347         Vector userList = new Vector ();
348
349         Object JavaDoc result = null;
350         try {
351             result = routeCall ("getUserList",
352                     null,
353                     null,
354                     ROUTE_CALL_ALL,
355                     null,
356                     false,
357                     null,
358                     null);
359         } catch (Throwable JavaDoc t) {
360             t.printStackTrace ();
361             return userList;
362         }
363         Vector resultVector = (Vector) result;
364         Enumeration resultEnum = resultVector.elements ();
365         while (resultEnum.hasMoreElements ()) {
366             Vector curResult = (Vector) resultEnum.nextElement ();
367             userList.addAll (curResult);
368         }
369         return userList;
370     }
371
372     public Vector getUserList (int siteID) {
373         Vector userList = new Vector ();
374
375         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
376         parameterTypes[0] = Integer.TYPE;
377         Object JavaDoc[] args = new Object JavaDoc[1];
378         args[0] = new Integer JavaDoc (siteID);
379
380         Object JavaDoc result = null;
381         try {
382             result = routeCall ("getUserList",
383                     parameterTypes,
384                     null,
385                     ROUTE_CALL_ALL,
386                     null,
387                     false,
388                     null,
389                     args);
390         } catch (Throwable JavaDoc t) {
391             t.printStackTrace ();
392             return userList;
393         }
394         Vector resultVector = (Vector) result;
395         Enumeration resultEnum = resultVector.elements ();
396         while (resultEnum.hasMoreElements ()) {
397             Vector curResult = (Vector) resultEnum.nextElement ();
398             if (curResult != null) {
399                 userList.addAll (curResult);
400             }
401         }
402         return userList;
403     }
404
405     public Vector getUsernameList (int siteID) {
406         Vector userNameList = new Vector ();
407
408         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
409         parameterTypes[0] = Integer.TYPE;
410         Object JavaDoc[] args = new Object JavaDoc[1];
411         args[0] = new Integer JavaDoc (siteID);
412
413         Object JavaDoc result = null;
414         try {
415             result = routeCall ("getUsernameList",
416                     parameterTypes,
417                     null,
418                     ROUTE_CALL_ALL,
419                     null,
420                     false,
421                     null,
422                     args);
423         } catch (Throwable JavaDoc t) {
424             t.printStackTrace ();
425             return userNameList;
426         }
427         Vector resultVector = (Vector) result;
428         Enumeration resultEnum = resultVector.elements ();
429         while (resultEnum.hasMoreElements ()) {
430             Vector curResult = (Vector) resultEnum.nextElement ();
431             userNameList.addAll (curResult);
432         }
433         return userNameList;
434     }
435
436     public JahiaDOMObject getUsersAsDOM (String JavaDoc providerName, int siteID)
437             throws org.jahia.exceptions.JahiaException {
438
439         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
440         parameterTypes[0] = Integer.TYPE;
441         Object JavaDoc[] args = new Object JavaDoc[1];
442         args[0] = new Integer JavaDoc (siteID);
443
444         Object JavaDoc result = null;
445         Vector providerList = new Vector ();
446         providerList.add (providerName);
447         try {
448             result = routeCall ("getUsersAsDOM",
449                     parameterTypes,
450                     null,
451                     ROUTE_CALL_ONE,
452                     providerList,
453                     false,
454                     null,
455                     args);
456         } catch (Throwable JavaDoc t) {
457             t.printStackTrace ();
458             return null;
459         }
460         return (JahiaDOMObject) result;
461     }
462
463     public JahiaDOMObject getUserPropsAsDOM (String JavaDoc providerName, int siteID)
464             throws org.jahia.exceptions.JahiaException {
465         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
466         parameterTypes[0] = Integer.TYPE;
467         Object JavaDoc[] args = new Object JavaDoc[1];
468         args[0] = new Integer JavaDoc (siteID);
469
470         Object JavaDoc result = null;
471         Vector providerList = new Vector ();
472         providerList.add (providerName);
473         try {
474             result = routeCall ("getUsersPropsAsDOM",
475                     parameterTypes,
476                     null,
477                     ROUTE_CALL_ONE,
478                     providerList,
479                     false,
480                     null,
481                     args);
482         } catch (Throwable JavaDoc t) {
483             t.printStackTrace ();
484             return null;
485         }
486         return (JahiaDOMObject) result;
487     }
488
489     /**
490      * Performs a login of the specified user.
491      *
492      * @param userKey the user identifier defined in this service properties
493      * @param userPassword the password of the user
494      *
495      * @return boolean true if the login succeeded, false otherwise
496      */

497     public boolean login (String JavaDoc userKey, String JavaDoc userPassword) {
498         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
499         parameterTypes[0] = stringClass;
500         parameterTypes[1] = stringClass;
501         Object JavaDoc[] args = new Object JavaDoc[2];
502         args[0] = userKey;
503         args[1] = userPassword;
504
505         Properties userProperties = new Properties ();
506         userProperties.setProperty ("user.key", userKey);
507         userProperties.setProperty ("user.password", userPassword);
508
509         Object JavaDoc result = null;
510         try {
511             result = routeCall ("login",
512                     parameterTypes,
513                     userProperties,
514                     ROUTE_CALL_ONE,
515                     null,
516                     false,
517                     null,
518                     args);
519         } catch (Throwable JavaDoc t) {
520             t.printStackTrace ();
521             return false;
522         }
523         Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
524         return resultBool.booleanValue ();
525     }
526
527     public JahiaUser lookupUser (String JavaDoc userKey) {
528         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
529         parameterTypes[0] = stringClass;
530         Object JavaDoc[] args = new Object JavaDoc[1];
531         args[0] = userKey;
532
533         Properties userProperties = new Properties ();
534         userProperties.setProperty ("user.key", userKey);
535
536         Object JavaDoc result = null;
537         try {
538             result = routeCall ("lookupUser",
539                     parameterTypes,
540                     userProperties,
541                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
542                     null,
543                     false,
544                     null,
545                     args);
546         } catch (Throwable JavaDoc t) {
547             t.printStackTrace ();
548             return null;
549         }
550         return (JahiaUser) result;
551     }
552
553     public JahiaUser lookupUser (int siteID, String JavaDoc name) {
554         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
555         parameterTypes[0] = Integer.TYPE;
556         parameterTypes[1] = stringClass;
557         Object JavaDoc[] args = new Object JavaDoc[2];
558         args[0] = new Integer JavaDoc (siteID);
559         args[1] = name;
560
561         Properties userProperties = new Properties ();
562         userProperties.setProperty ("user.name", name);
563
564         Object JavaDoc result = null;
565         try {
566             result = routeCall ("lookupUser",
567                     parameterTypes,
568                     userProperties,
569                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
570                     null,
571                     false,
572                     null,
573                     args);
574         } catch (Throwable JavaDoc t) {
575             t.printStackTrace ();
576             return null;
577         }
578         return (JahiaUser) result;
579     }
580
581     public boolean userExists (int siteID, String JavaDoc name) {
582         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
583         parameterTypes[0] = Integer.TYPE;
584         parameterTypes[1] = stringClass;
585         Object JavaDoc[] args = new Object JavaDoc[2];
586         args[0] = new Integer JavaDoc (siteID);
587         args[1] = name;
588
589         Properties userProperties = new Properties ();
590         userProperties.setProperty ("user.name", name);
591
592         Object JavaDoc result = null;
593         try {
594             result = routeCall ("userExists",
595                     parameterTypes,
596                     userProperties,
597                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
598                     null,
599                     false,
600                     null,
601                     args);
602         } catch (Throwable JavaDoc t) {
603             t.printStackTrace ();
604             return false;
605         }
606         Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
607         return resultBool.booleanValue ();
608     }
609
610     /**
611      * Find users according to a table of name=value properties. If the left
612      * side value is "*" for a property then it will be tested against all the
613      * properties. ie *=test* will match every property that starts with "test"
614      *
615      * @param siteID site identifier
616      * @param searchCriterias a Properties object that contains search criterias
617      * in the format name,value (for example "*"="*" or "username"="*test*")
618      *
619      * @return Set a set of JahiaUser elements that correspond to those
620      * search criterias
621      */

622     public Set searchUsers (int siteID, Properties searchCriterias) {
623         Set userList = new HashSet ();
624
625         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
626         parameterTypes[0] = Integer.TYPE;
627         parameterTypes[1] = propertiesClass;
628         Object JavaDoc[] args = new Object JavaDoc[2];
629         args[0] = new Integer JavaDoc (siteID);
630         args[1] = searchCriterias;
631
632         Object JavaDoc result = null;
633         try {
634             result = routeCall ("searchUsers",
635                     parameterTypes,
636                     null,
637                     ROUTE_CALL_ALL,
638                     null,
639                     false,
640                     null,
641                     args);
642         } catch (Throwable JavaDoc t) {
643             t.printStackTrace ();
644             return userList;
645         }
646         Vector resultVector = (Vector) result;
647         Enumeration resultEnum = resultVector.elements ();
648         while (resultEnum.hasMoreElements ()) {
649             Set curResult = (Set) resultEnum.nextElement ();
650             userList.addAll (curResult);
651         }
652         return userList;
653     }
654
655     /**
656      * Find users according to a table of name=value properties. If the left
657      * side value is "*" for a property then it will be tested against all the
658      * properties. ie *=test* will match every property that starts with "test"
659      *
660      * @param providerKey key of the provider in which to search, may be
661      * obtained by calling getProviderList()
662      * @param siteID site identifier
663      * @param searchCriterias a Properties object that contains search criterias
664      * in the format name,value (for example "*"="*" or "username"="*test*") or
665      * null to search without criterias
666      *
667      * @return Set a set of JahiaUser elements that correspond to those
668      * search criterias
669      */

670     public Set searchUsers (String JavaDoc providerKey, int siteID,
671                             Properties searchCriterias) {
672         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
673         parameterTypes[0] = Integer.TYPE;
674         parameterTypes[1] = propertiesClass;
675         Object JavaDoc[] args = new Object JavaDoc[2];
676         args[0] = new Integer JavaDoc (siteID);
677         args[1] = searchCriterias;
678
679         Object JavaDoc result = null;
680         Vector providerList = new Vector ();
681         providerList.add (providerKey);
682         try {
683             result = routeCall ("searchUsers",
684                     parameterTypes,
685                     null,
686                     ROUTE_CALL_ONE,
687                     providerList,
688                     false,
689                     null,
690                     args);
691         } catch (Throwable JavaDoc t) {
692             t.printStackTrace ();
693             return null;
694         }
695         return (Set) result;
696     }
697
698     /**
699      * This method indicates that any internal cache for a provider should be
700      * updated because the value has changed and needs to be transmitted to the
701      * other nodes in a clustering environment.
702      * @param jahiaGroup JahiaGroup the group to be updated in the cache.
703      */

704     public void updateCache(JahiaUser jahiaUser) {
705         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
706         parameterTypes[0] = JahiaUser.class;
707         Object JavaDoc[] args = new Object JavaDoc[1];
708         args[0] = jahiaUser;
709         Object JavaDoc result = null;
710         try {
711             result = routeCall ("updateCache",
712                     parameterTypes,
713                     jahiaUser.getProperties (),
714                     ROUTE_CALL_ONE,
715                     null,
716                     false,
717                     null,
718                     args);
719         } catch (Throwable JavaDoc t) {
720             t.printStackTrace ();
721         }
722     }
723
724     /**
725      * Routes the calls to a set of providers. This method is the center of this
726      * routing service. It allows for a very flexible way of calling back-end
727      * providers by leaving the routing decision until runtime and making it
728      * therefore very configurable.
729      *
730      * @param methodName the name of the method to invoke
731      * @param parameterTypes an array of Class objects that specify the
732      * type of the parameters for the method. This
733      * is necessary since we cannot determine this
734      * at runtime because of primitive type
735      * encapsulation. Therefore for primitive types
736      * these must be specified here such as
737      * Integer.TYPE, Boolean.TYPE, etc in order to
738      * look up the correct method
739      * @param userProperties the properties that will be used to evaluate
740      * the provider to call. This is defined by a
741      * set of rules.
742      * @param typeOfCall the type of call, values are ROUTE_CALL_ONE
743      * which means only one provider will be called
744      * based on rule evaluation, ROUTE_CALL_ALL
745      * means the method will be invoked for all the
746      * providers, ROUTE_CALL_ALL_UNTIL_SUCCESS means
747      * all the providers methods will be called
748      * until the first one returns a success. A
749      * success depends on the return type. For int
750      * it means all values exception -1, for
751      * booleans it means a "true" value, and for all
752      * other object it means a non null value
753      * @param providersToCall a vector of String objects that specify a
754      * list of providers to call in order. There may
755      * be zero, one or multiple providers. This
756      * parameter bypasses the criteria evaluation
757      * so be careful how you use it. It also uses
758      * the typeOfCall parameter to define the type
759      * of call, so make sure you use combinations
760      * that correspond, such as ROUTE_CALL_ONE and
761      * a single entry, ROUTE_CALL_ALL and at least
762      * one or more entries, or
763      * ROUTE_CALL_ALL_UNTIL_SUCCESS with one or
764      * more. Passing an empty vector or null will
765      * disable the forcing of the list of providers.
766      * @param useDifferentInstance set to true if you want to use the specified
767      * instance passed in parameter. If false it
768      * will use the provider's singleton instance.
769      * This is mostly useful if you want to be able
770      * to call static instances, or do something
771      * very special.
772      * @param instance the instance of the object we're using for
773      * this call. May be null if we are calling a
774      * static method.
775      * @param args an array of Objects which contain the
776      * parameters. The primitive types must be
777      * encapsulated into objects.
778      *
779      * @return the object returned. In the case of the ROUTE_CALL_ALL this
780      * returns a Vector containing all the results of the calls. If there were
781      * any errors this is very likely to return null.
782      *
783      * @throws Throwable this exception is actually an exception that has been
784      * generated within the called method, and that's just being passed upwards.
785      */

786     private Object JavaDoc routeCall (String JavaDoc methodName,
787                               Class JavaDoc[] parameterTypes,
788                               Properties userProperties,
789                               int typeOfCall,
790                               Vector providersToCall,
791                               boolean useDifferentInstance,
792                               Object JavaDoc instance,
793                               Object JavaDoc[] args)
794             throws Throwable JavaDoc {
795
796         switch (typeOfCall) {
797             case ROUTE_CALL_ONE:
798                 {
799                     // we're calling only one of the provider, we must determine
800
// which one by using the user properties matched against the
801
// criteria , or by using the providersToCall parameter
802
JahiaUserManagerProvider providerInstance = null;
803                     if (providersToCall != null) {
804                         if (providersToCall.size () >= 1) {
805                             Object JavaDoc providerItem = providersToCall.elementAt (0);
806                             if (providerItem instanceof String JavaDoc) {
807                                 String JavaDoc providerKey = (String JavaDoc) providerItem;
808                                 UserManagerProviderBean aProvider = (UserManagerProviderBean) providersTable.get (
809                                         providerKey);
810                                 providerInstance = aProvider.getInstance ();
811                             }
812                         }
813                     }
814                     if (providerInstance == null) {
815                         Enumeration criteriaEnum = criteriaList.elements ();
816                         while (criteriaEnum.hasMoreElements ()) {
817                             RoutingCriteria curCriteria = (RoutingCriteria) criteriaEnum.nextElement ();
818                             if (curCriteria.matchesValues (userProperties)) {
819                                 String JavaDoc providerKey = curCriteria.getDestination ();
820                                 UserManagerProviderBean curProvider = (UserManagerProviderBean) providersTable.get (
821                                         providerKey);
822                                 providerInstance = curProvider.getInstance ();
823                                 break;
824                             }
825                         }
826                     }
827                     if (providerInstance == null) {
828                         // fallback, we must find at least one provider to call.
829
providerInstance = defaultProviderInstance;
830                         if (providerInstance == null) {
831                             // what no default provider ?? exit immediately.
832
return null;
833                         }
834                     }
835                     
836                     Object JavaDoc result = null;
837                     try {
838                         Method JavaDoc methodToInvoke =
839                                 providerInstance.getClass ().getMethod (methodName,
840                                         parameterTypes);
841                         if (useDifferentInstance) {
842                             result = methodToInvoke.invoke (instance, args);
843                         } else {
844                             result = methodToInvoke.invoke (providerInstance, args);
845                         }
846                     } catch (NoSuchMethodException JavaDoc nsme) {
847                         nsme.printStackTrace ();
848                         result = null;
849                     } catch (InvocationTargetException JavaDoc ite) {
850                         ite.printStackTrace ();
851                         result = null;
852                         throw ite.getTargetException ();
853                     } catch (IllegalAccessException JavaDoc iae) {
854                         iae.printStackTrace ();
855                         result = null;
856                     }
857                     return result;
858                 }
859
860             case ROUTE_CALL_ALL:
861                 {
862                     // we're calling all the providers
863
Vector results = new Vector ();
864
865                     Enumeration providerEnum = getProvidersToCall (providersToCall).elements ();
866
867                     while (providerEnum.hasMoreElements ()) {
868                         JahiaUserManagerProvider curProvider =
869                                 (JahiaUserManagerProvider) providerEnum.nextElement ();
870                         Object JavaDoc result = null;
871                     
872                         try {
873                             Method JavaDoc methodToInvoke =
874                                     curProvider.getClass ().getMethod (methodName,
875                                             parameterTypes);
876                             if (useDifferentInstance) {
877                                 result = methodToInvoke.invoke (instance, args);
878                             } else {
879                                 result = methodToInvoke.invoke (curProvider, args);
880                             }
881                             results.add (result);
882                         } catch (NoSuchMethodException JavaDoc nsme) {
883                             nsme.printStackTrace ();
884                             results = null;
885                             break;
886                         } catch (InvocationTargetException JavaDoc ite) {
887                             ite.printStackTrace ();
888                             results = null;
889                             throw ite.getTargetException ();
890                         } catch (IllegalAccessException JavaDoc iae) {
891                             iae.printStackTrace ();
892                             results = null;
893                             break;
894                         }
895                     }
896                     
897                     return results;
898                 }
899
900             case ROUTE_CALL_ALL_UNTIL_SUCCESS:
901                 {
902                     // we're calling the providers in order, until one returns
903
// a success condition (to be defined)
904

905                     Enumeration providerEnum = getProvidersToCall (providersToCall).elements ();
906                     Object JavaDoc result = null;
907                     while (providerEnum.hasMoreElements ()) {
908                         JahiaUserManagerProvider curProvider =
909                                 (JahiaUserManagerProvider) providerEnum.nextElement ();
910                     
911                         try {
912                             Method JavaDoc methodToInvoke =
913                                     curProvider.getClass ().getMethod (methodName,
914                                             parameterTypes);
915                             if (useDifferentInstance) {
916                                 result = methodToInvoke.invoke (instance, args);
917                             } else {
918                                 result = methodToInvoke.invoke (curProvider, args);
919                             }
920                             Class JavaDoc returnType = methodToInvoke.getReturnType ();
921                             // conditions for success are :
922
// for int, all values except -1
923
// for booleans, true value
924
// for all other object, a non null value
925
if (returnType.isPrimitive ()) {
926                                 if ("int".equals (returnType.getName ())) {
927                                     Integer JavaDoc resultInt = (Integer JavaDoc) result;
928                                     if (resultInt.intValue () != -1) {
929                                         return result;
930                                     }
931                                 } else if ("boolean".equals (returnType.getName ())) {
932                                     Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
933                                     if (resultBool.booleanValue ()) {
934                                         return result;
935                                     }
936                                 } else {
937                                     if (result != null) {
938                                         return result;
939                                     }
940                                 }
941                             } else {
942                                 if (result != null) {
943                                     return result;
944                                 }
945                             }
946                         } catch (NoSuchMethodException JavaDoc nsme) {
947                             nsme.printStackTrace ();
948                             result = null;
949                         } catch (InvocationTargetException JavaDoc ite) {
950                             ite.printStackTrace ();
951                             result = null;
952                             throw ite.getTargetException ();
953                         } catch (IllegalAccessException JavaDoc iae) {
954                             iae.printStackTrace ();
955                             result = null;
956                         }
957                     }
958                     
959                     return result;
960                 }
961         }
962         return null;
963     }
964
965     /**
966      * Returns a vector of provider instances to call depending on the specified
967      * list of provider names.
968      *
969      * @param providersToCall a vector containing String that contain names
970      * of the providers to call. If no list is specified, the result list is
971      * composed of the internal list of providers
972      *
973      * @return a Vector of provider instances, may be empty if none was found,
974      * but never null. If no name list is specified, returns the internal list
975      * of providers
976      */

977     private Vector getProvidersToCall (Vector providersToCall) {
978         // we use this temporary vector in order to avoid having twice
979
// the same code for the dispatching...
980
Vector tmpProviderInstances = new Vector ();
981
982         if (providersToCall != null) {
983             if (providersToCall.size () >= 1) {
984                 Enumeration providerEnum = providersToCall.elements ();
985                 while (providerEnum.hasMoreElements ()) {
986                     Object JavaDoc curProviderEntry = providerEnum.nextElement ();
987                     if (curProviderEntry instanceof String JavaDoc) {
988                         String JavaDoc curProviderKey = (String JavaDoc) curProviderEntry;
989                         UserManagerProviderBean curProviderBean =
990                                 (UserManagerProviderBean) providersTable.get (curProviderKey);
991                         JahiaUserManagerProvider curProviderInstance = curProviderBean.getInstance ();
992                         if (curProviderInstance != null) {
993                             tmpProviderInstances.add (curProviderInstance);
994                         }
995                     }
996                 }
997             }
998         }
999
1000        if (tmpProviderInstances.size () == 0) {
1001            tmpProviderInstances = new Vector ();
1002            Iterator providerIter = sortedProviders.iterator ();
1003            while (providerIter.hasNext ()) {
1004                UserManagerProviderBean curBean = (UserManagerProviderBean) providerIter.next ();
1005                JahiaUserManagerProvider curProvider = curBean.getInstance ();
1006                tmpProviderInstances.add (curProvider);
1007            }
1008        }
1009
1010        return tmpProviderInstances;
1011    }
1012
1013    private boolean loadConfiguration (String JavaDoc configFileName)
1014            throws JahiaException {
1015
1016        Document documentRoot = null;
1017
1018        try {
1019
1020            DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance ();
1021            // dfactory.setValidating(true); // create only parsers that are validating
1022
DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder ();
1023
1024            FileInputStream JavaDoc configStream = new FileInputStream JavaDoc (configFileName);
1025            documentRoot = docBuilder.parse (configStream);
1026            documentRoot.normalize (); // clean up DOM tree a little
1027

1028        } catch (Throwable JavaDoc t) {
1029            throw new JahiaException ("JahiaUserManagerRoutingService.loadConfiguration",
1030                    "Exception while loading configuration",
1031                    JahiaException.CRITICAL_SEVERITY,
1032                    JahiaException.CONFIG_ERROR,
1033                    t);
1034        }
1035
1036        Element rootTag = documentRoot.getDocumentElement ();
1037        if (!rootTag.getTagName ().equals (JahiaUserManagerRoutingService.XML_ROOT_TAG)) {
1038            throw new JahiaException ("JahiaUserManagerRoutingService.loadConfiguration",
1039                    "Invalid root tag in configuration. Expected " + JahiaUserManagerRoutingService.XML_ROOT_TAG + ", found " + rootTag.getTagName (),
1040                    JahiaException.CRITICAL_SEVERITY,
1041                    JahiaException.CONFIG_ERROR);
1042        }
1043
1044        this.providersTable = getProvidersFromDOM (rootTag);
1045        sortedProviders.addAll (providersTable.values ());
1046        this.criteriaList = getCriteriasFromDOM (rootTag);
1047
1048    // EP: 2004/28/06 : parsing new elements for servers specifications
1049
this.serversTable = getServersFromDOM (rootTag);
1050    
1051        return true;
1052    }
1053
1054    private Map getProvidersFromDOM (Element rootElement)
1055            throws JahiaException {
1056        Map result = new InsertionSortedMap ();
1057        // we have to do copies because we cannot do nested node list parsing...
1058
NodeList providerElementList = rootElement.getElementsByTagName (
1059                JahiaUserManagerRoutingService.XML_PROVIDER_ROOT_TAG);
1060        Vector providerElements = new Vector ();
1061        for (int i = 0; i < providerElementList.getLength (); i++) {
1062            providerElements.add ((Element) providerElementList.item (i));
1063        }
1064
1065        Enumeration providerEnum = providerElements.elements ();
1066        int providerCounter = 0;
1067        while (providerEnum.hasMoreElements ()) {
1068            Element curProviderElement = (Element) providerEnum.nextElement ();
1069            UserManagerProviderBean providerBean = getProviderFromDOM (curProviderElement,
1070                    providerCounter);
1071            if (providerBean != null) {
1072                if (providerBean.getIsDefault ()) {
1073                    /** @todo FIXME : this is a class variable set, not very
1074                     * elegant... how can we avoid this ?
1075                     */

1076                    this.defaultProviderInstance = providerBean.getInstance ();
1077                }
1078                result.put (providerBean.getKey (), providerBean);
1079            }
1080            providerCounter++;
1081        }
1082
1083        return result;
1084    }
1085
1086    /**
1087     * EP: 2004/28/06
1088     * Returns the Map of all declared servers organized by provider and sorted by priority.
1089     */

1090    private Map getServersFromDOM (Element rootElement)
1091            throws JahiaException {
1092        Map result = new HashMap ();
1093        // we have to do copies because we cannot do nested node list parsing...
1094
NodeList serversList = rootElement.getElementsByTagName (XML_SERVER_ROOT_TAG);
1095
1096        for (int i = 0; i < serversList.getLength (); i++) {
1097            ServerBean sb = getServerFromDOM((Element)serversList.item(i));
1098            TreeSet servers = (TreeSet)result.get(sb.getProvider());
1099            if (servers == null) {
1100                servers = new TreeSet (new Comparator () {
1101                public int compare (Object JavaDoc o1, Object JavaDoc o2) {
1102                    return ((ServerBean) o1).getPriority () - ((ServerBean) o2).getPriority ();
1103                }
1104            });
1105            result.put(sb.getProvider(), servers);
1106        }
1107        servers.add(sb);
1108        }
1109        return result;
1110    }
1111
1112    private ServerBean getServerFromDOM (Element providerElement)
1113            throws JahiaException {
1114
1115        String JavaDoc provider = getElementValue (XML_SERVER_DESTINATION_TAG, providerElement);
1116        String JavaDoc priority = getElementValue (XML_SERVER_PRIORITY_TAG, providerElement);
1117        String JavaDoc maxreconn = getElementValue (XML_SERVER_MAX_RECONN_TAG, providerElement);
1118        ServerBean result = new ServerBean(priority, provider, maxreconn);
1119        
1120        result.setFactoryName(getElementValue (XML_SERVER_FACTORY_TAG, providerElement));
1121        result.setServerUrl(getElementValue (XML_SERVER_URL_TAG, providerElement));
1122        result.setUserName(getElementValue (XML_SERVER_USERNAME_TAG, providerElement));
1123        result.setAuthenticationMode(getElementValue (XML_SERVER_AUTH_MODE_TAG, providerElement));
1124        result.setUserPassword(getElementValue (XML_SERVER_PASSWORD_TAG, providerElement));
1125        result.setReferralBehavior(getElementValue (XML_SERVER_REFERRAL, providerElement));
1126    
1127        return result;
1128    }
1129    
1130    private Vector getCriteriasFromDOM (Element rootElement)
1131            throws JahiaException {
1132        Vector result = new Vector ();
1133        // we have to do copies because we cannot do nested node list parsing...
1134
NodeList criteriaElementList = rootElement.getElementsByTagName (
1135                JahiaUserManagerRoutingService.XML_CRITERIA_ROOT_TAG);
1136        Vector criteriaElements = new Vector ();
1137        for (int i = 0; i < criteriaElementList.getLength (); i++) {
1138            criteriaElements.add ((Element) criteriaElementList.item (i));
1139        }
1140
1141        Enumeration criteriaEnum = criteriaElements.elements ();
1142        while (criteriaEnum.hasMoreElements ()) {
1143            Element curCriteriaElement = (Element) criteriaEnum.nextElement ();
1144            RoutingCriteria curCriteria = getCriteriaFromDOM (curCriteriaElement);
1145            if (curCriteria != null) {
1146                result.add (curCriteria);
1147            }
1148        }
1149
1150        return result;
1151    }
1152
1153    /**
1154     * Returns a UserManagerProviderBean from a XML DOM. Format :
1155     * <provider>
1156     * <key>jahia_db</key>
1157     * <class>org.jahia.services.usermanager.JahiaUserManagerDBProvider</class>
1158     * <title>Jahia User Manager Database Provider</title>
1159     * <description>
1160     * ...
1161     * </description>
1162     * <is-default>true</is-default>
1163     * </provider>
1164     *
1165     * @param providerElement the Element containing the tag for a provider
1166     * DOM.
1167     *
1168     * @return UserManagerProviderBean a bean constructed by loading the values
1169     * from the XML DOM.
1170     *
1171     * @throws JahiaException
1172     */

1173    private UserManagerProviderBean getProviderFromDOM (Element providerElement,
1174                                                        int providerCounter)
1175            throws JahiaException {
1176
1177        if (!providerElement.getTagName ().equals (
1178                JahiaUserManagerRoutingService.XML_PROVIDER_ROOT_TAG)) {
1179            throw new JahiaException ("JahiaUserManagerRoutingService.getProviderFromDOM",
1180                    "May not call getProviderFromDOM on elements other than " + JahiaUserManagerRoutingService.XML_PROVIDER_ROOT_TAG,
1181                    JahiaException.CRITICAL_SEVERITY,
1182                    JahiaException.CONFIG_ERROR);
1183        }
1184
1185        UserManagerProviderBean result = null;
1186        String JavaDoc key = getElementValue (JahiaUserManagerRoutingService.XML_PROVIDER_KEY_TAG,
1187                providerElement);
1188        String JavaDoc className = getElementValue (
1189                JahiaUserManagerRoutingService.XML_PROVIDER_CLASS_TAG, providerElement);
1190        String JavaDoc title = getElementValue (JahiaUserManagerRoutingService.XML_PROVIDER_TITLE_TAG,
1191                providerElement);
1192        String JavaDoc description = getElementValue (
1193                JahiaUserManagerRoutingService.XML_PROVIDER_DESCRIPTION_TAG, providerElement);
1194        String JavaDoc isDefaultStr = getElementValue (
1195                JahiaUserManagerRoutingService.XML_PROVIDER_ISDEFAULT_TAG, providerElement);
1196        boolean isDefault = ("true".equals (isDefaultStr));
1197
1198        boolean isReadOnly = false;
1199        try {
1200            // this tag is currently optional, so if we don't find it we set it to false by default
1201
String JavaDoc isReadOnlyStr = getElementValue (
1202                    JahiaUserManagerRoutingService.XML_PROVIDER_ISREADONLY_TAG,
1203                    providerElement);
1204            isReadOnly = ("true".equals (isReadOnlyStr));
1205        } catch (JahiaException je) {
1206            logger.error ("Warning : error loading tag <" +
1207                    JahiaUserManagerRoutingService.XML_PROVIDER_ISREADONLY_TAG +
1208                    ">. Please update your configuration file to include it !");
1209            isReadOnly = false;
1210        }
1211
1212        int priority = 99;
1213        try {
1214            String JavaDoc priorityStr = getElementValue (
1215                    JahiaUserManagerRoutingService.XML_PROVIDER_PRIORITY_TAG, providerElement);
1216            priority = Integer.parseInt (priorityStr);
1217        } catch (Exception JavaDoc e) {
1218            // optional parameter, ignore exception
1219
priority = providerCounter;
1220        }
1221
1222        result =
1223                new UserManagerProviderBean (key, className, title, description, isDefault,
1224                        isReadOnly, priority);
1225        return result;
1226    }
1227
1228    /**
1229     * Returns a RoutingCriteria from a XML DOM. Format :
1230     * <criteria>
1231     * <key>jahia_db_criteria</key>
1232     * <description>
1233     * ...
1234     * </description>
1235     * <condition propertyName="user.key" propertyValue="{jahia_db}*"/>
1236     * <condition ... />
1237     * <provider-destination>jahia_db</provider-destination>
1238     * </criteria>
1239     *
1240     * @param criteriaElement the Element containing the tag for a criteria
1241     * DOM.
1242     *
1243     * @return UserManagerProviderBean a bean constructed by loading the values
1244     * from the XML DOM.
1245     *
1246     * @throws JahiaException
1247     */

1248    private RoutingCriteria getCriteriaFromDOM (Element criteriaElement)
1249            throws JahiaException {
1250
1251        RoutingCriteria result = null;
1252
1253        if (!criteriaElement.getTagName ().equals (
1254                JahiaUserManagerRoutingService.XML_CRITERIA_ROOT_TAG)) {
1255            throw new JahiaException ("JahiaUserManagerRoutingService.getProviderFromDOM",
1256                    "May not call getCriteriaFromDOM on elements other than " + JahiaUserManagerRoutingService.XML_CRITERIA_ROOT_TAG,
1257                    JahiaException.CRITICAL_SEVERITY,
1258                    JahiaException.CONFIG_ERROR);
1259        }
1260
1261        String JavaDoc key = getElementValue (JahiaUserManagerRoutingService.XML_CRITERIA_KEY_TAG,
1262                criteriaElement);
1263        String JavaDoc description = getElementValue (
1264                JahiaUserManagerRoutingService.XML_CRITERIA_DESCRIPTION_TAG, criteriaElement);
1265        Properties conditions = getConditionsFromDOM (criteriaElement);
1266        String JavaDoc providerDestination = getElementValue (
1267                JahiaUserManagerRoutingService.XML_CRITERIA_DESTINATION_TAG, criteriaElement);
1268
1269        result = new RoutingCriteria (key, description, conditions, providerDestination);
1270        return result;
1271    }
1272
1273    private Properties getConditionsFromDOM (Element criteriaElement)
1274            throws JahiaException {
1275        Properties result = new Properties ();
1276
1277        NodeList criteriaElementList = criteriaElement.getElementsByTagName (
1278                JahiaUserManagerRoutingService.XML_CRITERIA_CONDITION_TAG);
1279        for (int i = 0; i < criteriaElementList.getLength (); i++) {
1280            Element curConditionElement = (Element) criteriaElementList.item (i);
1281            NamedNodeMap attrib = curConditionElement.getAttributes ();
1282            Node propertyNameAttrib = attrib.getNamedItem (
1283                    JahiaUserManagerRoutingService.XML_CRITERIA_CONDITION_PROPNAME_ATTR);
1284            if (propertyNameAttrib == null) {
1285                throw new JahiaException (
1286                        "JahiaUserManagerRoutingService.getConditionsFromDOM",
1287                        JahiaUserManagerRoutingService.XML_CRITERIA_CONDITION_PROPNAME_ATTR + " attribute not found on tag " + curConditionElement.getTagName (),
1288                        JahiaException.CRITICAL_SEVERITY,
1289                        JahiaException.CONFIG_ERROR);
1290            }
1291            String JavaDoc propertyName = propertyNameAttrib.getNodeValue ();
1292            Node propertyValueAttrib = attrib.getNamedItem (
1293                    JahiaUserManagerRoutingService.XML_CRITERIA_CONDITION_PROPVALUE_ATTR);
1294            if (propertyValueAttrib == null) {
1295                throw new JahiaException (
1296                        "JahiaUserManagerRoutingService.getConditionsFromDOM",
1297                        JahiaUserManagerRoutingService.XML_CRITERIA_CONDITION_PROPVALUE_ATTR + " attribute not found on tag " + curConditionElement.getTagName (),
1298                        JahiaException.CRITICAL_SEVERITY,
1299                        JahiaException.CONFIG_ERROR);
1300            }
1301            String JavaDoc propertyValue = propertyValueAttrib.getNodeValue ();
1302            result.setProperty (propertyName, propertyValue);
1303        }
1304        return result;
1305    }
1306
1307    private String JavaDoc getElementTextValue (Element rootElement)
1308            throws JahiaException {
1309        NodeList childNodes = rootElement.getChildNodes ();
1310        StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
1311        for (int i = 0; i < childNodes.getLength (); i++) {
1312            if (childNodes.item (i).getNodeType () == Node.TEXT_NODE) {
1313                result.append (childNodes.item (i).getNodeValue ());
1314            } else {
1315                throw new JahiaException ("JahiaUserManagerRoutingService.getElementTextValue",
1316                        "Only text values are allowed in tag " + rootElement.getTagName (),
1317                        JahiaException.CRITICAL_SEVERITY,
1318                        JahiaException.CONFIG_ERROR);
1319            }
1320        }
1321        return result.toString ();
1322    }
1323
1324    private String JavaDoc getElementValue (String JavaDoc tagName, Element parentElement)
1325            throws JahiaException {
1326        NodeList foundElements = parentElement.getElementsByTagName (tagName);
1327        if (foundElements.getLength () == 1) {
1328            return getElementTextValue ((Element) foundElements.item (0));
1329        } else {
1330            if (foundElements.getLength () != 0) {
1331                throw new JahiaException ("JahiaUserManagerRoutingService.getElementValue",
1332                        "Only one <" + tagName + "> is allowed in a <" + parentElement.getTagName () + "> tag.",
1333                        JahiaException.CRITICAL_SEVERITY,
1334                        JahiaException.CONFIG_ERROR);
1335            } else {
1336                throw new JahiaException ("JahiaUserManagerRoutingService.getElementValue",
1337                        "Couldn't find tag <" + tagName + "> in <" + parentElement.getTagName () + "> tag.",
1338                        JahiaException.CRITICAL_SEVERITY,
1339                        JahiaException.CONFIG_ERROR);
1340            }
1341        }
1342    }
1343
1344}
1345
Popular Tags