1 16 17 package org.springframework.jms.support.converter; 18 19 import java.io.Serializable ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.jms.BytesMessage ; 26 import javax.jms.JMSException ; 27 import javax.jms.MapMessage ; 28 import javax.jms.Message ; 29 import javax.jms.ObjectMessage ; 30 import javax.jms.Session ; 31 import javax.jms.TextMessage ; 32 33 import org.springframework.util.ObjectUtils; 34 35 57 public class SimpleMessageConverter implements MessageConverter { 58 59 68 public Message toMessage(Object object, Session session) throws JMSException , MessageConversionException { 69 if (object instanceof Message ) { 70 return (Message ) object; 71 } 72 else if (object instanceof String ) { 73 return createMessageForString((String ) object, session); 74 } 75 else if (object instanceof byte[]) { 76 return createMessageForByteArray((byte[]) object, session); 77 } 78 else if (object instanceof Map ) { 79 return createMessageForMap((Map ) object, session); 80 } 81 else if (object instanceof Serializable ) { 82 return createMessageForSerializable(((Serializable ) object), session); 83 } 84 else { 85 throw new MessageConversionException("Cannot convert object [" + object + "] to JMS message"); 86 } 87 } 88 89 99 public Object fromMessage(Message message) throws JMSException , MessageConversionException { 100 if (message instanceof TextMessage ) { 101 return extractStringFromMessage((TextMessage ) message); 102 } 103 else if (message instanceof BytesMessage ) { 104 return extractByteArrayFromMessage((BytesMessage ) message); 105 } 106 else if (message instanceof MapMessage ) { 107 return extractMapFromMessage((MapMessage ) message); 108 } 109 else if (message instanceof ObjectMessage ) { 110 return extractSerializableFromMessage((ObjectMessage ) message); 111 } 112 else { 113 return message; 114 } 115 } 116 117 118 126 protected TextMessage createMessageForString(String text, Session session) throws JMSException { 127 return session.createTextMessage(text); 128 } 129 130 138 protected BytesMessage createMessageForByteArray(byte[] bytes, Session session) throws JMSException { 139 BytesMessage message = session.createBytesMessage(); 140 message.writeBytes(bytes); 141 return message; 142 } 143 144 152 protected MapMessage createMessageForMap(Map map, Session session) throws JMSException { 153 MapMessage message = session.createMapMessage(); 154 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 155 Map.Entry entry = (Map.Entry ) it.next(); 156 if (!(entry.getKey() instanceof String )) { 157 throw new MessageConversionException("Cannot convert non-String key of type [" + 158 ObjectUtils.nullSafeClassName(entry.getKey()) + "] to JMS MapMessage entry"); 159 } 160 message.setObject((String ) entry.getKey(), entry.getValue()); 161 } 162 return message; 163 } 164 165 173 protected ObjectMessage createMessageForSerializable(Serializable object, Session session) throws JMSException { 174 return session.createObjectMessage(object); 175 } 176 177 178 184 protected String extractStringFromMessage(TextMessage message) throws JMSException { 185 return message.getText(); 186 } 187 188 194 protected byte[] extractByteArrayFromMessage(BytesMessage message) throws JMSException { 195 byte[] bytes = new byte[(int) message.getBodyLength()]; 196 message.readBytes(bytes); 197 return bytes; 198 } 199 200 206 protected Map extractMapFromMessage(MapMessage message) throws JMSException { 207 Map map = new HashMap (); 208 Enumeration en = message.getMapNames(); 209 while (en.hasMoreElements()) { 210 String key = (String ) en.nextElement(); 211 map.put(key, message.getObject(key)); 212 } 213 return map; 214 } 215 216 222 protected Serializable extractSerializableFromMessage(ObjectMessage message) throws JMSException { 223 return message.getObject(); 224 } 225 226 } 227 | Popular Tags |