KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2007 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 java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.jms.Destination JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.jms.Topic JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import org.springframework.jndi.JndiLocatorSupport;
31 import org.springframework.util.Assert;
32
33 /**
34  * {@link DestinationResolver} implementation which interprets destination names
35  * as JNDI locations (with a configurable fallback strategy).
36  *
37  * <p>Allows for customizing the JNDI environment if necessary, for example
38  * specifying appropriate JNDI environment properties.
39  *
40  * <p>Dynamic queues and topics get cached by destination name. As a consequence,
41  * you need to use unique destination names across both queues and topics.
42  * Caching can be turned off through the {@link #setCache "cache"} flag.
43  *
44  * <p>Note that the fallback to resolution of dynamic destinations
45  * is turned <i>off</i> by default. Switch the
46  * {@link #setFallbackToDynamicDestination "fallbackToDynamicDestination"}
47  * flag on to enable this functionality.
48  *
49  * @author Mark Pollack
50  * @author Juergen Hoeller
51  * @since 1.1
52  * @see #setJndiTemplate
53  * @see #setJndiEnvironment
54  * @see #setCache
55  * @see #setFallbackToDynamicDestination
56  */

57 public class JndiDestinationResolver extends JndiLocatorSupport implements CachingDestinationResolver {
58
59     private boolean cache = true;
60
61     private boolean fallbackToDynamicDestination = false;
62
63     private DestinationResolver dynamicDestinationResolver = new DynamicDestinationResolver();
64
65     private final Map JavaDoc destinationCache = Collections.synchronizedMap(new HashMap JavaDoc());
66
67
68     /**
69      * Set whether to cache resolved destinations. Default is "true".
70      * <p>This flag can be turned off to re-lookup a destination for each operation,
71      * which allows for hot restarting of destinations. This is mainly useful
72      * during development.
73      * <p>Note that dynamic queues and topics get cached by destination name.
74      * As a consequence, you need to use unique destination names across both
75      * queues and topics.
76      */

77     public void setCache(boolean cache) {
78         this.cache = cache;
79     }
80
81     /**
82      * Set whether this resolver is supposed to create dynamic destinations
83      * if the destination name is not found in JNDI. Default is "false".
84      * <p>Turn this flag on to enable transparent fallback to dynamic destinations.
85      * @see #setDynamicDestinationResolver
86      */

87     public void setFallbackToDynamicDestination(boolean fallbackToDynamicDestination) {
88         this.fallbackToDynamicDestination = fallbackToDynamicDestination;
89     }
90
91     /**
92      * Set the {@link DestinationResolver} to use when falling back to dynamic
93      * destinations.
94      * <p>The default is Spring's standard {@link DynamicDestinationResolver}.
95      * @see #setFallbackToDynamicDestination
96      * @see DynamicDestinationResolver
97      */

98     public void setDynamicDestinationResolver(DestinationResolver dynamicDestinationResolver) {
99         this.dynamicDestinationResolver = dynamicDestinationResolver;
100     }
101
102
103     public Destination JavaDoc resolveDestinationName(Session JavaDoc session, String JavaDoc destinationName, boolean pubSubDomain)
104             throws JMSException JavaDoc {
105
106         Assert.notNull(destinationName, "Destination name must not be null");
107         Destination JavaDoc dest = (Destination JavaDoc) this.destinationCache.get(destinationName);
108         if (dest != null) {
109             validateDestination(dest, destinationName, pubSubDomain);
110         }
111         else {
112             try {
113                 dest = (Destination JavaDoc) lookup(destinationName, Destination JavaDoc.class);
114                 validateDestination(dest, destinationName, pubSubDomain);
115             }
116             catch (NamingException JavaDoc ex) {
117                 if (logger.isDebugEnabled()) {
118                     logger.debug("Destination [" + destinationName + "] not found in JNDI", ex);
119                 }
120                 if (this.fallbackToDynamicDestination) {
121                     dest = this.dynamicDestinationResolver.resolveDestinationName(session, destinationName, pubSubDomain);
122                 }
123                 else {
124                     throw new DestinationResolutionException(
125                             "Destination [" + destinationName + "] not found in JNDI", ex);
126                 }
127             }
128             if (this.cache) {
129                 this.destinationCache.put(destinationName, dest);
130             }
131         }
132         return dest;
133     }
134
135     /**
136      * Validate the given Destination object, checking whether it matches
137      * the expected type.
138      * @param destination the Destination object to validate
139      * @param destinationName the name of the destination
140      * @param pubSubDomain <code>true</code> if a Topic is expected,
141      * <code>false</code> in case of a Queue
142      */

143     protected void validateDestination(Destination JavaDoc destination, String JavaDoc destinationName, boolean pubSubDomain) {
144         Class JavaDoc targetClass = Queue JavaDoc.class;
145         if (pubSubDomain) {
146             targetClass = Topic JavaDoc.class;
147         }
148         if (!targetClass.isInstance(destination)) {
149             throw new DestinationResolutionException(
150                     "Destination [" + destinationName + "] is not of expected type [" + targetClass.getName() + "]");
151         }
152     }
153
154
155     public void removeFromCache(String JavaDoc destinationName) {
156         this.destinationCache.remove(destinationName);
157     }
158
159     public void clearCache() {
160         this.destinationCache.clear();
161     }
162
163 }
164
Popular Tags