1 /***
2 * Copyright 2003, 2004, 2005. CodeStreet LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15 package com.codestreet.selector.parser;
16
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 /***
24 * Class to represent an identifier. Thread safe class may be freely used across
25 * threads. Identifiers can either be JMS header fields, or JMS
26 * provider-specific properties, or application properties.
27 * <p>
28 * The following JMS header fields are supported: <tt>JMSDeliveryMode</tt>,
29 * <tt>JMSPriority</tt>, <tt>JMSMessageID</tt>, <tt>JMSTimestamp</tt>,
30 * <tt>JMSCorrelationID,
31 * and <tt>JMSType</tt>. In addition, the header fields <tt>JMSRedelivered</tt> and
32 * <tt>JMSExpiration</tt> are also supported. These additional fields are relevant only
33 * for the receiving application and not for the sender.<p>
34 * Support is provided for <tt>nested</tt> fields. Nested fields are referenced using a <tt>dot</tt>
35 * notation. For example, <tt>order.quantity</tt> would select the <tt>quantity</tt> field of
36 * the nested sub-message field <tt>order</tt> if it exists. Otherwise, it selects nothing
37 * (<tt>null</tt>).
38 * @author Jawaid Hakim.
39 */
40 public final class Identifier implements IExpression
41 {
42 /***
43 * Factory.
44 *
45 * @param id
46 * Identifier name.
47 * @return Instance.
48 * @throws IllegalArgumentException
49 * Invalid identifier name.
50 */
51 public static synchronized Identifier valueOf(final String id)
52 {
53 if (reservedNamesSet_.contains(id))
54 throw new IllegalArgumentException(
55 "Identifier name cannot be a reserved name: " + id);
56
57 Identifier instance = (Identifier) idMap_.get(id);
58 if (instance == null)
59 {
60 instance = new Identifier(id);
61 idMap_.put(id, instance);
62 }
63 return instance;
64 }
65
66 /***
67 * Ctor.
68 *
69 * @param id
70 * Identifier name.
71 */
72 private Identifier(final String id)
73 {
74 id_ = id;
75 jmsHeader_ = jmsHeadersSet_.contains(id);
76 }
77
78 /***
79 * Get identifier name.
80 *
81 * @return Identifier name.
82 */
83 public String getIdentifier()
84 {
85 return id_;
86 }
87
88 /***
89 * Check if this is a JMS header property.
90 *
91 * @return <tt>true</tt> if this identifier is a JMS header property.
92 * Otherwise, returns <tt>false</tt>.
93 */
94 public boolean isJMSHeader()
95 {
96 return jmsHeader_;
97 }
98
99 public Object eval(final Map identifiers)
100 {
101 return getValue(identifiers);
102 }
103
104 public Object eval(final IValueProvider provider, final Object corr)
105 {
106 return provider.getValue(this, corr);
107 }
108
109 Object getValue(final Map identifiers)
110 {
111 return identifiers.get(id_);
112 }
113
114 public String toString()
115 {
116 return id_;
117 }
118
119 private final String id_;
120
121 private final boolean jmsHeader_;
122
123 private static Set jmsHeadersSet_;
124
125 private static Set reservedNamesSet_;
126
127 private static Map idMap_ = new HashMap();
128
129 static
130 {
131 // Valid JMS header fields
132 String[] jmsHeaders_ = { "JMSDeliveryMode", "JMSPriority",
133 "JMSMessageID", "JMSTimestamp", "JMSCorrelationID", "JMSType",
134 // Extension to the JMS Spec. 1.1
135 "JMSRedelivered", "JMSExpiration" };
136 jmsHeadersSet_ = new HashSet(Arrays.asList(jmsHeaders_));
137
138 // Reserved words - not allowed as identifier names
139 String[] reservedNames_ = { "NULL", "TRUE", "FALSE", "NULL", "NOT",
140 "AND", "OR", "BETWEEN", "LIKE", "IN", "IS", "ESCAPE" };
141 reservedNamesSet_ = new HashSet(Arrays.asList(reservedNames_));
142 }
143 }
This page was automatically generated by Maven