KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > jndi > jnditest


1 package com.ca.commons.jndi;
2
3 import java.io.*;
4 import com.ca.commons.naming.*;
5 // REQUIRES EXTENSION JAR FILE FOR SORT CONTROL TESTING import com.sun.jndi.ldap.ctl.*;
6
import javax.naming.*;
7 import javax.naming.directory.*;
8
9
10
11 /**
12 * This is a general test bed interface to test the various jndi calls
13 * used by JXplorer. It used ldif change file format requests, and can
14 * be run directly from the command line, or given an ldif change file
15 * to process.<p>
16 *
17 * Requests are either standard ldif change file requests, such as:
18 * <pre>
19 *
20 * version: 1
21 * # Add a new entry
22 * dn: cn=Fiona Jensen, ou=Marketing, dc=airius, dc=com
23 * changetype: add
24 * objectclass: top
25 * objectclass: person
26 * objectclass: organizationalPerson
27 * cn: Fiona Jensen
28 * sn: Jensen
29 * uid: fiona
30 * telephonenumber: +1 408 555 1212
31 * jpegphoto:< file:///usr/local/directory/photos/fiona.jpg
32 * <pre>
33 * where valid 'changetype' values are [add|delete|modrdn|modify]
34 * (see draft-good-ldap-ldif-04.html) <p>
35 * This is extended by jnditest to include extra (non-standard) changetype
36 * commands, such as [connect|disconnect|list|search|searchOneLevel|read], to allow
37 * general directory access and testing.
38 * The 'connect' command takes the attributes; 'url', and the optional \n"+
39 * attributes: 'user','pwd','tracing','ldapVersion','referral' and 'useSSL' \n" +
40 *
41 * XXX coming soon - [copyTree|deleteTree|moveTree]
42 *
43 */

44
45 public class jnditest
46 {
47    /**
48     * Open the appropriate input stream, and create a jdbctest object.
49     */

50   // static String version = "1.0(build"+BuildNumber.value +")";
51

52     BufferedReader in;
53
54     PrintStream out;
55
56     DXOps myOps = null;
57     //AdvancedOps myAdvOps = null;
58

59     LdifUtility ldifutil = new LdifUtility(); // ldif utility used to read / write ldif files
60

61     boolean debug = true;
62     boolean terminating = false; // whether the program should fail, returning -1, on error
63
boolean printstack = false; //TE: whether to print the stack trace.
64

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 JavaDoc args[])
70     {
71        String JavaDoc fileName = null;
72         String JavaDoc url = null;
73         String JavaDoc user = null;
74         String JavaDoc pwd = null;
75         String JavaDoc version = "3";
76         String JavaDoc 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 JavaDoc 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; // print and exit program
99

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; // print and exit program
136
}
137                 i++;
138             }
139         }
140         catch (Exception JavaDoc e)
141         {
142             System.out.println("Error reading command line arguments.");
143             printHelp("");
144             System.exit(-1);
145         }
146
147         // create jnditest object
148
jnditest tester = new jnditest(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag);
149
150         // use jnditest object to process data until input finished.
151
tester.processInput();
152
153         tester.out.println("\nnormal finish\n");
154     }
155
156
157     public static void printHelp(String JavaDoc 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    /**
223     * Constructs a jnditest object, opening a connection (if non-null input is
224     * passed for connection opening) and opening an input file (if a non-null
225     * file name is passed).<p>
226     *
227     * All the following parameters can be null.
228     *
229     * @param fileName the name of an LDIF changes input file.
230     * @param url a url of the form ldap://hostname:portnumber.
231     * @param user a user to bind to the directory as.
232     * @param pwd the user's password.
233     * @param tracing whether to set BER tracing on or not.
234     * @param version the LDAP Version (2 or 3) being used.
235     * @param debugFlag echo all system statement.
236     * @param terminateFlag exit on error, returning -1.
237     * @param referral the jndi referral type [follow|ignore|throw].
238     * @param useSSL to use SSL.
239     * @param printstackFlag whether to print a stack trace.
240     */

241
242     public jnditest(String JavaDoc fileName, String JavaDoc url, String JavaDoc user, String JavaDoc pwd, boolean tracing, String JavaDoc version, boolean debugFlag, boolean terminateFlag, String JavaDoc referral, boolean useSSL, boolean printstackFlag)
243     {
244         out = System.out; // may want to make this configurable in future :-)
245

246         debug = debugFlag;
247         terminating = terminateFlag;
248         printstack = printstackFlag;
249
250         // open a connection (if we've been given one)
251
if (url != null)
252             openConnection(url, user, pwd, tracing, version, referral, useSSL);
253
254         // open an input stream - file if we have one, console if not...
255
if (fileName != null)
256             in = openFile(fileName);
257         else
258             in = new BufferedReader(new InputStreamReader(System.in));
259     }
260
261    /**
262     * Open an ldap connection using jndi, via the standard JXplorer classes
263     * (DXOps.java, which wraps BasicOps.java).
264     *
265     * @param url a url of the form ldap://hostname:portnumber
266     * @param user a user to bind to the directory as.
267     * @param pwd the user's password.
268     * @param tracing whether to set BER tracing on or not
269     * @param version the LDAP Version (2 or 3) being used.
270     * @param referralType the jndi referral type [follow|ignore|throw].
271     * @param useSSL to use SSL.
272     */

273
274     public void openConnection(String JavaDoc url, String JavaDoc user, String JavaDoc pwd, boolean tracing, String JavaDoc version, String JavaDoc 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"; // default to ldap version 3
291

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 JavaDoc e)
314         {
315             error("error opening connection " + url + "\n ", e);
316         }
317     }
318
319    /**
320     * Opens a text file as a Buffered Reader.
321     *
322     * @param fileName the name of the file to open.
323     * @return the Input Stream.
324     */

325
326     public BufferedReader openFile(String JavaDoc 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 JavaDoc e)
335         {
336             error("unable to open file : " + fileName + "\n ", e);
337             System.exit(-1);
338         }
339         return null;
340     }
341
342    /**
343     * Reads input as an ldif entry at a time
344     * (i.e. as a series of colon seperated att:value pairs, until
345     * a blank line or eof is read).
346     */

347
348     public void processInput()
349     {
350         // start the infinite loop...
351
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 JavaDoc e)
364         {
365             error("error reading input - program stopped." + "\n ", e);
366             System.exit(-1);
367         }
368     }
369
370    /**
371     * This checks that an entry is valid, and
372     * then passes it to the appropriate command handler.
373     * @param entry the DXEntry object containing a dn attribute,
374     * a changetype attribute, and any data attributes required
375     * by the ldif command.
376     * @return code - 0 = normal, 1 = error, -1 = finished.
377     */

378
379     public int processCommand(DXEntry entry)
380     {
381     // do some sanity checking
382
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"); // strip out version number.
387

388         if (entry.getDN().size() == 0)
389         {
390             DXAttribute temp = (DXAttribute)entry.get("changetype");
391             String JavaDoc test = "";
392             try{ test = temp.get().toString(); } catch (Exception JavaDoc e) {} // never happen
393
// two commands do not require a dn...
394
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 JavaDoc commandString = "";
406
407         try { commandString = command.get().toString(); }
408         catch (NamingException e) { error("internal error in processCommand()\n ", e); return ERROR;} // never happen :-)
409

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         // branch to appropriate handler
418
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    /**
461     * Gets the 'returntype' value from the test ldif file and parses it into a string.
462     * 'returntype' is a boolean value pertaining to the add, delete or modify 'changetype'
463     * functions in the ldif file. i.e. it signifies if an entry should actually be
464     * added or deleted. Changes the value to boolean. Removes the 'returntype' attribute.
465     * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
466     * @return expectedVal a boolean value that represents the success or failure of the modification.
467     */

468
469     public boolean returnType(DXEntry entry)
470     {
471
472         DXAttribute returnType = (DXAttribute)entry.get("returntype");
473
474         String JavaDoc myType = "";
475         try{ myType = returnType.get().toString(); } catch (Exception JavaDoc 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    /**
491     * Add an entry to a directory. Compares the 'returntype' value in the ldif file (if
492     * present) to the result of the add operation. If the two don't match the program exits.
493     * @param entry contains the att/val pairs to be added, and the dn to add them to.
494     * @param expectedValue a flag that indicates the modification success.
495     */

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); //TE: flag to test if the modification succeeded or not.
504
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    /**
516     * Delete an entry from a directory. Compares the 'returntype' value in the ldif file (if
517     * present) to the result of the delete operation. If the two don't match the program exits.
518     * @param entry contains dn to delete.
519     * @param expectedValue exptectedValue a flag that indicates the modification success.
520     */

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()); //TE: flag to test if the modification succeeded or not.
529
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     /**
541      * Deletes a sub tree from a directory. Compares the 'returntype' value in the ldif file (if
542      * present) to the result of the delete operation. If the two don't match the program exits.
543      * @param entry contains dn to delete.
544      * @param expectedValue exptectedValue a flag that indicates the modification success.
545      */

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    /**
567     * Gets the 'modrdnresult' value from the test ldif file and parses into a string.
568     * 'modrdnresult' is the 'cn' attribute of the entry after the rename.
569     * Removes the 'modrdnresult' attribute. Note: 'modrdnresult' can be configured to
570     * represent any string attribute.
571     * @param entry contains the dn to start searching from, and a 'filter' attribute with a search filter.
572     * @return myModRdnCn
573     */

574
575     public String JavaDoc modRDN(DXEntry entry)
576     {
577
578         DXAttribute modRdnResult = (DXAttribute)entry.get("modrdnresult");
579
580         String JavaDoc myModRdnCn = "";
581         try{ myModRdnCn = modRdnResult.get().toString(); } catch (Exception JavaDoc 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    /**
594     * Modifies the rdn of a directory. Compares the 'modrdnresult' value in the ldif file (if
595     * present) to the rdn of the entry. If the two don't match the program exits.
596     * @param entry contains dn to be modified.
597     * @param expectedModrdn a flag that indicates the value that the modify is expected to return.
598     */

599
600     public void modrdnEntry(DXEntry entry, String JavaDoc expectedModrdn)
601     {
602         String JavaDoc 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; //TE: the flag to compare the expected DN with the actual DN.
616

617         compare = expectedModrdn.compareTo(newDNString); //TE: the compare of the expected DN with the actual DN.
618

619         if(compare != 0) //TE: if 0, the two results are the same, therefore the modify performed as expected.
620
{
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    /**
630     * Modifies the attributes of an entry in a directory. Compares the 'returntype' value in the ldif file (if
631     * present) to the result of the modify operation. If the two don't match the program exits.
632     * @param entry contains the entry to be modified, and the list of att/val modification pairs (see ldif spec.).
633     * @param expectedValue a flag that indicates the modification success.
634     */

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         /*TE: The three operations to modify an entry are:
644                                 add
645                                 delete
646                                 replace
647                 A test is done to see which operation is to be carried out.
648
649                 Note: within one ldif entry at this stage we can't do multiple operations
650                 on the same attribute or the same operations on multiple attributes
651                 because of the way the parser works,
652                 for example:
653                                 dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
654                                 changetype: modify
655                                 add: drink
656                                 drink: red wine
657                                 drink: white wine
658                                 -
659                                 delete: drink
660                                 drink: red wine
661
662                 would result in both the 'drink' attributes being deleted. To delete just the
663                 'red wine' attribute, write another ldif entry,
664                 for example:
665                                 dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
666                                 changetype: modify
667                                 delete: drink
668                                 drink: red wine
669
670                 however, deleting another attribute not used by the add operation should work fine
671                 for example:
672                                 dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
673                                 changetype: modify
674                                 add: drink
675                                 drink: red wine
676                                 drink: white wine
677                                 -
678                                 delete: cn
679                                 cn: TEST
680         */

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         //TE: Adds new attribute values to the specified attribute in the directory,
692
// for example:
693
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
694
// changetype: modify
695
// add: drink
696
// drink: red wine
697
// drink: white wine
698
// drink: water
699
//
700
// will add three new 'drink' attributes to the entry.
701

702         if(add != null) //TE: tests if the modify operation is 'add'.
703
{
704             String JavaDoc attrString = "";
705             try{ attrString = add.get().toString(); } catch (Exception JavaDoc e) {} // never happen
706

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         //TE: Deletes attribute values of a specified attribute within a specified entry,
724
// for example:
725
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
726
// changetype: modify
727
// delete: drink
728
// drink: red wine
729
// drink: white wine
730
// drink: water
731
//
732
// will delete these three 'drink' attributes from the entry.
733

734         if(delete != null) //TE: tests if the modify operation is 'delete'.
735
{
736             String JavaDoc attrString = "";
737             try{ attrString = delete.get().toString(); } catch (Exception JavaDoc e) {} // never happen
738

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         //TE: Replaces all of the values of a specified attribute in the specified entry,
756
// for example:
757
// dn: cn=T,ou=Applications,ou=Customer,o=Democorp,c=AU
758
// changetype: modify
759
// replace: drink
760
// drink: beer
761
//
762
// will replace all of the current values of attribute 'drink' with value 'beer'.
763
// In other words, if there are currently multiple 'drink' attributes they all
764
// will be removed and replaced with this one new value.
765

766         if(replace != null) //TE: tests if the modify operation is 'replace'.
767
{
768             String JavaDoc attrString = "";
769             try{ attrString = replace.get().toString(); } catch (Exception JavaDoc e) {} // never happen
770

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    /**
790     * Gets the 'listresult' from the test ldif file and parses it into a string.
791     * 'searchresult' is an integer value pertaining to the amount of list results
792     * expected to be returned from a search. Converts to int. Removes the 'listresult' attribute.
793     * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
794     * @return list an integer value representing the amount of entries that are expected to be returned by the list.
795     */

796
797     public int list(DXEntry entry)
798     {
799
800         DXAttribute listResult = (DXAttribute)entry.get("listresult");
801
802         String JavaDoc myListResult = "";
803         try{ myListResult = listResult.get().toString(); } catch (Exception JavaDoc e) {}
804
805         int list = -1;
806         try{list = Integer.parseInt(myListResult); } catch (Exception JavaDoc 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    /**
819     * Lists an entry in a directory. Compares the 'listresult' value in the ldif file to the
820     * number of entries returned by the list. If the two are not equal the program exists.
821     * @param entry contains the dn to start searching from, and a 'filter' attribute with a search filter.
822     * @param expectedList a flag that indicates the number of entries that are expected to be listed.
823     */

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; //TE: the flag to compare the expected number of list results with the actual number of list results.
834

835         while (names.hasMore()) //TE: Counts & lists entries.
836
{
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    /**
848     * Gets the 'searchresult' value from the test ldif file and parses it into a string.
849     * 'searchresult' is an integer value pertaining to the amount of search results
850     * expected to be returned from a search. Converts to int. Removes the 'searchresult' attribute.
851     * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
852     * @return search an integer value representing the amount of entries expected to be returned by the search.
853     */

854
855     public int getSearchResultAtt(DXEntry entry)
856     {
857
858         DXAttribute searchResult = (DXAttribute)entry.get("searchresult");
859
860         String JavaDoc mySearch = "";
861         try{ mySearch = searchResult.get().toString(); } catch (Exception JavaDoc e) {}
862
863         int search = -1;
864         try{search = Integer.parseInt(mySearch); } catch (Exception JavaDoc 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    /**
878     * Searches a directory. Compares the 'searchresult' value in the ldif file to the
879     * number of entries returned by the search. If the two are not equal the program exists.
880     * @param entry contains.
881     * @param expectedSearch a flag that indicates the number of results returned by the search.
882     * <ul>
883     * <li> dn to start searching from
884     * <li> a 'filter' attribute containing an ldap search filter
885     * <li> an (optional) 'limit' attribute containing a number of returned entries limit
886     * <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
887     * </ul>
888     */

889
890     public void searchEntry(DXEntry entry, int expectedSearch)
891     throws NamingException
892     {
893         String JavaDoc filter = entry.getString("filter");
894         String JavaDoc limit = entry.getString("limit");
895         String JavaDoc 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; //TE: the flag to compare the expected number of search results with the actual number of search results.
911

912         while (names.hasMore()) //TE: Counts & lists search results.
913
{
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    /**
925     * Searches one level of a directory. Compares the 'searchresult' value in the ldif file to the
926     * number of entries returned by the search one level. If the two are not equal the program exists.
927     * @param entry contains.
928     * @param expectedSearch a flag that indicates the number of results returned by the search.
929     * <ul>
930     * <li> dn to start searching from.
931     * <li> a 'filter' attribute containing an ldap search filter.
932     * <li> an (optional) 'limit' attribute containing a number of returned entries limit.
933     * <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
934     * </ul>
935     */

936
937     public void searchOneLevelEntry(DXEntry entry, int expectedSearch)
938     throws NamingException
939     {
940         String JavaDoc filter = entry.getString("filter");
941         String JavaDoc limit = entry.getString("limit");
942         String JavaDoc 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; //TE: the flag to compare the expected number of search results with the actual number of search results.
954

955         while (names.hasMore()) //TE: Counts & lists search results.
956
{
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     /**
966      * Searches one level of a directory, and verifies that the returned results are sorted in the
967      * requested manner.
968      * @param entry contains.
969      * @param expectedSearch a flag that indicates the number of results returned by the search.
970      * <ul>
971      * <li> dn to start searching from.
972      * <li> a 'filter' attribute containing an ldap search filter.
973      * <li> an (optional) 'limit' attribute containing a number of returned entries limit.
974      * <li> an (optional) 'timeout' attribute containing a time limit (milliseconds).
975      * <li> a 'searchAttribute1' attribute as the primary search key
976      * <li> an (optional) 'searchAttribute2' attribute as the secondary search key
977      * </ul>
978      * <p>WARNING: This is a LIMITED test of sorting behaviour. Specifically, it uses case sensitive string matching,
979      * which is not appropriate to all attributes. However, any other behaviour would require this client to be
980      * aware of syntax matching rules, which is currently not implemented.
981      * </p>
982      */

983
984     public void searchSorted(DXEntry entry, int expectedSearch) {}
985     /* // REQUIRES SORT CONTROL CAPABILITY - USUALLY VIA AN EXTENSION JAR FILE...
986      public void searchSorted(DXEntry entry, int expectedSearch)
987      throws NamingException
988      {
989          try
990          {
991              String filter = entry.getString("filter");
992              String limit = entry.getString("limit");
993              String timeout = entry.getString("timeout");
994              String att1 = entry.getString("searchAttribute1");
995              String att2 = entry.getString("searchAttribute2");
996              if (limit == null) limit = "0";
997              if (timeout == null) timeout = "0";
998
999              if (debug) System.out.println("\n\nSEARCHSORTED: " + entry + "\n filter: " + filter + "\n limit: " + limit + "\n timeout: " + timeout);
1000
1001             int lim = Integer.parseInt(limit);
1002             int time = Integer.parseInt(timeout);
1003
1004            // set up server side sorting control
1005            Control[] ctxCtls;
1006
1007             String[] searchAttributes;
1008
1009             if (att2 == null)
1010                 searchAttributes = new String[]{att1};
1011             else
1012                 searchAttributes = new String[]{att1, att2};
1013
1014             ctxCtls = new Control[] { new SortControl(searchAttributes, Control.CRITICAL) };
1015
1016             LdapContext ctx = new InitialLdapContext(myOps.getContext().getEnvironment(), null);
1017             ctx.setRequestControls(ctxCtls);
1018
1019             // set up search controls
1020             SearchControls constraints = new SearchControls();
1021
1022             constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
1023             constraints.setCountLimit(lim);
1024             constraints.setTimeLimit(time);
1025
1026             constraints.setReturningAttributes(searchAttributes); // return the search attributes for verification
1027
1028             // do search
1029             NamingEnumeration names = ctx.search(entry.getDN(), filter, null);
1030
1031             if (debug)
1032                 out.println("\n\nsorted one level search results:");
1033
1034             int i = 0; //TE: the flag to compare the expected number of search results with the actual number of search results.
1035
1036
1037             String previousAtt1 = "";
1038             String previousAtt2 = "";
1039
1040             while (names.hasMore()) //TE: Counts & lists search results.
1041             {
1042                 i++;
1043                 SearchResult latest = (SearchResult)names.next();
1044                 if (debug) out.println(latest.getName());
1045
1046
1047                 String currentAtt1 = (String)latest.getAttributes().get(att1).get();
1048                 if (debug) out.println(" ... " + att1 + ": " + currentAtt1);
1049                 if (previousAtt1.compareTo(currentAtt1) < 0)
1050    // error("sorted results failed: attribute " + currentAtt1 + " is not greater than " + previousAtt1 + " in entry " + latest.getName(), null);
1051                 out.println("sorted results failed: attribute " + currentAtt1 + " is not greater than " + previousAtt1 + " in entry " + latest.getName());
1052
1053                 if (att2 != null)
1054                 {
1055                    String currentAtt2 = (String)latest.getAttributes().get(att2).get();
1056                     if (debug) out.println(" ... " + att2 + ": " + currentAtt2);
1057                     if ((previousAtt1.equals(currentAtt1)) && (previousAtt2.compareTo(currentAtt2) < 0))
1058    // error("sorted results failed: attribute " + currentAtt2 + " is not greater than " + previousAtt2 + " in entry " + latest.getName(), null);
1059                     out.println("sorted results failed: attribute " + currentAtt2 + " is not greater than " + previousAtt2 + " in entry " + latest.getName());
1060                 }
1061             }
1062
1063             if(i != expectedSearch)
1064                 error("\n\nSearchSorted (OneLevel) operation failed for: " + entry + "\n\nExpected search results: " + expectedSearch + "\nActual search results: " + i, null);
1065         }
1066         catch (IOException e)
1067         {
1068             error("unexpected IOException constructing sorted search control", e);
1069         }
1070     }
1071    */

1072
1073
1074   /**
1075    * Gets the 'readresultcn' value from the test ldif file and parses into a string.
1076    * 'readresultcn' is the 'cn' attribute of the entry, for example cn: Cora BALDWIN.
1077    * Removes the 'readresultcn' attribute. Note: 'readresultcn' can be configured to
1078    * represent any string attribute.
1079    * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
1080    * @return myReadCn a string 'cn' that is expected to be returned by the read.
1081    */

1082
1083    public String JavaDoc read(DXEntry entry)
1084    {
1085        DXAttribute readResultCn = (DXAttribute)entry.get("readresultcn");
1086
1087        String JavaDoc myReadCn = "";
1088        try{ myReadCn = readResultCn.get().toString(); } catch (Exception JavaDoc 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   /**
1101    * Reads an entry in a directory. Currently compares the 'readresultcn' value in the ldif file
1102    * (can be configured to any value), to the 'cn' value of the result of the read. If the two
1103    * values are not equal the program exits.
1104    * @param entry contains the dn to list the attributes of.
1105    * @param expectedRead a flag that indicates the value that the read is expected to return.
1106    */

1107
1108    public void readEntry(DXEntry entry, String JavaDoc 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)) //TE: if 0, the two results are the same, therefore the read performed as expected.
1125
{
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 JavaDoc e) {}
1134
1135
1136        DXEntry val = new DXEntry(atts);
1137        if (debug) out.println(val);
1138    }
1139
1140
1141
1142   /**
1143    * Gets the 'copyTo' value from the test ldif file and parses into a string.
1144    * 'copyTo' is the 'DN' of the new entry to where the DN of the old entry gets copied to.
1145    * Removes the 'copyTo' attribute.
1146    * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
1147    * @param command the type of test to be performed, i.e copy.
1148    * @return myCopyTo the DN of the where the old entry is to be copy (or moved) to.
1149    */

1150
1151    public String JavaDoc copy(DXEntry entry, DXAttribute command)
1152    {
1153        DXAttribute copyTo = (DXAttribute)entry.get("copyTo");
1154
1155        String JavaDoc myCopyTo = "";
1156        try{ myCopyTo = copyTo.get().toString(); } catch (Exception JavaDoc 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   /**
1170    * Copies an entry to a new DN.
1171    * @param oldEntry is what is being copied.
1172    * @param newEntry is the new DN of where the entry is copied to.
1173    */

1174
1175    public void copy(DXEntry oldEntry, String JavaDoc newEntry)
1176    {
1177
1178
1179        Name newDN = myOps.postParse(newEntry); //TE: Converts the new DN string (copyTo) from the LDIF file into Name.
1180
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; //TE: Counter for OLD entries.
1198

1199        try
1200        {
1201            while (namesOld.hasMore()) //TE: Counts & lists OLD entries.
1202
{
1203                n++;
1204                if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName());
1205            }
1206        }
1207        catch (Exception JavaDoc 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); //TE: copies old entry to new entry.
1215
}
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; //TE: Counter for NEW entries.
1237

1238        try
1239        {
1240            while (namesNew.hasMore()) //TE: Counts & lists NEW entries.
1241
{
1242                i++;
1243                if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName());
1244            }
1245        }
1246        catch (Exception JavaDoc e)
1247        {
1248            if (debug) System.out.println("List for NEW entries failed during copy process");
1249        }
1250
1251        if(i != n) //TE: checks that the list contains the same number of entries.
1252
error("\nCopy operation failed for: " + oldEntry + "\nExpected number of copied entries: " + n + "\nActual number of copied entries: " + i, null);
1253    }
1254
1255
1256
1257   /**
1258    * Gets the 'cutTo' value from the test ldif file and parses into a string.
1259    * 'cutTo' is the 'DN' of the new entry to where the DN of the old entry gets cut (or moved) to.
1260    * Removes the 'cutTo' attribute.
1261    * @param entry the DXEntry object containing a dn attribute, a changetype attribute, and any data attributes required.
1262    * @param command the type of test to be performed, i.e cut.
1263    * @return myCutTo the DN of the where the old entry is to be cut (or moved) to.
1264    */

1265
1266    public String JavaDoc cut(DXEntry entry, DXAttribute command)
1267    {
1268        DXAttribute cutTo = (DXAttribute)entry.get("cutTo");
1269
1270        String JavaDoc myCutTo = "";
1271        try{ myCutTo = cutTo.get().toString(); } catch (Exception JavaDoc 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   /**
1285    * Cuts an entry to a new DN.
1286    * @param oldEntry is what is being cut.
1287    * @param newEntry is the new DN of where the entry is cut to.
1288    */

1289
1290    public void cut(DXEntry oldEntry, String JavaDoc newEntry)
1291    {
1292
1293
1294        Name newDN = myOps.postParse(newEntry); //TE: Converts the new DN string (copyTo) from the LDIF file into Name.
1295
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; //TE: Counter for OLD entries.
1313

1314        try
1315        {
1316            while (namesOld.hasMore()) //TE: Counts & lists OLD entries.
1317
{
1318                n++;
1319                if (debug) out.println("Old Entries: " + ((NameClassPair)namesOld.next()).getName());
1320            }
1321        }
1322        catch (Exception JavaDoc 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); //TE: cuts (or moves) old entry to new entry.
1330
}
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; //TE: Counter for NEW entries.
1352

1353        try
1354        {
1355            while (namesNew.hasMore()) //TE: Counts & lists NEW entries.
1356
{
1357                i++;
1358                if (debug) out.println("New Entries: " + ((NameClassPair)namesNew.next()).getName());
1359            }
1360        }
1361        catch (Exception JavaDoc e)
1362        {
1363            if (debug) System.out.println("List for NEW entries failed during cut process");
1364        }
1365
1366        if(i != n) //TE: checks that the list contains the same number of entries.
1367
error("\nCut operation failed for: " + oldEntry + "\nExpected number of cut entries: " + n + "\nActual number of cut entries: " + i, null);
1368    }
1369
1370
1371
1372   /**
1373    * Opens a connection.
1374    * @param entry contains a 'fake' entry with no dn, but a bunch of attributes.
1375    */

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 JavaDoc url = entry.getString("url");
1390        String JavaDoc user = entry.getString("user");
1391        String JavaDoc pwd = entry.getString("pwd");
1392        String JavaDoc tracing = entry.getString("tracing");
1393        String JavaDoc version = entry.getString("ldapVersion");
1394        String JavaDoc referral = entry.getString("referral");
1395        String JavaDoc 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   /**
1405    * Disconnected from the directory.
1406    */

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   /**
1424    * Prints error message then terminates.
1425    */

1426
1427    public void error(String JavaDoc msg, Exception JavaDoc 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