1 7 package org.jboss.jms.util; 8 9 import javax.jms.JMSException ; 10 import javax.jms.MessageFormatException ; 11 import javax.jms.MessageNotWriteableException ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.Enumeration ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 27 public final class MessageProperties extends JMSMap 28 { 29 30 private static final String [] illegalIdentifiers = 31 new String []{ 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 name) 47 throws JMSException 48 { 49 if (name == null) 50 { 51 throw new JMSException (""); } 53 if (name.startsWith("JMSX") || name.startsWith("JMS_")) 54 { 55 throw new JMSException (""); } 57 char[] identifierCharArray = name.toCharArray(); 58 if (identifierCharArray.length < 1) 59 { 60 throw new JMSException (""); } 62 if (!Character.isJavaIdentifierStart(identifierCharArray[0])) 63 { 64 throw new JMSException (""); } 66 for (int i = 1; i < identifierCharArray.length; i++) 67 { 68 if (!Character.isJavaIdentifierPart(identifierCharArray[i])) 69 { 70 throw new JMSException (""); 71 } 73 } 74 for (int i = 0; i < illegalIdentifiers.length; i++) 75 { 76 if (name.equalsIgnoreCase(illegalIdentifiers[i])) 77 { 78 throw new JMSException (""); 79 } 81 } 82 } 83 84 public Enumeration getMapNames() 85 { 86 List filteredList = new ArrayList (this.contents.size()); 87 Iterator keys = this.contents.keySet().iterator(); 88 while (keys.hasNext()) 89 { 90 String key = (String ) 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 name, boolean value) throws JMSException 105 { 106 this.throwExceptionIfReadOnly(); 107 throwExceptionIfNameIsIllegal(name); 108 super.contents.put(name, new Boolean (value)); 109 } 110 111 public void setByte(String name, byte value) throws JMSException 112 { 113 this.throwExceptionIfReadOnly(); 114 throwExceptionIfNameIsIllegal(name); 115 super.contents.put(name, new Byte (value)); 116 } 117 118 public void setDouble(String name, double value) throws JMSException 119 { 120 this.throwExceptionIfReadOnly(); 121 throwExceptionIfNameIsIllegal(name); 122 super.contents.put(name, new Double (value)); 123 } 124 125 public void setFloat(String name, float value) throws JMSException 126 { 127 this.throwExceptionIfReadOnly(); 128 throwExceptionIfNameIsIllegal(name); 129 super.contents.put(name, new Float (value)); 130 } 131 132 public void setInt(String name, int value) throws JMSException 133 { 134 this.throwExceptionIfReadOnly(); 135 throwExceptionIfNameIsIllegal(name); 136 super.contents.put(name, new Integer (value)); 137 } 138 139 public void setLong(String name, long value) throws JMSException 140 { 141 this.throwExceptionIfReadOnly(); 142 throwExceptionIfNameIsIllegal(name); 143 super.contents.put(name, new Long (value)); 144 } 145 146 public void setObject(String name, Object value) throws JMSException 147 { 148 this.throwExceptionIfReadOnly(); 149 throwExceptionIfNameIsIllegal(name); 150 if (value instanceof Boolean 151 || value instanceof Byte 152 || value instanceof Double 153 || value instanceof Float 154 || value instanceof Integer 155 || value instanceof Long 156 || value instanceof Short 157 || value instanceof String ) 158 { 159 super.contents.put(name, value); 160 } 161 else 162 { 163 throw new MessageFormatException (""); } 165 } 166 167 public final void setReadOnly(boolean value) 168 { 169 this.readOnly = value; 170 } 171 172 public void setShort(String name, short value) throws JMSException 173 { 174 this.throwExceptionIfReadOnly(); 175 throwExceptionIfNameIsIllegal(name); 176 super.contents.put(name, new Short (value)); 177 } 178 179 public void setString(String name, String value) throws JMSException 180 { 181 this.throwExceptionIfReadOnly(); 182 throwExceptionIfNameIsIllegal(name); 183 super.contents.put(name, value); 184 } 185 186 private void throwExceptionIfReadOnly() throws JMSException 187 { 188 if (this.isReadOnly()) 189 { 190 throw new MessageNotWriteableException ("Unable to write property: the message properties are currently read only."); 191 } 192 } 193 194 } | Popular Tags |