FilterImpl.java

00001 /*
00002  * Copyright (c) 2003-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package org.knopflerfish.framework;
00036 
00037 import java.util.Dictionary;
00038 import java.util.Hashtable;
00039 
00040 import org.osgi.framework.*;
00041 
00042 public class FilterImpl implements Filter {
00043 
00044   private String filter = null;
00045   private LDAPExpr ldap;
00046 
00047 
00048   public FilterImpl(String filter) throws InvalidSyntaxException {
00049     ldap = new LDAPExpr(filter);
00050   }
00051 
00052 
00053   public boolean match(ServiceReference reference) {
00054     if(reference instanceof ServiceReferenceImpl) {
00055       // This is the normal case
00056       return ldap.evaluate(((ServiceReferenceImpl)reference).getProperties(), false);
00057     } else {
00058       // This might happen if we live in a remote framework world
00059       // Copy the properties the hard way
00060       Hashtable props = new Hashtable();
00061       String[] keys = reference.getPropertyKeys();
00062       for(int i = 0; i < keys.length; i++) {
00063           props.put(keys[i], reference.getProperty(keys[i]));
00064       }
00065       return ldap.evaluate(props, false);
00066     }
00067   }
00068 
00069 
00070   public boolean match(Dictionary dictionary) {
00071     return ldap.evaluate(new PropertiesDictionary(dictionary), false);
00072   }
00073 
00074   
00075   public boolean matchCase(Dictionary dictionary) {
00076           return ldap.evaluate(new PropertiesDictionary(dictionary), true);
00077   }
00078 
00079 
00080   public String toString() {
00081     if (filter == null) {
00082       filter = ldap.toString();
00083     }
00084     return filter;
00085   }
00086 
00087 
00088   public boolean equals(Object obj) {
00089     return toString().equals(obj.toString());
00090   }
00091 
00092 
00093   public int hashCode() {
00094     return toString().hashCode();
00095   }
00096 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4