1 18 19 package org.apache.jmeter.protocol.ldap.sampler; 20 21 import java.util.Hashtable ; 22 23 import javax.naming.Context ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 import javax.naming.directory.BasicAttributes ; 28 import javax.naming.directory.DirContext ; 29 import javax.naming.directory.InitialDirContext ; 30 import javax.naming.directory.ModificationItem ; 31 import javax.naming.directory.SearchControls ; 32 34 import org.apache.jorphan.logging.LoggingManager; 35 import org.apache.log.Logger; 36 37 38 47 public class LdapClient 48 { 49 transient private static Logger log = LoggingManager.getLoggerForClass(); 50 private DirContext dirContext = null; 51 52 55 public LdapClient() 56 { 57 } 58 59 62 public void connect( 63 String host, 64 String port, 65 String rootdn, 66 String username, 67 String password) 68 throws NamingException 69 { 70 Hashtable env = new Hashtable (); 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 (env); 79 } 80 81 84 public void disconnect() 85 { 86 try 87 { 88 if (dirContext != null){ 89 dirContext.close(); 90 dirContext=null; 91 } 92 } 93 catch (NamingException e) 94 { 95 log.error("Ldap client - ",e); 96 } 97 } 98 99 105 public boolean searchTest(String searchBase, String searchFilter) 106 throws NamingException 107 { 108 SearchControls searchcontrols = 110 new SearchControls (SearchControls.SUBTREE_SCOPE, 111 1L, 0, null, false, false); NamingEnumeration ne = 117 dirContext.search(searchBase, searchFilter, searchcontrols); 118 return ne.hasMore(); 128 } 129 130 136 public void modifyTest(ModificationItem [] mods, String string) 137 throws NamingException 138 { 139 dirContext.modifyAttributes(string, mods); 140 } 141 142 148 public void createTest(BasicAttributes basicattributes, String string) 149 throws NamingException 150 { 151 dirContext.createSubcontext(string, basicattributes); 153 } 154 155 160 public void deleteTest(String string) 161 throws NamingException 162 { 163 dirContext.destroySubcontext(string); 164 } 165 } | Popular Tags |