001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jms.support.destination;
018:
019: import java.util.Map;
020:
021: import javax.jms.Destination;
022: import javax.jms.JMSException;
023: import javax.jms.Queue;
024: import javax.jms.Session;
025: import javax.jms.Topic;
026: import javax.naming.NamingException;
027:
028: import org.springframework.core.CollectionFactory;
029: import org.springframework.jndi.JndiLocatorSupport;
030: import org.springframework.util.Assert;
031:
032: /**
033: * {@link DestinationResolver} implementation which interprets destination names
034: * as JNDI locations (with a configurable fallback strategy).
035: *
036: * <p>Allows for customizing the JNDI environment if necessary, for example
037: * specifying appropriate JNDI environment properties.
038: *
039: * <p>Dynamic queues and topics get cached by destination name. As a consequence,
040: * you need to use unique destination names across both queues and topics.
041: * Caching can be turned off through the {@link #setCache "cache"} flag.
042: *
043: * <p>Note that the fallback to resolution of dynamic destinations
044: * is turned <i>off</i> by default. Switch the
045: * {@link #setFallbackToDynamicDestination "fallbackToDynamicDestination"}
046: * flag on to enable this functionality.
047: *
048: * @author Mark Pollack
049: * @author Juergen Hoeller
050: * @since 1.1
051: * @see #setJndiTemplate
052: * @see #setJndiEnvironment
053: * @see #setCache
054: * @see #setFallbackToDynamicDestination
055: */
056: public class JndiDestinationResolver extends JndiLocatorSupport
057: implements CachingDestinationResolver {
058:
059: private boolean cache = true;
060:
061: private boolean fallbackToDynamicDestination = false;
062:
063: private DestinationResolver dynamicDestinationResolver = new DynamicDestinationResolver();
064:
065: private final Map destinationCache = CollectionFactory
066: .createConcurrentMapIfPossible(16);
067:
068: /**
069: * Set whether to cache resolved destinations. Default is "true".
070: * <p>This flag can be turned off to re-lookup a destination for each operation,
071: * which allows for hot restarting of destinations. This is mainly useful
072: * during development.
073: * <p>Note that dynamic queues and topics get cached by destination name.
074: * As a consequence, you need to use unique destination names across both
075: * queues and topics.
076: */
077: public void setCache(boolean cache) {
078: this .cache = cache;
079: }
080:
081: /**
082: * Set whether this resolver is supposed to create dynamic destinations
083: * if the destination name is not found in JNDI. Default is "false".
084: * <p>Turn this flag on to enable transparent fallback to dynamic destinations.
085: * @see #setDynamicDestinationResolver
086: */
087: public void setFallbackToDynamicDestination(
088: boolean fallbackToDynamicDestination) {
089: this .fallbackToDynamicDestination = fallbackToDynamicDestination;
090: }
091:
092: /**
093: * Set the {@link DestinationResolver} to use when falling back to dynamic
094: * destinations.
095: * <p>The default is Spring's standard {@link DynamicDestinationResolver}.
096: * @see #setFallbackToDynamicDestination
097: * @see DynamicDestinationResolver
098: */
099: public void setDynamicDestinationResolver(
100: DestinationResolver dynamicDestinationResolver) {
101: this .dynamicDestinationResolver = dynamicDestinationResolver;
102: }
103:
104: public Destination resolveDestinationName(Session session,
105: String destinationName, boolean pubSubDomain)
106: throws JMSException {
107:
108: Assert.notNull(destinationName,
109: "Destination name must not be null");
110: Destination dest = (Destination) this .destinationCache
111: .get(destinationName);
112: if (dest != null) {
113: validateDestination(dest, destinationName, pubSubDomain);
114: } else {
115: try {
116: dest = (Destination) lookup(destinationName,
117: Destination.class);
118: validateDestination(dest, destinationName, pubSubDomain);
119: } catch (NamingException ex) {
120: if (logger.isDebugEnabled()) {
121: logger.debug("Destination [" + destinationName
122: + "] not found in JNDI", ex);
123: }
124: if (this .fallbackToDynamicDestination) {
125: dest = this .dynamicDestinationResolver
126: .resolveDestinationName(session,
127: destinationName, pubSubDomain);
128: } else {
129: throw new DestinationResolutionException(
130: "Destination [" + destinationName
131: + "] not found in JNDI", ex);
132: }
133: }
134: if (this .cache) {
135: this .destinationCache.put(destinationName, dest);
136: }
137: }
138: return dest;
139: }
140:
141: /**
142: * Validate the given Destination object, checking whether it matches
143: * the expected type.
144: * @param destination the Destination object to validate
145: * @param destinationName the name of the destination
146: * @param pubSubDomain <code>true</code> if a Topic is expected,
147: * <code>false</code> in case of a Queue
148: */
149: protected void validateDestination(Destination destination,
150: String destinationName, boolean pubSubDomain) {
151: Class targetClass = Queue.class;
152: if (pubSubDomain) {
153: targetClass = Topic.class;
154: }
155: if (!targetClass.isInstance(destination)) {
156: throw new DestinationResolutionException("Destination ["
157: + destinationName + "] is not of expected type ["
158: + targetClass.getName() + "]");
159: }
160: }
161:
162: public void removeFromCache(String destinationName) {
163: this .destinationCache.remove(destinationName);
164: }
165:
166: public void clearCache() {
167: this.destinationCache.clear();
168: }
169:
170: }
|