KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.services.usermanager;
2
3
4 import org.jahia.data.JahiaDOMObject;
5 import org.jahia.exceptions.JahiaException;
6 import org.jahia.exceptions.JahiaInitializationException;
7 import org.jahia.settings.SettingsBean;
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: </p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2003</p>
22  * <p>Company: </p>
23  *
24  * @author not attributable
25  * @version 1.0
26  */

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

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

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

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

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

125         // for JahiaGroup we cannot do this properly so we will just manually
126
// add the test inside the methods that need one. (The problem being that
127
// JahiaGroup is an abstract class :( )
128

129         // for Integer we don't have to do it since we have Integer.TYPE that's
130
// already done for us...
131

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

145     public Vector getProviderList () {
146         Vector result = new Vector (sortedProviders);
147 // Iterator providerIter = providersTable.keySet().iterator();
148
// while (providerIter.hasNext()) {
149
// String curProviderKey = (String) providerIter.next();
150
// GroupManagerProviderBean curProvider = (GroupManagerProviderBean) providersTable.get(curProviderKey);
151
// result.add(curProvider);
152
// }
153

154         return result;
155     }
156
157     public TreeSet getServerList (String JavaDoc name) {
158         return (TreeSet)serversTable.get(name);
159     }
160     
161     public void init (SettingsBean jSettings)
162             throws JahiaInitializationException {
163
164         try {
165             loadConfiguration (
166                     jSettings.getJahiaEtcDiskPath () + JahiaGroupManagerRoutingService.CONFIG_PATH +
167                     File.separator + JahiaGroupManagerRoutingService.CONFIG_FILE_NAME);
168         } catch (JahiaException je) {
169             logger.error (
170                     "Error while trying to load configuration from " +
171                     jSettings.getJahiaEtcDiskPath () + JahiaGroupManagerRoutingService.CONFIG_PATH +
172                     File.separator + JahiaGroupManagerRoutingService.CONFIG_FILE_NAME,
173                     je);
174             throw new JahiaInitializationException ("Error while trying to load configuration from " +
175                     jSettings.getJahiaEtcDiskPath () + JahiaGroupManagerRoutingService.CONFIG_PATH +
176                     File.separator + JahiaGroupManagerRoutingService.CONFIG_FILE_NAME +
177                     " Exception:" + je.getMessage (), je);
178         }
179
180         Iterator providerIter = providersTable.keySet ().iterator ();
181         while (providerIter.hasNext ()) {
182             String JavaDoc curProviderKey = (String JavaDoc) providerIter.next ();
183             GroupManagerProviderBean curProvider = (GroupManagerProviderBean) providersTable.get (
184                     curProviderKey);
185             curProvider.getInstance ().init (jSettings);
186         }
187
188     }
189
190
191 //------------------------------------------------------------------------
192

193
194     public Vector getAdminGrantedSites (JahiaUser user)
195             throws org.jahia.exceptions.JahiaException {
196         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
197
198         // all this code just to get the Class of the interfaces, Java sucks here :(
199
Class JavaDoc[] interfaces = user.getClass ().getInterfaces ();
200         Class JavaDoc jahiaUserClass = null;
201         for (int i = 0; i < interfaces.length; i++) {
202             logger.debug ("Interface[" + i + "].getName()=[" + interfaces[i].getName () + "]");
203             if (JAHIAUSER_INTERFACE_NAME.equals (interfaces[i].getName ())) {
204                 jahiaUserClass = interfaces[i];
205             }
206         }
207
208         if (jahiaUserClass == null) {
209             logger.debug (
210                     "Cannot find interface JahiaGroup in passed user variables !! Aborting call ! ");
211             return null;
212         }
213
214         parameterTypes[0] = jahiaUserClass;
215         Object JavaDoc[] args = new Object JavaDoc[1];
216         args[0] = user;
217         Object JavaDoc result = null;
218         Vector sitesList = new Vector ();
219         try {
220             result = routeCall ("getAdminGrantedSites",
221                     parameterTypes,
222                     null,
223                     ROUTE_CALL_ALL,
224                     null,
225                     false,
226                     null,
227                     args);
228         } catch (Throwable JavaDoc t) {
229             t.printStackTrace ();
230             return sitesList;
231         }
232         Vector resultVector = (Vector) result;
233         Enumeration resultEnum = resultVector.elements ();
234         while (resultEnum.hasMoreElements ()) {
235             Vector curResult = (Vector) resultEnum.nextElement ();
236             if (curResult != null) {
237                 int size = 0;
238                 for (int i = 0; i < curResult.size (); i++) {
239                     if (!sitesList.contains (curResult.get (i))) {
240                         sitesList.add (curResult.get (i));
241                     }
242                 }
243             }
244         }
245         return sitesList;
246     }
247
248
249     public JahiaDOMObject getAppRoleGroupsAsDOM (int siteID)
250             throws org.jahia.exceptions.JahiaException {
251         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
252         parameterTypes[0] = Integer.TYPE;
253         Object JavaDoc[] args = new Object JavaDoc[1];
254         args[0] = new Integer JavaDoc (siteID);
255         String JavaDoc providerName = "jahia"; //not implemented for LDAP
256
Object JavaDoc result = null;
257         Vector providerList = new Vector ();
258         providerList.add (providerName);
259         try {
260             result = routeCall ("getAppRoleGroupsAsDOM",
261                     parameterTypes,
262                     null,
263                     ROUTE_CALL_ONE,
264                     providerList,
265                     false,
266                     null,
267                     args);
268         } catch (Throwable JavaDoc t) {
269             t.printStackTrace ();
270             return null;
271         }
272         return (JahiaDOMObject) result;
273
274     }
275
276     public boolean groupExists (int siteID, String JavaDoc name) {
277         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
278         parameterTypes[0] = Integer.TYPE;
279         parameterTypes[1] = stringClass;
280         Object JavaDoc[] args = new Object JavaDoc[2];
281         args[0] = new Integer JavaDoc (siteID);
282         args[1] = name;
283
284         Properties groupProperties = new Properties ();
285         groupProperties.setProperty ("group.name", name);
286
287         Object JavaDoc result = null;
288         try {
289             result = routeCall ("groupExists",
290                     parameterTypes,
291                     groupProperties,
292                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
293                     null,
294                     false,
295                     null,
296                     args);
297         } catch (Throwable JavaDoc t) {
298             t.printStackTrace ();
299             return false;
300         }
301         Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
302         return resultBool.booleanValue ();
303
304     }
305
306     public JahiaGroup getGuestGroup (int siteID) {
307         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
308         parameterTypes[0] = Integer.TYPE;
309         Object JavaDoc[] args = new Object JavaDoc[1];
310         args[0] = new Integer JavaDoc (siteID);
311
312
313         Object JavaDoc result = null;
314         try {
315             result = routeCall ("getGuestGroup",
316                     parameterTypes,
317                     null,
318                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
319                     null,
320                     false,
321                     null,
322                     args);
323         } catch (Throwable JavaDoc t) {
324             t.printStackTrace ();
325             return null;
326         }
327         return (JahiaGroup) result;
328
329     }
330
331     public JahiaGroup getUsersGroup (int siteID) {
332         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
333         parameterTypes[0] = Integer.TYPE;
334         Object JavaDoc[] args = new Object JavaDoc[1];
335         args[0] = new Integer JavaDoc (siteID);
336
337
338         Object JavaDoc result = null;
339         try {
340             result = routeCall ("getUsersGroup",
341                     parameterTypes,
342                     null,
343                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
344                     null,
345                     false,
346                     null,
347                     args);
348         } catch (Throwable JavaDoc t) {
349             t.printStackTrace ();
350             return null;
351         }
352         return (JahiaGroup) result;
353
354     }
355
356
357     public Vector getGroupnameList (int siteID) {
358         Vector groupNameList = new Vector ();
359
360         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
361         parameterTypes[0] = Integer.TYPE;
362         Object JavaDoc[] args = new Object JavaDoc[1];
363         args[0] = new Integer JavaDoc (siteID);
364
365         Object JavaDoc result = null;
366         try {
367             result = routeCall ("getGroupnameList",
368                     parameterTypes,
369                     null,
370                     ROUTE_CALL_ALL,
371                     null,
372                     false,
373                     null,
374                     args);
375         } catch (Throwable JavaDoc t) {
376             t.printStackTrace ();
377             return groupNameList;
378         }
379         Vector resultVector = (Vector) result;
380         Enumeration resultEnum = resultVector.elements ();
381         while (resultEnum.hasMoreElements ()) {
382             Vector curResult = (Vector) resultEnum.nextElement ();
383             groupNameList.addAll (curResult);
384         }
385         return groupNameList;
386
387     }
388
389     public boolean removeUserFromAllGroups (JahiaUser user) {
390         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
391
392         // all this code just to get the Class of the interfaces, Java sucks here :(
393
Class JavaDoc[] interfaces = user.getClass ().getInterfaces ();
394         Class JavaDoc jahiaUserClass = null;
395         for (int i = 0; i < interfaces.length; i++) {
396             logger.debug ("Interface[" + i + "].getName()=[" + interfaces[i].getName () + "]");
397             if (JAHIAUSER_INTERFACE_NAME.equals (interfaces[i].getName ())) {
398                 jahiaUserClass = interfaces[i];
399             }
400         }
401
402         if (jahiaUserClass == null) {
403             logger.debug (
404                     "Cannot find interface JahiaGroup in passed user variables !! Aborting call ! ");
405             return false;
406         }
407
408         parameterTypes[0] = jahiaUserClass;
409         Object JavaDoc[] args = new Object JavaDoc[1];
410         args[0] = user;
411         Object JavaDoc result = null;
412         try {
413             result = routeCall ("removeUserFromAllGroups",
414                     parameterTypes,
415                     null,
416                     ROUTE_CALL_ALL,
417                     null,
418                     false,
419                     null,
420                     args);
421         } catch (Throwable JavaDoc t) {
422             t.printStackTrace ();
423             return false;
424         }
425         try {
426             Vector resultVector = (Vector) result;
427             boolean success = true;
428             Enumeration resultEnum = resultVector.elements ();
429             while (resultEnum.hasMoreElements ()) {
430                 Boolean JavaDoc resultBool = (Boolean JavaDoc) resultEnum.nextElement ();
431                 if (!resultBool.booleanValue ()) {
432                     success = false;
433                     break;
434                 }
435             }
436             return success;
437         } catch (java.lang.ClassCastException JavaDoc cce) {
438             logger.debug ("Error casting result to Boolean", cce);
439         }
440         return false;
441     }
442
443     public boolean deleteGroup (JahiaGroup group) {
444         if (group == null) {
445             return false;
446         }
447         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
448         /*
449         // all this code just to get the Class of the interfaces, Java sucks here :(
450         Class[] interfaces = group.getClass().getInterfaces();
451         Class jahiaGroupClass = null;
452         for (int i=0; i < interfaces.length; i++) {
453         logger.debug("Interface[" + i + "].getName()=[" + interfaces[i].getName() + "]");
454             if (JAHIAGROUP_INTERFACE_NAME.equals(interfaces[i].getName())) {
455                 jahiaGroupClass = interfaces[i];
456             }
457         }
458         if (jahiaGroupClass == null) {
459         logger.debug("Cannot find interface JahiaGroup in passed user variables !! Aborting deleteGroup call ! ");
460             return false;
461         }
462
463         parameterTypes[0] = jahiaGroupClass;
464         */

465         parameterTypes[0] = group.getClass ().getSuperclass ();
466         Object JavaDoc[] args = new Object JavaDoc[1];
467         args[0] = group;
468         Object JavaDoc result = null;
469         try {
470             result = routeCall ("deleteGroup",
471                     parameterTypes,
472                     group.getProperties (),
473                     ROUTE_CALL_ONE,
474                     null,
475                     false,
476                     null,
477                     args);
478         } catch (Throwable JavaDoc t) {
479             t.printStackTrace ();
480             return false;
481         }
482         try {
483             Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
484             return resultBool.booleanValue ();
485         } catch (java.lang.ClassCastException JavaDoc cce) {
486             logger.debug ("Error casting result to Boolean", cce);
487         }
488         return false;
489     }
490
491     public Vector getUserMembership (JahiaUser user) {
492         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
493
494         // all this code just to get the Class of the interfaces, Java sucks here :(
495
Class JavaDoc[] interfaces = user.getClass ().getInterfaces ();
496         Class JavaDoc jahiaUserClass = null;
497         for (int i = 0; i < interfaces.length; i++) {
498             logger.debug ("Interface[" + i + "].getName()=[" + interfaces[i].getName () + "]");
499             if (JAHIAUSER_INTERFACE_NAME.equals (interfaces[i].getName ())) {
500                 jahiaUserClass = interfaces[i];
501             }
502         }
503
504         if (jahiaUserClass == null) {
505             logger.debug (
506                     "Cannot find interface JahiaGroup in passed user variables !! Aborting call ! ");
507             return null;
508         }
509
510         parameterTypes[0] = jahiaUserClass;
511         Object JavaDoc[] args = new Object JavaDoc[1];
512         args[0] = user;
513         Object JavaDoc result = null;
514         Vector sitesList = new Vector ();
515         try {
516             result = routeCall ("getUserMembership",
517                     parameterTypes,
518                     null,
519                     ROUTE_CALL_ALL,
520                     null,
521                     false,
522                     null,
523                     args);
524         } catch (Throwable JavaDoc t) {
525             t.printStackTrace ();
526             return sitesList;
527         }
528         Vector resultVector = (Vector) result;
529         Enumeration resultEnum = resultVector.elements ();
530         while (resultEnum.hasMoreElements ()) {
531             Vector curResult = (Vector) resultEnum.nextElement ();
532             if (curResult != null) {
533                 sitesList.addAll (curResult);
534             }
535         }
536         return sitesList;
537
538     }
539
540     public Vector getGroupList (int siteID) {
541         Vector groupList = new Vector ();
542
543         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
544         parameterTypes[0] = Integer.TYPE;
545         Object JavaDoc[] args = new Object JavaDoc[1];
546         args[0] = new Integer JavaDoc (siteID);
547
548         Object JavaDoc result = null;
549         try {
550             result = routeCall ("getGroupList",
551                     parameterTypes,
552                     null,
553                     ROUTE_CALL_ALL,
554                     null,
555                     false,
556                     null,
557                     args);
558         } catch (Throwable JavaDoc t) {
559             t.printStackTrace ();
560             return groupList;
561         }
562         Vector resultVector = (Vector) result;
563         Enumeration resultEnum = resultVector.elements ();
564         while (resultEnum.hasMoreElements ()) {
565             Vector curResult = (Vector) resultEnum.nextElement ();
566             if (curResult != null) {
567                 groupList.addAll (curResult);
568             }
569         }
570         return groupList;
571     }
572
573     public JahiaDOMObject getUserGroupAccessAsDOM (int siteID)
574             throws org.jahia.exceptions.JahiaException {
575         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
576         parameterTypes[0] = Integer.TYPE;
577         Object JavaDoc[] args = new Object JavaDoc[1];
578         args[0] = new Integer JavaDoc (siteID);
579         String JavaDoc providerName = "jahia"; //not implemented for LDAP
580
Object JavaDoc result = null;
581         Vector providerList = new Vector ();
582         providerList.add (providerName);
583         try {
584             result = routeCall ("getUserGroupAccesAsDOM",
585                     parameterTypes,
586                     null,
587                     ROUTE_CALL_ONE,
588                     providerList,
589                     false,
590                     null,
591                     args);
592         } catch (Throwable JavaDoc t) {
593             t.printStackTrace ();
594             return null;
595         }
596         return (JahiaDOMObject) result;
597
598     }
599
600     public JahiaGroup lookupGroup (String JavaDoc groupKey) {
601         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
602         parameterTypes[0] = stringClass;
603         Object JavaDoc[] args = new Object JavaDoc[1];
604         args[0] = groupKey;
605
606         Properties groupProperties = new Properties ();
607         groupProperties.setProperty ("group.key", groupKey);
608
609         Object JavaDoc result = null;
610         try {
611             result = routeCall ("lookupGroup",
612                     parameterTypes,
613                     groupProperties,
614                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
615                     null,
616                     false,
617                     null,
618                     args);
619         } catch (Throwable JavaDoc t) {
620             t.printStackTrace ();
621             return null;
622         }
623         return (JahiaGroup) result;
624
625     }
626
627     public JahiaGroup getAdministratorGroup (int siteID) {
628         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
629         parameterTypes[0] = Integer.TYPE;
630         Object JavaDoc[] args = new Object JavaDoc[1];
631         args[0] = new Integer JavaDoc (siteID);
632
633
634         Object JavaDoc result = null;
635         try {
636             result = routeCall ("getAdministratorGroup",
637                     parameterTypes,
638                     null,
639                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
640                     null,
641                     false,
642                     null,
643                     args);
644         } catch (Throwable JavaDoc t) {
645             t.printStackTrace ();
646             return null;
647         }
648         return (JahiaGroup) result;
649
650     }
651
652     public Vector getGroupnameList () {
653         Vector groupNameList = new Vector ();
654
655
656         Object JavaDoc result = null;
657         try {
658             result = routeCall ("getGroupnameList",
659                     null,
660                     null,
661                     ROUTE_CALL_ALL,
662                     null,
663                     false,
664                     null,
665                     null);
666         } catch (Throwable JavaDoc t) {
667             t.printStackTrace ();
668             return groupNameList;
669         }
670         Vector resultVector = (Vector) result;
671         Enumeration resultEnum = resultVector.elements ();
672         while (resultEnum.hasMoreElements ()) {
673             Vector curResult = (Vector) resultEnum.nextElement ();
674             groupNameList.addAll (curResult);
675         }
676         return groupNameList;
677
678     }
679
680     public JahiaDOMObject getAppGroupAccessAsDOM (int siteID)
681             throws org.jahia.exceptions.JahiaException {
682         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
683         parameterTypes[0] = Integer.TYPE;
684         Object JavaDoc[] args = new Object JavaDoc[1];
685         args[0] = new Integer JavaDoc (siteID);
686         String JavaDoc providerName = "jahia"; //not implemented for LDAP
687
Object JavaDoc result = null;
688         Vector providerList = new Vector ();
689         providerList.add (providerName);
690         try {
691             result = routeCall ("getGroupAccesAsDOM",
692                     parameterTypes,
693                     null,
694                     ROUTE_CALL_ONE,
695                     providerList,
696                     false,
697                     null,
698                     args);
699         } catch (Throwable JavaDoc t) {
700             t.printStackTrace ();
701             return null;
702         }
703         return (JahiaDOMObject) result;
704
705     }
706
707     public Vector getGroupList () {
708         Vector groupList = new Vector ();
709
710         Object JavaDoc result = null;
711         try {
712             result = routeCall ("getGroupList",
713                     null,
714                     null,
715                     ROUTE_CALL_ALL,
716                     null,
717                     false,
718                     null,
719                     null);
720         } catch (Throwable JavaDoc t) {
721             t.printStackTrace ();
722             return groupList;
723         }
724         Vector resultVector = (Vector) result;
725         Enumeration resultEnum = resultVector.elements ();
726         while (resultEnum.hasMoreElements ()) {
727             Vector curResult = (Vector) resultEnum.nextElement ();
728             groupList.addAll (curResult);
729         }
730         return groupList;
731
732     }
733
734     /**
735      * Find groups according to a table of name=value properties. If the left
736      * side value is "*" for a property then it will be tested against all the
737      * properties. ie *=test* will match every property that starts with "test"
738      *
739      * @param siteID site identifier
740      * @param searchCriterias a Properties object that contains search criterias
741      * in the format name,value (for example "*"="*" or "groupname"="*test*")
742      *
743      * @return Set a set of JahiaGroup elements that correspond to those
744      * search criterias
745      */

746     public Set searchGroups (int siteID, Properties searchCriterias) {
747         Set groupList = new HashSet ();
748
749         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
750         parameterTypes[0] = Integer.TYPE;
751         parameterTypes[1] = propertiesClass;
752         Object JavaDoc[] args = new Object JavaDoc[2];
753         args[0] = new Integer JavaDoc (siteID);
754         args[1] = searchCriterias;
755
756         Object JavaDoc result = null;
757         try {
758             result = routeCall ("searchGroups",
759                     parameterTypes,
760                     null,
761                     ROUTE_CALL_ALL,
762                     null,
763                     false,
764                     null,
765                     args);
766         } catch (Throwable JavaDoc t) {
767             t.printStackTrace ();
768             return groupList;
769         }
770         Vector resultVector = (Vector) result;
771         Enumeration resultEnum = resultVector.elements ();
772         while (resultEnum.hasMoreElements ()) {
773             Set curResult = (Set) resultEnum.nextElement ();
774             groupList.addAll (curResult);
775         }
776         return groupList;
777     }
778
779     /**
780      * Find groups according to a table of name=value properties. If the left
781      * side value is "*" for a property then it will be tested against all the
782      * properties. ie *=test* will match every property that starts with "test"
783      *
784      * @param providerKey key of the provider in which to search, may be
785      * obtained by calling getProviderList()
786      * @param siteID site identifier
787      * @param searchCriterias a Properties object that contains search criterias
788      * in the format name,value (for example "*"="*" or "groupname"="*test*") or
789      * null to search without criterias
790      *
791      * @return Set a set of JahiaGroup elements that correspond to those
792      * search criterias
793      */

794     public Set searchGroups (String JavaDoc providerKey, int siteID,
795                              Properties searchCriterias) {
796         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
797         parameterTypes[0] = Integer.TYPE;
798         parameterTypes[1] = propertiesClass;
799         Object JavaDoc[] args = new Object JavaDoc[2];
800         args[0] = new Integer JavaDoc (siteID);
801         args[1] = searchCriterias;
802
803         Object JavaDoc result = null;
804         Vector providerList = new Vector ();
805         providerList.add (providerKey);
806         try {
807             result = routeCall ("searchGroups",
808                     parameterTypes,
809                     null,
810                     ROUTE_CALL_ONE,
811                     providerList,
812                     false,
813                     null,
814                     args);
815         } catch (Throwable JavaDoc t) {
816             t.printStackTrace ();
817             return null;
818         }
819         return (Set) result;
820     }
821
822     public JahiaDOMObject getGroupsAsDOM (int siteID)
823             throws org.jahia.exceptions.JahiaException {
824         /**@todo provider in superclass**/
825         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
826         parameterTypes[0] = Integer.TYPE;
827         Object JavaDoc[] args = new Object JavaDoc[1];
828         args[0] = new Integer JavaDoc (siteID);
829
830         Object JavaDoc result = null;
831
832         try {
833             result = routeCall ("getGroupsAsDOM",
834                     parameterTypes,
835                     null,
836                     ROUTE_CALL_ALL, //provider not specified in superclass
837
null,
838                     false,
839                     null,
840                     args);
841         } catch (Throwable JavaDoc t) {
842             t.printStackTrace ();
843             return null;
844         }
845         return (JahiaDOMObject) result;
846
847     }
848
849     public JahiaDOMObject getGroupPropsAsDOM (int siteID)
850             throws org.jahia.exceptions.JahiaException {
851         /**@todo provider in superclass**/
852         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
853         parameterTypes[0] = Integer.TYPE;
854         Object JavaDoc[] args = new Object JavaDoc[1];
855         args[0] = new Integer JavaDoc (siteID);
856
857         Object JavaDoc result = null;
858         try {
859             result = routeCall ("getGroupPropsAsDOM",
860                     parameterTypes,
861                     null,
862                     ROUTE_CALL_ALL, //provider not specified in superclass
863
null,
864                     false,
865                     null,
866                     args);
867         } catch (Throwable JavaDoc t) {
868             t.printStackTrace ();
869             return null;
870         }
871         return (JahiaDOMObject) result;
872
873     }
874
875     public JahiaGroup lookupGroup (int siteID, String JavaDoc name) {
876         Class JavaDoc[] parameterTypes = new Class JavaDoc[2];
877         parameterTypes[0] = Integer.TYPE;
878         parameterTypes[1] = stringClass;
879         Object JavaDoc[] args = new Object JavaDoc[2];
880         args[0] = new Integer JavaDoc (siteID);
881         args[1] = name;
882
883         Properties groupProperties = new Properties ();
884         groupProperties.setProperty ("group.name", name);
885
886         Object JavaDoc result = null;
887         try {
888             result = routeCall ("lookupGroup",
889                     parameterTypes,
890                     groupProperties,
891                     ROUTE_CALL_ALL_UNTIL_SUCCESS,
892                     null,
893                     false,
894                     null,
895                     args);
896         } catch (Throwable JavaDoc t) {
897             t.printStackTrace ();
898             return null;
899         }
900         return (JahiaGroup) result;
901
902     }
903
904     public JahiaGroup createGroup (int siteID, String JavaDoc name, Properties properties) {
905         JahiaGroup group = null;
906         Class JavaDoc[] parameterTypes = new Class JavaDoc[3];
907         parameterTypes[0] = Integer.TYPE;
908         parameterTypes[1] = stringClass;
909         parameterTypes[2] = propertiesClass;
910         Object JavaDoc[] args = new Object JavaDoc[3];
911         args[0] = new Integer JavaDoc (siteID);
912         args[1] = name;
913         args[2] = properties;
914         Object JavaDoc result = null;
915         try {
916             result = routeCall ("createGroup",
917                     parameterTypes,
918                     properties,
919                     ROUTE_CALL_ONE,
920                     null,
921                     false,
922                     null,
923                     args);
924         } catch (Throwable JavaDoc t) {
925             t.printStackTrace ();
926             return null;
927         }
928         group = (JahiaGroup) result;
929         return group;
930     }
931
932     /**
933      * This method indicates that any internal cache for a provider should be
934      * updated because the value has changed and needs to be transmitted to the
935      * other nodes in a clustering environment.
936      * @param jahiaGroup JahiaGroup the group to be updated in the cache.
937      */

938     public void updateCache(JahiaGroup jahiaGroup) {
939         Class JavaDoc[] parameterTypes = new Class JavaDoc[1];
940         parameterTypes[0] = jahiaGroup.getClass ().getSuperclass ();
941         Object JavaDoc[] args = new Object JavaDoc[1];
942         args[0] = jahiaGroup;
943         Object JavaDoc result = null;
944         try {
945             result = routeCall ("updateCache",
946                     parameterTypes,
947                     jahiaGroup.getProperties (),
948                     ROUTE_CALL_ONE,
949                     null,
950                     false,
951                     null,
952                     args);
953         } catch (Throwable JavaDoc t) {
954             t.printStackTrace ();
955         }
956     }
957
958 //Route call from this point..
959
/**
960      * Routes the calls to a set of providers. This method is the center of this
961      * routing service. It allows for a very flexible way of calling back-end
962      * providers by leaving the routing decision until runtime and making it
963      * therefore very configurable.
964      *
965      * @param methodName the name of the method to invoke
966      * @param parameterTypes an array of Class objects that specify the
967      * type of the parameters for the method. This
968      * is necessary since we cannot determine this
969      * at runtime because of primitive type
970      * encapsulation. Therefore for primitive types
971      * these must be specified here such as
972      * Integer.TYPE, Boolean.TYPE, etc in order to
973      * look up the correct method
974      * @param userProperties the properties that will be used to evaluate
975      * the provider to call. This is defined by a
976      * set of rules.
977      * @param typeOfCall the type of call, values are ROUTE_CALL_ONE
978      * which means only one provider will be called
979      * based on rule evaluation, ROUTE_CALL_ALL
980      * means the method will be invoked for all the
981      * providers, ROUTE_CALL_ALL_UNTIL_SUCCESS means
982      * all the providers methods will be called
983      * until the first one returns a success. A
984      * success depends on the return type. For int
985      * it means all values exception -1, for
986      * booleans it means a "true" value, and for all
987      * other object it means a non null value
988      * @param providersToCall a vector of String objects that specify a
989      * list of providers to call in order. There may
990      * be zero, one or multiple providers. This
991      * parameter bypasses the criteria evaluation
992      * so be careful how you use it. It also uses
993      * the typeOfCall parameter to define the type
994      * of call, so make sure you use combinations
995      * that correspond, such as ROUTE_CALL_ONE and
996      * a single entry, ROUTE_CALL_ALL and at least
997      * one or more entries, or
998      * ROUTE_CALL_ALL_UNTIL_SUCCESS with one or
999      * more. Passing an empty vector or null will
1000     * disable the forcing of the list of providers.
1001     * @param useDifferentInstance set to true if you want to use the specified
1002     * instance passed in parameter. If false it
1003     * will use the provider's singleton instance.
1004     * This is mostly useful if you want to be able
1005     * to call static instances, or do something
1006     * very special.
1007     * @param instance the instance of the object we're using for
1008     * this call. May be null if we are calling a
1009     * static method.
1010     * @param args an array of Objects which contain the
1011     * parameters. The primitive types must be
1012     * encapsulated into objects.
1013     *
1014     * @return the object returned. In the case of the ROUTE_CALL_ALL this
1015     * returns a Vector containing all the results of the calls. If there were
1016     * any errors this is very likely to return null.
1017     *
1018     * @throws Throwable this exception is actually an exception that has been
1019     * generated within the called method, and that's just being passed upwards.
1020     */

1021    private Object JavaDoc routeCall (String JavaDoc methodName,
1022                              Class JavaDoc[] parameterTypes,
1023                              Properties groupProperties,
1024                              int typeOfCall,
1025                              Vector providersToCall,
1026                              boolean useDifferentInstance,
1027                              Object JavaDoc instance,
1028                              Object JavaDoc[] args)
1029            throws Throwable JavaDoc {
1030
1031        switch (typeOfCall) {
1032            case ROUTE_CALL_ONE:
1033                {
1034                    // we're calling only one of the provider, we must determine
1035
// which one by using the user properties matched against the
1036
// criteria , or by using the providersToCall parameter
1037
JahiaGroupManagerProvider providerInstance = null;
1038                    if (providersToCall != null) {
1039                        if (providersToCall.size () >= 1) {
1040                            Object JavaDoc providerItem = providersToCall.elementAt (0);
1041                            if (providerItem instanceof String JavaDoc) {
1042                                String JavaDoc providerKey = (String JavaDoc) providerItem;
1043                                GroupManagerProviderBean aProvider = (GroupManagerProviderBean) providersTable.get (
1044                                        providerKey);
1045                                providerInstance = aProvider.getInstance ();
1046                            }
1047                        }
1048                    }
1049                    if (providerInstance == null) {
1050                        Enumeration criteriaEnum = criteriaList.elements ();
1051                        while (criteriaEnum.hasMoreElements ()) {
1052                            GroupRoutingCriteria curCriteria = (GroupRoutingCriteria) criteriaEnum.nextElement ();
1053                            if (curCriteria.matchesValues (groupProperties)) {
1054                                String JavaDoc providerKey = curCriteria.getDestination ();
1055                                GroupManagerProviderBean curProvider = (GroupManagerProviderBean) providersTable.get (
1056                                        providerKey);
1057                                providerInstance = curProvider.getInstance ();
1058                                break;
1059                            }
1060                        }
1061                    }
1062                    if (providerInstance == null) {
1063                        // fallback, we must find at least one provider to call.
1064
providerInstance = defaultProviderInstance;
1065                        if (providerInstance == null) {
1066                            // what no default provider ?? exit immediately.
1067
return null;
1068                        }
1069                    }
1070                    Object JavaDoc result = null;
1071                    
1072                    try {
1073                        Method JavaDoc methodToInvoke =
1074                                providerInstance.getClass ().getMethod (methodName,
1075                                        parameterTypes);
1076                        if (useDifferentInstance) {
1077                            result = methodToInvoke.invoke (instance, args);
1078                        } else {
1079                            result = methodToInvoke.invoke (providerInstance, args);
1080                        }
1081                    } catch (NoSuchMethodException JavaDoc nsme) {
1082                        nsme.printStackTrace ();
1083                        result = null;
1084                    } catch (InvocationTargetException JavaDoc ite) {
1085                        ite.printStackTrace ();
1086                        result = null;
1087                        throw ite.getTargetException ();
1088                    } catch (IllegalAccessException JavaDoc iae) {
1089                        iae.printStackTrace ();
1090                        result = null;
1091                    }
1092
1093                    return result;
1094               }
1095
1096            case ROUTE_CALL_ALL:
1097                {
1098                    // we're calling all the providers
1099
Vector results = new Vector ();
1100
1101                    Enumeration providerEnum = getProvidersToCall (providersToCall).elements ();
1102
1103                    while (providerEnum.hasMoreElements ()) {
1104                        JahiaGroupManagerProvider curProvider =
1105                                (JahiaGroupManagerProvider) providerEnum.nextElement ();
1106                        Object JavaDoc result = null;
1107                    
1108                        try {
1109                            Method JavaDoc methodToInvoke =
1110                                    curProvider.getClass ().getMethod (methodName,
1111                                            parameterTypes);
1112                            if (useDifferentInstance) {
1113                                result = methodToInvoke.invoke (instance, args);
1114                            } else {
1115                                result = methodToInvoke.invoke (curProvider, args);
1116                            }
1117                            results.add (result);
1118                        } catch (NoSuchMethodException JavaDoc nsme) {
1119                            nsme.printStackTrace ();
1120                            results = null;
1121                            break;
1122                        } catch (InvocationTargetException JavaDoc ite) {
1123                            ite.printStackTrace ();
1124                            results = null;
1125                            throw ite.getTargetException ();
1126                        } catch (IllegalAccessException JavaDoc iae) {
1127                            iae.printStackTrace ();
1128                            results = null;
1129                            break;
1130                        }
1131                    }
1132                    
1133                    return results;
1134                }
1135
1136            case ROUTE_CALL_ALL_UNTIL_SUCCESS:
1137                {
1138                    // we're calling the providers in order, until one returns
1139
// a success condition (to be defined)
1140

1141                    Enumeration providerEnum = getProvidersToCall (providersToCall).elements ();
1142                    Object JavaDoc result = null;
1143                    while (providerEnum.hasMoreElements ()) {
1144                        JahiaGroupManagerProvider curProvider =
1145                                (JahiaGroupManagerProvider) providerEnum.nextElement ();
1146                                
1147                        try {
1148
1149                            Method JavaDoc methodToInvoke =
1150                                    curProvider.getClass ().getMethod (methodName,
1151                                            parameterTypes);
1152                            if (useDifferentInstance) {
1153                                result = methodToInvoke.invoke (instance, args);
1154                            } else {
1155                                result = methodToInvoke.invoke (curProvider, args);
1156                            }
1157
1158                            Class JavaDoc returnType = methodToInvoke.getReturnType ();
1159                            // conditions for success are :
1160
// for int, all values except -1
1161
// for booleans, true value
1162
// for all other object, a non null value
1163
if (returnType.isPrimitive ()) {
1164                                if ("int".equals (returnType.getName ())) {
1165                                    Integer JavaDoc resultInt = (Integer JavaDoc) result;
1166                                    if (resultInt.intValue () != -1) {
1167                                        return result;
1168                                    }
1169                                } else if ("boolean".equals (returnType.getName ())) {
1170                                    Boolean JavaDoc resultBool = (Boolean JavaDoc) result;
1171                                    if (resultBool.booleanValue ()) {
1172                                        return result;
1173                                    }
1174                                } else {
1175                                    if (result != null) {
1176                                        return result;
1177                                    }
1178                                }
1179                            } else {
1180                                if (result != null) {
1181                                    return result;
1182                                }
1183                            }
1184                        } catch (NoSuchMethodException JavaDoc nsme) {
1185                            nsme.printStackTrace ();
1186                            result = null;
1187                        } catch (InvocationTargetException JavaDoc ite) {
1188                            ite.printStackTrace ();
1189                            result = null;
1190                            throw ite.getTargetException ();
1191                        } catch (IllegalAccessException JavaDoc iae) {
1192                            iae.printStackTrace ();
1193                            result = null;
1194                        }
1195                    }
1196                    
1197                    return result;
1198                }
1199        }
1200        return null;
1201    }
1202                        
1203    /**
1204     * Returns a vector of provider instances to call depending on the specified
1205     * list of provider names.
1206     *
1207     * @param providersToCall a vector containing String that contain names
1208     * of the providers to call. If no list is specified, the result list is
1209     * composed of the internal list of providers
1210     *
1211     * @return a Vector of provider instances, may be empty if none was found,
1212     * but never null. If no name list is specified, returns the internal list
1213     * of providers
1214     */

1215    private Vector getProvidersToCall (Vector providersToCall) {
1216        // we use this temporary vector in order to avoid having twice
1217
// the same code for the dispatching...
1218
Vector tmpProviderInstances = new Vector ();
1219
1220        if (providersToCall != null) {
1221            if (providersToCall.size () >= 1) {
1222                Enumeration providerEnum = providersToCall.elements ();
1223                while (providerEnum.hasMoreElements ()) {
1224                    Object JavaDoc curProviderEntry = providerEnum.nextElement ();
1225                    if (curProviderEntry instanceof String JavaDoc) {
1226                        String JavaDoc curProviderKey = (String JavaDoc) curProviderEntry;
1227                        GroupManagerProviderBean curProviderBean =
1228                                (GroupManagerProviderBean) providersTable.get (curProviderKey);
1229                        JahiaGroupManagerProvider curProviderInstance = curProviderBean.getInstance ();
1230                        if (curProviderInstance != null) {
1231                            tmpProviderInstances.add (curProviderInstance);
1232                        }
1233                    }
1234                }
1235            }
1236        }
1237
1238        if (tmpProviderInstances.size () == 0) {
1239            Iterator providerIter = sortedProviders.iterator ();
1240            while (providerIter.hasNext ()) {
1241                GroupManagerProviderBean curBean = (GroupManagerProviderBean) providerIter.next ();
1242                JahiaGroupManagerProvider curProvider = curBean.getInstance ();
1243                tmpProviderInstances.add (curProvider);
1244            }
1245        }
1246
1247        return tmpProviderInstances;
1248    }
1249
1250    private boolean loadConfiguration (String JavaDoc configFileName)
1251            throws JahiaException {
1252
1253        Document documentRoot = null;
1254
1255        try {
1256
1257            DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance ();
1258            // dfactory.setValidating(true); // create only parsers that are validating
1259
DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder ();
1260
1261            FileInputStream JavaDoc configStream = new FileInputStream JavaDoc (configFileName);
1262            documentRoot = docBuilder.parse (configStream);
1263            documentRoot.normalize (); // clean up DOM tree a little
1264

1265        } catch (Throwable JavaDoc t) {
1266            throw new JahiaException ("JahiaGroupManagerRoutingService.loadConfiguration",
1267                    "Exception while loading configuration",
1268                    JahiaException.CRITICAL_SEVERITY,
1269                    JahiaException.CONFIG_ERROR,
1270                    t);
1271        }
1272
1273        Element rootTag = documentRoot.getDocumentElement ();
1274        if (!rootTag.getTagName ().equals (JahiaGroupManagerRoutingService.XML_ROOT_TAG)) {
1275            throw new JahiaException ("JahiaGroupManagerRoutingService.loadConfiguration",
1276                    "Invalid root tag in configuration. Expected " + JahiaGroupManagerRoutingService.XML_ROOT_TAG + ", found " + rootTag.getTagName (),
1277                    JahiaException.CRITICAL_SEVERITY,
1278                    JahiaException.CONFIG_ERROR);
1279        }
1280
1281        this.providersTable = getProvidersFromDOM (rootTag);
1282        this.sortedProviders.addAll (providersTable.values ());
1283        this.criteriaList = getCriteriasFromDOM (rootTag);
1284
1285    // EP: 2004/28/06 : parsing new elements for servers specifications
1286
this.serversTable = getServersFromDOM (rootTag);
1287    
1288        return true;
1289    }
1290
1291    private Map getProvidersFromDOM (Element rootElement)
1292            throws JahiaException {
1293        Map result = new HashMap ();
1294        // we have to do copies because we cannot do nested node list parsing...
1295
NodeList providerElementList = rootElement.getElementsByTagName (
1296                JahiaGroupManagerRoutingService.XML_PROVIDER_ROOT_TAG);
1297        Vector providerElements = new Vector ();
1298        for (int i = 0; i < providerElementList.getLength (); i++) {
1299            providerElements.add ((Element) providerElementList.item (i));
1300        }
1301
1302        Enumeration providerEnum = providerElements.elements ();
1303        int providerCounter = 0;
1304        while (providerEnum.hasMoreElements ()) {
1305            Element curProviderElement = (Element) providerEnum.nextElement ();
1306            GroupManagerProviderBean providerBean = getProviderFromDOM (curProviderElement,
1307                    providerCounter);
1308            if (providerBean != null) {
1309                if (providerBean.getIsDefault ()) {
1310                    /** @todo FIXME : this is a class variable set, not very
1311                     * elegant... how can we avoid this ?
1312                     */

1313                    this.defaultProviderInstance = providerBean.getInstance ();
1314                }
1315                result.put (providerBean.getKey (), providerBean);
1316            }
1317            providerCounter++;
1318        }
1319
1320        return result;
1321    }
1322
1323    /**
1324     * EP: 2004/28/06
1325     * Returns the Map of all declared servers organized by provider and sorted by priority.
1326     */

1327    private Map getServersFromDOM (Element rootElement)
1328            throws JahiaException {
1329        Map result = new HashMap ();
1330        // we have to do copies because we cannot do nested node list parsing...
1331
NodeList serversList = rootElement.getElementsByTagName (XML_SERVER_ROOT_TAG);
1332
1333        for (int i = 0; i < serversList.getLength (); i++) {
1334            ServerBean sb = getServerFromDOM((Element)serversList.item(i));
1335            TreeSet servers = (TreeSet)result.get(sb.getProvider());
1336            if (servers == null) {
1337                servers = new TreeSet (new Comparator () {
1338                public int compare (Object JavaDoc o1, Object JavaDoc o2) {
1339                    return ((ServerBean) o1).getPriority () - ((ServerBean) o2).getPriority ();
1340                }
1341            });
1342            result.put(sb.getProvider(), servers);
1343        }
1344        servers.add(sb);
1345        }
1346
1347    logger.debug("group servers map :: " + result);
1348
1349        return result;
1350    }
1351
1352    private ServerBean getServerFromDOM (Element providerElement)
1353            throws JahiaException {
1354
1355        String JavaDoc provider = getElementValue (XML_SERVER_DESTINATION_TAG, providerElement);
1356        String JavaDoc priority = getElementValue (XML_SERVER_PRIORITY_TAG, providerElement);
1357        String JavaDoc maxreconn = getElementValue (XML_SERVER_MAX_RECONN_TAG, providerElement);
1358        ServerBean result = new ServerBean(priority, provider, maxreconn);
1359        
1360        result.setFactoryName(getElementValue (XML_SERVER_FACTORY_TAG, providerElement));
1361        result.setServerUrl(getElementValue (XML_SERVER_URL_TAG, providerElement));
1362        result.setUserName(getElementValue (XML_SERVER_USERNAME_TAG, providerElement));
1363        result.setAuthenticationMode(getElementValue (XML_SERVER_AUTH_MODE_TAG, providerElement));
1364        result.setUserPassword(getElementValue (XML_SERVER_PASSWORD_TAG, providerElement));
1365        result.setReferralBehavior(getElementValue (XML_SERVER_REFERRAL, providerElement));
1366    
1367        return result;
1368    }
1369    
1370    /**
1371     * Returns a RoutingCriteria from a XML DOM. Format :
1372     * <criteria>
1373     * <key>jahia_db_criteria</key>
1374     * <description>
1375     * ...
1376     * </description>
1377     * <condition propertyName="group.key" propertyValue="{jahia_db}*"/>
1378     * <condition ... />
1379     * <provider-destination>jahia_db</provider-destination>
1380     * </criteria>
1381     *
1382     * @param criteriaElement the Element containing the tag for a criteria
1383     * DOM.
1384     *
1385     * @return GroupManagerProviderBean a bean constructed by loading the values
1386     * from the XML DOM.
1387     *
1388     * @throws JahiaException
1389     */

1390    private GroupRoutingCriteria getCriteriaFromDOM (Element criteriaElement)
1391            throws JahiaException {
1392
1393        GroupRoutingCriteria result = null;
1394
1395        if (!criteriaElement.getTagName ().equals (
1396                JahiaGroupManagerRoutingService.XML_CRITERIA_ROOT_TAG)) {
1397            throw new JahiaException ("JahiaGroupManagerRoutingService.getProviderFromDOM",
1398                    "May not call getCriteriaFromDOM on elements other than " + JahiaGroupManagerRoutingService.XML_CRITERIA_ROOT_TAG,
1399                    JahiaException.CRITICAL_SEVERITY,
1400                    JahiaException.CONFIG_ERROR);
1401        }
1402
1403        String JavaDoc key = getElementValue (JahiaGroupManagerRoutingService.XML_CRITERIA_KEY_TAG,
1404                criteriaElement);
1405        String JavaDoc description = getElementValue (
1406                JahiaGroupManagerRoutingService.XML_CRITERIA_DESCRIPTION_TAG, criteriaElement);
1407        Properties conditions = getConditionsFromDOM (criteriaElement);
1408        String JavaDoc providerDestination = getElementValue (
1409                JahiaGroupManagerRoutingService.XML_CRITERIA_DESTINATION_TAG, criteriaElement);
1410
1411        result = new GroupRoutingCriteria (key, description, conditions, providerDestination);
1412        return result;
1413    }
1414
1415    private Vector getCriteriasFromDOM (Element rootElement)
1416            throws JahiaException {
1417        Vector result = new Vector ();
1418        // we have to do copies because we cannot do nested node list parsing...
1419
NodeList criteriaElementList = rootElement.getElementsByTagName (
1420                JahiaGroupManagerRoutingService.XML_CRITERIA_ROOT_TAG);
1421        Vector criteriaElements = new Vector ();
1422        for (int i = 0; i < criteriaElementList.getLength (); i++) {
1423            criteriaElements.add ((Element) criteriaElementList.item (i));
1424        }
1425
1426        Enumeration criteriaEnum = criteriaElements.elements ();
1427        while (criteriaEnum.hasMoreElements ()) {
1428            Element curCriteriaElement = (Element) criteriaEnum.nextElement ();
1429            GroupRoutingCriteria curCriteria = getCriteriaFromDOM (curCriteriaElement);
1430            if (curCriteria != null) {
1431                result.add (curCriteria);
1432            }
1433        }
1434
1435        return result;
1436    }
1437
1438    /**
1439     * Returns a GroupManagerProviderBean from a XML DOM. Format :
1440     * <provider>
1441     * <key>jahia_db</key>
1442     * <class>org.jahia.services.usermanager.JahiaGroupManagerDBProvider</class>
1443     * <title>Jahia Group Manager Database Provider</title>
1444     * <description>
1445     * ...
1446     * </description>
1447     * <is-default>true</is-default>
1448     * </provider>
1449     *
1450     * @param providerElement the Element containing the tag for a provider
1451     * DOM.
1452     *
1453     * @return GroupManagerProviderBean a bean constructed by loading the values
1454     * from the XML DOM.
1455     *
1456     * @throws JahiaException
1457     */

1458    private GroupManagerProviderBean getProviderFromDOM (Element providerElement,
1459                                                         int providerCounter)
1460            throws JahiaException {
1461
1462        if (!providerElement.getTagName ().equals (
1463                JahiaGroupManagerRoutingService.XML_PROVIDER_ROOT_TAG)) {
1464            throw new JahiaException ("JahiaGroupManagerRoutingService.getProviderFromDOM",
1465                    "May not call getProviderFromDOM on elements other than " + JahiaGroupManagerRoutingService.XML_PROVIDER_ROOT_TAG,
1466                    JahiaException.CRITICAL_SEVERITY,
1467                    JahiaException.CONFIG_ERROR);
1468        }
1469
1470        GroupManagerProviderBean result = null;
1471        String JavaDoc key = getElementValue (JahiaGroupManagerRoutingService.XML_PROVIDER_KEY_TAG,
1472                providerElement);
1473        String JavaDoc className = getElementValue (
1474                JahiaGroupManagerRoutingService.XML_PROVIDER_CLASS_TAG, providerElement);
1475        String JavaDoc title = getElementValue (JahiaGroupManagerRoutingService.XML_PROVIDER_TITLE_TAG,
1476                providerElement);
1477        String JavaDoc description = getElementValue (
1478                JahiaGroupManagerRoutingService.XML_PROVIDER_DESCRIPTION_TAG, providerElement);
1479        String JavaDoc isDefaultStr = getElementValue (
1480                JahiaGroupManagerRoutingService.XML_PROVIDER_ISDEFAULT_TAG, providerElement);
1481        boolean isDefault = ("true".equals (isDefaultStr));
1482
1483        boolean isReadOnly = false;
1484        try {
1485            // this tag is currently optional, so if we don't find it we set it to false by default
1486
String JavaDoc isReadOnlyStr = getElementValue (
1487                    JahiaGroupManagerRoutingService.XML_PROVIDER_ISREADONLY_TAG,
1488                    providerElement);
1489            isReadOnly = ("true".equals (isReadOnlyStr));
1490        } catch (JahiaException je) {
1491            logger.error ("Warning : error loading tag <" +
1492                    JahiaGroupManagerRoutingService.XML_PROVIDER_ISREADONLY_TAG +
1493                    ">. Please update your configuration file to include it !");
1494            isReadOnly = false;
1495        }
1496
1497        int priority = 99;
1498        try {
1499            String JavaDoc priorityStr = getElementValue (
1500                    JahiaGroupManagerRoutingService.XML_PROVIDER_PRIORITY_TAG, providerElement);
1501            priority = Integer.parseInt (priorityStr);
1502        } catch (Exception JavaDoc e) {
1503            // optional parameter, ignore exception
1504
priority = providerCounter;
1505        }
1506
1507        result =
1508                new GroupManagerProviderBean (key, className, title, description, isDefault,
1509                        isReadOnly, priority);
1510        return result;
1511    }
1512
1513    private String JavaDoc getElementTextValue (Element rootElement)
1514            throws JahiaException {
1515        NodeList childNodes = rootElement.getChildNodes ();
1516        StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
1517        for (int i = 0; i < childNodes.getLength (); i++) {
1518            if (childNodes.item (i).getNodeType () == Node.TEXT_NODE) {
1519                result.append (childNodes.item (i).getNodeValue ());
1520            } else {
1521                throw new JahiaException (
1522                        "JahiaGroupManagerRoutingService.getElementTextValue",
1523                        "Only text values are allowed in tag " + rootElement.getTagName (),
1524                        JahiaException.CRITICAL_SEVERITY,
1525                        JahiaException.CONFIG_ERROR);
1526            }
1527        }
1528        return result.toString ();
1529    }
1530
1531    private String JavaDoc getElementValue (String JavaDoc tagName, Element parentElement)
1532            throws JahiaException {
1533        NodeList foundElements = parentElement.getElementsByTagName (tagName);
1534        if (foundElements.getLength () == 1) {
1535            return getElementTextValue ((Element) foundElements.item (0));
1536        } else {
1537            if (foundElements.getLength () != 0) {
1538                throw new JahiaException ("JahiaGroupManagerRoutingService.getElementValue",
1539                        "Only one <" + tagName + "> is allowed in a <" + parentElement.getTagName () + "> tag.",
1540                        JahiaException.CRITICAL_SEVERITY,
1541                        JahiaException.CONFIG_ERROR);
1542            } else {
1543                throw new JahiaException ("JahiaGroupManagerRoutingService.getElementValue",
1544                        "Couldn't find tag <" + tagName + "> in <" + parentElement.getTagName () + "> tag.",
1545                        JahiaException.CRITICAL_SEVERITY,
1546                        JahiaException.CONFIG_ERROR);
1547            }
1548        }
1549    }
1550
1551    private Properties getConditionsFromDOM (Element criteriaElement)
1552            throws JahiaException {
1553        Properties result = new Properties ();
1554
1555        NodeList criteriaElementList = criteriaElement.getElementsByTagName (
1556                JahiaGroupManagerRoutingService.XML_CRITERIA_CONDITION_TAG);
1557        for (int i = 0; i < criteriaElementList.getLength (); i++) {
1558            Element curConditionElement = (Element) criteriaElementList.item (i);
1559            NamedNodeMap attrib = curConditionElement.getAttributes ();
1560            Node propertyNameAttrib = attrib.getNamedItem (
1561                    JahiaGroupManagerRoutingService.XML_CRITERIA_CONDITION_PROPNAME_ATTR);
1562            if (propertyNameAttrib == null) {
1563                throw new JahiaException (
1564                        "JahiaGroupManagerRoutingService.getConditionsFromDOM",
1565                        JahiaGroupManagerRoutingService.XML_CRITERIA_CONDITION_PROPNAME_ATTR + " attribute not found on tag " + curConditionElement.getTagName (),
1566                        JahiaException.CRITICAL_SEVERITY,
1567                        JahiaException.CONFIG_ERROR);
1568            }
1569            String JavaDoc propertyName = propertyNameAttrib.getNodeValue ();
1570            Node propertyValueAttrib = attrib.getNamedItem (
1571                    JahiaGroupManagerRoutingService.XML_CRITERIA_CONDITION_PROPVALUE_ATTR);
1572            if (propertyValueAttrib == null) {
1573                throw new JahiaException (
1574                        "JahiaGroupManagerRoutingService.getConditionsFromDOM",
1575                        JahiaGroupManagerRoutingService.XML_CRITERIA_CONDITION_PROPVALUE_ATTR + " attribute not found on tag " + curConditionElement.getTagName (),
1576                        JahiaException.CRITICAL_SEVERITY,
1577                        JahiaException.CONFIG_ERROR);
1578            }
1579            String JavaDoc propertyValue = propertyValueAttrib.getNodeValue ();
1580            result.setProperty (propertyName, propertyValue);
1581        }
1582        return result;
1583    }
1584
1585
1586}
1587
Popular Tags