KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > util > MessageProperties


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.util;
8
9 import javax.jms.JMSException JavaDoc;
10 import javax.jms.MessageFormatException JavaDoc;
11 import javax.jms.MessageNotWriteableException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Overrides {@link JMSMap} to enforce the message property specific
20  * conversion rules specified in section 3.5.4 of the JMS specification.
21  * Additionally, enforces naming restrictions imposed on property names in
22  * section 3.5.1 of the JMS specification.
23  *
24  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
25  * @version $Revision: 1.2 $ $Date: 2003/08/21 10:13:33 $
26  */

27 public final class MessageProperties extends JMSMap
28 {
29
30     private static final String JavaDoc[] illegalIdentifiers =
31             new String JavaDoc[]{
32                 "NULL",
33                 "TRUE",
34                 "FALSE",
35                 "NOT",
36                 "AND",
37                 "OR",
38                 "BETWEEN",
39                 "LIKE",
40                 "IN",
41                 "IS",
42                 "ESCAPE"};
43
44     private boolean readOnly = false;
45
46     private static void throwExceptionIfNameIsIllegal(String JavaDoc name)
47             throws JMSException JavaDoc
48     {
49         if (name == null)
50         {
51             throw new JMSException JavaDoc(""); //TOD: Write exception method
52
}
53         if (name.startsWith("JMSX") || name.startsWith("JMS_"))
54         {
55             throw new JMSException JavaDoc(""); //TOD: Write exception method.
56
}
57         char[] identifierCharArray = name.toCharArray();
58         if (identifierCharArray.length < 1)
59         {
60             throw new JMSException JavaDoc(""); //TOD: Write exception method
61
}
62         if (!Character.isJavaIdentifierStart(identifierCharArray[0]))
63         {
64             throw new JMSException JavaDoc(""); //TOD: Write exception method
65
}
66         for (int i = 1; i < identifierCharArray.length; i++)
67         {
68             if (!Character.isJavaIdentifierPart(identifierCharArray[i]))
69             {
70                 throw new JMSException JavaDoc("");
71                 //TOD: Write exception method
72
}
73         }
74         for (int i = 0; i < illegalIdentifiers.length; i++)
75         {
76             if (name.equalsIgnoreCase(illegalIdentifiers[i]))
77             {
78                 throw new JMSException JavaDoc("");
79                 //TOD: Write exception method
80
}
81         }
82     }
83
84     public Enumeration JavaDoc getMapNames()
85     {
86         List JavaDoc filteredList = new ArrayList JavaDoc(this.contents.size());
87         Iterator JavaDoc keys = this.contents.keySet().iterator();
88         while (keys.hasNext())
89         {
90             String JavaDoc key = (String JavaDoc) keys.next();
91             if (!key.startsWith("JMSX") || !key.startsWith("JMS_"))
92             {
93                 filteredList.add(key);
94             }
95         }
96         return Collections.enumeration(filteredList);
97     }
98
99     public final boolean isReadOnly()
100     {
101         return this.readOnly;
102     }
103
104     public void setBoolean(String JavaDoc name, boolean value) throws JMSException JavaDoc
105     {
106         this.throwExceptionIfReadOnly();
107         throwExceptionIfNameIsIllegal(name);
108         super.contents.put(name, new Boolean JavaDoc(value));
109     }
110
111     public void setByte(String JavaDoc name, byte value) throws JMSException JavaDoc
112     {
113         this.throwExceptionIfReadOnly();
114         throwExceptionIfNameIsIllegal(name);
115         super.contents.put(name, new Byte JavaDoc(value));
116     }
117
118     public void setDouble(String JavaDoc name, double value) throws JMSException JavaDoc
119     {
120         this.throwExceptionIfReadOnly();
121         throwExceptionIfNameIsIllegal(name);
122         super.contents.put(name, new Double JavaDoc(value));
123     }
124
125     public void setFloat(String JavaDoc name, float value) throws JMSException JavaDoc
126     {
127         this.throwExceptionIfReadOnly();
128         throwExceptionIfNameIsIllegal(name);
129         super.contents.put(name, new Float JavaDoc(value));
130     }
131
132     public void setInt(String JavaDoc name, int value) throws JMSException JavaDoc
133     {
134         this.throwExceptionIfReadOnly();
135         throwExceptionIfNameIsIllegal(name);
136         super.contents.put(name, new Integer JavaDoc(value));
137     }
138
139     public void setLong(String JavaDoc name, long value) throws JMSException JavaDoc
140     {
141         this.throwExceptionIfReadOnly();
142         throwExceptionIfNameIsIllegal(name);
143         super.contents.put(name, new Long JavaDoc(value));
144     }
145
146     public void setObject(String JavaDoc name, Object JavaDoc value) throws JMSException JavaDoc
147     {
148         this.throwExceptionIfReadOnly();
149         throwExceptionIfNameIsIllegal(name);
150         if (value instanceof Boolean JavaDoc
151                 || value instanceof Byte JavaDoc
152                 || value instanceof Double JavaDoc
153                 || value instanceof Float JavaDoc
154                 || value instanceof Integer JavaDoc
155                 || value instanceof Long JavaDoc
156                 || value instanceof Short JavaDoc
157                 || value instanceof String JavaDoc)
158         {
159             super.contents.put(name, value);
160         }
161         else
162         {
163             throw new MessageFormatException JavaDoc(""); //TOD: Implement message
164
}
165     }
166
167     public final void setReadOnly(boolean value)
168     {
169         this.readOnly = value;
170     }
171
172     public void setShort(String JavaDoc name, short value) throws JMSException JavaDoc
173     {
174         this.throwExceptionIfReadOnly();
175         throwExceptionIfNameIsIllegal(name);
176         super.contents.put(name, new Short JavaDoc(value));
177     }
178
179     public void setString(String JavaDoc name, String JavaDoc value) throws JMSException JavaDoc
180     {
181         this.throwExceptionIfReadOnly();
182         throwExceptionIfNameIsIllegal(name);
183         super.contents.put(name, value);
184     }
185
186     private void throwExceptionIfReadOnly() throws JMSException JavaDoc
187     {
188         if (this.isReadOnly())
189         {
190             throw new MessageNotWriteableException JavaDoc("Unable to write property: the message properties are currently read only.");
191         }
192     }
193
194 }
Popular Tags