View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2000,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42   */
43  
44  package org.exolab.jms.selector;
45  
46  import javax.jms.DeliveryMode;
47  import javax.jms.JMSException;
48  import javax.jms.Message;
49  
50  
51  /***
52   * This class implements an identifier. When evaluated, this returns
53   * the value of named header identifier or property, or null if the
54   * identifier is null or the property does not exist.
55   *
56   * @version     $Revision: 1.1 $ $Date: 2004/11/26 01:50:44 $
57   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
58   * @see         org.exolab.jms.selector.Expression
59   * @see         org.exolab.jms.selector.Identifiers
60   */
61  class Identifier implements Expression {
62  
63      /***
64       * The identifier name
65       */
66      private final String _name;
67  
68      /***
69       * If true the identifier is a header identifier, otherwise it is an user
70       * identifier
71       */
72      private final boolean _headerField;
73  
74      /***
75       * Persistent delivery mode
76       */
77      private static final SString PERSISTENT =
78          new SString(Identifiers.PERSISTENT);
79  
80      /***
81       * Non-persistent delivery mode
82       */
83      private static final SString NON_PERSISTENT =
84          new SString(Identifiers.NON_PERSISTENT);
85  
86  
87      /***
88       * Construct a new <code>Identifier</code>
89       *
90       * @param name the identifier name
91       * @throws SelectorException if the identifier cannot be queried by
92       * selectors
93       */
94      public Identifier(final String name) throws SelectorException {
95          _name = name;
96  
97          if (Identifiers.isJMSIdentifier(_name)) {
98              if (!Identifiers.isQueryableJMSIdentifier(_name)) {
99                  throw new SelectorException("Invalid header field: " + _name);
100             }
101             _headerField = true;
102         } else {
103             _headerField = false;
104         }
105     }
106 
107     /***
108      * Evaluate the expression
109      *
110      * @param msg the message to use to obtain any header identifier and
111      * property values
112      * @return the evaluated result, or <code>null</code> if the value of the
113      *  expression is unknown
114      */
115     public final SObject evaluate(final Message msg) {
116         SObject value = null;
117         try {
118             if (_headerField) {
119                 if (_name.equals(Identifiers.JMS_DELIVERY_MODE)) {
120                     value = deliveryMode(msg.getJMSDeliveryMode());
121                 } else if (_name.equals(Identifiers.JMS_PRIORITY)) {
122                     value = new SLong(msg.getJMSPriority());
123                 } else if (_name.equals(Identifiers.JMS_TIMESTAMP)) {
124                     value = new SLong(msg.getJMSTimestamp());
125                 } else if (_name.equals(Identifiers.JMS_MESSAGE_ID)) {
126                     String id = msg.getJMSMessageID();
127                     if (id != null) {
128                         value = new SString(id);
129                     }
130                 } else if (_name.equals(Identifiers.JMS_CORRELATION_ID)) {
131                     String id = msg.getJMSCorrelationID();
132                     if (id != null) {
133                         value = new SString(id);
134                     }
135                 } else if (_name.equals(Identifiers.JMS_TYPE)) {
136                     String type = msg.getJMSType();
137                     if (type != null) {
138                         value = new SString(type);
139                     }
140                 }
141             } else {
142                 value = SObjectFactory.create(msg.getObjectProperty(_name));
143             }
144         } catch (JMSException ignore) {
145             // do nothing
146         }
147         return value;
148     }
149 
150     /***
151      * Return a string representation of this expression.
152      *
153      * @return a string representation of this expression
154      */
155     public final String toString() {
156         return _name;
157     }
158 
159     /***
160      * Converts the delivery mode to a string
161      *
162      * @param mode the delivery mode. One of
163      * <code>DeliveryMode.PERSISTENT</code> or
164      * <code>DeliveryMode.NON_PERSISTENT</code>
165      * @return the stringified representation of the delivery mode
166      */
167     private SString deliveryMode(final int mode) {
168         SString result = PERSISTENT;
169         if (mode == DeliveryMode.NON_PERSISTENT) {
170             result = NON_PERSISTENT;
171         }
172         return result;
173     }
174 
175 }