1 17 package org.apache.servicemix.components.jms; 18 19 import org.apache.servicemix.jbi.jaxp.SourceMarshaler; 20 21 import javax.jbi.messaging.MessagingException; 22 import javax.jbi.messaging.NormalizedMessage; 23 import javax.jms.JMSException ; 24 import javax.jms.Message ; 25 import javax.jms.Session ; 26 import javax.jms.TextMessage ; 27 import javax.xml.transform.Source ; 28 import javax.xml.transform.TransformerException ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import java.util.Iterator ; 32 33 38 public class JmsMarshaler { 39 private SourceMarshaler sourceMarshaler; 40 41 private boolean needJavaIdentifiers; 42 43 public JmsMarshaler() { 44 this(new SourceMarshaler()); 45 } 46 47 public JmsMarshaler(SourceMarshaler sourceMarshaler) { 48 this.sourceMarshaler = sourceMarshaler; 49 } 50 51 56 public void toNMS(NormalizedMessage normalizedMessage, Message message) throws JMSException , MessagingException { 57 addNmsProperties(normalizedMessage, message); 58 if (message instanceof TextMessage ) { 59 TextMessage textMessage = (TextMessage ) message; 60 Source source = sourceMarshaler.asSource(textMessage.getText()); 61 normalizedMessage.setContent(source); 62 } 63 64 } 68 69 public Message createMessage(NormalizedMessage normalizedMessage, Session session) throws JMSException , TransformerException { 70 String xml = messageAsString(normalizedMessage); 72 TextMessage message = session.createTextMessage(xml); 73 addJmsProperties(message, normalizedMessage); 74 return message; 75 } 76 77 public SourceMarshaler getSourceMarshaller() { 80 return sourceMarshaler; 81 } 82 83 public void setSourceMarshaller(SourceMarshaler sourceMarshaler) { 84 this.sourceMarshaler = sourceMarshaler; 85 } 86 87 90 93 protected String messageAsString(NormalizedMessage normalizedMessage) throws TransformerException { 94 return sourceMarshaler.asString(normalizedMessage.getContent()); 95 } 96 97 100 protected void addJmsProperties(Message message, NormalizedMessage normalizedMessage) throws JMSException { 101 for (Iterator iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) { 102 String name = (String ) iter.next(); 103 Object value = normalizedMessage.getProperty(name); 104 if (shouldIncludeHeader(normalizedMessage, name, value)) { 105 message.setObjectProperty(name, value); 106 } 107 } 108 } 109 110 protected void addNmsProperties(NormalizedMessage message, Message jmsMessage) throws JMSException { 111 Enumeration enumeration = jmsMessage.getPropertyNames(); 112 while (enumeration.hasMoreElements()) { 113 String name = (String ) enumeration.nextElement(); 114 Object value = jmsMessage.getObjectProperty(name); 115 message.setProperty(name, value); 116 } 117 } 118 119 123 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 124 return (value instanceof String || value instanceof Number || value instanceof Date ) && 125 (!isNeedJavaIdentifiers() || isJavaIdentifier(name)); 126 } 127 128 private static boolean isJavaIdentifier(String s) { 129 int n = s.length(); 130 if (n==0) return false; 131 if (!Character.isJavaIdentifierStart(s.charAt(0))) 132 return false; 133 for (int i = 1; i < n; i++) 134 if (!Character.isJavaIdentifierPart(s.charAt(i))) 135 return false; 136 return true; 137 } 138 139 142 public boolean isNeedJavaIdentifiers() { 143 return needJavaIdentifiers; 144 } 145 146 149 public void setNeedJavaIdentifiers(boolean needJavaIdentifiers) { 150 this.needJavaIdentifiers = needJavaIdentifiers; 151 } 152 } 153 | Popular Tags |