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 ; 11 import javax.xml.parsers.DocumentBuilderFactory ; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.util.*; 17 18 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 45 private static final String JAHIAUSER_INTERFACE_NAME = "org.jahia.services.usermanager.JahiaUser"; 46 private static final String XML_ROOT_TAG = "router-config"; 47 48 private static final String XML_PROVIDER_ROOT_TAG = "provider"; 49 private static final String XML_PROVIDER_KEY_TAG = "key"; 50 private static final String XML_PROVIDER_CLASS_TAG = "class"; 51 private static final String XML_PROVIDER_TITLE_TAG = "title"; 52 private static final String XML_PROVIDER_DESCRIPTION_TAG = "description"; 53 private static final String XML_PROVIDER_ISDEFAULT_TAG = "is-default"; 54 private static final String XML_PROVIDER_ISREADONLY_TAG = "is-readonly"; 55 private static final String XML_PROVIDER_PRIORITY_TAG = "priority"; 56 57 private static final String XML_CRITERIA_ROOT_TAG = "criteria"; 58 private static final String XML_CRITERIA_KEY_TAG = "key"; 59 private static final String XML_CRITERIA_DESCRIPTION_TAG = "description"; 60 private static final String XML_CRITERIA_CONDITION_TAG = "condition"; 61 private static final String XML_CRITERIA_DESTINATION_TAG = "provider-destination"; 62 private static final String XML_CRITERIA_CONDITION_PROPNAME_ATTR = "propertyName"; 63 private static final String XML_CRITERIA_CONDITION_PROPVALUE_ATTR = "propertyValue"; 64 65 69 private static final String XML_SERVER_ROOT_TAG = "server"; 70 private static final String XML_SERVER_URL_TAG = "url"; 71 private static final String XML_SERVER_USERNAME_TAG = "username"; 72 private static final String XML_SERVER_PASSWORD_TAG = "password"; 73 private static final String XML_SERVER_FACTORY_TAG = "factory"; 74 private static final String XML_SERVER_AUTH_MODE_TAG = "authentication-mode"; 75 private static final String XML_SERVER_DESTINATION_TAG = "provider-destination"; 76 private static final String XML_SERVER_PRIORITY_TAG = "priority"; 77 private static final String XML_SERVER_MAX_RECONN_TAG = "max-reconnection"; 78 private static final String XML_SERVER_REFERRAL = "referral"; 79 80 private Map serversTable = null; 81 83 private static final String CONFIG_PATH = File.separator + "services" + 84 File.separator + "usermanager"; 85 private static final String 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 stringClass = null; 94 private Class propertiesClass = null; 95 96 97 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 serversTable = new HashMap (); 117 118 sortedProviders = new TreeSet (new Comparator () { 119 public int compare (Object o1, Object o2) { 120 return ((UserManagerProviderBean) o1).getPriority () - ((UserManagerProviderBean) o2).getPriority (); 121 } 122 }); 123 124 129 133 136 stringClass = (new String ()).getClass (); 137 propertiesClass = (new Properties ()).getClass (); 138 139 } 140 141 149 public Vector getProviderList () { 150 Vector result = new Vector (sortedProviders); 151 158 return result; 159 } 160 161 public TreeSet getServerList (String 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 curProviderKey = (String ) 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 name, 195 String password, 196 String userKey, 197 int siteID, 198 Properties properties) { 199 JahiaUser user = null; 200 Class [] parameterTypes = new Class [5]; 201 parameterTypes[0] = stringClass; 202 parameterTypes[1] = stringClass; 203 parameterTypes[2] = stringClass; 204 parameterTypes[3] = Integer.TYPE; 205 parameterTypes[4] = propertiesClass; 206 Object [] args = new Object [5]; 207 args[0] = name; 208 args[1] = password; 209 args[2] = userKey; 210 args[3] = new Integer (siteID); 211 args[4] = properties; 212 Object 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 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 [] parameterTypes = new Class [1]; 235 236 Class [] interfaces = user.getClass ().getInterfaces (); 238 Class 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 [] args = new Object [1]; 253 args[0] = user; 254 Object 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 t) { 265 t.printStackTrace (); 266 return false; 267 } 268 Boolean resultBool = (Boolean ) result; 269 return resultBool.booleanValue (); 270 } 271 272 public int getNbUsers () 273 throws org.jahia.exceptions.JahiaException { 274 Object 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 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 curResult = (Integer ) 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 [] parameterTypes = new Class [1]; 301 parameterTypes[0] = Integer.TYPE; 302 Object [] args = new Object [1]; 303 args[0] = new Integer (siteID); 304 Object 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 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 curResult = (Integer ) resultEnum.nextElement (); 323 nbUsers += curResult.intValue (); 324 } 325 return nbUsers; 326 } 327 328 public String getUniqueKey () { 329 Object 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 t) { 340 t.printStackTrace (); 341 return null; 342 } 343 return (String ) result; 344 } 345 346 public Vector getUserList () { 347 Vector userList = new Vector (); 348 349 Object 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 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 [] parameterTypes = new Class [1]; 376 parameterTypes[0] = Integer.TYPE; 377 Object [] args = new Object [1]; 378 args[0] = new Integer (siteID); 379 380 Object 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 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 [] parameterTypes = new Class [1]; 409 parameterTypes[0] = Integer.TYPE; 410 Object [] args = new Object [1]; 411 args[0] = new Integer (siteID); 412 413 Object 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 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 providerName, int siteID) 437 throws org.jahia.exceptions.JahiaException { 438 439 Class [] parameterTypes = new Class [1]; 440 parameterTypes[0] = Integer.TYPE; 441 Object [] args = new Object [1]; 442 args[0] = new Integer (siteID); 443 444 Object 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 t) { 457 t.printStackTrace (); 458 return null; 459 } 460 return (JahiaDOMObject) result; 461 } 462 463 public JahiaDOMObject getUserPropsAsDOM (String providerName, int siteID) 464 throws org.jahia.exceptions.JahiaException { 465 Class [] parameterTypes = new Class [1]; 466 parameterTypes[0] = Integer.TYPE; 467 Object [] args = new Object [1]; 468 args[0] = new Integer (siteID); 469 470 Object 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 t) { 483 t.printStackTrace (); 484 return null; 485 } 486 return (JahiaDOMObject) result; 487 } 488 489 497 public boolean login (String userKey, String userPassword) { 498 Class [] parameterTypes = new Class [2]; 499 parameterTypes[0] = stringClass; 500 parameterTypes[1] = stringClass; 501 Object [] args = new Object [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 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 t) { 520 t.printStackTrace (); 521 return false; 522 } 523 Boolean resultBool = (Boolean ) result; 524 return resultBool.booleanValue (); 525 } 526 527 public JahiaUser lookupUser (String userKey) { 528 Class [] parameterTypes = new Class [1]; 529 parameterTypes[0] = stringClass; 530 Object [] args = new Object [1]; 531 args[0] = userKey; 532 533 Properties userProperties = new Properties (); 534 userProperties.setProperty ("user.key", userKey); 535 536 Object 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 t) { 547 t.printStackTrace (); 548 return null; 549 } 550 return (JahiaUser) result; 551 } 552 553 public JahiaUser lookupUser (int siteID, String name) { 554 Class [] parameterTypes = new Class [2]; 555 parameterTypes[0] = Integer.TYPE; 556 parameterTypes[1] = stringClass; 557 Object [] args = new Object [2]; 558 args[0] = new Integer (siteID); 559 args[1] = name; 560 561 Properties userProperties = new Properties (); 562 userProperties.setProperty ("user.name", name); 563 564 Object 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 t) { 575 t.printStackTrace (); 576 return null; 577 } 578 return (JahiaUser) result; 579 } 580 581 public boolean userExists (int siteID, String name) { 582 Class [] parameterTypes = new Class [2]; 583 parameterTypes[0] = Integer.TYPE; 584 parameterTypes[1] = stringClass; 585 Object [] args = new Object [2]; 586 args[0] = new Integer (siteID); 587 args[1] = name; 588 589 Properties userProperties = new Properties (); 590 userProperties.setProperty ("user.name", name); 591 592 Object 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 t) { 603 t.printStackTrace (); 604 return false; 605 } 606 Boolean resultBool = (Boolean ) result; 607 return resultBool.booleanValue (); 608 } 609 610 622 public Set searchUsers (int siteID, Properties searchCriterias) { 623 Set userList = new HashSet (); 624 625 Class [] parameterTypes = new Class [2]; 626 parameterTypes[0] = Integer.TYPE; 627 parameterTypes[1] = propertiesClass; 628 Object [] args = new Object [2]; 629 args[0] = new Integer (siteID); 630 args[1] = searchCriterias; 631 632 Object 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 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 670 public Set searchUsers (String providerKey, int siteID, 671 Properties searchCriterias) { 672 Class [] parameterTypes = new Class [2]; 673 parameterTypes[0] = Integer.TYPE; 674 parameterTypes[1] = propertiesClass; 675 Object [] args = new Object [2]; 676 args[0] = new Integer (siteID); 677 args[1] = searchCriterias; 678 679 Object 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 t) { 692 t.printStackTrace (); 693 return null; 694 } 695 return (Set) result; 696 } 697 698 704 public void updateCache(JahiaUser jahiaUser) { 705 Class [] parameterTypes = new Class [1]; 706 parameterTypes[0] = JahiaUser.class; 707 Object [] args = new Object [1]; 708 args[0] = jahiaUser; 709 Object 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 t) { 720 t.printStackTrace (); 721 } 722 } 723 724 786 private Object routeCall (String methodName, 787 Class [] parameterTypes, 788 Properties userProperties, 789 int typeOfCall, 790 Vector providersToCall, 791 boolean useDifferentInstance, 792 Object instance, 793 Object [] args) 794 throws Throwable { 795 796 switch (typeOfCall) { 797 case ROUTE_CALL_ONE: 798 { 799 JahiaUserManagerProvider providerInstance = null; 803 if (providersToCall != null) { 804 if (providersToCall.size () >= 1) { 805 Object providerItem = providersToCall.elementAt (0); 806 if (providerItem instanceof String ) { 807 String providerKey = (String ) 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 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 providerInstance = defaultProviderInstance; 830 if (providerInstance == null) { 831 return null; 833 } 834 } 835 836 Object result = null; 837 try { 838 Method 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 nsme) { 847 nsme.printStackTrace (); 848 result = null; 849 } catch (InvocationTargetException ite) { 850 ite.printStackTrace (); 851 result = null; 852 throw ite.getTargetException (); 853 } catch (IllegalAccessException iae) { 854 iae.printStackTrace (); 855 result = null; 856 } 857 return result; 858 } 859 860 case ROUTE_CALL_ALL: 861 { 862 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 result = null; 871 872 try { 873 Method 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 nsme) { 883 nsme.printStackTrace (); 884 results = null; 885 break; 886 } catch (InvocationTargetException ite) { 887 ite.printStackTrace (); 888 results = null; 889 throw ite.getTargetException (); 890 } catch (IllegalAccessException 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 905 Enumeration providerEnum = getProvidersToCall (providersToCall).elements (); 906 Object result = null; 907 while (providerEnum.hasMoreElements ()) { 908 JahiaUserManagerProvider curProvider = 909 (JahiaUserManagerProvider) providerEnum.nextElement (); 910 911 try { 912 Method 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 returnType = methodToInvoke.getReturnType (); 921 if (returnType.isPrimitive ()) { 926 if ("int".equals (returnType.getName ())) { 927 Integer resultInt = (Integer ) result; 928 if (resultInt.intValue () != -1) { 929 return result; 930 } 931 } else if ("boolean".equals (returnType.getName ())) { 932 Boolean resultBool = (Boolean ) 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 nsme) { 947 nsme.printStackTrace (); 948 result = null; 949 } catch (InvocationTargetException ite) { 950 ite.printStackTrace (); 951 result = null; 952 throw ite.getTargetException (); 953 } catch (IllegalAccessException iae) { 954 iae.printStackTrace (); 955 result = null; 956 } 957 } 958 959 return result; 960 } 961 } 962 return null; 963 } 964 965 977 private Vector getProvidersToCall (Vector providersToCall) { 978 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 curProviderEntry = providerEnum.nextElement (); 987 if (curProviderEntry instanceof String ) { 988 String curProviderKey = (String ) 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 configFileName) 1014 throws JahiaException { 1015 1016 Document documentRoot = null; 1017 1018 try { 1019 1020 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance (); 1021 DocumentBuilder docBuilder = dfactory.newDocumentBuilder (); 1023 1024 FileInputStream configStream = new FileInputStream (configFileName); 1025 documentRoot = docBuilder.parse (configStream); 1026 documentRoot.normalize (); 1028 } catch (Throwable 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 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 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 1076 this.defaultProviderInstance = providerBean.getInstance (); 1077 } 1078 result.put (providerBean.getKey (), providerBean); 1079 } 1080 providerCounter++; 1081 } 1082 1083 return result; 1084 } 1085 1086 1090 private Map getServersFromDOM (Element rootElement) 1091 throws JahiaException { 1092 Map result = new HashMap (); 1093 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 o1, Object 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 provider = getElementValue (XML_SERVER_DESTINATION_TAG, providerElement); 1116 String priority = getElementValue (XML_SERVER_PRIORITY_TAG, providerElement); 1117 String 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 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 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 key = getElementValue (JahiaUserManagerRoutingService.XML_PROVIDER_KEY_TAG, 1187 providerElement); 1188 String className = getElementValue ( 1189 JahiaUserManagerRoutingService.XML_PROVIDER_CLASS_TAG, providerElement); 1190 String title = getElementValue (JahiaUserManagerRoutingService.XML_PROVIDER_TITLE_TAG, 1191 providerElement); 1192 String description = getElementValue ( 1193 JahiaUserManagerRoutingService.XML_PROVIDER_DESCRIPTION_TAG, providerElement); 1194 String isDefaultStr = getElementValue ( 1195 JahiaUserManagerRoutingService.XML_PROVIDER_ISDEFAULT_TAG, providerElement); 1196 boolean isDefault = ("true".equals (isDefaultStr)); 1197 1198 boolean isReadOnly = false; 1199 try { 1200 String 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 priorityStr = getElementValue ( 1215 JahiaUserManagerRoutingService.XML_PROVIDER_PRIORITY_TAG, providerElement); 1216 priority = Integer.parseInt (priorityStr); 1217 } catch (Exception e) { 1218 priority = providerCounter; 1220 } 1221 1222 result = 1223 new UserManagerProviderBean (key, className, title, description, isDefault, 1224 isReadOnly, priority); 1225 return result; 1226 } 1227 1228 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 key = getElementValue (JahiaUserManagerRoutingService.XML_CRITERIA_KEY_TAG, 1262 criteriaElement); 1263 String description = getElementValue ( 1264 JahiaUserManagerRoutingService.XML_CRITERIA_DESCRIPTION_TAG, criteriaElement); 1265 Properties conditions = getConditionsFromDOM (criteriaElement); 1266 String 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 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 propertyValue = propertyValueAttrib.getNodeValue (); 1302 result.setProperty (propertyName, propertyValue); 1303 } 1304 return result; 1305 } 1306 1307 private String getElementTextValue (Element rootElement) 1308 throws JahiaException { 1309 NodeList childNodes = rootElement.getChildNodes (); 1310 StringBuffer result = new StringBuffer (); 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 getElementValue (String 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 |