KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > selectors > Selector


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.selectors;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.InvalidSelectorException JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30
31 import org.jboss.logging.Logger;
32 import org.jboss.mq.SpyMessage;
33 import org.jboss.util.NestedRuntimeException;
34
35 /**
36  * This class implements a Message Selector.
37  *
38  * @author Norbert Lataille (Norbert.Lataille@m4x.org)
39  * @author Juha Lindfors (jplindfo@helsinki.fi)
40  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
41  * @author Scott.Stark@jboss.org
42  * @author adrian@jboss.com
43  * @version $Revision: 38248 $
44  */

45 public class Selector
46 {
47    /** The logging interface */
48    static Logger cat = Logger.getLogger(Selector.class);
49    
50    /** The ISelectorParser implementation class */
51    private static Class JavaDoc parserClass = SelectorParser.class;
52
53    /** The system property property for numeric delivery mode */
54    public static final String JavaDoc USE_NUMERIC_DELIVERY_MODE = "org.jboss.mq.selectors.useNumericDeliveryMode";
55    
56    /** Backwards compatibility flag */
57    private static boolean useNumericDeliveryMode;
58    
59    public String JavaDoc selector;
60
61    public HashMap JavaDoc identifiers;
62    
63    public Object JavaDoc result;
64    
65    private Class JavaDoc resultType;
66
67    static
68    {
69       try
70       {
71          String JavaDoc property = System.getProperty(USE_NUMERIC_DELIVERY_MODE, "false");
72          useNumericDeliveryMode = Boolean.valueOf(property).booleanValue();
73       }
74       catch (Exception JavaDoc ignored)
75       {
76          cat.trace("Cannot get property " + USE_NUMERIC_DELIVERY_MODE, ignored);
77       }
78    }
79    
80    /**
81     * Get the class that implements the ISelectorParser interface to be used by
82     * Selector instances.
83     */

84    public static Class JavaDoc getSelectorParserClass()
85    {
86       return Selector.parserClass;
87    }
88    
89    /**
90     * Set the class that implements the ISelectorParser interface to be used by
91     * Selector instances.
92     *
93     * @param parserClass the ISelectorParser implementation. This must have a
94     * public no-arg constructor.
95     */

96    public static void setSelectorParserClass(Class JavaDoc parserClass)
97    {
98       Selector.parserClass = parserClass;
99    }
100
101    public Selector(String JavaDoc sel) throws InvalidSelectorException JavaDoc
102    {
103       selector = sel;
104       identifiers = new HashMap JavaDoc();
105       
106       try
107       {
108          ISelectorParser bob = (ISelectorParser) parserClass.newInstance();
109          result = bob.parse(sel, identifiers);
110          resultType = result.getClass();
111       }
112       catch (Exception JavaDoc e)
113       {
114          InvalidSelectorException JavaDoc exception = new InvalidSelectorException JavaDoc("The selector is invalid: " + sel);
115          exception.setLinkedException(e);
116          throw exception;
117       }
118       catch (Error JavaDoc e)
119       {
120          InvalidSelectorException JavaDoc exception = new InvalidSelectorException JavaDoc("The selector is invalid: " + sel);
121          exception.setLinkedException(new NestedRuntimeException(e));
122          throw exception;
123       }
124    }
125
126    public synchronized boolean test(SpyMessage.Header mes) throws JMSException JavaDoc
127    {
128       try
129       {
130          // Set the identifiers values
131
Iterator JavaDoc i = identifiers.values().iterator();
132          
133          while (i.hasNext())
134          {
135             Identifier id = (Identifier) i.next();
136             Object JavaDoc find = mes.jmsProperties.get(id.name);
137             
138             if (find == null)
139                find = getHeaderFieldReferences(mes, id.name);
140             
141             if (find == null)
142                id.value = null;
143             else
144             {
145                Class JavaDoc type = find.getClass();
146                if (type.equals(Boolean JavaDoc.class) ||
147                   type.equals(String JavaDoc.class) ||
148                   type.equals(Double JavaDoc.class) ||
149                   type.equals(Float JavaDoc.class) ||
150                   type.equals(Integer JavaDoc.class) ||
151                   type.equals(Long JavaDoc.class) ||
152                   type.equals(Short JavaDoc.class) ||
153                   type.equals(Byte JavaDoc.class))
154                   id.value = find;
155                else
156                   throw new Exception JavaDoc("Bad property '" + id.name + "' type: " + type);
157             }
158          }
159          
160          // Compute the result of this operator
161
Object JavaDoc res;
162          
163          if (resultType.equals(Identifier.class))
164             res = ((Identifier)result).value;
165          else if (resultType.equals(Operator.class))
166          {
167             Operator op = (Operator) result;
168             res = op.apply();
169          }
170          else
171             res = result;
172          
173          if (res == null)
174             return false;
175          
176          if (!(res.getClass().equals(Boolean JavaDoc.class)))
177             throw new Exception JavaDoc("Bad object type: " + res);
178          
179          return ((Boolean JavaDoc) res).booleanValue();
180       }
181       catch (Exception JavaDoc e)
182       {
183          cat.warn("Invalid selector: " + selector, e);
184          return false;
185       }
186    }
187
188    public boolean test(SpyMessage msg) throws JMSException JavaDoc
189    {
190       return test(msg.header);
191    }
192    
193    // [JPL]
194
private Object JavaDoc getHeaderFieldReferences(SpyMessage.Header header, String JavaDoc idName)
195       throws JMSException JavaDoc
196    {
197       // JMS 3.8.1.1 -- Message header field references are restricted to:
198
// JMSDeliveryMode, JMSPriority, JMSMessageID,
199
// JMSTimeStamp, JMSCorrelationID and JMSType
200
//
201
if (idName.equals("JMSDeliveryMode"))
202       {
203          if (useNumericDeliveryMode)
204             return new Integer JavaDoc(header.jmsDeliveryMode);
205          // JMS 3.8.1.3 -- Use 'PERISTENT' and 'NON_PERSISTENT'
206
else if (header.jmsDeliveryMode == DeliveryMode.NON_PERSISTENT)
207             return "NON_PERSISTENT";
208          else
209             return "PERSISTENT";
210       }
211       else if (idName.equals("JMSPriority"))
212          return new Integer JavaDoc(header.jmsPriority);
213       else if (idName.equals("JMSMessageID"))
214          return header.jmsMessageID;
215       else if (idName.equals("JMSTimestamp"))
216          return new Long JavaDoc(header.jmsTimeStamp);
217       else if (idName.equals("JMSCorrelationID"))
218          return header.jmsCorrelationIDString;
219       else if (idName.equals("JMSType"))
220          return header.jmsType;
221       else
222          return null;
223    }
224 }
225
Popular Tags