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 ; 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 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 41 private static final String JAHIAGROUP_INTERFACE_NAME = "org.jahia.services.usermanager.JahiaGroup"; 42 private static final String JAHIAUSER_INTERFACE_NAME = "org.jahia.services.usermanager.JahiaUser"; 43 private static final String XML_ROOT_TAG = "router-config"; 44 45 private static final String XML_PROVIDER_ROOT_TAG = "provider"; 46 private static final String XML_PROVIDER_KEY_TAG = "key"; 47 private static final String XML_PROVIDER_CLASS_TAG = "class"; 48 private static final String XML_PROVIDER_TITLE_TAG = "title"; 49 private static final String XML_PROVIDER_DESCRIPTION_TAG = "description"; 50 private static final String XML_PROVIDER_ISDEFAULT_TAG = "is-default"; 51 private static final String XML_PROVIDER_ISREADONLY_TAG = "is-readonly"; 52 private static final String XML_PROVIDER_PRIORITY_TAG = "priority"; 53 54 private static final String XML_CRITERIA_ROOT_TAG = "criteria"; 55 private static final String XML_CRITERIA_KEY_TAG = "key"; 56 private static final String XML_CRITERIA_DESCRIPTION_TAG = "description"; 57 private static final String XML_CRITERIA_CONDITION_TAG = "condition"; 58 private static final String XML_CRITERIA_DESTINATION_TAG = "provider-destination"; 59 private static final String XML_CRITERIA_CONDITION_PROPNAME_ATTR = "propertyName"; 60 private static final String XML_CRITERIA_CONDITION_PROPVALUE_ATTR = "propertyValue"; 61 62 66 private static final String XML_SERVER_ROOT_TAG = "server"; 67 private static final String XML_SERVER_URL_TAG = "url"; 68 private static final String XML_SERVER_USERNAME_TAG = "username"; 69 private static final String XML_SERVER_PASSWORD_TAG = "password"; 70 private static final String XML_SERVER_FACTORY_TAG = "factory"; 71 private static final String XML_SERVER_AUTH_MODE_TAG = "authentication-mode"; 72 private static final String XML_SERVER_DESTINATION_TAG = "provider-destination"; 73 private static final String XML_SERVER_PRIORITY_TAG = "priority"; 74 private static final String XML_SERVER_MAX_RECONN_TAG = "max-reconnection"; 75 private static final String XML_SERVER_REFERRAL = "referral"; 76 77 private Map serversTable = null; 78 80 private static final String CONFIG_PATH = File.separator + "services" + 81 File.separator + "usermanager"; 82 private static final String 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 stringClass = null; 92 private Class propertiesClass = null; 93 94 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 serversTable = new HashMap (); 114 115 sortedProviders = new TreeSet (new Comparator () { 116 public int compare (Object o1, Object o2) { 117 return ((GroupManagerProviderBean) o1).getPriority () - ((GroupManagerProviderBean) o2).getPriority (); 118 } 119 }); 120 125 129 132 stringClass = (new String ()).getClass (); 133 propertiesClass = (new Properties ()).getClass (); 134 135 } 136 137 145 public Vector getProviderList () { 146 Vector result = new Vector (sortedProviders); 147 154 return result; 155 } 156 157 public TreeSet getServerList (String 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 curProviderKey = (String ) providerIter.next (); 183 GroupManagerProviderBean curProvider = (GroupManagerProviderBean) providersTable.get ( 184 curProviderKey); 185 curProvider.getInstance ().init (jSettings); 186 } 187 188 } 189 190 191 193 194 public Vector getAdminGrantedSites (JahiaUser user) 195 throws org.jahia.exceptions.JahiaException { 196 Class [] parameterTypes = new Class [1]; 197 198 Class [] interfaces = user.getClass ().getInterfaces (); 200 Class 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 [] args = new Object [1]; 216 args[0] = user; 217 Object 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 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 [] parameterTypes = new Class [1]; 252 parameterTypes[0] = Integer.TYPE; 253 Object [] args = new Object [1]; 254 args[0] = new Integer (siteID); 255 String providerName = "jahia"; Object 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 t) { 269 t.printStackTrace (); 270 return null; 271 } 272 return (JahiaDOMObject) result; 273 274 } 275 276 public boolean groupExists (int siteID, String name) { 277 Class [] parameterTypes = new Class [2]; 278 parameterTypes[0] = Integer.TYPE; 279 parameterTypes[1] = stringClass; 280 Object [] args = new Object [2]; 281 args[0] = new Integer (siteID); 282 args[1] = name; 283 284 Properties groupProperties = new Properties (); 285 groupProperties.setProperty ("group.name", name); 286 287 Object 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 t) { 298 t.printStackTrace (); 299 return false; 300 } 301 Boolean resultBool = (Boolean ) result; 302 return resultBool.booleanValue (); 303 304 } 305 306 public JahiaGroup getGuestGroup (int siteID) { 307 Class [] parameterTypes = new Class [1]; 308 parameterTypes[0] = Integer.TYPE; 309 Object [] args = new Object [1]; 310 args[0] = new Integer (siteID); 311 312 313 Object 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 t) { 324 t.printStackTrace (); 325 return null; 326 } 327 return (JahiaGroup) result; 328 329 } 330 331 public JahiaGroup getUsersGroup (int siteID) { 332 Class [] parameterTypes = new Class [1]; 333 parameterTypes[0] = Integer.TYPE; 334 Object [] args = new Object [1]; 335 args[0] = new Integer (siteID); 336 337 338 Object 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 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 [] parameterTypes = new Class [1]; 361 parameterTypes[0] = Integer.TYPE; 362 Object [] args = new Object [1]; 363 args[0] = new Integer (siteID); 364 365 Object 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 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 [] parameterTypes = new Class [1]; 391 392 Class [] interfaces = user.getClass ().getInterfaces (); 394 Class 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 [] args = new Object [1]; 410 args[0] = user; 411 Object 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 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 resultBool = (Boolean ) resultEnum.nextElement (); 431 if (!resultBool.booleanValue ()) { 432 success = false; 433 break; 434 } 435 } 436 return success; 437 } catch (java.lang.ClassCastException 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 [] parameterTypes = new Class [1]; 448 465 parameterTypes[0] = group.getClass ().getSuperclass (); 466 Object [] args = new Object [1]; 467 args[0] = group; 468 Object 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 t) { 479 t.printStackTrace (); 480 return false; 481 } 482 try { 483 Boolean resultBool = (Boolean ) result; 484 return resultBool.booleanValue (); 485 } catch (java.lang.ClassCastException cce) { 486 logger.debug ("Error casting result to Boolean", cce); 487 } 488 return false; 489 } 490 491 public Vector getUserMembership (JahiaUser user) { 492 Class [] parameterTypes = new Class [1]; 493 494 Class [] interfaces = user.getClass ().getInterfaces (); 496 Class 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 [] args = new Object [1]; 512 args[0] = user; 513 Object 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 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 [] parameterTypes = new Class [1]; 544 parameterTypes[0] = Integer.TYPE; 545 Object [] args = new Object [1]; 546 args[0] = new Integer (siteID); 547 548 Object 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 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 [] parameterTypes = new Class [1]; 576 parameterTypes[0] = Integer.TYPE; 577 Object [] args = new Object [1]; 578 args[0] = new Integer (siteID); 579 String providerName = "jahia"; Object 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 t) { 593 t.printStackTrace (); 594 return null; 595 } 596 return (JahiaDOMObject) result; 597 598 } 599 600 public JahiaGroup lookupGroup (String groupKey) { 601 Class [] parameterTypes = new Class [1]; 602 parameterTypes[0] = stringClass; 603 Object [] args = new Object [1]; 604 args[0] = groupKey; 605 606 Properties groupProperties = new Properties (); 607 groupProperties.setProperty ("group.key", groupKey); 608 609 Object 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 t) { 620 t.printStackTrace (); 621 return null; 622 } 623 return (JahiaGroup) result; 624 625 } 626 627 public JahiaGroup getAdministratorGroup (int siteID) { 628 Class [] parameterTypes = new Class [1]; 629 parameterTypes[0] = Integer.TYPE; 630 Object [] args = new Object [1]; 631 args[0] = new Integer (siteID); 632 633 634 Object 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 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 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 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 [] parameterTypes = new Class [1]; 683 parameterTypes[0] = Integer.TYPE; 684 Object [] args = new Object [1]; 685 args[0] = new Integer (siteID); 686 String providerName = "jahia"; Object 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 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 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 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 746 public Set searchGroups (int siteID, Properties searchCriterias) { 747 Set groupList = new HashSet (); 748 749 Class [] parameterTypes = new Class [2]; 750 parameterTypes[0] = Integer.TYPE; 751 parameterTypes[1] = propertiesClass; 752 Object [] args = new Object [2]; 753 args[0] = new Integer (siteID); 754 args[1] = searchCriterias; 755 756 Object 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 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 794 public Set searchGroups (String providerKey, int siteID, 795 Properties searchCriterias) { 796 Class [] parameterTypes = new Class [2]; 797 parameterTypes[0] = Integer.TYPE; 798 parameterTypes[1] = propertiesClass; 799 Object [] args = new Object [2]; 800 args[0] = new Integer (siteID); 801 args[1] = searchCriterias; 802 803 Object 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 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 825 Class [] parameterTypes = new Class [1]; 826 parameterTypes[0] = Integer.TYPE; 827 Object [] args = new Object [1]; 828 args[0] = new Integer (siteID); 829 830 Object result = null; 831 832 try { 833 result = routeCall ("getGroupsAsDOM", 834 parameterTypes, 835 null, 836 ROUTE_CALL_ALL, null, 838 false, 839 null, 840 args); 841 } catch (Throwable 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 852 Class [] parameterTypes = new Class [1]; 853 parameterTypes[0] = Integer.TYPE; 854 Object [] args = new Object [1]; 855 args[0] = new Integer (siteID); 856 857 Object result = null; 858 try { 859 result = routeCall ("getGroupPropsAsDOM", 860 parameterTypes, 861 null, 862 ROUTE_CALL_ALL, null, 864 false, 865 null, 866 args); 867 } catch (Throwable t) { 868 t.printStackTrace (); 869 return null; 870 } 871 return (JahiaDOMObject) result; 872 873 } 874 875 public JahiaGroup lookupGroup (int siteID, String name) { 876 Class [] parameterTypes = new Class [2]; 877 parameterTypes[0] = Integer.TYPE; 878 parameterTypes[1] = stringClass; 879 Object [] args = new Object [2]; 880 args[0] = new Integer (siteID); 881 args[1] = name; 882 883 Properties groupProperties = new Properties (); 884 groupProperties.setProperty ("group.name", name); 885 886 Object 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 t) { 897 t.printStackTrace (); 898 return null; 899 } 900 return (JahiaGroup) result; 901 902 } 903 904 public JahiaGroup createGroup (int siteID, String name, Properties properties) { 905 JahiaGroup group = null; 906 Class [] parameterTypes = new Class [3]; 907 parameterTypes[0] = Integer.TYPE; 908 parameterTypes[1] = stringClass; 909 parameterTypes[2] = propertiesClass; 910 Object [] args = new Object [3]; 911 args[0] = new Integer (siteID); 912 args[1] = name; 913 args[2] = properties; 914 Object 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 t) { 925 t.printStackTrace (); 926 return null; 927 } 928 group = (JahiaGroup) result; 929 return group; 930 } 931 932 938 public void updateCache(JahiaGroup jahiaGroup) { 939 Class [] parameterTypes = new Class [1]; 940 parameterTypes[0] = jahiaGroup.getClass ().getSuperclass (); 941 Object [] args = new Object [1]; 942 args[0] = jahiaGroup; 943 Object 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 t) { 954 t.printStackTrace (); 955 } 956 } 957 958 1021 private Object routeCall (String methodName, 1022 Class [] parameterTypes, 1023 Properties groupProperties, 1024 int typeOfCall, 1025 Vector providersToCall, 1026 boolean useDifferentInstance, 1027 Object instance, 1028 Object [] args) 1029 throws Throwable { 1030 1031 switch (typeOfCall) { 1032 case ROUTE_CALL_ONE: 1033 { 1034 JahiaGroupManagerProvider providerInstance = null; 1038 if (providersToCall != null) { 1039 if (providersToCall.size () >= 1) { 1040 Object providerItem = providersToCall.elementAt (0); 1041 if (providerItem instanceof String ) { 1042 String providerKey = (String ) 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 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 providerInstance = defaultProviderInstance; 1065 if (providerInstance == null) { 1066 return null; 1068 } 1069 } 1070 Object result = null; 1071 1072 try { 1073 Method 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 nsme) { 1082 nsme.printStackTrace (); 1083 result = null; 1084 } catch (InvocationTargetException ite) { 1085 ite.printStackTrace (); 1086 result = null; 1087 throw ite.getTargetException (); 1088 } catch (IllegalAccessException iae) { 1089 iae.printStackTrace (); 1090 result = null; 1091 } 1092 1093 return result; 1094 } 1095 1096 case ROUTE_CALL_ALL: 1097 { 1098 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 result = null; 1107 1108 try { 1109 Method 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 nsme) { 1119 nsme.printStackTrace (); 1120 results = null; 1121 break; 1122 } catch (InvocationTargetException ite) { 1123 ite.printStackTrace (); 1124 results = null; 1125 throw ite.getTargetException (); 1126 } catch (IllegalAccessException 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 1141 Enumeration providerEnum = getProvidersToCall (providersToCall).elements (); 1142 Object result = null; 1143 while (providerEnum.hasMoreElements ()) { 1144 JahiaGroupManagerProvider curProvider = 1145 (JahiaGroupManagerProvider) providerEnum.nextElement (); 1146 1147 try { 1148 1149 Method 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 returnType = methodToInvoke.getReturnType (); 1159 if (returnType.isPrimitive ()) { 1164 if ("int".equals (returnType.getName ())) { 1165 Integer resultInt = (Integer ) result; 1166 if (resultInt.intValue () != -1) { 1167 return result; 1168 } 1169 } else if ("boolean".equals (returnType.getName ())) { 1170 Boolean resultBool = (Boolean ) 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 nsme) { 1185 nsme.printStackTrace (); 1186 result = null; 1187 } catch (InvocationTargetException ite) { 1188 ite.printStackTrace (); 1189 result = null; 1190 throw ite.getTargetException (); 1191 } catch (IllegalAccessException iae) { 1192 iae.printStackTrace (); 1193 result = null; 1194 } 1195 } 1196 1197 return result; 1198 } 1199 } 1200 return null; 1201 } 1202 1203 1215 private Vector getProvidersToCall (Vector providersToCall) { 1216 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 curProviderEntry = providerEnum.nextElement (); 1225 if (curProviderEntry instanceof String ) { 1226 String curProviderKey = (String ) 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 configFileName) 1251 throws JahiaException { 1252 1253 Document documentRoot = null; 1254 1255 try { 1256 1257 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance (); 1258 DocumentBuilder docBuilder = dfactory.newDocumentBuilder (); 1260 1261 FileInputStream configStream = new FileInputStream (configFileName); 1262 documentRoot = docBuilder.parse (configStream); 1263 documentRoot.normalize (); 1265 } catch (Throwable 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 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 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 1313 this.defaultProviderInstance = providerBean.getInstance (); 1314 } 1315 result.put (providerBean.getKey (), providerBean); 1316 } 1317 providerCounter++; 1318 } 1319 1320 return result; 1321 } 1322 1323 1327 private Map getServersFromDOM (Element rootElement) 1328 throws JahiaException { 1329 Map result = new HashMap (); 1330 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 o1, Object 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 provider = getElementValue (XML_SERVER_DESTINATION_TAG, providerElement); 1356 String priority = getElementValue (XML_SERVER_PRIORITY_TAG, providerElement); 1357 String 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 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 key = getElementValue (JahiaGroupManagerRoutingService.XML_CRITERIA_KEY_TAG, 1404 criteriaElement); 1405 String description = getElementValue ( 1406 JahiaGroupManagerRoutingService.XML_CRITERIA_DESCRIPTION_TAG, criteriaElement); 1407 Properties conditions = getConditionsFromDOM (criteriaElement); 1408 String 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 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 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 key = getElementValue (JahiaGroupManagerRoutingService.XML_PROVIDER_KEY_TAG, 1472 providerElement); 1473 String className = getElementValue ( 1474 JahiaGroupManagerRoutingService.XML_PROVIDER_CLASS_TAG, providerElement); 1475 String title = getElementValue (JahiaGroupManagerRoutingService.XML_PROVIDER_TITLE_TAG, 1476 providerElement); 1477 String description = getElementValue ( 1478 JahiaGroupManagerRoutingService.XML_PROVIDER_DESCRIPTION_TAG, providerElement); 1479 String isDefaultStr = getElementValue ( 1480 JahiaGroupManagerRoutingService.XML_PROVIDER_ISDEFAULT_TAG, providerElement); 1481 boolean isDefault = ("true".equals (isDefaultStr)); 1482 1483 boolean isReadOnly = false; 1484 try { 1485 String 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 priorityStr = getElementValue ( 1500 JahiaGroupManagerRoutingService.XML_PROVIDER_PRIORITY_TAG, providerElement); 1501 priority = Integer.parseInt (priorityStr); 1502 } catch (Exception e) { 1503 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 getElementTextValue (Element rootElement) 1514 throws JahiaException { 1515 NodeList childNodes = rootElement.getChildNodes (); 1516 StringBuffer result = new StringBuffer (); 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 getElementValue (String 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 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 propertyValue = propertyValueAttrib.getNodeValue (); 1580 result.setProperty (propertyName, propertyValue); 1581 } 1582 return result; 1583 } 1584 1585 1586} 1587 | Popular Tags |