KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > support > destination > DynamicDestinationResolver


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.support.destination;
18
19 import javax.jms.Destination JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.Queue JavaDoc;
22 import javax.jms.QueueSession JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.jms.Topic JavaDoc;
25 import javax.jms.TopicSession JavaDoc;
26
27 import org.springframework.util.Assert;
28
29 /**
30  * Simple {@link DestinationResolver} implementation resolving destination names
31  * as dynamic destinations.
32  *
33  * <p>This implementation will work on both JMS 1.1 and JMS 1.0.2,
34  * because it uses the {@link javax.jms.QueueSession} or {@link javax.jms.TopicSession}
35  * methods if possible, falling back to JMS 1.1's generic {@link javax.jms.Session}
36  * methods.
37  *
38  * @author Juergen Hoeller
39  * @since 1.1
40  * @see javax.jms.QueueSession#createQueue
41  * @see javax.jms.TopicSession#createTopic
42  * @see javax.jms.Session#createQueue
43  * @see javax.jms.Session#createTopic
44  */

45 public class DynamicDestinationResolver implements DestinationResolver {
46
47     /**
48      * Resolve the specified destination name as a dynamic destination.
49      * @param session the current JMS Session
50      * @param destinationName the name of the destination
51      * @param pubSubDomain <code>true</code> if the domain is pub-sub, <code>false</code> if P2P
52      * @return the JMS destination (either a topic or a queue)
53      * @throws javax.jms.JMSException if resolution failed
54      * @see #resolveTopic(javax.jms.Session, String)
55      * @see #resolveQueue(javax.jms.Session, String)
56      */

57     public Destination JavaDoc resolveDestinationName(Session JavaDoc session, String JavaDoc destinationName, boolean pubSubDomain)
58             throws JMSException JavaDoc {
59
60         Assert.notNull(session, "Session must not be null");
61         Assert.notNull(destinationName, "Destination name must not be null");
62         if (pubSubDomain) {
63             return resolveTopic(session, destinationName);
64         }
65         else {
66             return resolveQueue(session, destinationName);
67         }
68     }
69
70
71     /**
72      * Resolve the given destination name to a {@link Topic}.
73      * @param session the current JMS Session
74      * @param topicName the name of the desired {@link Topic}
75      * @return the JMS {@link Topic}
76      * @throws javax.jms.JMSException if resolution failed
77      * @see Session#createTopic(String)
78      */

79     protected Topic JavaDoc resolveTopic(Session JavaDoc session, String JavaDoc topicName) throws JMSException JavaDoc {
80         if (session instanceof TopicSession JavaDoc) {
81             // Cast to TopicSession: will work on both JMS 1.1 and 1.0.2
82
return ((TopicSession JavaDoc) session).createTopic(topicName);
83         }
84         else {
85             // Fall back to generic JMS Session: will only work on JMS 1.1
86
return session.createTopic(topicName);
87         }
88     }
89
90     /**
91      * Resolve the given destination name to a {@link Queue}.
92      * @param session the current JMS Session
93      * @param queueName the name of the desired {@link Queue}
94      * @return the JMS {@link Queue}
95      * @throws javax.jms.JMSException if resolution failed
96      * @see Session#createQueue(String)
97      */

98     protected Queue JavaDoc resolveQueue(Session JavaDoc session, String JavaDoc queueName) throws JMSException JavaDoc {
99         if (session instanceof QueueSession JavaDoc) {
100             // Cast to QueueSession: will work on both JMS 1.1 and 1.0.2
101
return ((QueueSession JavaDoc) session).createQueue(queueName);
102         }
103         else {
104             // Fall back to generic JMS Session: will only work on JMS 1.1
105
return session.createQueue(queueName);
106         }
107     }
108
109 }
110
Popular Tags