KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensugar > cube > ldap > LDAPFilter


1 /*
2  * JEFFREE: Java(TM) Embedded Framework FREE
3  * Copyright (C) 1999-2003 - Opensugar
4  *
5  * The contents of this file are subject to the Jeffree Public License,
6  * as defined by the file JEFFREE_LICENSE.TXT
7  *
8  * You may not use this file except in compliance with the License.
9  * You may obtain a copy of the License on the Objectweb web site
10  * (www.objectweb.org).
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14  * the specific terms governing rights and limitations under the License.
15  *
16  * The Original Code is JEFFREE, including the java package com.opensugar.cube,
17  * released January 1, 2003.
18  *
19  * The Initial Developer of the Original Code is Opensugar.
20  * The Original Code is Copyright Opensugar.
21  * All Rights Reserved.
22  *
23  * Initial developer(s): Pierre Scokaert (Opensugar)
24  * Contributor(s):
25  */

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 JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 public class LDAPFilter implements org.osgi.framework.Filter {
39
40    private String JavaDoc filterString;
41    private Clause clause;
42
43    public LDAPFilter( String JavaDoc filterString, boolean keysCaseSensitive ) throws InvalidSyntaxException {
44       this.filterString = filterString;
45       clause = Filter.parse( filterString, keysCaseSensitive );
46    }
47
48    public String JavaDoc getFilterString() {
49       return filterString;
50    }
51
52    public boolean equals( Object JavaDoc obj ) {
53       return filterString.equals( obj );
54    }
55
56    public int hashCode() {
57       return filterString.hashCode();
58    }
59
60    public boolean match( Dictionary JavaDoc dictionary ) throws IllegalArgumentException JavaDoc {
61       // Throw IllegalArgumentException if dictionary contains case variants of
62
// the same key name
63
Enumeration JavaDoc keys = dictionary.keys();
64       Vector JavaDoc tmp = new Vector JavaDoc();
65       String JavaDoc key;
66       while ( keys.hasMoreElements() ) {
67          key = (String JavaDoc)keys.nextElement();
68          tmp.addElement( key.toLowerCase() );
69       }
70       while ( tmp.size() > 0 ) {
71          key = (String JavaDoc)tmp.elementAt( 0 );
72          tmp.removeElementAt( 0 );
73          if ( tmp.indexOf( key ) != -1 ) {
74             throw new IllegalArgumentException JavaDoc( "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 JavaDoc props = new Hashtable JavaDoc();
83       String JavaDoc[] 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 JavaDoc toString() {
91       return clause.getCanonicalForm();
92    }
93
94 }
Popular Tags