KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.ca.commons.jndi;
2
3
4 import java.util.*;
5 import java.util.logging.*;
6
7 import javax.naming.NamingException JavaDoc;
8
9 import com.ca.commons.cbutil.*;
10 import com.ca.commons.naming.DXEntry;
11
12
13
14 /**
15 * Opens and reads the property file @see FILENAME. Creates a BulkJndiTest
16 * object and passes args[] and the property file to it.
17 * Determines the options supplied by the user and sets these as global variables.
18 * Reads the property file (BulkJndiTest.txt) to determine:
19 * <p>
20 * The computer name.
21 * The port number.
22 * The number of Threads to be used.
23 * The number of times each Thread should run the jnditests on the LDIF data.
24 * The names of the LDIF files to be used by each Thread.
25 * </p>
26 * Creates a new Test object for each Thread then starts the Thread.
27 * @author Trudi.
28 */

29
30 public class TestJNDIBulkOps
31 {
32    /**
33     * The name of the property file that lists the LDIF files and which Threads they pertain to.
34     */

35     
36     final static String JavaDoc FILENAME = "BulkJndiTest.txt";
37         
38     private final static Logger log = Logger.getLogger(TestJNDIBulkOps.class.getName());
39
40         
41    /**
42     * Opens and reads the property file @see FILENAME. Creates a BulkJndiTest
43     * object and passes args[] and the property file to it.
44     * @param args any options supplied by the user. Can be
45     * <p>
46     * -d debug (verbose) mode. <br>
47     * -e exit on error, returning -1. <br>
48     * -h [subject] this help message [full|changetype]. <br>
49     * -p password an option password. <br>
50     * -r referral the jndi referral type [follow|ignore|throw]. <br>
51     * -t set BER tracing on. <br>
52     * -u userdn an optional user dn. <br>
53     * -v set ldap version (default 3). <br>
54     * -x print stack trace. <br>
55     * </p>
56     */

57             
58     public static void main (String JavaDoc args[])
59     {
60         log.addHandler(new ConsoleHandler());
61         log.setLevel(Level.FINE);
62
63         Properties properties = new Properties();
64
65         properties = CBUtility.readPropertyFile(FILENAME);
66         if (properties.size()==0) { log.warning("Can't find: " + FILENAME); return;}
67             
68         new TestJNDIBulkOps(args, properties);
69     }
70     
71     
72     
73    /**
74     * Determines the options supplied by the user and sets these as global variables.
75     * Reads the property file (BulkJndiTest.txt) to determine:
76     * <p>
77     * The number of Threads to be used.
78     * The number of times each Thread should run the jnditests on the LDIF data.
79     * The names of the LDIF files to be used by each Thread.
80     * </p>
81     * Creates a new Test object for each Thread then starts the Thread.
82     * @param args any options supplied by the user. Can be
83     * <p>
84     * -d debug (verbose) mode. <br>
85     * -e exit on error, returning -1. <br>
86     * -h [subject] this help message [full|changetype]. <br>
87     * -p password an option password. <br>
88     * -r referral the jndi referral type [follow|ignore|throw]. <br>
89     * -t set BER tracing on. <br>
90     * -u userdn an optional user dn. <br>
91     * -v set ldap version (default 3). <br>
92     * -x print stack trace. <br>
93     * </p>
94     * @param properties the property file (BulkJndiTest.txt).
95     */

96         
97     public TestJNDIBulkOps(String JavaDoc args[], Properties properties)
98     {
99         String JavaDoc url = null;
100         String JavaDoc user = null;
101         String JavaDoc pwd = null;
102         String JavaDoc version = "3";
103         String JavaDoc referral = "follow";
104         boolean useSSL = false;
105         boolean tracing = false;
106         boolean debugFlag = false;
107         boolean terminateFlag = false;
108         boolean printstackFlag = false;
109         
110         try
111         {
112             int i=0;
113             
114             while (i<args.length)
115             {
116                 String JavaDoc arg = (args[i].charAt(0) != '-')?args[i]:args[i].substring(1);
117                 switch(arg.charAt(0))
118                 {
119                     case '?':
120                     case 'H':
121                     case 'h': if (args.length>i+1)
122                                     System.out.println(args[i+1]);
123                               else
124                                     return;
125                     case 'D':
126                     case 'd': debugFlag = true; break;
127                     case 'E':
128                     case 'e': terminateFlag = true; break;
129                     case 'P':
130                     case 'p': pwd = args[++i]; break;
131                     case 'R':
132                     case 'r': referral = args[++i]; break;
133                     case 'S':
134                     case 's': useSSL = true; break;
135                     case 'T':
136                     case 't': tracing = true; break;
137                     case 'U':
138                     case 'u': user = args[++i]; break;
139                     case 'V':
140                     case 'v': version = args[++i]; break;
141                     case 'X':
142                     case 'x': printstackFlag = true; break;
143                     
144                     default : System.out.println("\n\nInvalid command line argument: -" + arg);
145                                     return;
146                 }
147                 i++;
148             }
149         }
150         catch (Exception JavaDoc e)
151         {
152             System.out.println("Error reading command line arguments.");
153             System.exit(-1);
154         }
155
156         
157         int numberOfThreads = -1; //TE: the number of threads that the user wants to use.
158
int numberOfIterations = -1; //TE: the number of times the ldif data is tested per thread.
159

160         try
161         {
162             //TE: find out how many threads the user wants to use...
163
numberOfThreads = Integer.parseInt(properties.getProperty("NumberOfThreads").toString());
164             
165             //TE: find out how many iterations per thread the user wants...
166
numberOfIterations = Integer.parseInt(properties.getProperty("NumberOfIterations").toString());
167             
168             //TE: sanity check...
169
if(numberOfThreads <=0 || numberOfThreads > 20)
170             {
171                 System.out.println("Problem accessing the number of threads you wish to run." +
172                                    " Check the property file 'BulkJndiTest.txt' and make sure the" +
173                                    " 'NumberOfThreads' entry contains a valid integer between 1 and 10.");
174                 System.exit(-1);
175             }
176         }
177         catch(NumberFormatException JavaDoc e)
178         {
179             System.out.println("Problem accessing the number of threads you wish to run." +
180                                " Check the property file 'BulkJndiTest.txt' and make sure the" +
181                                " 'NumberOfThreads' entry contains a valid integer between 1 and 10." +
182                                " Also check that the 'NumberOfIterations' contains a valid integer.");
183             System.exit(-1);
184         }
185         
186         url = properties.getProperty("ComputerName").toString()+":"+properties.getProperty("Port").toString();
187         
188         if (url==null)
189         {
190             System.out.println("Problem accessing the computer name from the property file 'BulkJndiTest.txt'."+
191                                 "Please check that a valid name is supplied in the 'ComputerName' entry.");
192             System.exit(-1);
193         }
194
195         Thread JavaDoc[] threads = new Thread JavaDoc[] {new Thread JavaDoc(new Test(properties.getProperty("ThreadOneLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread ONE"),
196                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadTwoLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread TWO"),
197                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadThreeLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread THREE"),
198                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadFourLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread FOUR"),
199                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadFiveLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread FIVE"),
200                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadSixLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread SIX"),
201                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadSevenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread SEVEN"),
202                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadEightLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread EIGHT"),
203                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadNineLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread NINE"),
204                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadTenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread TEN"),
205                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadElevenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread ELEVEN"),
206                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadTwelveLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread TWELVE"),
207                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadThreeteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread THIRTEEN"),
208                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadFourteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread FOURTEEN"),
209                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadFifteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread FIFTTEEN"),
210                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadSixteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread SIXTEEN"),
211                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadSeventeenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread SEVENTEEN"),
212                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadEighteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread EIGHTEEN"),
213                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadNineteenLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread NINETEEN"),
214                                          new Thread JavaDoc(new Test(properties.getProperty("ThreadTwentyLDIFFileName").toString(), url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag, numberOfIterations), "Test Thread TWENTY")};
215
216         for(int i=0; i<numberOfThreads; i++)
217         {
218             try
219             {
220                 threads[i].start();
221                 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"+
222                                  "Starting Thread: " + threads[i].getName() +
223                                  "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
224             }
225             catch(IllegalThreadStateException JavaDoc e)
226             {
227                 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"+
228                                    "Error Starting Thread: " + threads[i].getName() +
229                                    "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +e);
230             }
231         }
232     }
233     
234
235
236    /**
237     * Class basically kicks off the jnditests, but instead of only running
238     * through an LDIF file once, this allows the user to specify (via BulkJndiTest)
239     * how many iterations to do.
240     * @author Trudi.
241     */

242         
243     class Test implements Runnable JavaDoc
244     {
245         String JavaDoc fileName = null;
246         String JavaDoc url = null;
247         String JavaDoc user = null;
248         String JavaDoc pwd = null;
249         String JavaDoc version = "3";
250         String JavaDoc referral = "follow";
251         boolean useSSL = false;
252         boolean tracing = false;
253         boolean debugFlag = false;
254         boolean terminateFlag = false;
255         boolean printstackFlag = false;
256         int numberOfIterations = -1;
257             
258        /**
259         * Constructor that assigns the following global variables.
260         * @param fileName the name of an LDIF changes input file.
261         * @param url a url of the form ldap://hostname:portnumber.
262         * @param user a user to bind to the directory as.
263         * @param pwd the user's password.
264         * @param tracing whether to set BER tracing on or not.
265         * @param version the LDAP Version (2 or 3) being used.
266         * @param debugFlag echo all system statement.
267         * @param terminateFlag exit on error, returning -1.
268         * @param referral the jndi referral type [follow|ignore|throw].
269         * @param useSSL to use SSL.
270         * @param printstackFlag whether to print a stack trace.
271         * @param numberOfIterations the number of times the user wants the tests to run through the LDIF file.
272         */

273                 
274         public Test(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, int numberOfIterations)
275         {
276             this.fileName = fileName;
277             this.url = url;
278             this.user = user;
279             this.pwd = pwd;
280             this.version = version;
281             this.referral = referral;
282             this.useSSL = useSSL;
283             this.tracing = tracing;
284             this.debugFlag = debugFlag;
285             this.terminateFlag = terminateFlag;
286             this.printstackFlag = printstackFlag;
287             this.numberOfIterations = numberOfIterations;
288         }
289         
290         
291
292        /**
293         * Creates a new jnditest object for each time the user wants the jnditests to
294         * test an LDIF file.
295         */

296                 
297         public void run()
298         {
299             for(int i=1;i<=numberOfIterations;i++)
300             {
301                 myJndiTest tester = new myJndiTest(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag);
302                 tester.processInput();
303             
304                 tester.out.println("\n*****************************************************************************************" +
305                                    "\n* FINISHED TESTING OF THREAD: '"+Thread.currentThread().toString()+ "' ITERATION: "+i+
306                                    "\n*****************************************************************************************");
307             }
308         }
309         
310     }
311     
312     
313     
314    /**
315     * Class extend jnditest to allow for custom url to be used on connection i.e so that
316     * instead of grabbing the url from the LDIF file this will set it from the url supplied
317     * in the constructor.
318     * @author Trudi.
319     */

320         
321     class myJndiTest extends TestJNDIOps
322     {
323         String JavaDoc myUrl;
324         
325         public myJndiTest(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)
326         {
327             super(fileName, url, user, pwd, tracing, version, debugFlag, terminateFlag, referral, useSSL, printstackFlag);
328             this.myUrl = url;
329         }
330         
331     
332        /**
333         * Opens a connection in the same manner as jnditest.connect but uses a url supplied by
334         * the constructor.
335         * @param entry a 'fake' entry with no dn, but a bunch of attributes.
336         */

337         
338         public void connect(DXEntry entry)
339         {
340             if (debug) System.out.println("connect: " + entry);
341             if (myOps != null)
342             {
343                 try
344                 {
345                     myOps.close();
346                 }
347                 catch (NamingException JavaDoc e)
348                 {
349                     System.err.println("exception closing ops");
350                     e.printStackTrace();
351                 }
352             }
353           
354             String JavaDoc url = "";
355             
356             if(myUrl!=null)
357                 url = myUrl;
358             else
359                 url = entry.getString("url");
360             String JavaDoc user = entry.getString("user");
361             String JavaDoc pwd = entry.getString("pwd");
362             String JavaDoc tracing = entry.getString("tracing");
363             String JavaDoc version = entry.getString("ldapVersion");
364             String JavaDoc referral = entry.getString("referral");
365             String JavaDoc useSSL = entry.getString("useSSL");
366          
367             boolean trace = ((tracing != null) && (tracing.equalsIgnoreCase("true")));
368             boolean ssl = ((useSSL != null) && (useSSL.equalsIgnoreCase("true")));
369             openConnection(url, user, pwd, trace, version, referral, ssl);
370         }
371     }
372 }
Popular Tags