1 package com.ca.commons.jndi; 2 3 import com.ca.commons.naming.*; 4 5 import javax.naming.*; 6 import javax.naming.directory.*; 7 import java.io.*; 8 9 10 44 45 public class TestJNDIOps 46 { 47 50 52 BufferedReader in; 53 54 PrintStream out; 55 56 DXOps myOps = null; 57 59 LdifUtility ldifutil = new LdifUtility(); 61 boolean debug = true; 62 boolean terminating = false; boolean printstack = false; 65 public static int OK = 0; 66 public static int ERROR = -1; 67 public static int FINISHED = 1; 68 69 public static void main(String args[]) 70 { 71 72 String fileName = null; 73 String url = null; 74 String user = null; 75 String pwd = null; 76 String version = "3"; 77 String referral = "follow"; 78 boolean useSSL = false; 79 boolean tracing = false; 80 boolean debugFlag = true; 81 boolean terminateFlag = false; 82 boolean printstackFlag = false; 83 84 int i = 0; 85 86 try 87 { 88 while (i < args.length) 89 { 90 String arg = (args[i].charAt(0) != '-') ? args[i] : args[i].substring(1); 91 switch (arg.charAt(0)) 92 { 93 case '?': 94 case 'H': 95 case 'h': 96 if (args.length > i + 1) 97 printHelp(args[i + 1]); 98 else 99 printHelp(null); 100 return; 102 case 'C': 103 case 'c': 104 url = args[++i]; 105 break; 106 107 case 'D': 108 case 'd': 109 debugFlag = true; 110 break; 111 112 case 'E': 113 case 'e': 114 terminateFlag = true; 115 break; 116 117 case 'F': 118 case 'f': 119 fileName = args[++i]; 120 break; 121 122 case 'P': 123 case 'p': 124 pwd = args[++i]; 125 break; 126 127 case 'R': 128 case 'r': 129 referral = args[++i]; 130 break; 131 132 case 'S': 133 case 's': 134 useSSL = true; 135 break; 136 137 case 'T': 138 case 't': 139 tracing = true; 140 break; 141 142 case 'U': 143 case 'u': 144 user = args[++i]; 145 break; 146 147 case 'V': 148 case 'v': 149 version = args[++i]; 150 break; 151 152 case 'X': 153 case 'x': 154 printstackFlag = true; 155 break; 156 157 default : 158 System.out.println("\n\nInvalid command line argument: -" + arg); 159 printHelp(null); 160 return; } 162 i++; 163 } 164 } 165 catch (Exception e) 166 { 167 System.out.println("Error reading command line arguments."); 168 printHelp(""); 169 System.exit(-1); 170 } 171 172 TestJNDIOps tester = new TestJNDIOps(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag); 174 175 tester.processInput(); 177 178 tester.out.println("\nnormal finish\n"); 179 } 180 181 182 public static void printHelp(String subject) 183 { 184 if ("full".equalsIgnoreCase(subject)) 185 { 186 printFullHelp(); 187 return; 188 } 189 if ("changetype".equalsIgnoreCase(subject)) 190 { 191 printChangeTypeHelp(); 192 return; 193 } 194 195 System.out.println("" + 196 "\n\njndiTest is a small utility/test program designed to check the ldap/jndi\n" + 197 "functionality of JXplorer. It reads input or files in ldif change file format\n" + 198 "with some extra commands added to test searching and attribute reading.\n\n" + 199 " usage: java jnditest [<options>]\n\n" + 200 " options:\n" + 201 " -c an optional connection url of the form ldap:\\host:port\n" + 202 " -d debug (verbose) mode\n" + 203 " -e exit on error, returning -1\n" + 204 " -f filename name of an ldif changes input file\n" + 205 " -h [subject] this help message [full|changetype]\n" + 206 " -p password an option password\n" + 207 " -r referral the jndi referral type [follow|ignore|throw]\n" + 208 " -t set BER tracing on\n" + 209 " -u userdn an optional user dn\n" + 210 " -v set ldap version (default 3)\n\n" + 211 " -x print stack trace\n\n" + 212 "in addition to normal ldif changes commands (cf draft-good-ldap-ldif-04.html) a:\n" + 213 "new keyword 'actiontype' with values [list|search|read|connect|disconnect]\n" + 214 "is defined, with a usage similar to 'changetype'"); 215 } 216 217 protected static void printFullHelp() 218 { 219 printChangeTypeHelp(); 220 } 221 222 protected static void printChangeTypeHelp() 223 { 224 System.out.println("" + 225 " * This is a general test bed interface to test the various jndi calls \n" + 226 " * used by JXplorer. It used ldif change file format requests, and can \n" + 227 " * be run directly from the command line, or given an ldif change file\n" + 228 " * to process.<p>\n" + 229 " *\n" + 230 " * Requests are either standard ldif change file requests, such as:\n" + 231 " *\n" + 232 " * version: 1\n" + 233 " * # Add a new entry\n" + 234 " * changetype: add\n" + 235 " * objectclass: top\n" + 236 " * objectclass: person\n" + 237 " * objectclass: organizationalPerson\n" + 238 " * cn: Fiona Jensen\n" + 239 " * sn: Jensen\n" + 240 " * uid: fiona\n" + 241 " * telephonenumber: +1 408 555 1212\n" + 242 " * jpegphoto:< file:///usr/local/directory/photos/fiona.jpg\n" + 243 " * <pre>\n" + 244 " * where valid 'changetype' values are [add|delete|modrdn|modify] \n" + 245 " * This is extended by jnditest to include extra (non-standard) changetype\n" + 246 " * commands, such as [connect|disconnect|list|search|searchOneLevel|read], to allow\n" + 247 " * general directory access and testing.\n" + 248 " * The 'connect' command takes the attributes; 'url', and the optional \n" + 249 " * attributes: 'user','pwd','tracing','ldapVersion','referral' and 'useSSL' \n" + 250 " *\n" + 251 " * XXX coming soon - [copyTree|deleteTree|moveTree]\n" + 252 " *"); 253 } 254 255 274 275 public TestJNDIOps(String fileName, String url, String user, String pwd, boolean tracing, String version, boolean debugFlag, boolean terminateFlag, String referral, boolean useSSL, boolean printstackFlag) 276 { 277 out = System.out; 279 debug = debugFlag; 280 terminating = terminateFlag; 281 printstack = printstackFlag; 282 283 if (url != null) 285 openConnection(url, user, pwd, tracing, version, referral, useSSL); 286 287 if (fileName != null) 289 in = openFile(fileName); 290 else 291 in = new BufferedReader(new InputStreamReader(System.in)); 292 } 293 294 306 307 public void openConnection(String url, String user, String pwd, boolean tracing, String version, String referralType, boolean useSSL) 308 { 309 if (referralType == null) referralType = "follow"; 310 if ("ignorefollowthrow".indexOf(referralType) == -1) 311 { 312 error("unknown referraltype " + referralType, null); 313 referralType = "follow"; 314 } 315 316 if (url.toLowerCase().startsWith("ldap://") == false) 317 url = "ldap://" + url; 318 319 try 320 { 321 if (debug) out.println("opening connection with :\n url: " + url + "\n user: " + user + "\n pwd: " + pwd + "\n tracing: " + tracing + "\n version: " + version + "\n referral: " + referralType); 322 323 if ((url == null) || (url.length() == 0)) 324 { 325 error("unable to open connection - no url supplied", null); 326 return; 327 } 328 329 if ((version == null) || (version.length() == 0)) version = "3"; 331 char[] password = (pwd == null || pwd.length() == 0) ? null : pwd.toCharArray(); 332 int versn = Integer.parseInt(version); 333 334 ConnectionData cData = new ConnectionData(versn, url, user, password, tracing, null, null); 335 DirContext ctx = BasicOps.openContext(cData); 336 if (ctx != null) 338 myOps = new DXOps(ctx); 339 340 if (myOps == null) 341 error("unable to open connection " + url, null); 342 else if (debug) out.println("connection " + url + " open. "); 343 } 344 catch (Exception e) 345 { 346 error("error opening connection " + url + "\n ", e); 347 348 } 349 } 350 351 357 358 public BufferedReader openFile(String fileName) 359 { 360 try 361 { 362 File myFile = new File(fileName); 363 ldifutil.setFileDir(myFile.getParent()); 364 return new BufferedReader(new InputStreamReader(new FileInputStream(myFile))); 365 } 366 catch (Exception e) 367 { 368 error("unable to open file : " + fileName + "\n ", e); 369 System.exit(-1); 370 } 371 return null; 372 } 373 374 379 380 public void processInput() 381 { 382 try 384 { 385 DXEntry commandSet; 386 while ((commandSet = ldifutil.readLdifEntry(in)) != null) 387 { 388 if (commandSet == null) 389 error("internal error in jnditest - parsed ldif command set null", null); 390 391 if (processCommand(commandSet) == FINISHED) 392 return; 393 } 394 } 395 catch (Exception e) 396 { 397 error("error reading input - program stopped." + "\n ", e); 398 System.exit(-1); 399 } 400 } 401 402 411 412 public int processCommand(DXEntry entry) 413 { 414 if (entry.size() == 0) 416 { 417 if (debug) out.println("\n\nEnding on double blank line"); 418 return FINISHED; 419 } 420 421 if (entry.get("version") != null) 422 entry.remove("version"); 424 if (entry.getDN().size() == 0) 425 { 426 DXAttribute temp = (DXAttribute) entry.get("changetype"); 427 String test = ""; 428 try 429 { 430 test = temp.get().toString(); 431 } 432 catch (Exception e) 433 { 434 } if ((test.equalsIgnoreCase("connect") == false) && 437 (test.equalsIgnoreCase("disconnect") == false)) 438 { 439 error("error reading input - no dn attribute in entry!\n*******\n" + entry + "******\n", null); 440 return ERROR; 441 } 442 } 443 444 DXAttribute command = (DXAttribute) entry.get("changetype"); 445 if (command == null || command.size() == 0) 446 { 447 error("error reading input - no 'changetype' attribute in entry.\n******\n" + entry + "******\n", null); 448 return ERROR; 449 } 450 451 String commandString = ""; 452 453 try 454 { 455 commandString = command.get().toString(); 456 } 457 catch (NamingException e) 458 { 459 error("internal error in processCommand()\n ", e); 460 return ERROR; 461 } 463 if (debug) System.out.println("\n\nCOMMAND= " + commandString); 464 465 entry.remove("changetype"); 466 467 if ((myOps == null) && !(commandString.equalsIgnoreCase("connect") || commandString.equalsIgnoreCase("disconnect"))) 468 error("Attempting operation " + commandString + " without an open connection!", null); 469 470 try 472 { 473 if (commandString.equalsIgnoreCase("add")) 474 addEntry(entry, returnType(entry)); 475 else if (commandString.equalsIgnoreCase("delete")) 476 deleteEntry(entry, returnType(entry)); 477 else if (commandString.equalsIgnoreCase("modrdn")) 478 modrdnEntry(entry, modRDN(entry)); 479 else if (commandString.equalsIgnoreCase("modify")) 480 modifyEntry(entry, returnType(entry)); 481 else if (commandString.equalsIgnoreCase("list")) 482 listEntry(entry, list(entry)); 483 else if (commandString.equalsIgnoreCase("search")) 484 searchEntry(entry, search(entry)); 485 else if (commandString.equalsIgnoreCase("searchOneLevel")) 486 searchOneLevelEntry(entry, search(entry)); 487 else if (commandString.equalsIgnoreCase("read")) 488 readEntry(entry, read(entry)); 489 else if (commandString.equalsIgnoreCase("connect")) 490 connect(entry); 491 else if (commandString.equalsIgnoreCase("disconnect")) 492 disconnect(entry); 493 else if (commandString.equalsIgnoreCase("copy")) 494 copy(entry, copy(entry, command)); 495 else if (commandString.equalsIgnoreCase("cut")) 496 cut(entry, cut(entry, command)); 497 } 498 catch (NamingException e) 499 { 500 error("Naming Exception in " + commandString + "\n ", e); 501 return ERROR; 502 } 503 504 return OK; 505 } 506 507 508 517 518 public boolean returnType(DXEntry entry) 519 { 520 521 DXAttribute returnType = (DXAttribute) entry.get("returntype"); 522 523 String myType = ""; 524 try 525 { 526 myType = returnType.get().toString(); 527 } 528 catch (Exception e) 529 { 530 } 531 532 boolean expectedVal = "true".equalsIgnoreCase(myType); 533 534 if (returnType != null) 535 { 536 if (debug) System.out.println("\n\nparsed returntype: " + myType + "\n as: " + expectedVal + "\n"); 537 entry.remove("returntype"); 538 } 539 return expectedVal; 540 } 541 542 543 550 551 public void addEntry(DXEntry entry, boolean expectedValue) 552 { 553 if (debug) System.out.println("\nADD: " + entry); 554 555 boolean ret = false; 556 557 try 558 { 559 myOps.addEntry(entry.getDN(), entry); 560 ret = true; 561 } 562 catch (NamingException e) 563 { 564 } 565 566 568 if (ret != expectedValue) { 570 if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n"); 571 if (debug) System.out.println("\n&&&& RET: " + ret + " &&&& EXPECTEDVALUE: " + expectedValue); 572 error("\nadd operation failed for: " + entry + "\n", null); 573 } 574 } 575 576 577 584 585 public void deleteEntry(DXEntry entry, boolean expectedValue) 586 { 587 if (debug) System.out.println("\nDELETE: " + entry); 588 589 boolean ret = false; 590 591 try 592 { 593 myOps.deleteEntry(entry.getDN()); 594 ret = true; 595 } 596 catch (NamingException e) 597 { 598 } 599 600 602 if (ret != expectedValue) { 604 if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue); 605 if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n"); 606 error("\ndelete operation failed for: " + entry + "\n", null); 607 } 608 } 609 610 611 620 621 public String modRDN(DXEntry entry) 622 { 623 624 DXAttribute modRdnResult = (DXAttribute) entry.get("modrdnresult"); 625 626 String myModRdnCn = ""; 627 try 628 { 629 myModRdnCn = modRdnResult.get().toString(); 630 } 631 catch (Exception e) 632 { 633 } 634 635 if (modRdnResult != null) 636 { 637 if (debug) System.out.println("\n\nparsed modrdnresult (DXAttribute): " + modRdnResult + "\n to mymodrdnCn(String): " + myModRdnCn); 638 entry.remove("modrdnresult"); 639 } 640 return myModRdnCn; 641 } 642 643 644 651 652 public void modrdnEntry(DXEntry entry, String expectedModrdn) 653 { 654 String newDNString = entry.getString("newrdn"); 655 DN newDN = new DN(newDNString); 656 if (debug) System.out.println("modrdn: " + entry.getDN() + "\n to: " + newDN); 657 658 boolean ret = false; 659 660 try 661 { 662 myOps.renameEntry(entry.getDN(), newDN); 663 ret = true; 664 } 665 catch (NamingException e) 666 { 667 } 668 669 if (ret == false) 670 error("modrdn operation failed for: " + entry, null); 671 672 int compare = -2; 674 compare = expectedModrdn.compareTo(newDNString); 676 if (compare != 0) { 678 if (debug) System.out.println("\n\nnewDN CN String: " + newDNString); 679 if (debug) System.out.println("EXPECTEDVALUE : " + expectedModrdn); 680 error("\nmodrdn operation failed for: " + entry + "\nExpected read result for cn: " + expectedModrdn + "\nActual result for cn: " + newDNString, null); 681 } 682 } 683 684 685 692 693 public void modifyEntry(DXEntry entry, boolean expectedValue) 694 { 695 if (debug) System.out.println("modify: " + entry); 696 697 Name myDN = entry.getDN(); 698 699 700 738 739 DXAttribute add = null; 740 DXAttribute delete = null; 741 DXAttribute replace = null; 742 743 add = (DXAttribute) entry.get("add"); 744 delete = (DXAttribute) entry.get("delete"); 745 replace = (DXAttribute) entry.get("replace"); 746 747 748 759 if (add != null) { 761 String attrString = ""; 762 try 763 { 764 attrString = add.get().toString(); 765 } 766 catch (Exception e) 767 { 768 } 770 DXAttribute attr = (DXAttribute) entry.get(attrString); 771 772 boolean ret = false; 773 774 try 775 { 776 myOps.addAttribute(myDN, attr); 777 ret = true; 778 } 779 catch (NamingException e1) 780 { 781 } 782 783 785 if (ret != expectedValue) { 787 if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue); 788 if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n"); 789 error("\nmodify-add operation failed for: " + entry + "\n", null); 790 } 791 } 792 793 794 805 if (delete != null) { 807 String attrString = ""; 808 try 809 { 810 attrString = delete.get().toString(); 811 } 812 catch (Exception e) 813 { 814 } 816 DXAttribute attr = (DXAttribute) entry.get(attrString); 817 818 boolean ret = false; 819 820 try 821 { 822 myOps.deleteAttribute(myDN, attr); 823 ret = true; 824 } 825 catch (NamingException e1) 826 { 827 } 828 829 831 if (ret != expectedValue) { 833 if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue); 834 if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n"); 835 error("\nmodify-delete operation failed for: " + entry + "\n", null); 836 } 837 } 838 839 840 851 if (replace != null) { 853 String attrString = ""; 854 try 855 { 856 attrString = replace.get().toString(); 857 } 858 catch (Exception e) 859 { 860 } 862 DXAttribute attr = (DXAttribute) entry.get(attrString); 863 864 boolean ret = false; 865 866 try 867 { 868 myOps.updateAttribute(myDN, attr); 869 ret = true; 870 } 871 catch (NamingException e1) 872 { 873 } 874 875 877 if (ret != expectedValue) { 879 if (debug) System.out.println("\n\nRET " + ret + " EXPECTEDVALUE " + expectedValue); 880 if (debug) System.out.println("\nret equals expectedValue: " + (ret == expectedValue) + "\n"); 881 error("\nmodify-replace operation failed for: " + entry + "\n", null); 882 } 883 } 884 } 885 886 887 895 896 public int list(DXEntry entry) 897 { 898 899 DXAttribute listResult = (DXAttribute) entry.get("listresult"); 900 901 String myListResult = ""; 902 try 903 { 904 myListResult = listResult.get().toString(); 905 } 906 catch (Exception e) 907 { 908 } 909 910 int list = -1; 911 try 912 { 913 list = Integer.parseInt(myListResult); 914 } 915 catch (Exception e) 916 { 917 } 918 919 if (listResult != null) 920 { 921 if (debug) System.out.println("\n\nparsed listresult(DXAttribute): " + listResult + "\n to myListResult(String): " + myListResult + ", to list(int): " + list); 922 entry.remove("listresult"); 923 } 924 return list; 925 } 926 927 928 935 936 public void listEntry(DXEntry entry, int expectedList) 937 throws NamingException 938 { 939 if (debug) System.out.println("\nlist: " + entry); 940 NamingEnumeration names = myOps.list(entry.getDN()); 941 942 if (debug) out.println("\nlist of children:"); 943 944 int i = 0; 946 while (names.hasMore()) { 948 i++; 949 if (debug) out.println(((NameClassPair) names.next()).getName()); 950 } 951 952 if (i != expectedList) 953 error("\nList operation failed for: " + entry + "\nExpected list results: " + expectedList + "\nActual list results: " + i, null); 954 } 955 956 957 965 966 public int search(DXEntry entry) 967 { 968 969 DXAttribute searchResult = (DXAttribute) entry.get("searchresult"); 970 971 String mySearch = ""; 972 try 973 { 974 mySearch = searchResult.get().toString(); 975 } 976 catch (Exception e) 977 { 978 } 979 980 int search = -1; 981 try 982 { 983 search = Integer.parseInt(mySearch); 984 } 985 catch (Exception e) 986 { 987 } 988 989 if (searchResult != null) 990 { 991 if (debug) System.out.println("\n\nparsed searchresult(DXAttribute): " + searchResult + "\n to mySearch(String): " + mySearch + ", to myResult(int): " + search); 992 entry.remove("searchresult"); 993 } 994 return search; 995 } 996 997 998 1011 1012 public void searchEntry(DXEntry entry, int expectedSearch) 1013 throws NamingException 1014 { 1015 String filter = entry.getString("filter"); 1016 String limit = entry.getString("limit"); 1017 String timeout = entry.getString("timeout"); 1018 if (limit == null) limit = "0"; 1019 if (timeout == null) timeout = "0"; 1020 1021 int lim = Integer.parseInt(limit); 1022 int time = Integer.parseInt(timeout); 1023 1024 if (debug) System.out.println("\nSEARCH " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout); 1025 1026 1027 NamingEnumeration names = myOps.searchSubTree(entry.getDN(), filter, lim, time); 1028 if (debug) out.println("\nNAMES: " + names + "\n\nDN: " + entry); 1029 1030 if (debug) out.println("\nsubtree search results:"); 1031 1032 int i = 0; 1034 while (names.hasMore()) { 1036 i++; 1037 if (debug) out.println(((SearchResult) names.next()).getName()); 1038 } 1039 1040 if (i != expectedSearch) 1041 error("\nSearch operation failed for: " + entry + "\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null); 1042 } 1043 1044 1045 1058 1059 public void searchOneLevelEntry(DXEntry entry, int expectedSearch) 1060 throws NamingException 1061 { 1062 String filter = entry.getString("filter"); 1063 String limit = entry.getString("limit"); 1064 String timeout = entry.getString("timeout"); 1065 if (limit == null) limit = "0"; 1066 if (timeout == null) timeout = "0"; 1067 1068 if (debug) System.out.println("\n\nSEARCHONELEVEL: " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout); 1069 1070 int lim = Integer.parseInt(limit); 1071 int time = Integer.parseInt(timeout); 1072 1073 NamingEnumeration names = myOps.searchOneLevel(entry.getDN(), filter, lim, time); 1074 if (debug) out.println("\n\none level search results:"); 1075 1076 int i = 0; 1078 while (names.hasMore()) { 1080 i++; 1081 if (debug) out.println(((SearchResult) names.next()).getName()); 1082 } 1083 1084 if (i != expectedSearch) 1085 error("\n\nSearchOneLevel operation failed for: " + entry + "\n\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null); 1086 } 1087 1088 1089 1098 1099 public String read(DXEntry entry) 1100 { 1101 DXAttribute readResultCn = (DXAttribute) entry.get("readresultcn"); 1102 1103 String myReadCn = ""; 1104 try 1105 { 1106 myReadCn = readResultCn.get().toString(); 1107 } 1108 catch (Exception e) 1109 { 1110 } 1111 1112 if (readResultCn != null) 1113 { 1114 if (debug) System.out.println("\n\nparsed readresultcn (DXAttribute): " + readResultCn + "\n to myReadCn(String): " + myReadCn); 1115 entry.remove("readresultcn"); 1116 } 1117 return myReadCn; 1118 } 1119 1120 1121 1129 1130 public void readEntry(DXEntry entry, String expectedRead) 1131 throws NamingException 1132 { 1133 if (debug) System.out.println("\nread: " + entry); 1134 1135 Attributes atts = myOps.read(entry.getDN()); 1136 if (atts == null) 1137 { 1138 throw new NamingException("\nUnable to read entry " + entry.getDN()); 1139 } 1140 1141 Attribute readCn = (Attribute) atts.get("cn"); 1142 if (debug) out.println("\nREAD: " + readCn); 1143 1144 String myReadCn = ""; 1145 try 1146 { 1147 myReadCn = readCn.get().toString(); 1148 } 1149 catch (Exception e) 1150 { 1151 } 1152 1153 int compare = -2; 1155 compare = expectedRead.compareTo(myReadCn); 1157 if (compare != 0) { 1159 if (debug) out.println("\n\nREAD CN String: " + myReadCn); 1160 if (debug) out.println("EXPECTEDVALUE : " + expectedRead); 1161 error("\nRead operation failed for: " + entry + "\nExpected read result for cn: " + expectedRead + "\nActual read result for cn: " + myReadCn, null); 1162 } 1163 1164 DXEntry val = new DXEntry(atts); 1165 if (debug) out.println(val); 1166 } 1167 1168 1169 1178 1179 public String copy(DXEntry entry, DXAttribute command) 1180 { 1181 DXAttribute copyTo = (DXAttribute) entry.get("copyTo"); 1182 1183 String myCopyTo = ""; 1184 try 1185 { 1186 myCopyTo = copyTo.get().toString(); 1187 } 1188 catch (Exception e) 1189 { 1190 } 1191 1192 if (copyTo != null) 1193 { 1194 if (debug) System.out.println("copyTo: " + copyTo + "\ncommand: " + command + "\nmyCopyTo: " + myCopyTo); 1195 if (debug) System.out.println("\n\nparsed copyTo (DXAttribute): " + copyTo + "\n to myCopyTo(String): " + myCopyTo); 1196 entry.remove("copyTo"); 1197 } 1198 return myCopyTo; 1199 } 1200 1201 1202 1208 1209 public void copy(DXEntry oldEntry, String newEntry) 1210 { 1211 1212 1213 Name newDN = myOps.postParse(newEntry); Name oldDN = oldEntry.getDN(); 1215 1216 if (debug) System.out.println("old DN: " + oldDN); 1217 if (debug) System.out.println("new DN: " + newDN); 1218 1219 NamingEnumeration namesOld; 1220 1221 try 1222 { 1223 namesOld = myOps.list(oldDN); 1224 } 1225 catch (NamingException e1) 1226 { 1227 System.err.println("failed getting old names"); 1228 e1.printStackTrace(); 1229 return; 1230 } 1231 1232 if (debug) out.println("\nCopy of OLD children:"); 1233 1234 int n = 0; 1236 try 1237 { 1238 while (namesOld.hasMore()) { 1240 n++; 1241 if (debug) out.println("Old Entries: " + ((NameClassPair) namesOld.next()).getName()); 1242 } 1243 } 1244 catch (Exception e) 1245 { 1246 if (debug) System.out.println("List for OLD entries failed during copy process"); 1247 } 1248 1249 try 1250 { 1251 myOps.copyTree(oldDN, newDN); 1252 } 1253 catch (NamingException e2) 1254 { 1255 System.err.println("error in copyTree()"); 1256 e2.printStackTrace(); 1257 } 1258 1259 1261 if (debug) System.out.println("Copy: " + oldEntry); 1262 NamingEnumeration namesNew; 1263 1264 try 1265 { 1266 namesNew = myOps.list(newDN); 1267 } 1268 catch (NamingException e3) 1269 { 1270 System.err.println("error in getting new list"); 1271 e3.printStackTrace(); 1272 return; 1273 } 1274 1275 1276 if (debug) out.println("\nCopy of NEW children:"); 1277 1278 int i = 0; 1280 try 1281 { 1282 while (namesNew.hasMore()) { 1284 i++; 1285 if (debug) out.println("New Entries: " + ((NameClassPair) namesNew.next()).getName()); 1286 } 1287 } 1288 catch (Exception e) 1289 { 1290 if (debug) System.out.println("List for NEW entries failed during copy process"); 1291 } 1292 1293 if (i != n) error("\nCopy operation failed for: " + oldEntry + "\nExpected number of copied entries: " + n + "\nActual number of copied entries: " + i, null); 1295 } 1296 1297 1298 1307 1308 public String cut(DXEntry entry, DXAttribute command) 1309 { 1310 DXAttribute cutTo = (DXAttribute) entry.get("cutTo"); 1311 1312 String myCutTo = ""; 1313 try 1314 { 1315 myCutTo = cutTo.get().toString(); 1316 } 1317 catch (Exception e) 1318 { 1319 } 1320 1321 if (cutTo != null) 1322 { 1323 if (debug) System.out.println("cutTo: " + cutTo + "\ncommand: " + command + "\nmyCutTo: " + myCutTo); 1324 if (debug) System.out.println("\n\nparsed cutTo (DXAttribute): " + cutTo + "\n to myCutTo(String): " + myCutTo); 1325 entry.remove("cutTo"); 1326 } 1327 return myCutTo; 1328 } 1329 1330 1331 1337 1338 public void cut(DXEntry oldEntry, String newEntry) 1339 { 1340 1341 1342 Name newDN = myOps.postParse(newEntry); Name oldDN = oldEntry.getDN(); 1344 1345 if (debug) System.out.println("old: " + oldDN); 1346 if (debug) System.out.println("new: " + newDN); 1347 1348 NamingEnumeration namesOld; 1349 1350 try 1351 { 1352 namesOld = myOps.list(oldDN); 1353 } 1354 catch (NamingException e1) 1355 { 1356 System.err.println("error getting namesOld"); 1357 e1.printStackTrace(); 1358 return; 1359 } 1360 1361 if (debug) out.println("\nCut of OLD children:"); 1362 1363 int n = 0; 1365 try 1366 { 1367 while (namesOld.hasMore()) { 1369 n++; 1370 if (debug) out.println("Old Entries: " + ((NameClassPair) namesOld.next()).getName()); 1371 } 1372 } 1373 catch (Exception e) 1374 { 1375 if (debug) System.out.println("List for OLD entries failed during cut process"); 1376 } 1377 1378 try 1379 { 1380 myOps.moveTree(oldDN, newDN); 1381 } 1382 catch (NamingException e2) 1383 { 1384 System.err.println("error in moveTree()"); 1385 e2.printStackTrace(); 1386 } 1388 if (debug) System.out.println("Copy: " + oldEntry); 1389 NamingEnumeration namesNew; 1390 1391 try 1392 { 1393 namesNew = myOps.list(newDN); 1394 } 1395 catch (NamingException e3) 1396 { 1397 System.err.println("error getting namesNew"); 1398 e3.printStackTrace(); 1399 return; 1400 } 1401 1402 1403 if (debug) out.println("\nCut of NEW children:"); 1404 1405 int i = 0; 1407 try 1408 { 1409 while (namesNew.hasMore()) { 1411 i++; 1412 if (debug) out.println("New Entries: " + ((NameClassPair) namesNew.next()).getName()); 1413 } 1414 } 1415 catch (Exception e) 1416 { 1417 if (debug) System.out.println("List for NEW entries failed during cut process"); 1418 } 1419 1420 if (i != n) error("\nCut operation failed for: " + oldEntry + "\nExpected number of cut entries: " + n + "\nActual number of cut entries: " + i, null); 1422 } 1423 1424 1425 1430 1431 public void connect(DXEntry entry) 1432 { 1433 if (debug) System.out.println("connect: " + entry); 1434 if (myOps != null) 1435 try 1436 { 1437 myOps.close(); 1438 } 1439 catch (NamingException e) 1440 { 1441 System.err.println("error in myOps.close()"); 1442 e.printStackTrace(); 1443 } 1444 1445 String url = entry.getString("url"); 1446 String user = entry.getString("user"); 1447 String pwd = entry.getString("pwd"); 1448 String tracing = entry.getString("tracing"); 1449 String version = entry.getString("ldapVersion"); 1450 String referral = entry.getString("referral"); 1451 String useSSL = entry.getString("useSSL"); 1452 1453 boolean trace = ((tracing != null) && (tracing.equalsIgnoreCase("true"))); 1454 boolean ssl = ((useSSL != null) && (useSSL.equalsIgnoreCase("true"))); 1455 openConnection(url, user, pwd, trace, version, referral, ssl); 1456 } 1457 1458 1459 1462 1463 public void disconnect(DXEntry entry) 1464 { 1465 if (debug) System.out.println("disconnected. "); 1466 try 1467 { 1468 myOps.close(); 1469 } 1470 catch (NamingException e) 1471 { 1472 System.err.println("error in myOps.close()"); 1473 e.printStackTrace(); 1474 } 1475 } 1476 1477 1478 1481 1482 public void error(String msg, Exception e) 1483 { 1484 out.println(msg + "\n"); 1485 1486 if (e != null && printstack) 1487 e.printStackTrace(); 1488 1489 if (terminating) 1490 System.exit(-1); 1491 } 1492} | Popular Tags |