1 26 27 package com.opensugar.cube.ldap; 28 29 import org.osgi.framework.InvalidSyntaxException; 30 import org.osgi.framework.ServiceReference; 31 import org.osgi.framework.Constants; 32 33 import java.util.Dictionary ; 34 import java.util.Hashtable ; 35 import java.util.Enumeration ; 36 import java.util.Vector ; 37 38 public class LDAPFilter implements org.osgi.framework.Filter { 39 40 private String filterString; 41 private Clause clause; 42 43 public LDAPFilter( String filterString, boolean keysCaseSensitive ) throws InvalidSyntaxException { 44 this.filterString = filterString; 45 clause = Filter.parse( filterString, keysCaseSensitive ); 46 } 47 48 public String getFilterString() { 49 return filterString; 50 } 51 52 public boolean equals( Object obj ) { 53 return filterString.equals( obj ); 54 } 55 56 public int hashCode() { 57 return filterString.hashCode(); 58 } 59 60 public boolean match( Dictionary dictionary ) throws IllegalArgumentException { 61 Enumeration keys = dictionary.keys(); 64 Vector tmp = new Vector (); 65 String key; 66 while ( keys.hasMoreElements() ) { 67 key = (String )keys.nextElement(); 68 tmp.addElement( key.toLowerCase() ); 69 } 70 while ( tmp.size() > 0 ) { 71 key = (String )tmp.elementAt( 0 ); 72 tmp.removeElementAt( 0 ); 73 if ( tmp.indexOf( key ) != -1 ) { 74 throw new IllegalArgumentException ( "Dictionary contains case variants of the same key name: " + key ); 75 } 76 } 77 78 return clause.filter( dictionary ); 79 } 80 81 public boolean match( ServiceReference reference ) { 82 Hashtable props = new Hashtable (); 83 String [] keys = reference.getPropertyKeys(); 84 for ( int i = 0; i < keys.length; i++ ) { 85 props.put( keys[ i ], reference.getProperty( keys[ i ] ) ); 86 } 87 return clause.filter( props ); 88 } 89 90 public String toString() { 91 return clause.getCanonicalForm(); 92 } 93 94 } | Popular Tags |