1 10 11 package org.mule.providers.jms.transformers; 12 13 import java.util.Iterator ; 14 15 import javax.jms.Destination ; 16 import javax.jms.JMSException ; 17 import javax.jms.Message ; 18 import javax.jms.Session ; 19 20 import org.mule.config.MuleProperties; 21 import org.mule.impl.RequestContext; 22 import org.mule.providers.jms.JmsConnector; 23 import org.mule.providers.jms.JmsConstants; 24 import org.mule.providers.jms.JmsMessageUtils; 25 import org.mule.transformers.AbstractTransformer; 26 import org.mule.umo.UMOEventContext; 27 import org.mule.umo.UMOException; 28 import org.mule.umo.UMOMessage; 29 import org.mule.umo.endpoint.UMOImmutableEndpoint; 30 import org.mule.umo.provider.UMOConnector; 31 import org.mule.umo.provider.UMOMessageDispatcher; 32 import org.mule.umo.transformer.TransformerException; 33 import org.mule.util.StringUtils; 34 35 40 41 public abstract class AbstractJmsTransformer extends AbstractTransformer 42 { 43 public static final char REPLACEMENT_CHAR = '_'; 44 45 51 public static String encodeHeader(String name) 52 { 53 if (StringUtils.isEmpty(name)) 54 { 55 throw new IllegalArgumentException ("Header name to encode must not be null or empty"); 56 } 57 58 int i = 0, length = name.length(); 59 while (i < length && Character.isJavaIdentifierPart(name.charAt(i))) 60 { 61 i++; 63 } 64 65 if (i == length) 66 { 67 return name; 69 } 70 else 71 { 72 StringBuffer sb = new StringBuffer (name); 74 for (int j = i; j < length; j++) 75 { 76 if (!Character.isJavaIdentifierPart(sb.charAt(j))) 77 { 78 sb.setCharAt(j, REPLACEMENT_CHAR); 79 } 80 } 81 return sb.toString(); 82 } 83 } 84 85 public AbstractJmsTransformer() 86 { 87 super(); 88 } 89 90 protected Message transformToMessage(Object src) throws TransformerException 91 { 92 try 93 { 94 Message result; 95 96 if (src instanceof Message ) 97 { 98 result = (Message )src; 99 result.clearProperties(); 100 } 101 else 102 { 103 result = JmsMessageUtils.toMessage(src, this.getSession()); 104 } 105 106 UMOEventContext ctx = RequestContext.getEventContext(); 108 if (ctx == null) 109 { 110 logger.warn("There is no current event context"); 111 return result; 112 } 113 114 this.setJmsProperties(ctx.getMessage(), result); 115 116 return result; 117 } 118 catch (Exception e) 119 { 120 throw new TransformerException(this, e); 121 } 122 } 123 124 protected Object transformFromMessage(Message source) throws TransformerException 125 { 126 try 127 { 128 if (logger.isDebugEnabled()) 129 { 130 logger.debug("Message type received is: " + source.getClass().getName()); 131 } 132 133 String jmsSpec = JmsConstants.JMS_SPECIFICATION_102B; 136 UMOImmutableEndpoint endpoint = this.getEndpoint(); 137 if (endpoint != null) 138 { 139 UMOConnector connector = endpoint.getConnector(); 140 if (connector instanceof JmsConnector) 141 { 142 jmsSpec = ((JmsConnector)connector).getSpecification(); 143 } 144 } 145 146 return JmsMessageUtils.toObject(source, jmsSpec); 147 } 148 catch (Exception e) 149 { 150 throw new TransformerException(this, e); 151 } 152 } 153 154 protected void setJmsProperties(UMOMessage umoMessage, Message msg) throws JMSException 155 { 156 for (Iterator iterator = umoMessage.getPropertyNames().iterator(); iterator.hasNext();) 157 { 158 String key = iterator.next().toString(); 159 160 if (!JmsConstants.JMS_PROPERTY_NAMES.contains(key)) 161 { 162 Object value = umoMessage.getProperty(key); 163 164 if (MuleProperties.MULE_CORRELATION_ID_PROPERTY.equals(key)) 165 { 166 msg.setJMSCorrelationID(umoMessage.getCorrelationId()); 167 } 168 169 if (!(MuleProperties.MULE_REPLY_TO_PROPERTY.equals(key) && value instanceof Destination )) 172 { 173 try 174 { 175 msg.setObjectProperty(encodeHeader(key), value); 176 } 177 catch (JMSException e) 178 { 179 if (logger.isDebugEnabled()) 183 { 184 logger.debug("Unable to set property '" + encodeHeader(key) + "' of type " 185 + value.getClass().getName() + "': " + e.getMessage()); 186 } 187 } 188 } 189 } 190 } 191 } 192 193 protected Session getSession() throws UMOException 194 { 195 UMOImmutableEndpoint endpoint = this.getEndpoint(); 198 if (endpoint != null) 199 { 200 UMOMessageDispatcher dispatcher = endpoint.getConnector().getDispatcher(endpoint); 201 return (Session )dispatcher.getDelegateSession(); 202 } 203 else 204 { 205 throw new TransformerException(this, new IllegalStateException ( 206 "This transformer needs a valid endpoint")); 207 } 208 } 209 210 } 211 | Popular Tags |