KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > selectors > Interpreter


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): ScalAgent Distributed Technologies
23  */

24 package org.objectweb.joram.shared.selectors;
25
26 import org.objectweb.joram.shared.excepts.MessageValueException;
27 import org.objectweb.joram.shared.messages.*;
28
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * The <code>Interpreter</code> class is used for interpreting selector
33  * queries.
34  */

35 class Interpreter
36 {
37   /**
38    * Calls the appropriate method for interpreting a field name according to
39    * the syntax type.
40    * <p>
41    * Method called by the <code>org.objectweb.joram.shared.selectors.Filter</code>
42    * class.
43    *
44    * @param name Name of a field to retrieve.
45    * @param message Message in which retrieving the field.
46    * @param syntaxType Type of the syntax; ex: "JMS".
47    */

48   static Object JavaDoc interpret(String JavaDoc name, Message message, String JavaDoc syntaxType)
49   {
50     if (syntaxType.equals("JMS"))
51       return jmsInterpret(name, message);
52     else
53       return null;
54   }
55
56   /** Gets the String value of the given object. */
57   public static String JavaDoc wrapToString(Object JavaDoc value)
58   {
59     if (value == null)
60       return null;
61
62     if (value instanceof byte[])
63       return new String JavaDoc((byte[]) value);
64     else
65       return value.toString();
66   }
67
68   /**
69    * Retrieves the value of a field following the JMS syntax rules.
70    *
71    * @param name Name of a field to retrieve.
72    * @param message Message in which retrieving the field.
73    */

74   private static Object JavaDoc jmsInterpret(String JavaDoc name, Message message)
75   {
76     Object JavaDoc value = null;
77
78     // Checking JMS header fields names:
79
if (name.equals("JMSMessageID")) {
80       value = message.id;
81     } else if (name.equals("JMSPriority")) {
82       value = new Integer JavaDoc(message.priority);
83     } else if (name.equals("JMSTimestamp")) {
84       value = new Long JavaDoc(message.timestamp);
85     } else if (name.equals("JMSCorrelationID")) {
86       value = message.correlationId;
87     } else if (name.equals("JMSDeliveryMode")) {
88       if (message.persistent)
89           value = "PERSISTENT";
90       else
91           value = "NON_PERSISTENT";
92     } else if (name.equals("JMSType")) {
93       value = wrapToString(message.getOptionalHeader("JMSType"));
94     } else if (name.startsWith("JMSX")) {
95       if (name.equals("JMSXDeliveryCounts"))
96         // Checking JMSX header names:
97
value = new Integer JavaDoc(message.deliveryCount);
98       else
99         value = message.getOptionalHeader(name);
100     } else if (name.equals("JMS_JORAM_DELETEDDEST")) {
101       // Checking JORAM specific header names:
102
value = new Boolean JavaDoc(message.deletedDest);
103     } else if (name.equals("JMS_JORAM_NOTWRITEABLE")) {
104       // Checking JORAM specific header names:
105
value = new Boolean JavaDoc(message.notWriteable);
106     } else if (name.equals("JMS_JORAM_EXPIRED")) {
107       // Checking JORAM specific header names:
108
value = new Boolean JavaDoc(message.expired);
109     } else if (name.equals("JMS_JORAM_UNDELIVERABLE")) {
110       // Checking JORAM specific header names:
111
value = new Boolean JavaDoc(message.undeliverable);
112     } else {
113       // Checking properties:
114
value = message.getProperty(name);
115     }
116
117     // If the value is a String, replacing its simple quote <'>
118
// by a double one <''> (see JMS 1.1 3.8.1.1):
119
if (value instanceof String JavaDoc) {
120       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc((String JavaDoc)value, "'");
121       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
122       while (tokenizer.hasMoreTokens()) {
123         buff.append(tokenizer.nextToken());
124         buff.append("''");
125       }
126       String JavaDoc s = buff.toString();
127       value = s.substring(0, s.length() - 2);
128     }
129     else if (value instanceof Number JavaDoc)
130       value = new Double JavaDoc(((Number JavaDoc) value).doubleValue());
131   
132     return value;
133   }
134 }
135
Popular Tags