1 20 package org.openi.test.jnditester; 21 22 import java.util.Hashtable ; 23 import javax.naming.Context ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.directory.*; 26 27 28 public class JndiChecker { 29 public static void main(String [] args) { 30 JndiProperties properties = JndiProperties.getInstance(); 31 Hashtable env = new Hashtable (); 32 System.out.println("Initializing conext environment for :" 33 + properties.getConnectionURL()); 34 env.put(Context.INITIAL_CONTEXT_FACTORY, properties.getContextFactory()); 35 env.put(Context.PROVIDER_URL, properties.getConnectionURL()); 36 37 if (properties.getConnectionName() != null) { 38 System.out.println("Setting up connection User '" 39 + properties.getConnectionName() 40 + "' and connection Password '" 41 + properties.getConnectionPassword() + "'"); 42 env.put(Context.SECURITY_PRINCIPAL, properties.getConnectionName()); 43 env.put(Context.SECURITY_CREDENTIALS, 44 properties.getConnectionPassword()); 45 } 46 47 if (properties.getReferal() != null) { 48 System.out.println("Setting up REFERRAL :'" 49 + properties.getReferal() + "'"); 50 env.put(Context.REFERRAL, properties.getReferal()); 51 } 52 53 try { 54 DirContext ctx = new InitialDirContext(env); 55 System.out.println("context initialization done.."); 56 System.out.println("initializing search control"); 57 58 SearchControls constraints = new SearchControls(); 59 60 if (properties.getSearchScope()) { 61 System.out.println("setting up search scope to sub tree level"); 62 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 63 } else { 64 System.out.println("setting up search scope to one level"); 65 constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE); 66 } 67 68 if (properties.getReturnAttibutes() != null) { 69 System.out.println("setting up returning attributes :" 70 + properties.getReturnAttibutes()); 71 constraints.setReturningAttributes(new String [] { 72 properties.getReturnAttibutes() 73 }); 74 } else { 75 } 77 78 System.out.println("initializing search control done.."); 79 System.out.println("searching with filter '" 80 + properties.getSearchFilter() + "' in context base '" 81 + properties.getSearchBase() + "' for attribute '" 82 + properties.getReturnAttibutes() + "'"); 83 84 NamingEnumeration results = ctx.search(properties.getSearchBase(), 85 properties.getSearchFilter(), constraints); 86 System.out.println("search executed succesfully"); 87 System.out.println("Looping search results :"); 88 89 while (results.hasMoreElements()) { 91 SearchResult sr = (SearchResult) results.next(); 92 93 if (properties.getReturnAttibutes() != null) { 94 System.out.println("Found :" 95 + sr.getAttributes().get(properties.getReturnAttibutes()) 96 .get().toString()); 97 } else { 98 System.out.println("Found :" 99 + sr.getAttributes().toString()); 100 } 101 } 102 } catch (Exception ex) { 103 System.out.println("Error :" + ex.toString()); 104 } 105 } 106 } 107 | Popular Tags |