1 package net.simya.ldap.beans; 2 3 import javax.naming.*; 4 import javax.naming.directory.*; 5 import java.util.StringTokenizer ; 6 import java.lang.Integer ; 7 8 13 public class LdapSearchBean 14 { 15 16 private SearchControls ctls = new SearchControls(); 17 private NamingEnumeration answer = null; 18 private Attributes attributes = null; 19 private SearchResult sr = null; 20 private String DN = null; 21 22 private String filter = null; 23 private String searchBase = null; 24 private String scope = null; 25 26 public void search( DirContext ctx ) 27 throws NamingException 28 { 29 if (scope == null || scope.equalsIgnoreCase("sub")) 31 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); else if ( scope.equalsIgnoreCase("one")) 33 ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); else if ( scope.equalsIgnoreCase("base")) 35 ctls.setSearchScope(SearchControls.OBJECT_SCOPE); 37 answer = ctx.search(searchBase, filter, ctls); 39 } 40 41 public boolean next() 42 throws NamingException 43 { 44 if ( answer.hasMoreElements() ) 45 { 46 sr = (SearchResult)answer.next(); 47 DN = sr.getName(); 49 attributes = sr.getAttributes(); 50 return true; 51 } 52 return false; 53 } 54 55 public void setReturningAttributes( String list ) 56 { 57 StringTokenizer st = new StringTokenizer ( list, "," ); 58 String [] attList = new String [ st.countTokens() ]; 59 for ( int i=0; st.hasMoreTokens(); i++ ) 60 attList[i] = st.nextToken().trim(); 61 ctls.setReturningAttributes( attList ); 62 } 63 64 public Attribute getAttribute (String attrName) 65 throws NamingException 66 { 67 return attributes.get(attrName); 68 } 69 70 public String getStringAttribute (String attrName) 71 throws NamingException 72 { 73 Attribute attr = attributes.get(attrName); 74 return attr != null ? (String ) attr.get() : ""; 75 } 76 77 public String getStringAttribute (String attrName, String idx ) 78 throws NamingException 79 { 80 Attribute attr = attributes.get(attrName); 81 return attr != null ? (String ) attr.get(new Integer (idx).intValue()) : ""; 82 } 83 84 public int getAttributeSize ( String attrName ) 85 throws NamingException 86 { 87 Attribute attr = attributes.get(attrName); 88 return attr != null ? attr.size() : 0; 89 } 90 91 public String getFilter() { return this.filter; } 92 public void setFilter(String filter) { this.filter = filter; } 93 94 public String getSearchBase() { return this.searchBase; } 95 public void setSearchBase(String searchBase) { this.searchBase = searchBase; } 96 97 public String getScope() { return this.scope; } 98 public void setScope(String scope) { this.scope = scope; } 99 100 public void setLimit(String limit) { 101 ctls.setCountLimit( new Integer (limit).intValue() ); 102 } 103 104 public String getDN () { return DN;} 105 106 107 } | Popular Tags |