KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > selector > JmsIdentifier


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.selector;
22
23 import javax.jms.Message JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 /**
28  * Expression node for identifiers.
29  *
30  * @author Dan Greff
31  *
32  */

33 class JmsIdentifier extends JmsOperand
34 {
35   // the value for this instance
36
private String JavaDoc identifier = null;
37  
38   // all instances of JmsIdentifier with (Identifier, JmsIdentifier instance)
39
// pairs.
40
private static Hashtable JavaDoc instances = null;
41
42
43     /////////////////////////////////////////////////////////////////////////
44
// Static Methods //
45
/////////////////////////////////////////////////////////////////////////
46
/**
47    * Psuedo-singleton access point. Only one instance for each long value
48    * will ever be created and returned.
49    */

50   static JmsIdentifier getInstance(String JavaDoc newValue) {
51     JmsIdentifier retval;
52
53     if (instances == null)
54       instances = new Hashtable JavaDoc();
55    
56     retval = (JmsIdentifier) instances.get(newValue);
57     if (retval == null) {
58       retval = new JmsIdentifier(newValue);
59       instances.put(newValue, retval);
60     }
61     else {
62       retval.incrementRefCount();
63     }
64        
65     return retval;
66   }
67
68     /////////////////////////////////////////////////////////////////////////
69
// Constructor //
70
/////////////////////////////////////////////////////////////////////////
71

72   /**
73    * Called by getInstance which controls instance creation.
74    */

75   protected JmsIdentifier(String JavaDoc newValue)
76   {
77     super(true);
78     this.identifier = newValue;
79   }
80
81     /////////////////////////////////////////////////////////////////////////
82
// Public Methods //
83
/////////////////////////////////////////////////////////////////////////
84
public String JavaDoc toString() { return identifier + " " + super.toString(); }
85
86
87
88     /////////////////////////////////////////////////////////////////////////
89
// Package Methods //
90
/////////////////////////////////////////////////////////////////////////
91

92   /**
93    * Used by JmsIs to test for the existance of an identifier. JmsIs
94    * can not use evalute() on the identifier because it will throw
95    * and InvalideSelectorException for Object Properties.
96    */

97   String JavaDoc getIdentifier()
98   {
99     return identifier;
100   }
101
102   JmsOperand evaluate(Message JavaDoc msg) throws SelectorFalseException {
103
104     Object JavaDoc value;
105     try {
106       value = msg.getObjectProperty(identifier);
107     } catch (JMSException JavaDoc e) { throw SelectorFalseException.getInstance(); }
108     
109     if (value == null)
110       return null;
111   
112     if (value instanceof String JavaDoc)
113       return JmsStringLiteral.getUniqueInstance((String JavaDoc)value);
114   
115     if (value instanceof Boolean JavaDoc) {
116       if (((Boolean JavaDoc)value).booleanValue())
117         return JmsBooleanLiteral.TRUE;
118       else
119         return JmsBooleanLiteral.FALSE;
120     }
121   
122     if (value instanceof Number JavaDoc) {
123       if ( (value instanceof Double JavaDoc) || (value instanceof Float JavaDoc) )
124         return JmsFloatLiteral.getUniqueInstance( ((Number JavaDoc)value).doubleValue() );
125       else
126         return JmsIntegerLiteral.getUniqueInstance( ((Number JavaDoc)value).longValue() );
127     }
128   
129     // Throw exception since no selector should ever reference an
130
// Object property header
131
//
132
throw SelectorFalseException.getInstance();
133   }
134
135   String JavaDoc unParse()
136   {
137     return identifier;
138   }
139
140   /**
141    * Used to identify this object in places where instanceof is
142    * inapplicable.
143    */

144   short getType() { return JmsOperand.JMS_IDENTIFIER; }
145
146
147     /////////////////////////////////////////////////////////////////////////
148
// Protected Methods //
149
/////////////////////////////////////////////////////////////////////////
150

151   protected void delete(JmsOperand parent) {
152     super.delete(parent);
153     if (refcount == 0 && instances != null) {
154       instances.remove(identifier);
155       if (instances.size() == 0) {
156         instances = null;
157       }
158     }
159   }
160   
161 }
162
163
164
165
Popular Tags