KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/ldap/org/apache/jmeter/protocol/ldap/sampler/LdapClient.java,v 1.7 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 java.util.Hashtable JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 //import javax.naming.directory.Attributes;
27
import javax.naming.directory.BasicAttributes JavaDoc;
28 import javax.naming.directory.DirContext JavaDoc;
29 import javax.naming.directory.InitialDirContext JavaDoc;
30 import javax.naming.directory.ModificationItem JavaDoc;
31 import javax.naming.directory.SearchControls JavaDoc;
32 //import javax.naming.directory.SearchResult;
33

34 import org.apache.jorphan.logging.LoggingManager;
35 import org.apache.log.Logger;
36
37
38 /**
39  * Ldap Client class is main class to create, modify, search and delete all the
40  * LDAP functionality available.
41  *
42  * @author T.Elanjchezhiyan(chezhiyan@siptech.co.in) - Sip Technologies and
43  * Exports Ltd.
44  * Created Apr 29 2003 11:00 AM
45  * @version $Revision: 1.7 $ Last updated: $Date: 2004/02/13 02:40:54 $
46  */

47 public class LdapClient
48 {
49     transient private static Logger log = LoggingManager.getLoggerForClass();
50     private DirContext JavaDoc dirContext = null;
51
52     /**
53      * Constructor for the LdapClient object.
54      */

55     public LdapClient()
56     {
57     }
58
59     /**
60      * Connect to server.
61      */

62     public void connect(
63         String JavaDoc host,
64         String JavaDoc port,
65         String JavaDoc rootdn,
66         String JavaDoc username,
67         String JavaDoc password)
68         throws NamingException JavaDoc
69     {
70         Hashtable JavaDoc env = new Hashtable JavaDoc();
71         env.put(
72             Context.INITIAL_CONTEXT_FACTORY,
73             "com.sun.jndi.ldap.LdapCtxFactory");
74         env.put(Context.PROVIDER_URL,"ldap://"+host +":"+port+"/"+rootdn);
75         env.put(Context.REFERRAL,"throw");
76         env.put(Context.SECURITY_CREDENTIALS,password);
77         env.put(Context.SECURITY_PRINCIPAL,username);
78         dirContext = new InitialDirContext JavaDoc(env);
79     }
80
81     /**
82      * Disconnect from the server.
83      */

84     public void disconnect()
85     {
86         try
87         {
88             if (dirContext != null){
89                 dirContext.close();
90                 dirContext=null;
91             }
92         }
93         catch (NamingException JavaDoc e)
94         {
95             log.error("Ldap client - ",e);
96         }
97     }
98
99     /**
100      * Filter the data in the ldap directory for the given search base.
101      *
102      * @param searchBase where the search should start
103      * @param searchFilter filter this value from the base
104      */

105     public boolean searchTest(String JavaDoc searchBase, String JavaDoc searchFilter)
106         throws NamingException JavaDoc
107     {
108         //System.out.println("Base="+searchBase+" Filter="+searchFilter);
109
SearchControls JavaDoc searchcontrols =
110             new SearchControls JavaDoc(SearchControls.SUBTREE_SCOPE,
111                                 1L, //count limit
112
0, //time limit
113
null,//attributes (null = all)
114
false,// return object ?
115
false);// dereference links?
116
NamingEnumeration JavaDoc ne =
117         dirContext.search(searchBase, searchFilter, searchcontrols);
118         //System.out.println("Loop "+ne.toString()+" "+ne.hasMore());
119
// while (ne.hasMore()){
120
// Object tmp = ne.next();
121
// System.out.println(tmp.getClass().getName());
122
// SearchResult sr = (SearchResult) tmp;
123
// Attributes at = sr.getAttributes();
124
// System.out.println(at.get("cn"));
125
// }
126
//System.out.println("Done "+ne.hasMore());
127
return ne.hasMore();
128     }
129
130     /**
131      * Modify the attribute in the ldap directory for the given string.
132      *
133      * @param mods add all the entry in to the ModificationItem
134      * @param string the string (dn) value
135      */

136     public void modifyTest(ModificationItem JavaDoc[] mods, String JavaDoc string)
137         throws NamingException JavaDoc
138     {
139         dirContext.modifyAttributes(string, mods);
140     }
141
142     /**
143      * Create the attribute in the ldap directory for the given string.
144      *
145      * @param basicattributes add all the entry in to the basicattribute
146      * @param string the string (dn) value
147      */

148     public void createTest(BasicAttributes JavaDoc basicattributes, String JavaDoc string)
149         throws NamingException JavaDoc
150     {
151         //DirContext dc = //TODO perhaps return this?
152
dirContext.createSubcontext(string, basicattributes);
153     }
154         
155     /**
156      * Delete the attribute from the ldap directory.
157      *
158      * @param string the string (dn) value
159      */

160     public void deleteTest(String JavaDoc string)
161         throws NamingException JavaDoc
162     {
163         dirContext.destroySubcontext(string);
164     }
165 }
Popular Tags