1 57 58 package org.apache.wsif.providers.jms; 59 60 import java.text.DateFormat ; 61 import java.text.SimpleDateFormat ; 62 63 import javax.jms.JMSException ; 64 import javax.jms.Message ; 65 import javax.xml.namespace.QName ; 66 67 import org.apache.wsif.format.jms.JMSFormatHandler; 68 import org.apache.wsif.logging.Trc; 69 70 public class PrimitiveTypeFormatHandler implements JMSFormatHandler { 71 private static final long serialVersionUID = 1L; 72 73 protected static DateFormat GREGORIAN_CALENDAR_DATE_FORMAT = 74 new SimpleDateFormat ("yyyy-MM-dd"); 75 protected static DateFormat STANDARD_DATE_FORMAT = 76 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 77 protected static DateFormat PRECISE_DATE_FORMAT = 78 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'"); 79 80 QName name; 81 Object primitiveType; 82 Class type; 83 84 87 public PrimitiveTypeFormatHandler(Class type) { 88 super(); 89 this.type = type; 90 } 91 92 95 public Object getElement(String arg0) { 96 return null; 97 } 98 99 102 public void setElement(String arg0, Object arg1) { 103 } 104 105 108 public Object getElement(String arg0, int arg1) { 109 return null; 110 } 111 112 115 public void setElement(String arg0, int arg1, Object arg2) { 116 } 117 118 121 public Object getObjectPart() { 122 return primitiveType; 123 } 124 125 128 public Object getObjectPart(Class arg0) { 129 return null; 130 } 131 132 public char getCharPart() { 133 return 0; 134 } 135 136 public byte getBytePart() { 137 return 0; 138 } 139 140 public short getShortPart() { 141 return 0; 142 } 143 144 public int getIntPart() { 145 return 0; 146 } 147 148 public long getLongPart() { 149 return 0; 150 } 151 152 public float getFloatPart() { 153 return 0; 154 } 155 156 public double getDoublePart() { 157 return 0; 158 } 159 160 public QName getPartQName() { 161 return name; 162 } 163 164 public void setObjectPart(Object arg0) { 165 primitiveType = arg0; 166 } 167 168 public void setCharPart(char arg0) { 169 } 170 171 public void setBytePart(byte arg0) { 172 } 173 174 public void setShortPart(short arg0) { 175 } 176 177 public void setIntPart(int arg0) { 178 } 179 180 public void setLongPart(long arg0) { 181 } 182 183 public void setFloatPart(float arg0) { 184 } 185 186 public void setDoublePart(double arg0) { 187 } 188 189 public void setPartQName(QName name) { 190 this.name = name; 191 } 192 193 196 public void write(Message message) { 197 Trc.entry(this, message); 198 try { 199 if (message instanceof javax.jms.TextMessage ) 200 write((javax.jms.TextMessage ) message); 201 else if (message instanceof javax.jms.ObjectMessage ) 202 write((javax.jms.ObjectMessage ) message); 203 } catch (Exception e) { 204 Trc.exception(e); 205 } 206 Trc.exit(); 207 } 208 209 212 public void read(Message message) { 213 Trc.entry(this, message); 214 try { 215 if (message instanceof javax.jms.TextMessage ) 216 read((javax.jms.TextMessage ) message); 217 else if (message instanceof javax.jms.ObjectMessage ) 218 read((javax.jms.ObjectMessage ) message); 219 } catch (Exception e) { 220 Trc.exception(e); 221 } 222 Trc.exit(); 223 224 } 225 226 private void write(javax.jms.TextMessage message) throws JMSException { 227 String value = null; 228 229 if (primitiveType == null) 230 value = ""; 231 else if (primitiveType instanceof java.util.GregorianCalendar ) 232 value = 233 GREGORIAN_CALENDAR_DATE_FORMAT.format( 234 ((java.util.GregorianCalendar ) primitiveType).getTime()); 235 else if (primitiveType instanceof java.util.Date ) 236 value = STANDARD_DATE_FORMAT.format((java.util.Date ) primitiveType); 237 else if (primitiveType instanceof byte[]) 238 value = new String ((byte[]) primitiveType); 239 else { 240 value = primitiveType.toString(); 242 } 243 244 if (value != null) 245 message.setText(value); 246 } 247 248 private void write(javax.jms.ObjectMessage message) throws JMSException { 249 if (primitiveType != null && primitiveType instanceof java.io.Serializable ) 250 message.setObject((java.io.Serializable ) primitiveType); 251 } 252 253 private void read(javax.jms.TextMessage message) throws JMSException { 254 String value = message.getText(); 255 256 Class cls = type; 257 try { 258 if (cls == String .class) { 259 primitiveType = value; 260 } 261 else if (cls == byte[].class) { 263 primitiveType = value.getBytes(); 264 } 265 else if (java.util.GregorianCalendar .class.isAssignableFrom(cls)) { 267 java.util.GregorianCalendar result = new java.util.GregorianCalendar (); 268 result.setTime(GREGORIAN_CALENDAR_DATE_FORMAT.parse(value)); 269 primitiveType = result; 270 } 271 else if (java.util.Date .class.isAssignableFrom(cls)) { 273 java.util.Date result = new java.util.Date (); 274 try { 275 result = STANDARD_DATE_FORMAT.parse(value); 276 } catch (java.text.ParseException standardException) { 277 Trc.exception(standardException); 278 result = PRECISE_DATE_FORMAT.parse(value); 279 } 280 primitiveType = result; 281 } 282 else { 284 java.lang.reflect.Constructor constructor = 285 cls.getConstructor(new Class [] { String .class }); 286 primitiveType = constructor.newInstance(new Object [] { value }); 287 } 288 } catch (Exception e) { 289 Trc.exception(e); 290 throw new JMSException ("Unable to parse message"); 291 } 292 293 } 294 295 private void read(javax.jms.ObjectMessage message) throws JMSException { 296 primitiveType = message.getObject(); 297 if (!primitiveType.getClass().isAssignableFrom(type)) 299 throw new JMSException ("Unable to parse message"); 300 } 301 } | Popular Tags |