1 36 package org.ungoverned.oscar; 37 38 import java.io.CharArrayReader ; 39 import java.io.IOException ; 40 import java.util.Dictionary ; 41 import java.util.Enumeration ; 42 import java.util.Map ; 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 56 class FilterImpl implements Filter 57 { 58 private String m_toString = null; 59 private Evaluator m_evaluator = null; 60 private SimpleMapper m_mapper = null; 61 62 66 public FilterImpl(String 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 car = new CharArrayReader (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 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 108 public boolean equals(Object 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 125 public int hashCode() 126 { 127 return toString().hashCode(); 128 } 129 130 140 public boolean match(Dictionary dict) 141 throws IllegalArgumentException 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 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 189 public String 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 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 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 keys = dict.keys(); 223 while (keys.hasMoreElements()) 224 { 225 Object 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 ( 233 "Duplicate attribute: " + key.toString()); 234 } 235 } 236 } 237 m_ref = null; 238 } 239 240 public Object lookup(String 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 |