001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.wsn.jms;
018
019 import java.util.Iterator;
020 import java.util.List;
021
022 import javax.jms.Topic;
023 import javax.xml.namespace.QName;
024
025 import org.apache.activemq.command.ActiveMQTopic;
026 import org.oasis_open.docs.wsn.b_2.TopicExpressionType;
027
028 public class JmsTopicExpressionConverter {
029
030 public static final String SIMPLE_DIALECT = "http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple";
031
032 public TopicExpressionType toTopicExpression(Topic topic) {
033 return toTopicExpression(topic.toString());
034 }
035
036 public TopicExpressionType toTopicExpression(ActiveMQTopic topic) {
037 return toTopicExpression(topic.getPhysicalName());
038 }
039
040 public TopicExpressionType toTopicExpression(String name) {
041 TopicExpressionType answer = new TopicExpressionType();
042 answer.getContent().add(QName.valueOf(name));
043 answer.setDialect(SIMPLE_DIALECT);
044 return answer;
045 }
046
047 public ActiveMQTopic toActiveMQTopic(List<TopicExpressionType> topics) throws InvalidTopicException {
048 if (topics == null || topics.size() == 0) {
049 return null;
050 }
051 int size = topics.size();
052 ActiveMQTopic childrenDestinations[] = new ActiveMQTopic[size];
053 for (int i = 0; i < size; i++) {
054 childrenDestinations[i] = toActiveMQTopic(topics.get(i));
055 }
056
057 ActiveMQTopic topic = new ActiveMQTopic();
058 topic.setCompositeDestinations(childrenDestinations);
059 return topic;
060 }
061
062 public ActiveMQTopic toActiveMQTopic(TopicExpressionType topic) throws InvalidTopicException {
063 String dialect = topic.getDialect();
064 if (dialect == null || SIMPLE_DIALECT.equals(dialect)) {
065 for (Iterator iter = topic.getContent().iterator(); iter.hasNext();) {
066 ActiveMQTopic answer = createActiveMQTopicFromContent(iter.next());
067 if (answer != null) {
068 return answer;
069 }
070 }
071 throw new InvalidTopicException("No topic name available topic: " + topic);
072 } else {
073 throw new InvalidTopicException("Topic dialect: " + dialect + " not supported");
074 }
075 }
076
077 // Implementation methods
078 // -------------------------------------------------------------------------
079 protected ActiveMQTopic createActiveMQTopicFromContent(Object contentItem) {
080 if (contentItem instanceof String) {
081 return new ActiveMQTopic(((String) contentItem).trim());
082 }
083 if (contentItem instanceof QName) {
084 return createActiveMQTopicFromQName((QName) contentItem);
085 }
086 return null;
087 }
088
089 protected ActiveMQTopic createActiveMQTopicFromQName(QName qName) {
090 return new ActiveMQTopic(qName.toString());
091 }
092
093 }