1 48 49 package org.exolab.jms.message; 50 51 import java.io.Externalizable ; 52 import java.io.IOException ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectOutput ; 55 import java.util.Collections ; 56 import java.util.Enumeration ; 57 import java.util.HashMap ; 58 59 import javax.jms.JMSException ; 60 import javax.jms.MessageFormatException ; 61 62 63 70 class MessageProperties implements Cloneable , Externalizable { 71 72 75 private HashMap _properties = new HashMap (20); 76 77 80 static final long serialVersionUID = 1; 81 82 85 private static final String [] RESERVED_WORDS = { 86 "and", "between", "escape", "in", "is", "like", "false", "null", "or", 87 "not", "true"}; 88 89 private static final String GROUP_ID = "JMSXGroupID"; 90 private static final String GROUP_SEQ = "JMSXGroupSeq"; 91 92 96 private static final Object [][] JMSX_CLIENT_NAMES = { 97 {GROUP_ID, String .class}, 98 {GROUP_SEQ, Integer .class}}; 99 100 103 public MessageProperties() { 104 } 105 106 112 public Object clone() throws CloneNotSupportedException { 113 MessageProperties result = new MessageProperties(); 114 result._properties = (HashMap ) _properties.clone(); 115 return result; 116 } 117 118 125 public void writeExternal(ObjectOutput out) throws IOException { 126 out.writeLong(serialVersionUID); 127 out.writeObject(_properties); 128 } 129 130 139 public void readExternal(ObjectInput in) 140 throws IOException , ClassNotFoundException { 141 long version = in.readLong(); 142 if (version == serialVersionUID) { 143 _properties = (HashMap ) in.readObject(); 144 } else { 145 throw new IOException ("Incorrect version enountered: " + 146 version + " This version = " + 147 serialVersionUID); 148 } 149 } 150 151 154 public void clearProperties() { 155 _properties.clear(); 156 } 157 158 public boolean propertyExists(String name) { 159 return _properties.containsKey(name); 160 } 161 162 public boolean getBooleanProperty(String name) throws JMSException { 163 return FormatConverter.getBoolean(_properties.get(name)); 164 } 165 166 public byte getByteProperty(String name) throws JMSException { 167 return FormatConverter.getByte(_properties.get(name)); 168 } 169 170 public short getShortProperty(String name) throws JMSException { 171 return FormatConverter.getShort(_properties.get(name)); 172 } 173 174 public int getIntProperty(String name) throws JMSException { 175 return FormatConverter.getInt(_properties.get(name)); 176 } 177 178 public long getLongProperty(String name) throws JMSException { 179 return FormatConverter.getLong(_properties.get(name)); 180 } 181 182 public float getFloatProperty(String name) throws JMSException { 183 return FormatConverter.getFloat(_properties.get(name)); 184 } 185 186 public double getDoubleProperty(String name) throws JMSException { 187 return FormatConverter.getDouble(_properties.get(name)); 188 } 189 190 public String getStringProperty(String name) throws JMSException { 191 return FormatConverter.getString(_properties.get(name)); 192 } 193 194 public Object getObjectProperty(String name) throws JMSException { 195 return _properties.get(name); 196 } 197 198 public Enumeration getPropertyNames() { 199 return Collections.enumeration(_properties.keySet()); 200 } 201 202 public void setBooleanProperty(String name, boolean value) 203 throws JMSException { 204 setProperty(name, new Boolean (value)); 205 } 206 207 public void setByteProperty(String name, byte value) throws JMSException { 208 setProperty(name, new Byte (value)); 209 } 210 211 public void setShortProperty(String name, short value) 212 throws JMSException { 213 setProperty(name, new Short (value)); 214 } 215 216 public void setIntProperty(String name, int value) throws JMSException { 217 setProperty(name, new Integer (value)); 218 } 219 220 public void setLongProperty(String name, long value) throws JMSException { 221 setProperty(name, new Long (value)); 222 } 223 224 public void setFloatProperty(String name, float value) 225 throws JMSException { 226 setProperty(name, new Float (value)); 227 } 228 229 public void setDoubleProperty(String name, double value) 230 throws JMSException { 231 setProperty(name, new Double (value)); 232 } 233 234 public void setStringProperty(String name, String value) 235 throws JMSException { 236 setProperty(name, value); 237 } 238 239 public void setObjectProperty(String name, Object value) 240 throws JMSException { 241 if (value instanceof Boolean || value instanceof Byte || 242 value instanceof Short || value instanceof Integer || 243 value instanceof Long || value instanceof Float || 244 value instanceof Double || value instanceof String || 245 (value == null)) { 246 setProperty(name, value); 247 } else { 248 throw new MessageFormatException ( 249 "Message.setObjectProperty() does not support objects of " + 250 "type=" + value.getClass().getName()); 251 } 252 } 253 254 public void setJMSXRcvTimestamp(long value) { 255 _properties.put("JMSXRcvTimestamp", new Long (value)); 256 } 257 258 protected void setProperty(String name, Object value) throws JMSException { 259 if (name == null) { 260 throw new JMSException ("<null> is not a valid property name"); 261 } 262 char[] chars = name.toCharArray(); 263 if (chars.length == 0) { 264 throw new JMSException ("zero-length name is not a valid " + 265 "property name"); 266 } 267 if (!Character.isJavaIdentifierStart(chars[0])) { 268 throw new JMSException ("name=" + name + " is not a valid " + 269 "property name"); 270 } 271 for (int i = 1; i < chars.length; ++i) { 272 if (!Character.isJavaIdentifierPart(chars[i])) { 273 throw new JMSException ("name=" + name + " is not a valid " + 274 "property name"); 275 } 276 } 277 for (int i = 0; i < RESERVED_WORDS.length; ++i) { 278 if (name.equalsIgnoreCase(RESERVED_WORDS[i])) { 279 throw new JMSException ("name=" + name + " is a reserved " + 280 "word; it cannot be used as a " + 281 "property name"); 282 } 283 } 284 285 if (name.startsWith("JMSX")) { 286 boolean found = false; 287 for (int i = 0; i < JMSX_CLIENT_NAMES.length; ++i) { 288 Object [] types = JMSX_CLIENT_NAMES[i]; 289 if (types[0].equals(name)) { 290 if (value == null) { 291 throw new MessageFormatException ("Property=" + name + 292 " may not be null"); 293 } 294 Class type = (Class ) types[1]; 295 if (!type.equals(value.getClass())) { 296 throw new MessageFormatException ( 297 "Expected type=" + type.getName() + 298 " for property=" + name + ", but got type=" + 299 value.getClass().getName()); 300 } 301 if (name.equals(GROUP_SEQ) && 302 ((Integer ) value).intValue() <= 0) { 303 throw new JMSException ( 304 GROUP_SEQ + " must have a value > 0"); 305 } 306 found = true; 307 break; 308 } 309 } 310 if (!found) { 311 throw new JMSException ("Property=" + name + 312 " cannot be set by clients"); 313 } 314 } 315 316 _properties.put(name, value); 317 } 318 319 } | Popular Tags |