KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > SQL92MessageSelector


1 package net.walend.somnifugi;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.Collection JavaDoc;
9
10 import javax.jms.Message JavaDoc;
11
12 import net.walend.somnifugi.sql92.Selector;
13
14 /**
15 An SQL92 MessageSelector. This class uses the net.walend.somnifugi.sql92 package to make standard message selectors.
16 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
17 */

18
19 class SQL92MessageSelector
20     implements SomniMessageSelector
21 {
22     private Selector selector;
23     
24     /**
25 @param selectorString an SQL92 selector string. See TODO the jms specification.
26     */

27     SQL92MessageSelector(String JavaDoc selectorString)
28         throws SomniMessageSelectorException
29     {
30         this.selector = Selector.compile(selectorString);
31         if(selector == null)
32         {
33             throw new NullPointerException JavaDoc("Selector compile for "+selectorString+" is null.");
34         }
35     }
36     
37     /**
38 To stay compatible with String message selectors specified by the JMS specification, just use methods associated with headers, like property methods.
39
40 @throws IllegalStateException if something goes wrong.
41
42 @return true for Messages that should pass this filter, false for those that don't.
43     */

44     public boolean matches(Message JavaDoc message)
45         throws SomniMessageSelectorException
46     {
47         SomniMessage somniMessage = (SomniMessage)message;
48         
49         if(somniMessage.getProperties()==null)
50         {
51             throw new NullPointerException JavaDoc("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 JavaDoc toString()
60     {
61         return selector.toString();
62     }
63     
64     private static class ReflectingMap
65         implements Map JavaDoc<String JavaDoc,Object JavaDoc>
66     {
67         private static final String JavaDoc GETTERPREFIX = "get";
68         
69         private Object JavaDoc hasGetters;
70         
71         
72         ReflectingMap(Object JavaDoc hasGetters)
73         {
74             this.hasGetters = hasGetters;
75         }
76         
77         private String JavaDoc getterNameForKey(String JavaDoc key)
78         {
79             return GETTERPREFIX+key;
80         }
81         
82         
83         public int size()
84         {
85             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
86         }
87         
88         public boolean isEmpty()
89         {
90             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
91         }
92
93         public boolean containsKey(Object JavaDoc key)
94         {
95             if(!(key instanceof String JavaDoc))
96             {
97                 return false;
98             }
99             try
100             {
101                 hasGetters.getClass().getMethod(getterNameForKey((String JavaDoc)key));
102                 return true;
103             }
104             catch(NoSuchMethodException JavaDoc nsme)
105             {
106                 return false;
107             }
108         }
109         
110         public boolean containsValue(Object JavaDoc value)
111         {
112             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
113         }
114         
115         public Object JavaDoc get(Object JavaDoc key)
116         {
117             if(!(key instanceof String JavaDoc))
118             {
119                 return null;
120             }
121             try
122             {
123                 Method JavaDoc method = hasGetters.getClass().getMethod(getterNameForKey((String JavaDoc)key));
124                 return method.invoke(hasGetters);
125             }
126             catch(NoSuchMethodException JavaDoc nsme)
127             {
128                 return null;
129             }
130             catch(IllegalAccessException JavaDoc iae)
131             {
132                 throw new SomniRuntimeException(iae);
133             }
134             catch(InvocationTargetException JavaDoc ite)
135             {
136                 throw new SomniRuntimeException(ite);
137             }
138         }
139         
140         public Object JavaDoc put(String JavaDoc key,Object JavaDoc value)
141         {
142             throw new UnsupportedOperationException JavaDoc("I haven't done this yet. Maybe someday when I think about setters.");
143         }
144       
145         public Object JavaDoc remove(Object JavaDoc key)
146         {
147             throw new UnsupportedOperationException JavaDoc("This will never work. What are you doing?");
148         }
149         
150         public void putAll(Map JavaDoc<? extends String JavaDoc,? extends Object JavaDoc> map)
151         {
152             throw new UnsupportedOperationException JavaDoc("This will never work. What are you doing?");
153         }
154         
155         public void clear()
156         {
157             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
158         }
159         
160         public Set JavaDoc<String JavaDoc> keySet()
161         {
162             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
163         }
164         
165         public Collection JavaDoc<Object JavaDoc> values()
166         {
167             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
168         }
169         
170         public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc,Object JavaDoc>> entrySet()
171         {
172             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
173         }
174         
175         public boolean equals(Object JavaDoc o)
176         {
177             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
178         }
179         
180         public int hashCode()
181         {
182             throw new UnsupportedOperationException JavaDoc("I haven't done this yet.");
183         }
184     }
185 }
186
187 /*
188 Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
189 All rights reserved.
190
191 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
192
193 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
194
195 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
196
197 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
198
199 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
200
201 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
202 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
203
204 =================================================================================
205
206 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
207
208 */

209
210
Popular Tags