KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > FilterImpl


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import java.io.CharArrayReader JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.Dictionary JavaDoc;
41 import java.util.Enumeration JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.osgi.framework.Filter;
45 import org.osgi.framework.InvalidSyntaxException;
46 import org.osgi.framework.ServiceReference;
47 import org.ungoverned.oscar.ldap.*;
48 import org.ungoverned.oscar.util.CaseInsensitiveMap;
49
50 /**
51  * This class implements an RFC 1960-based filter. The syntax of the
52  * filter string is the string representation of LDAP search filters
53  * as defined in RFC 1960. These filters are used to search for services
54  * and to track services using <tt>ServiceTracker</tt> objects.
55 **/

56 class FilterImpl implements Filter
57 {
58     private String JavaDoc m_toString = null;
59     private Evaluator m_evaluator = null;
60     private SimpleMapper m_mapper = null;
61
62     /**
63      * Construct a filter for a given filter expression string.
64      * @param expr the filter expression string for the filter.
65     **/

66     public FilterImpl(String JavaDoc expr) throws InvalidSyntaxException
67     {
68         if (expr == null)
69         {
70             throw new InvalidSyntaxException("Filter cannot be null", null);
71         }
72
73         if (expr != null)
74         {
75             CharArrayReader JavaDoc car = new CharArrayReader JavaDoc(expr.toCharArray());
76             LdapLexer lexer = new LdapLexer(car);
77             Parser parser = new Parser(lexer);
78             try
79             {
80                 if (!parser.start())
81                 {
82                     throw new InvalidSyntaxException(
83                         "Failed to parse LDAP query.", expr);
84                 }
85             }
86             catch (ParseException ex)
87             {
88                 throw new InvalidSyntaxException(
89                     ex.getMessage(), expr);
90             }
91             catch (IOException JavaDoc ex)
92             {
93                 throw new InvalidSyntaxException(
94                     ex.getMessage(), expr);
95             }
96             m_evaluator = new Evaluator(parser.getProgram());
97             m_mapper = new SimpleMapper();
98         }
99     }
100
101     /**
102      * Compares the <tt>Filter</tt> object to another.
103      * @param o the object to compare this <tt>Filter</tt> against.
104      * @return If the other object is a <tt>Filter</tt> object, it
105      * returns <tt>this.toString().equals(obj.toString())</tt>;
106      * <tt>false</tt> otherwise.
107     **/

108     public boolean equals(Object JavaDoc o)
109     {
110         if (o == null)
111         {
112             return false;
113         }
114         else if (o instanceof Filter)
115         {
116             return toString().equals(o.toString());
117         }
118         return false;
119     }
120
121     /**
122      * Returns the hash code for the <tt>Filter</tt> object.
123      * @return The value <tt>this.toString().hashCode()</tt>.
124     **/

125     public int hashCode()
126     {
127         return toString().hashCode();
128     }
129
130     /**
131      * Filter using a <tt>Dictionary</tt> object. The <tt>Filter</tt>
132      * is executed using the <tt>Dictionary</tt> object's keys and values.
133      * @param dict the <tt>Dictionary</tt> object whose keys and values
134      * are used to determine a match.
135      * @return <tt>true</tt> if the <tt>Dictionary</tt> object's keys
136      * and values match this filter; <tt>false</tt> otherwise.
137      * @throws IllegalArgumentException if the dictionary contains case
138      * variants of the same key name.
139     **/

140     public boolean match(Dictionary JavaDoc dict)
141         throws IllegalArgumentException JavaDoc
142     {
143         try
144         {
145             m_mapper.setSource(dict);
146             return m_evaluator.evaluate(m_mapper);
147         }
148         catch (AttributeNotFoundException ex)
149         {
150             Oscar.debug("FilterImpl: " + ex);
151         }
152         catch (EvaluationException ex)
153         {
154             Oscar.error("FilterImpl: " + toString(), ex);
155         }
156         return false;
157     }
158
159     /**
160      * Filter using a service's properties. The <tt>Filter</tt>
161      * is executed using the properties of the referenced service.
162      * @param ref A reference to the service whose properties
163      * are used to determine a match.
164      * @return <tt>true</tt> if the service's properties match this
165      * filter; <tt>false</tt> otherwise.
166     **/

167     public boolean match(ServiceReference ref)
168     {
169         try
170         {
171             m_mapper.setSource(ref);
172             return m_evaluator.evaluate(m_mapper);
173         }
174         catch (AttributeNotFoundException ex)
175         {
176             Oscar.debug("FilterImpl: " + ex);
177         }
178         catch (EvaluationException ex)
179         {
180             Oscar.error("FilterImpl: " + toString(), ex);
181         }
182         return false;
183     }
184
185     /**
186      * Returns the <tt>Filter</tt> object's filter string.
187      * @return Filter string.
188     **/

189     public String JavaDoc toString()
190     {
191         if (m_toString == null)
192         {
193             m_toString = m_evaluator.toStringInfix();
194         }
195         return m_toString;
196     }
197
198     static class SimpleMapper implements Mapper
199     {
200         private ServiceReference m_ref = null;
201         private Map JavaDoc m_map = null;
202
203         public void setSource(ServiceReference ref)
204         {
205             m_ref = ref;
206             m_map = null;
207         }
208
209         public void setSource(Dictionary JavaDoc dict)
210         {
211             if (m_map == null)
212             {
213                 m_map = new CaseInsensitiveMap();
214             }
215             else
216             {
217                 m_map.clear();
218             }
219
220             if (dict != null)
221             {
222                 Enumeration JavaDoc keys = dict.keys();
223                 while (keys.hasMoreElements())
224                 {
225                     Object JavaDoc key = keys.nextElement();
226                     if (m_map.get(key) == null)
227                     {
228                         m_map.put(key, dict.get(key));
229                     }
230                     else
231                     {
232                         throw new IllegalArgumentException JavaDoc(
233                             "Duplicate attribute: " + key.toString());
234                     }
235                 }
236             }
237             m_ref = null;
238         }
239
240         public Object JavaDoc lookup(String JavaDoc name)
241         {
242             if (m_map == null)
243             {
244                 return m_ref.getProperty(name);
245             }
246             return m_map.get(name);
247         }
248     }
249 }
Popular Tags