1 package net.walend.somnifugi; 2 3 import java.lang.reflect.Method ; 4 import java.lang.reflect.InvocationTargetException ; 5 6 import java.util.Map ; 7 import java.util.Set ; 8 import java.util.Collection ; 9 10 import javax.jms.Message ; 11 12 import net.walend.somnifugi.sql92.Selector; 13 14 18 19 class SQL92MessageSelector 20 implements SomniMessageSelector 21 { 22 private Selector selector; 23 24 27 SQL92MessageSelector(String selectorString) 28 throws SomniMessageSelectorException 29 { 30 this.selector = Selector.compile(selectorString); 31 if(selector == null) 32 { 33 throw new NullPointerException ("Selector compile for "+selectorString+" is null."); 34 } 35 } 36 37 44 public boolean matches(Message message) 45 throws SomniMessageSelectorException 46 { 47 SomniMessage somniMessage = (SomniMessage)message; 48 49 if(somniMessage.getProperties()==null) 50 { 51 throw new NullPointerException ("somniMessage.getProperties() is null"); 52 } 53 54 boolean result = selector.match(somniMessage.getProperties(),new ReflectingMap(message)); 55 56 return result; 57 } 58 59 public String toString() 60 { 61 return selector.toString(); 62 } 63 64 private static class ReflectingMap 65 implements Map <String ,Object > 66 { 67 private static final String GETTERPREFIX = "get"; 68 69 private Object hasGetters; 70 71 72 ReflectingMap(Object hasGetters) 73 { 74 this.hasGetters = hasGetters; 75 } 76 77 private String getterNameForKey(String key) 78 { 79 return GETTERPREFIX+key; 80 } 81 82 83 public int size() 84 { 85 throw new UnsupportedOperationException ("I haven't done this yet."); 86 } 87 88 public boolean isEmpty() 89 { 90 throw new UnsupportedOperationException ("I haven't done this yet."); 91 } 92 93 public boolean containsKey(Object key) 94 { 95 if(!(key instanceof String )) 96 { 97 return false; 98 } 99 try 100 { 101 hasGetters.getClass().getMethod(getterNameForKey((String )key)); 102 return true; 103 } 104 catch(NoSuchMethodException nsme) 105 { 106 return false; 107 } 108 } 109 110 public boolean containsValue(Object value) 111 { 112 throw new UnsupportedOperationException ("I haven't done this yet."); 113 } 114 115 public Object get(Object key) 116 { 117 if(!(key instanceof String )) 118 { 119 return null; 120 } 121 try 122 { 123 Method method = hasGetters.getClass().getMethod(getterNameForKey((String )key)); 124 return method.invoke(hasGetters); 125 } 126 catch(NoSuchMethodException nsme) 127 { 128 return null; 129 } 130 catch(IllegalAccessException iae) 131 { 132 throw new SomniRuntimeException(iae); 133 } 134 catch(InvocationTargetException ite) 135 { 136 throw new SomniRuntimeException(ite); 137 } 138 } 139 140 public Object put(String key,Object value) 141 { 142 throw new UnsupportedOperationException ("I haven't done this yet. Maybe someday when I think about setters."); 143 } 144 145 public Object remove(Object key) 146 { 147 throw new UnsupportedOperationException ("This will never work. What are you doing?"); 148 } 149 150 public void putAll(Map <? extends String ,? extends Object > map) 151 { 152 throw new UnsupportedOperationException ("This will never work. What are you doing?"); 153 } 154 155 public void clear() 156 { 157 throw new UnsupportedOperationException ("I haven't done this yet."); 158 } 159 160 public Set <String > keySet() 161 { 162 throw new UnsupportedOperationException ("I haven't done this yet."); 163 } 164 165 public Collection <Object > values() 166 { 167 throw new UnsupportedOperationException ("I haven't done this yet."); 168 } 169 170 public Set <Map.Entry <String ,Object >> entrySet() 171 { 172 throw new UnsupportedOperationException ("I haven't done this yet."); 173 } 174 175 public boolean equals(Object o) 176 { 177 throw new UnsupportedOperationException ("I haven't done this yet."); 178 } 179 180 public int hashCode() 181 { 182 throw new UnsupportedOperationException ("I haven't done this yet."); 183 } 184 } 185 } 186 187 209 210 | Popular Tags |