1 16 17 package org.springframework.jms.support.destination; 18 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.jms.Destination ; 24 import javax.jms.JMSException ; 25 import javax.jms.Queue ; 26 import javax.jms.Session ; 27 import javax.jms.Topic ; 28 import javax.naming.NamingException ; 29 30 import org.springframework.jndi.JndiLocatorSupport; 31 import org.springframework.util.Assert; 32 33 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 destinationCache = Collections.synchronizedMap(new HashMap ()); 66 67 68 77 public void setCache(boolean cache) { 78 this.cache = cache; 79 } 80 81 87 public void setFallbackToDynamicDestination(boolean fallbackToDynamicDestination) { 88 this.fallbackToDynamicDestination = fallbackToDynamicDestination; 89 } 90 91 98 public void setDynamicDestinationResolver(DestinationResolver dynamicDestinationResolver) { 99 this.dynamicDestinationResolver = dynamicDestinationResolver; 100 } 101 102 103 public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) 104 throws JMSException { 105 106 Assert.notNull(destinationName, "Destination name must not be null"); 107 Destination dest = (Destination ) this.destinationCache.get(destinationName); 108 if (dest != null) { 109 validateDestination(dest, destinationName, pubSubDomain); 110 } 111 else { 112 try { 113 dest = (Destination ) lookup(destinationName, Destination .class); 114 validateDestination(dest, destinationName, pubSubDomain); 115 } 116 catch (NamingException 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 143 protected void validateDestination(Destination destination, String destinationName, boolean pubSubDomain) { 144 Class targetClass = Queue .class; 145 if (pubSubDomain) { 146 targetClass = Topic .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 destinationName) { 156 this.destinationCache.remove(destinationName); 157 } 158 159 public void clearCache() { 160 this.destinationCache.clear(); 161 } 162 163 } 164 | Popular Tags |