KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > ldap > sampler > LDAPSampler


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/ldap/org/apache/jmeter/protocol/ldap/sampler/LDAPSampler.java,v 1.10 2004/02/13 02:40:54 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.ldap.sampler;
20
21 import javax.naming.NamingException JavaDoc;
22 import javax.naming.directory.Attribute JavaDoc;
23 import javax.naming.directory.BasicAttribute JavaDoc;
24 import javax.naming.directory.BasicAttributes JavaDoc;
25 import javax.naming.directory.DirContext JavaDoc;
26 import javax.naming.directory.ModificationItem JavaDoc;
27
28 import org.apache.jmeter.config.Argument;
29 import org.apache.jmeter.config.Arguments;
30 import org.apache.jmeter.config.ConfigTestElement;
31 import org.apache.jmeter.config.LoginConfig;
32 import org.apache.jmeter.protocol.ldap.config.LdapConfig;
33 import org.apache.jmeter.samplers.AbstractSampler;
34 import org.apache.jmeter.samplers.Entry;
35 import org.apache.jmeter.samplers.SampleResult;
36 import org.apache.jmeter.testelement.TestElement;
37 import org.apache.jmeter.testelement.property.BooleanProperty;
38 import org.apache.jmeter.testelement.property.PropertyIterator;
39 import org.apache.jmeter.testelement.property.StringProperty;
40 import org.apache.jmeter.testelement.property.TestElementProperty;
41 import org.apache.jorphan.logging.LoggingManager;
42 import org.apache.log.Logger;
43
44
45 /**
46  * Ldap Sampler class is main class for the LDAP test. This will control all
47  * the test available in the LDAP Test.
48  *
49  * @author T.Elanjchezhiyan(chezhiyan@siptech.co.in) - Sip Technologies and
50  * Exports Ltd.
51  * Created Apr 29 2003 11:00 AM
52  * @version $Revision: 1.10 $ Last updated: $Date: 2004/02/13 02:40:54 $
53  */

54 public class LDAPSampler extends AbstractSampler
55 {
56     transient private static Logger log = LoggingManager.getLoggerForClass();
57
58     public final static String JavaDoc SERVERNAME = "servername";
59     public final static String JavaDoc PORT = "port";
60     public final static String JavaDoc ROOTDN = "rootdn";
61     public final static String JavaDoc TEST = "test";
62     public final static String JavaDoc ADD = "add";
63     public final static String JavaDoc MODIFY = "modify";
64     public final static String JavaDoc DELETE = "delete";
65     public final static String JavaDoc SEARCHBASE = "search";
66     public final static String JavaDoc SEARCHFILTER = "searchfilter";
67     public final static String JavaDoc USER_DEFINED = "user_defined";
68     public final static String JavaDoc ARGUMENTS = "arguments";
69     public final static String JavaDoc BASE_ENTRY_DN = "base_entry_dn";
70     
71     //For In build test case using this counter
72
//create the new entry in the server
73
private static int counter=0;
74
75     private boolean searchFoundEntries;//TODO turn into parameter?
76

77     public LDAPSampler()
78     {
79     }
80
81
82     public void addCustomTestElement(TestElement element)
83     {
84         if(element instanceof LdapConfig || element instanceof LoginConfig)
85         {
86             mergeIn(element);
87         }
88     }
89
90     /**
91      * Gets the username attribute of the LDAP object.
92      *
93      * @return the username
94      */

95     public String JavaDoc getUsername()
96     {
97         return getPropertyAsString(ConfigTestElement.USERNAME);
98     }
99
100     /**
101      * Gets the password attribute of the LDAP object.
102      *
103      * @return the password
104      */

105     public String JavaDoc getPassword()
106     {
107         return getPropertyAsString(ConfigTestElement.PASSWORD);
108     }
109
110     /**
111      * Sets the Servername attribute of the ServerConfig object.
112      *
113      * @param servername the new servername value
114      */

115     public void setServername(String JavaDoc servername)
116     {
117         setProperty(new StringProperty(SERVERNAME, servername));
118     }
119
120     /**
121      * Sets the Port attribute of the ServerConfig object.
122      *
123      * @param port the new Port value
124      */

125     public void setPort(String JavaDoc port)
126     {
127         setProperty(new StringProperty(PORT, port));
128     }
129
130
131     /**
132      * Gets the servername attribute of the LDAPSampler object.
133      *
134      * @return the Servername value
135      */

136     public String JavaDoc getServername()
137     {
138         return getPropertyAsString(SERVERNAME);
139     }
140
141     /**
142      * Gets the Port attribute of the LDAPSampler object.
143      *
144      * @return the Port value
145      */

146     public String JavaDoc getPort()
147     {
148         return getPropertyAsString(PORT);
149     }
150
151     /**
152      * Sets the Rootdn attribute of the LDAPSampler object.
153      *
154      * @param newRootdn the new rootdn value
155      */

156     public void setRootdn(String JavaDoc newRootdn)
157     {
158         this.setProperty(ROOTDN,newRootdn);
159     }
160     
161     /**
162      * Gets the Rootdn attribute of the LDAPSampler object.
163      *
164      * @return the Rootdn value
165      */

166     public String JavaDoc getRootdn()
167     {
168         return getPropertyAsString(ROOTDN);
169     }
170
171     /**
172      * Sets the Test attribute of the LdapConfig object.
173      *
174      * @param newTest the new test value(Add,Modify,Delete and search)
175      */

176     public void setTest(String JavaDoc newTest)
177     {
178         this.setProperty(TEST,newTest);
179     }
180     
181     /**
182      * Gets the test attribute of the LDAPSampler object.
183      *
184      * @return the test value (Add, Modify, Delete and search)
185      */

186     public String JavaDoc getTest()
187     {
188         return getPropertyAsString(TEST);
189     }
190
191     /**
192      * Sets the UserDefinedTest attribute of the LDAPSampler object.
193      *
194      * @param value the new UserDefinedTest value
195      */

196     public void setUserDefinedTest(boolean value)
197     {
198         setProperty(new BooleanProperty(USER_DEFINED, value));
199     }
200
201     /**
202      * Gets the UserDefinedTest attribute of the LDAPSampler object.
203      *
204      * @return the test value true or false. If true it will do the
205      * UserDefinedTest else our own inbuild test case.
206      */

207     public boolean getUserDefinedTest()
208     {
209         return getPropertyAsBoolean(USER_DEFINED);
210     }
211
212     /**
213      * Sets the Base Entry DN attribute of the LDAPSampler object.
214      *
215      * @param newbaseentry the new Base entry DN value
216      */

217     public void setBaseEntryDN(String JavaDoc newbaseentry)
218     {
219         setProperty(new StringProperty(BASE_ENTRY_DN, newbaseentry));
220     }
221
222     /**
223      * Gets the BaseEntryDN attribute of the LDAPSampler object.
224      *
225      * @return the Base entry DN value
226      */

227     public String JavaDoc getBaseEntryDN()
228     {
229         return getPropertyAsString(BASE_ENTRY_DN);
230     }
231
232     /**
233      * Sets the Arguments attribute of the LdapConfig object. This will
234      * collect values from the table for user defined test case.
235      *
236      * @param value the arguments
237      */

238     public void setArguments(Arguments value)
239     {
240         setProperty(new TestElementProperty(ARGUMENTS, value));
241     }
242
243     /**
244      * Gets the Arguments attribute of the LdapConfig object.
245      *
246      * @return the arguments. User defined test case.
247      */

248     public Arguments getArguments()
249     {
250         return (Arguments) getProperty(ARGUMENTS).getObjectValue();
251     }
252
253
254     /**
255      * Collect all the value from the table (Arguments), using this create the
256      * basicAttributes. This will create the Basic Attributes for the User
257      * defined TestCase for Add Test.
258      *
259      * @return the BasicAttributes
260      */

261     private BasicAttributes JavaDoc getUserAttributes()
262     {
263         BasicAttribute JavaDoc basicattribute = new BasicAttribute JavaDoc("objectclass");
264         basicattribute.add("top");
265         basicattribute.add("person");
266         basicattribute.add("organizationalPerson");
267         basicattribute.add("inetOrgPerson");
268         BasicAttributes JavaDoc attrs = new BasicAttributes JavaDoc(true);
269         attrs.put(basicattribute);
270         BasicAttribute JavaDoc attr;
271         PropertyIterator iter = getArguments().iterator();
272          
273         while (iter.hasNext())
274         {
275             Argument item = (Argument) iter.next().getObjectValue();
276             attr = getBasicAttribute( item.getName(),item.getValue());
277             attrs.put(attr);
278         }
279         return attrs;
280     }
281
282
283     /**
284      * Collect all the value from the table (Arguments), using this create the
285      * basicAttributes. This will create the Basic Attributes for the User
286      * defined TestCase for Modify test.
287      *
288      * @return the BasicAttributes
289      */

290     private ModificationItem JavaDoc[] getUserModAttributes()
291     {
292         ModificationItem JavaDoc[] mods =
293             new ModificationItem JavaDoc[getArguments().getArguments().size()];
294         BasicAttribute JavaDoc attr;
295         PropertyIterator iter = getArguments().iterator();
296         int count =0;
297         while (iter.hasNext())
298         {
299             Argument item = (Argument) iter.next().getObjectValue();
300             attr = getBasicAttribute( item.getName(),item.getValue());
301             mods[count] =
302                 new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE, attr);
303             count=+1;
304         }
305         return mods;
306     }
307
308
309     /**
310      * This will create the Basic Attributes for the Inbuilt TestCase for
311      * Modify test.
312      *
313      * @return the BasicAttributes
314      */

315     private ModificationItem JavaDoc[] getModificationItem()
316     {
317         ModificationItem JavaDoc[] mods = new ModificationItem JavaDoc[2];
318         // replace (update) attribute
319
Attribute JavaDoc mod0 = new BasicAttribute JavaDoc("userpassword",
320                                             "secret");
321         // add mobile phone number attribute
322
Attribute JavaDoc mod1 = new BasicAttribute JavaDoc("mobile",
323                                             "123-456-1234");
324
325         mods[0] = new ModificationItem JavaDoc(DirContext.REPLACE_ATTRIBUTE, mod0);
326         mods[1] = new ModificationItem JavaDoc(DirContext.ADD_ATTRIBUTE, mod1);
327
328         return mods;
329     }
330
331     /**
332      * This will create the Basic Attributes for the In build TestCase for Add
333      * Test.
334      *
335      * @return the BasicAttributes
336      */

337     private BasicAttributes JavaDoc getBasicAttributes()
338     {
339         BasicAttributes JavaDoc basicattributes = new BasicAttributes JavaDoc();
340         BasicAttribute JavaDoc basicattribute = new BasicAttribute JavaDoc("objectclass");
341         basicattribute.add("top");
342         basicattribute.add("person");
343         basicattribute.add("organizationalPerson");
344         basicattribute.add("inetOrgPerson");
345         basicattributes.put(basicattribute);
346         String JavaDoc s1 = "User";
347         String JavaDoc s3 = "Test";
348         String JavaDoc s5 = "user";
349         String JavaDoc s6 = "test";
350         counter+=1;
351         basicattributes.put(new BasicAttribute JavaDoc("givenname", s1));
352         basicattributes.put(new BasicAttribute JavaDoc("sn", s3));
353         basicattributes.put(new BasicAttribute JavaDoc("cn","TestUser"+counter));
354         basicattributes.put(new BasicAttribute JavaDoc("uid", s5));
355         basicattributes.put(new BasicAttribute JavaDoc("userpassword", s6));
356         setProperty(new StringProperty(ADD,"cn=TestUser"+counter));
357         return basicattributes;
358     }
359
360     /**
361      * This will create the Basic Attribute for the given name value pair.
362      *
363      * @return the BasicAttribute
364      */

365     private BasicAttribute JavaDoc getBasicAttribute(String JavaDoc name, String JavaDoc value)
366     {
367         BasicAttribute JavaDoc attr = new BasicAttribute JavaDoc(name,value);
368         return attr;
369     }
370
371     /**
372      * Returns a formatted string label describing this sampler
373      *
374      * @return a formatted string label describing this sampler
375      */

376     public String JavaDoc getLabel()
377     {
378         return (
379             "ldap://"
380                 + this.getServername()
381                 + ":"
382                 + getPort()
383                 + "/"
384                 + this.getRootdn());
385     }
386
387
388     /**
389      * This will do the add test for the User defined TestCase as well as
390      * inbuilt test case.
391      *
392      * @return executed time for the give test case
393      */

394     private void addTest(LdapClient ldap, SampleResult res)
395         throws NamingException JavaDoc
396     {
397         if (getPropertyAsBoolean(USER_DEFINED))
398         {
399             res.sampleStart();
400             ldap.createTest(
401                 getUserAttributes(),
402                 getPropertyAsString(BASE_ENTRY_DN));
403             res.sampleEnd();
404         }
405         else
406         {
407             res.sampleStart();
408             ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD));
409             res.sampleEnd();
410             ldap.deleteTest(getPropertyAsString(ADD));
411         }
412     }
413          
414
415     /**
416      * This will do the delete test for the User defined TestCase as well as
417      * inbuilt test case.
418      *
419      * @return executed time for the give test case
420      */

421     private void deleteTest(LdapClient ldap, SampleResult res)
422         throws NamingException JavaDoc
423     {
424         if (!getPropertyAsBoolean(USER_DEFINED))
425         {
426             ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD));
427             setProperty(new StringProperty(DELETE, getPropertyAsString(ADD)));
428         }
429         res.sampleStart();
430         ldap.deleteTest(getPropertyAsString(DELETE));
431         res.sampleEnd();
432     }
433
434     /**
435      * This will do the search test for the User defined TestCase as well as
436      * inbuilt test case.
437      *
438      * @return executed time for the give test case
439      */

440     private void searchTest(LdapClient ldap, SampleResult res)
441         throws NamingException JavaDoc
442     {
443         if (!getPropertyAsBoolean(USER_DEFINED))
444         {
445             ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD));
446             setProperty(
447                 new StringProperty(SEARCHBASE, getPropertyAsString(ADD)));
448             setProperty(
449                 new StringProperty(SEARCHFILTER, getPropertyAsString(ADD)));
450         }
451         res.sampleStart();
452         searchFoundEntries = ldap.searchTest(
453             getPropertyAsString(SEARCHBASE),
454             getPropertyAsString(SEARCHFILTER));
455         res.sampleEnd();
456         if (!getPropertyAsBoolean(USER_DEFINED))
457         {
458             ldap.deleteTest(getPropertyAsString(ADD));
459         }
460     }
461
462     /**
463      * This will do the search test for the User defined TestCase as well as
464      * inbuilt test case.
465      *
466      * @return executed time for the give test case
467      */

468     private void modifyTest(LdapClient ldap, SampleResult res)
469         throws NamingException JavaDoc
470     {
471         if (getPropertyAsBoolean(USER_DEFINED))
472         {
473             res.sampleStart();
474             ldap.modifyTest(
475                 getUserModAttributes(),
476                 getPropertyAsString(BASE_ENTRY_DN));
477             res.sampleEnd();
478         }
479         else
480         {
481             ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD));
482             setProperty(new StringProperty(MODIFY, getPropertyAsString(ADD)));
483             res.sampleStart();
484             ldap.modifyTest(getModificationItem(), getPropertyAsString(MODIFY));
485             res.sampleEnd();
486             ldap.deleteTest(getPropertyAsString(ADD));
487         }
488     }
489          
490     public SampleResult sample(Entry e)
491     {
492         SampleResult res = new SampleResult();
493         boolean isSuccessful = false;
494         res.setSampleLabel(getLabel());
495         res.setSamplerData(getPropertyAsString(TEST));//TODO improve this
496
LdapClient ldap = new LdapClient();
497
498         try
499         {
500             ldap.connect(
501                 getServername(),
502                 getPort(),
503                 getRootdn(),
504                 getUsername(),
505                 getPassword());
506
507             if (getPropertyAsString(TEST).equals("add"))
508             {
509                 addTest(ldap,res);
510             }
511             else if (getPropertyAsString(TEST).equals("delete"))
512             {
513                 deleteTest(ldap,res);
514             }
515             else if (getPropertyAsString(TEST).equals("modify"))
516             {
517                 modifyTest(ldap,res);
518             }
519             else if (getPropertyAsString(TEST).equals("search"))
520             {
521                 searchTest(ldap,res);
522             }
523
524             //TODO - needs more work ...
525
if (getPropertyAsString(TEST).equals("search")
526             && !searchFoundEntries )
527             {
528                 res.setResponseCode("201");//TODO is this a sensible number?
529
res.setResponseMessage("OK - no results");
530                 res.setResponseData("successful - no results".getBytes());
531             } else {
532                 res.setResponseCode("200");
533                 res.setResponseMessage("OK");
534                 res.setResponseData("successful".getBytes());
535             }
536             res.setDataType(SampleResult.TEXT);
537             isSuccessful = true;
538             ldap.disconnect();
539         }
540         catch (Exception JavaDoc ex)
541         {
542             log.error("Ldap client - ",ex);
543             // Could time this
544
// res.sampleEnd();
545
// if sampleEnd() is not called, elapsed time will remain zero
546
res.setResponseCode("500");//TODO distinguish errors better
547
res.setResponseMessage(ex.toString());
548             ldap.disconnect();
549             isSuccessful = false;
550         }
551
552         // Set if we were successful or not
553
res.setSuccessful(isSuccessful);
554         return res;
555     }
556 }
Popular Tags