1 17 package org.apache.servicemix.wsn.jms; 18 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import javax.jms.Topic ; 23 import javax.xml.namespace.QName ; 24 25 import org.apache.activemq.command.ActiveMQTopic; 26 import org.oasis_open.docs.wsn.b_2.TopicExpressionType; 27 28 public class JmsTopicExpressionConverter { 29 30 public static final String SIMPLE_DIALECT = "http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple"; 31 32 public TopicExpressionType toTopicExpression(Topic topic) { 33 return toTopicExpression(topic.toString()); 34 } 35 36 public TopicExpressionType toTopicExpression(ActiveMQTopic topic) { 37 return toTopicExpression(topic.getPhysicalName()); 38 } 39 40 public TopicExpressionType toTopicExpression(String name) { 41 TopicExpressionType answer = new TopicExpressionType(); 42 answer.getContent().add(QName.valueOf(name)); 43 answer.setDialect(SIMPLE_DIALECT); 44 return answer; 45 } 46 47 public ActiveMQTopic toActiveMQTopic(List <TopicExpressionType> topics) throws InvalidTopicException { 48 if (topics == null || topics.size() == 0) { 49 return null; 50 } 51 int size = topics.size(); 52 ActiveMQTopic childrenDestinations[] = new ActiveMQTopic[size]; 53 for (int i = 0; i < size; i++) { 54 childrenDestinations[i] = toActiveMQTopic(topics.get(i)); 55 } 56 57 ActiveMQTopic topic = new ActiveMQTopic(); 58 topic.setCompositeDestinations(childrenDestinations); 59 return topic; 60 } 61 62 public ActiveMQTopic toActiveMQTopic(TopicExpressionType topic) throws InvalidTopicException { 63 String dialect = topic.getDialect(); 64 if (dialect == null || SIMPLE_DIALECT.equals(dialect)) { 65 for (Iterator iter = topic.getContent().iterator(); iter.hasNext();) { 66 ActiveMQTopic answer = createActiveMQTopicFromContent(iter.next()); 67 if (answer != null) { 68 return answer; 69 } 70 } 71 throw new InvalidTopicException("No topic name available topic: " + topic); 72 } 73 else { 74 throw new InvalidTopicException("Topic dialect: " + dialect + " not supported"); 75 } 76 } 77 78 protected ActiveMQTopic createActiveMQTopicFromContent(Object contentItem) { 81 if (contentItem instanceof String ) { 82 return new ActiveMQTopic(((String ) contentItem).trim()); 83 } 84 if (contentItem instanceof QName ) { 85 return createActiveMQTopicFromQName((QName ) contentItem); 86 } 87 return null; 88 } 89 90 protected ActiveMQTopic createActiveMQTopicFromQName(QName qName) { 91 return new ActiveMQTopic(qName.toString()); 92 } 93 94 } 95 | Popular Tags |