KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > jms > JmsTopicExpressionConverter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.wsn.jms;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.jms.Topic JavaDoc;
23 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc SIMPLE_DIALECT = "http://docs.oasis-open.org/wsn/t-1/TopicExpression/Simple";
31
32     public TopicExpressionType toTopicExpression(Topic JavaDoc 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 JavaDoc 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 JavaDoc<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 JavaDoc dialect = topic.getDialect();
64         if (dialect == null || SIMPLE_DIALECT.equals(dialect)) {
65             for (Iterator JavaDoc 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     // Implementation methods
79
// -------------------------------------------------------------------------
80
protected ActiveMQTopic createActiveMQTopicFromContent(Object JavaDoc contentItem) {
81         if (contentItem instanceof String JavaDoc) {
82             return new ActiveMQTopic(((String JavaDoc) contentItem).trim());
83         }
84         if (contentItem instanceof QName JavaDoc) {
85             return createActiveMQTopicFromQName((QName JavaDoc) contentItem);
86         }
87         return null;
88     }
89
90     protected ActiveMQTopic createActiveMQTopicFromQName(QName JavaDoc qName) {
91         return new ActiveMQTopic(qName.toString());
92     }
93
94 }
95
Popular Tags