1 package com.ca.commons.jndi; 2 3 import java.io.*; 4 import com.ca.commons.naming.*; 5 import javax.naming.*; 7 import javax.naming.directory.*; 8 9 10 11 44 45 public class jnditest 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 String fileName = null; 72 String url = null; 73 String user = null; 74 String pwd = null; 75 String version = "3"; 76 String referral = "follow"; 77 boolean useSSL = false; 78 boolean tracing = false; 79 boolean debugFlag = true; 80 boolean terminateFlag = false; 81 boolean printstackFlag = false; 82 83 int i=0; 84 85 try 86 { 87 while (i<args.length) 88 { 89 String arg = (args[i].charAt(0) != '-')?args[i]:args[i].substring(1); 90 switch(arg.charAt(0)) 91 { 92 case '?': 93 case 'H': 94 case 'h': if (args.length>i+1) 95 printHelp(args[i+1]); 96 else 97 printHelp(null); 98 return; 100 case 'C': 101 case 'c': url = args[++i]; break; 102 103 case 'D': 104 case 'd': debugFlag = true; break; 105 106 case 'E': 107 case 'e': terminateFlag = true; break; 108 109 case 'F': 110 case 'f': fileName = args[++i]; break; 111 112 case 'P': 113 case 'p': pwd = args[++i]; break; 114 115 case 'R': 116 case 'r': referral = args[++i]; break; 117 118 case 'S': 119 case 's': useSSL = true; break; 120 121 case 'T': 122 case 't': tracing = true; break; 123 124 case 'U': 125 case 'u': user = args[++i]; break; 126 127 case 'V': 128 case 'v': version = args[++i]; break; 129 130 case 'X': 131 case 'x': printstackFlag = true; break; 132 133 default : System.out.println("\n\nInvalid command line argument: -" + arg); 134 printHelp(null); 135 return; } 137 i++; 138 } 139 } 140 catch (Exception e) 141 { 142 System.out.println("Error reading command line arguments."); 143 printHelp(""); 144 System.exit(-1); 145 } 146 147 jnditest tester = new jnditest(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag); 149 150 tester.processInput(); 152 153 tester.out.println("\nnormal finish\n"); 154 } 155 156 157 public static void printHelp(String subject) 158 { 159 if ("full".equalsIgnoreCase(subject)) {printFullHelp(); return; } 160 if ("changetype".equalsIgnoreCase(subject)) {printChangeTypeHelp(); return; } 161 162 System.out.println(""+ 163 "\n\njndiTest is a small utility/test program designed to check the ldap/jndi\n"+ 164 "functionality of JXplorer. It reads input or files in ldif change file format\n"+ 165 "with some extra commands added to test searching and attribute reading.\n\n"+ 166 " usage: java jnditest [<options>]\n\n" + 167 " options:\n" + 168 " -c an optional connection url of the form ldap:\\host:port\n" + 169 " -d debug (verbose) mode\n" + 170 " -e exit on error, returning -1\n" + 171 " -f filename name of an ldif changes input file\n" + 172 " -h [subject] this help message [full|changetype]\n" + 173 " -p password an option password\n" + 174 " -r referral the jndi referral type [follow|ignore|throw]\n" + 175 " -t set BER tracing on\n" + 176 " -u userdn an optional user dn\n" + 177 " -v set ldap version (default 3)\n\n" + 178 " -x print stack trace\n\n" + 179 "in addition to normal ldif changes commands (cf draft-good-ldap-ldif-04.html) a:\n" + 180 "new keyword 'actiontype' with values [list|search|read|connect|disconnect]\n" + 181 "is defined, with a usage similar to 'changetype'"); 182 } 183 184 protected static void printFullHelp() 185 { 186 printChangeTypeHelp(); 187 } 188 189 protected static void printChangeTypeHelp() 190 { 191 System.out.println("" + 192 " * This is a general test bed interface to test the various jndi calls \n"+ 193 " * used by JXplorer. It used ldif change file format requests, and can \n"+ 194 " * be run directly from the command line, or given an ldif change file\n"+ 195 " * to process.<p>\n"+ 196 " *\n"+ 197 " * Requests are either standard ldif change file requests, such as:\n"+ 198 " *\n"+ 199 " * version: 1\n"+ 200 " * # Add a new entry\n"+ 201 " * changetype: add\n"+ 202 " * objectclass: top\n"+ 203 " * objectclass: person\n"+ 204 " * objectclass: organizationalPerson\n"+ 205 " * cn: Fiona Jensen\n"+ 206 " * sn: Jensen\n"+ 207 " * uid: fiona\n"+ 208 " * telephonenumber: +1 408 555 1212\n"+ 209 " * jpegphoto:< file:///usr/local/directory/photos/fiona.jpg\n"+ 210 " * <pre>\n"+ 211 " * where valid 'changetype' values are [add|delete|modrdn|modify] \n"+ 212 " * This is extended by jnditest to include extra (non-standard) changetype\n"+ 213 " * commands, such as [connect|disconnect|list|search|searchOneLevel|read], to allow\n"+ 214 " * general directory access and testing.\n"+ 215 " * The 'connect' command takes the attributes; 'url', and the optional \n"+ 216 " * attributes: 'user','pwd','tracing','ldapVersion','referral' and 'useSSL' \n" + 217 " *\n"+ 218 " * XXX coming soon - [copyTree|deleteTree|moveTree]\n"+ 219 " *"); 220 } 221 222 241 242 public jnditest(String fileName, String url, String user, String pwd, boolean tracing, String version, boolean debugFlag, boolean terminateFlag, String referral, boolean useSSL, boolean printstackFlag) 243 { 244 out = System.out; 246 debug = debugFlag; 247 terminating = terminateFlag; 248 printstack = printstackFlag; 249 250 if (url != null) 252 openConnection(url, user, pwd, tracing, version, referral, useSSL); 253 254 if (fileName != null) 256 in = openFile(fileName); 257 else 258 in = new BufferedReader(new InputStreamReader(System.in)); 259 } 260 261 273 274 public void openConnection(String url, String user, String pwd, boolean tracing, String version, String referralType, boolean useSSL) 275 { 276 if (referralType == null) referralType = "follow"; 277 if ("ignorefollowthrow".indexOf(referralType)==-1) 278 { 279 error("unknown referraltype " + referralType, null); 280 referralType = "follow"; 281 } 282 283 try 284 { 285 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); 286 287 if ((url == null) || (url.length()==0)) 288 { error("Unable to open connection - no url supplied", null); return;} 289 290 if ((version == null) || (version.length()==0)) version = "3"; 292 char[] password = (pwd==null||pwd.length()==0)?null:pwd.toCharArray(); 293 int versn = Integer.parseInt(version); 294 295 ConnectionData connectionData = new ConnectionData(versn, url, user, password, tracing, null, null, false, null, null, null, null, null, null, false, null); 296 if(url.startsWith("http://")) 297 connectionData.setProtocol(ConnectionData.DSML); 298 else if(url.startsWith("ldap://")) 299 connectionData.setProtocol(ConnectionData.LDAP); 300 else 301 error("Unable to open connection due to invalid url - url must start with either http:// or ldap://", null); 302 303 DirContext ctx = BasicOps.openContext(connectionData); 304 305 if (ctx != null) 306 myOps = new DXOps(ctx); 307 308 if (myOps == null) 309 error("unable to open connection " + url, null); 310 else 311 if (debug) out.println("connection " + url + " open. "); 312 } 313 catch (Exception e) 314 { 315 error("error opening connection " + url + "\n ", e); 316 } 317 } 318 319 325 326 public BufferedReader openFile(String fileName) 327 { 328 try 329 { 330 File myFile = new File(fileName); 331 ldifutil.setFileDir(myFile.getParent()); 332 return new BufferedReader(new InputStreamReader(new FileInputStream(myFile))); 333 } 334 catch (Exception e) 335 { 336 error("unable to open file : " + fileName + "\n ", e); 337 System.exit(-1); 338 } 339 return null; 340 } 341 342 347 348 public void processInput() 349 { 350 try 352 { 353 DXEntry commandSet; 354 while ((commandSet = ldifutil.readLdifEntry(in))!=null) 355 { 356 if (commandSet == null) 357 error ("internal error in jnditest - parsed ldif command set null", null); 358 359 if (processCommand(commandSet) == FINISHED) 360 return; 361 } 362 } 363 catch (Exception e) 364 { 365 error("error reading input - program stopped." + "\n ", e); 366 System.exit(-1); 367 } 368 } 369 370 378 379 public int processCommand(DXEntry entry) 380 { 381 if (entry.size() == 0) 383 { if (debug) out.println("\n\nEnding on double blank line"); return FINISHED; } 384 385 if (entry.get("version") != null) 386 entry.remove("version"); 388 if (entry.getDN().size() == 0) 389 { 390 DXAttribute temp = (DXAttribute)entry.get("changetype"); 391 String test = ""; 392 try{ test = temp.get().toString(); } catch (Exception e) {} if ((test.equalsIgnoreCase("connect") == false) && 395 (test.equalsIgnoreCase("disconnect") == false)) 396 { 397 error("error reading input - no dn attribute in entry!\n*******\n" + entry + "******\n", null); return ERROR; 398 } 399 } 400 401 DXAttribute command = (DXAttribute)entry.get("changetype"); 402 if (command == null || command.size() == 0) 403 { error("error reading input - no 'changetype' attribute in entry.\n******\n" + entry + "******\n", null); return ERROR; } 404 405 String commandString = ""; 406 407 try { commandString = command.get().toString(); } 408 catch (NamingException e) { error("internal error in processCommand()\n ", e); return ERROR;} 410 if (debug) System.out.println("\n\nCOMMAND= " + commandString); 411 412 entry.remove("changetype"); 413 414 if ((myOps == null) && !(commandString.equalsIgnoreCase("connect")||commandString.equalsIgnoreCase("disconnect"))) 415 error("Attempting operation " + commandString + " without an open connection!", null); 416 417 try 419 { 420 if (commandString.equalsIgnoreCase("add")) 421 addEntry(entry, returnType(entry)); 422 else if (commandString.equalsIgnoreCase("delete")) 423 deleteEntry(entry, returnType(entry)); 424 else if (commandString.equalsIgnoreCase("deleteTree")) 425 deleteTree(entry, returnType(entry)); 426 else if (commandString.equalsIgnoreCase("modrdn")) 427 modrdnEntry(entry, modRDN(entry)); 428 else if (commandString.equalsIgnoreCase("modify")) 429 modifyEntry(entry, returnType(entry)); 430 else if (commandString.equalsIgnoreCase("list")) 431 listEntry(entry, list(entry)); 432 else if (commandString.equalsIgnoreCase("search")) 433 searchEntry(entry, getSearchResultAtt(entry)); 434 else if (commandString.equalsIgnoreCase("searchOneLevel")) 435 searchOneLevelEntry(entry, getSearchResultAtt(entry)); 436 else if (commandString.equalsIgnoreCase("searchSorted")) 437 searchSorted(entry, getSearchResultAtt(entry)); 438 else if (commandString.equalsIgnoreCase("read")) 439 readEntry(entry, read(entry)); 440 else if (commandString.equalsIgnoreCase("connect")) 441 connect(entry); 442 else if (commandString.equalsIgnoreCase("disconnect")) 443 disconnect(entry); 444 else if (commandString.equalsIgnoreCase("copy")) 445 copy(entry, copy(entry, command)); 446 else if (commandString.equalsIgnoreCase("cut")) 447 cut(entry, cut(entry, command)); 448 } 449 catch (NamingException e) 450 { 451 error("Naming Exception in " + commandString + "\n ", e); return ERROR; 452 } 453 454 return OK; 455 } 456 457 458 459 460 468 469 public boolean returnType(DXEntry entry) 470 { 471 472 DXAttribute returnType = (DXAttribute)entry.get("returntype"); 473 474 String myType = ""; 475 try{ myType = returnType.get().toString(); } catch (Exception e) {} 476 477 boolean expectedVal = "true".equalsIgnoreCase(myType); 478 479 if(returnType != null) 480 { 481 if (debug) System.out.println("\n\nparsed returntype: " + myType + "\n as: " + expectedVal + "\n"); 482 entry.remove("returntype"); 483 } 484 return expectedVal; 485 } 486 487 488 489 490 496 497 public void addEntry(DXEntry entry, boolean expectedValue) 498 { 499 if (debug) System.out.println("\nADD: " + entry); 500 501 try 502 { 503 myOps.addEntry(entry.getDN(), entry); if (expectedValue == false) 505 error("\nadd operation unexpectedly succeeded for: " + entry + "\n", null); 506 } 507 catch (NamingException e) 508 { 509 if (expectedValue == true) 510 error("\nadd operation failed for: " + entry + "\n", e); 511 } 512 } 513 514 515 521 522 public void deleteEntry(DXEntry entry, boolean expectedValue) 523 { 524 if (debug) System.out.println("\nDELETE: " + entry); 525 526 try 527 { 528 myOps.deleteEntry(entry.getDN()); if (expectedValue == false) 530 error("\ndelete operation unexpectedly succeeded for: " + entry + "\n", null); 531 } 532 catch (NamingException e) 533 { 534 if (expectedValue == true) 535 error("\ndelete operation failed for: " + entry + "\n", e); 536 } 537 } 538 539 540 546 547 public void deleteTree(DXEntry entry, boolean expectedValue) 548 { 549 if (debug) System.out.println("\nDELETE: " + entry); 550 551 try 552 { 553 myOps.deleteTree(entry.getDN()); 554 if (expectedValue == false) 555 error("\ndeleteTree operation unexpectedly succeeded for: " + entry + "\n", null); 556 } 557 catch (NamingException e) 558 { 559 if (expectedValue == true) 560 error("\ndeleteTree operation failed for: " + entry + "\n", e); 561 } 562 } 563 564 565 566 574 575 public String modRDN(DXEntry entry) 576 { 577 578 DXAttribute modRdnResult = (DXAttribute)entry.get("modrdnresult"); 579 580 String myModRdnCn = ""; 581 try{ myModRdnCn = modRdnResult.get().toString(); } catch (Exception e) {} 582 583 if(modRdnResult != null) 584 { 585 if (debug) System.out.println("\n\nparsed modrdnresult (DXAttribute): " + modRdnResult + "\n to mymodrdnCn(String): " + myModRdnCn); 586 entry.remove("modrdnresult"); 587 } 588 return myModRdnCn; 589 } 590 591 592 593 599 600 public void modrdnEntry(DXEntry entry, String expectedModrdn) 601 { 602 String newDNString = entry.getString("newrdn"); 603 DN newDN = new DN(newDNString); 604 if (debug) System.out.println("modrdn: " + entry.getDN() + "\n to: " + newDN); 605 606 try 607 { 608 myOps.renameEntry(entry.getDN(), newDN); 609 } 610 catch (NamingException e) 611 { 612 error("modrdn operation failed for: " + entry, null); 613 } 614 615 int compare = -2; 617 compare = expectedModrdn.compareTo(newDNString); 619 if(compare != 0) { 621 if (debug) System.out.println("\n\nnewDN CN String: " + newDNString); 622 if (debug) System.out.println( "EXPECTEDVALUE : " + expectedModrdn); 623 error("\nmodrdn operation failed for: " + entry + "\nExpected read result for cn: " + expectedModrdn + "\nActual result for cn: " + newDNString, null); 624 } 625 } 626 627 628 629 635 636 public void modifyEntry(DXEntry entry, boolean expectedValue) 637 { 638 if (debug) System.out.println("modify: " + entry); 639 640 Name myDN = entry.getDN(); 641 642 643 681 682 DXAttribute add = null; 683 DXAttribute delete = null; 684 DXAttribute replace = null; 685 686 add = (DXAttribute)entry.get("add"); 687 delete = (DXAttribute)entry.get("delete"); 688 replace = (DXAttribute)entry.get("replace"); 689 690 691 702 if(add != null) { 704 String attrString = ""; 705 try{ attrString = add.get().toString(); } catch (Exception e) {} 707 DXAttribute attr = (DXAttribute)entry.get(attrString); 708 709 try 710 { 711 myOps.addAttribute(myDN, attr); 712 if (expectedValue == false) 713 error("\nmodify-add operation unexpectedly succeeded for: " + entry + "\n", null); 714 } 715 catch (NamingException e) 716 { 717 if(expectedValue == true) 718 error("\nmodify-add operation failed for: " + entry + "\n", e); 719 } 720 } 721 722 723 734 if(delete != null) { 736 String attrString = ""; 737 try{ attrString = delete.get().toString(); } catch (Exception e) {} 739 DXAttribute attr = (DXAttribute)entry.get(attrString); 740 741 try 742 { 743 myOps.deleteAttribute(myDN, attr); 744 if (expectedValue == false) 745 error("\nmodify-delete operation unexpectedly succeeded for: " + entry + "\n", null); 746 } 747 catch (NamingException e) 748 { 749 if(expectedValue == true) 750 error("\nmodify-delete operation failed for: " + entry + "\n", e); 751 } 752 } 753 754 755 766 if(replace != null) { 768 String attrString = ""; 769 try{ attrString = replace.get().toString(); } catch (Exception e) {} 771 DXAttribute attr = (DXAttribute)entry.get(attrString); 772 773 try 774 { 775 myOps.updateAttribute(myDN, attr); 776 if (expectedValue == false) 777 error("\nmodify-replace operation unexpectedly succeeded for: " + entry + "\n", null); 778 } 779 catch (NamingException e) 780 { 781 if(expectedValue == true) 782 error("\nmodify-replace operation failed for: " + entry + "\n", e); 783 } 784 } 785 } 786 787 788 789 796 797 public int list(DXEntry entry) 798 { 799 800 DXAttribute listResult = (DXAttribute)entry.get("listresult"); 801 802 String myListResult = ""; 803 try{ myListResult = listResult.get().toString(); } catch (Exception e) {} 804 805 int list = -1; 806 try{list = Integer.parseInt(myListResult); } catch (Exception e) {} 807 808 if(listResult != null) 809 { 810 if (debug) System.out.println("\n\nparsed listresult(DXAttribute): " + listResult + "\n to myListResult(String): " + myListResult + ", to list(int): " + list); 811 entry.remove("listresult"); 812 } 813 return list; 814 } 815 816 817 818 824 825 public void listEntry(DXEntry entry, int expectedList) 826 throws NamingException 827 { 828 if (debug) System.out.println("\nlist: " + entry); 829 NamingEnumeration names = myOps.list(entry.getDN()); 830 831 if (debug) out.println("\nlist of children:"); 832 833 int i = 0; 835 while (names.hasMore()) { 837 i++; 838 if (debug) out.println(((NameClassPair)names.next()).getName()); 839 } 840 841 if(i != expectedList) 842 error("\nList operation failed for: " + entry + "\nExpected list results: " + expectedList + "\nActual list results: " + i, null); 843 } 844 845 846 847 854 855 public int getSearchResultAtt(DXEntry entry) 856 { 857 858 DXAttribute searchResult = (DXAttribute)entry.get("searchresult"); 859 860 String mySearch = ""; 861 try{ mySearch = searchResult.get().toString(); } catch (Exception e) {} 862 863 int search = -1; 864 try{search = Integer.parseInt(mySearch); } catch (Exception e) {} 865 866 if(searchResult != null) 867 { 868 if (debug) System.out.println("\n\nparsed searchresult(DXAttribute): " + searchResult + "\n to mySearch(String): " + mySearch + ", to myResult(int): " + search); 869 entry.remove("searchresult"); 870 } 871 return search; 872 } 873 874 875 876 877 889 890 public void searchEntry(DXEntry entry, int expectedSearch) 891 throws NamingException 892 { 893 String filter = entry.getString("filter"); 894 String limit = entry.getString("limit"); 895 String timeout = entry.getString("timeout"); 896 if (limit == null) limit = "0"; 897 if (timeout == null) timeout = "0"; 898 899 int lim = Integer.parseInt(limit); 900 int time = Integer.parseInt(timeout); 901 902 if (debug) System.out.println("\nSEARCH " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout); 903 904 905 NamingEnumeration names = myOps.searchSubTree(entry.getDN(), filter, lim, time); 906 if (debug) out.println("\nNAMES: " + names + "\n\nDN: " + entry); 907 908 if (debug) out.println("\nsubtree search results:"); 909 910 int i = 0; 912 while (names.hasMore()) { 914 i++; 915 if (debug) out.println(((SearchResult)names.next()).getName()); 916 } 917 918 if(i != expectedSearch) 919 error("\nSearch operation failed for: " + entry + "\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null); 920 } 921 922 923 924 936 937 public void searchOneLevelEntry(DXEntry entry, int expectedSearch) 938 throws NamingException 939 { 940 String filter = entry.getString("filter"); 941 String limit = entry.getString("limit"); 942 String timeout = entry.getString("timeout"); 943 if (limit == null) limit = "0"; 944 if (timeout == null) timeout = "0"; 945 946 if (debug) System.out.println("\n\nSEARCHONELEVEL: " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout); 947 948 int lim = Integer.parseInt(limit); 949 int time = Integer.parseInt(timeout); 950 NamingEnumeration names = myOps.searchOneLevel(entry.getDN(), filter, lim, time); 951 if (debug) out.println("\n\none level search results:"); 952 953 int i = 0; 955 while (names.hasMore()) { 957 i++; 958 if (debug) out.println(((SearchResult)names.next()).getName()); 959 } 960 961 if(i != expectedSearch) 962 error("\n\nSearchOneLevel operation failed for: " + entry + "\n\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null); 963 } 964 965 983 984 public void searchSorted(DXEntry entry, int expectedSearch) {} 985 1072 1073 1074 1082 1083 public String read(DXEntry entry) 1084 { 1085 DXAttribute readResultCn = (DXAttribute)entry.get("readresultcn"); 1086 1087 String myReadCn = ""; 1088 try{ myReadCn = readResultCn.get().toString(); } catch (Exception e) {} 1089 1090 if(readResultCn != null) 1091 { 1092 if (debug) System.out.println("\n\nparsed readresultcn (DXAttribute): " + readResultCn + "\n to myReadCn(String): " + myReadCn); 1093 entry.remove("readresultcn"); 1094 } 1095 return myReadCn; 1096 } 1097 1098 1099 1100 1107 1108 public void readEntry(DXEntry entry, String expectedRead) 1109 throws NamingException 1110 { 1111 if (debug) System.out.println("\nread: " + entry); 1112 1113 Attributes atts = myOps.read(entry.getDN()); 1114 if (atts == null) 1115 { 1116 throw new NamingException("\nUnable to read entry " + entry.getDN()); 1117 } 1118 1119 Attribute readCn = atts.get("cn"); 1120 if (debug) out.println("\nREAD: " + readCn); 1121 1122 try 1123 { 1124 if(!readCn.contains(expectedRead)) { 1126 for(int i=0; i<readCn.size();i++) 1127 if (debug) out.println("\n\nREAD CN String: " + readCn.get(i)); 1128 if (debug) out.println( "EXPECTEDVALUE : " + expectedRead); 1129 1130 error("\nRead operation failed for: " + entry + "\nExpected read result for cn: " + expectedRead + "\nActual read result for cn: " + readCn.toString(), null); 1131 } 1132 } 1133 catch (Exception e) {} 1134 1135 1136 DXEntry val = new DXEntry(atts); 1137 if (debug) out.println(val); 1138 } 1139 1140 1141 1142 1150 1151 public String copy(DXEntry entry, DXAttribute command) 1152 { 1153 DXAttribute copyTo = (DXAttribute)entry.get("copyTo"); 1154 1155 String myCopyTo = ""; 1156 try{ myCopyTo = copyTo.get().toString(); } catch (Exception e) {} 1157 1158 if(copyTo != null) 1159 { 1160 if (debug) System.out.println("copyTo: " + copyTo + "\ncommand: " + command + "\nmyCopyTo: " + myCopyTo); 1161 if (debug) System.out.println("\n\nparsed copyTo (DXAttribute): " + copyTo + "\n to myCopyTo(String): " + myCopyTo); 1162 entry.remove("copyTo"); 1163 } 1164 return myCopyTo; 1165 } 1166 1167 1168 1169 1174 1175 public void copy(DXEntry oldEntry, String newEntry) 1176 { 1177 1178 1179 Name newDN = myOps.postParse(newEntry); Name oldDN = oldEntry.getDN(); 1181 1182 if (debug) System.out.println("old DN: " + oldDN); 1183 if (debug) System.out.println("new DN: " + newDN); 1184 1185 NamingEnumeration namesOld = null; 1186 try 1187 { 1188 namesOld = myOps.list(oldDN); 1189 } 1190 catch (NamingException e) 1191 { 1192 error("\nUnable to list: " + oldDN, e); 1193 } 1194 1195 if (debug) out.println("\nCopy of OLD children:"); 1196 1197 int n = 0; 1199 try 1200 { 1201 while (namesOld.hasMore()) { 1203 n++; 1204 if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName()); 1205 } 1206 } 1207 catch (Exception e) 1208 { 1209 if (debug) System.out.println("List for OLD entries failed during copy process"); 1210 } 1211 1212 try 1213 { 1214 myOps.copyTree(oldDN, newDN); } 1216 catch (NamingException e) 1217 { 1218 error("\nUnable to copy tree" + newDN, e); 1219 } 1220 1221 if (debug) System.out.println("Copy: " + oldEntry); 1222 1223 NamingEnumeration namesNew = null; 1224 try 1225 { 1226 namesNew = myOps.list(newDN); 1227 } 1228 catch (NamingException e) 1229 { 1230 error("\nUnable to list " + newDN, e); 1231 } 1232 1233 1234 if (debug) out.println("\nCopy of NEW children:"); 1235 1236 int i = 0; 1238 try 1239 { 1240 while (namesNew.hasMore()) { 1242 i++; 1243 if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName()); 1244 } 1245 } 1246 catch (Exception e) 1247 { 1248 if (debug) System.out.println("List for NEW entries failed during copy process"); 1249 } 1250 1251 if(i != n) error("\nCopy operation failed for: " + oldEntry + "\nExpected number of copied entries: " + n + "\nActual number of copied entries: " + i, null); 1253 } 1254 1255 1256 1257 1265 1266 public String cut(DXEntry entry, DXAttribute command) 1267 { 1268 DXAttribute cutTo = (DXAttribute)entry.get("cutTo"); 1269 1270 String myCutTo = ""; 1271 try{ myCutTo = cutTo.get().toString(); } catch (Exception e) {} 1272 1273 if(cutTo != null) 1274 { 1275 if (debug) System.out.println("cutTo: " + cutTo + "\ncommand: " + command + "\nmyCutTo: " + myCutTo); 1276 if (debug) System.out.println("\n\nparsed cutTo (DXAttribute): " + cutTo + "\n to myCutTo(String): " + myCutTo); 1277 entry.remove("cutTo"); 1278 } 1279 return myCutTo; 1280 } 1281 1282 1283 1284 1289 1290 public void cut(DXEntry oldEntry, String newEntry) 1291 { 1292 1293 1294 Name newDN = myOps.postParse(newEntry); Name oldDN = oldEntry.getDN(); 1296 1297 if (debug) System.out.println("old: " + oldDN); 1298 if (debug) System.out.println("new: " + newDN); 1299 1300 NamingEnumeration namesOld = null; 1301 try 1302 { 1303 namesOld = myOps.list(oldDN); 1304 } 1305 catch (NamingException e) 1306 { 1307 error("\nUnable to list " + oldDN, e); 1308 } 1309 1310 if (debug) out.println("\nCut of OLD children:"); 1311 1312 int n = 0; 1314 try 1315 { 1316 while (namesOld.hasMore()) { 1318 n++; 1319 if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName()); 1320 } 1321 } 1322 catch (Exception e) 1323 { 1324 if (debug) System.out.println("List for OLD entries failed during cut process"); 1325 } 1326 1327 try 1328 { 1329 myOps.moveTree(oldDN, newDN); } 1331 catch (NamingException e) 1332 { 1333 error("\nUnable to list " + newDN, e); 1334 1335 } 1336 1337 if (debug) System.out.println("Copy: " + oldEntry); 1338 NamingEnumeration namesNew = null; 1339 try 1340 { 1341 namesNew = myOps.list(newDN); 1342 } 1343 catch (NamingException e) 1344 { 1345 error("\nUnable to list " + newDN, e); 1346 } 1347 1348 1349 if (debug) out.println("\nCut of NEW children:"); 1350 1351 int i = 0; 1353 try 1354 { 1355 while (namesNew.hasMore()) { 1357 i++; 1358 if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName()); 1359 } 1360 } 1361 catch (Exception e) 1362 { 1363 if (debug) System.out.println("List for NEW entries failed during cut process"); 1364 } 1365 1366 if(i != n) error("\nCut operation failed for: " + oldEntry + "\nExpected number of cut entries: " + n + "\nActual number of cut entries: " + i, null); 1368 } 1369 1370 1371 1372 1376 1377 public void connect(DXEntry entry) 1378 { 1379 if (debug) System.out.println("connect: " + entry); 1380 try 1381 { 1382 if (myOps != null) myOps.close(); 1383 } 1384 catch (NamingException e) 1385 { 1386 error("\nUnable to close old connection when opening new connection", e); 1387 } 1388 1389 String url = entry.getString("url"); 1390 String user = entry.getString("user"); 1391 String pwd = entry.getString("pwd"); 1392 String tracing = entry.getString("tracing"); 1393 String version = entry.getString("ldapVersion"); 1394 String referral = entry.getString("referral"); 1395 String useSSL = entry.getString("useSSL"); 1396 1397 boolean trace = ((tracing != null) && (tracing.equalsIgnoreCase("true"))); 1398 boolean ssl = ((useSSL != null) && (useSSL.equalsIgnoreCase("true"))); 1399 openConnection(url, user, pwd, trace, version, referral, ssl); 1400 } 1401 1402 1403 1404 1407 1408 public void disconnect(DXEntry entry) 1409 { 1410 if (debug) System.out.println("disconnected. "); 1411 try 1412 { 1413 myOps.close(); 1414 } 1415 catch (NamingException e) 1416 { 1417 error("\nUnable to close connection in disconnect", e); 1418 } 1419 } 1420 1421 1422 1423 1426 1427 public void error(String msg, Exception e) 1428 { 1429 out.println(msg + "\n"); 1430 1431 if (e==null) 1432 out.println(" (null error)"); 1433 else 1434 if(printstack) 1435 e.printStackTrace(out); 1436 else 1437 out.println(" error: " + e); 1438 1439 if (terminating) 1440 System.exit(-1); 1441 } 1442} | Popular Tags |