1 package net.simya.ldap.beans; 2 3 import javax.naming.*; 4 import javax.naming.directory.*; 5 6 import java.util.Hashtable ; 7 8 9 public class LdapConnectBean 10 { 11 private static final String DEFAULT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; 12 private static final String DEFAULT_AUTHENTICATION = "simple"; 13 14 private DirContext ctx = null; 15 private Hashtable env = new Hashtable (11); 16 17 public void connect() 18 throws NamingException 19 { 20 if ( !env.containsKey( Context.INITIAL_CONTEXT_FACTORY ) ) 22 env.put( Context.INITIAL_CONTEXT_FACTORY, DEFAULT_FACTORY ); 23 if ( env.containsKey( Context.SECURITY_PRINCIPAL ) 24 && !env.containsKey( Context.SECURITY_AUTHENTICATION ) ) 25 env.put( Context.SECURITY_AUTHENTICATION, DEFAULT_AUTHENTICATION ); 26 27 ctx = new InitialDirContext(env); 28 } 29 30 public void disconnect () 31 throws NamingException 32 { 33 try 34 { 35 ctx.close(); 36 } 37 38 catch (NamingException e) 39 { 40 throw e; 41 } 42 finally 43 { 44 ctx = null; 45 } 46 } 47 48 public boolean isConnected () 49 { 50 return (ctx != null); 51 } 52 53 public DirContext getContext () 54 { 55 return this.ctx; 56 } 57 58 public Hashtable getEnv() 59 { 60 return env; 61 } 62 63 public void setProperty( String name, Object value ) 64 { 65 env.put( name, value ); 66 } 67 public Object getProperty( String name ) 68 { 69 return env.get( name ); 70 } 71 } | Popular Tags |