1 package net.walend.somnifugi; 2 3 import java.util.Set ; 4 import java.util.Map ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Collections ; 8 9 import javax.naming.NamingException ; 10 import javax.naming.Context ; 11 12 import javax.jms.JMSException ; 13 import javax.jms.Message ; 14 15 import net.walend.somnifugi.channel.ChannelFactory; 16 import net.walend.somnifugi.channel.FanOutFactory; 17 18 23 24 class SomniTopicCache 25 { 26 public static final SomniTopicCache IT = new SomniTopicCache(); 27 28 private Map <String ,SomniTopic> keysToTopics = new HashMap <String ,SomniTopic>(); 29 30 private final Object guard = new Object (); 31 32 private SomniTopicCache() 33 {} 34 35 SomniTopic getTopic(String key,Context context) 36 throws SomniNamingException 37 { 38 try 39 { 40 synchronized(guard) 41 { 42 if(containsTopic(key)) 43 { 44 return (SomniTopic)keysToTopics.get(key); 45 } 46 else 47 { 48 ChannelFactory<Message > factory = ChannelFactoryCache.IT.getChannelFactory(key,context,false); 49 FanOutFactory<Message > fanOutFactory = ChannelFactoryCache.IT.getFanOutFactory(key,context); 50 SomniTopic topic = new SomniTopic(key,factory,fanOutFactory,context); 51 keysToTopics.put(key,topic); 52 SomniLogger.IT.config("Added "+topic.getName()+" Topic."); 53 return topic; 54 } 55 } 56 } 57 catch(NamingException ne) 58 { 59 throw new SomniNamingException(ne); 60 } 61 } 62 63 void putTemporaryTopic(SomniTemporaryTopic topic) 64 throws JMSException 65 { 66 if(!containsTopic(topic.getTopicName())) 67 { 68 keysToTopics.put(topic.getTopicName(),topic); 69 SomniLogger.IT.config("Added "+topic.getTopicName()+" Topic."); 70 } 71 else 72 { 73 throw new SomniTempDestinationNameException(topic.getTopicName()); 74 } 75 76 } 77 78 SomniTopic removeTopic(String key) 79 { 80 synchronized(guard) 81 { 82 SomniLogger.IT.config("Removed "+key+" Topic."); 83 return (SomniTopic)keysToTopics.remove(key); 84 } 85 } 86 87 boolean containsTopic(String key) 88 { 89 synchronized(guard) 90 { 91 return keysToTopics.containsKey(key); 92 } 93 } 94 95 Set <String > getNames() 96 { 97 synchronized(guard) 98 { 99 return Collections.unmodifiableSet(keysToTopics.keySet()); 100 } 101 } 102 103 void endDurableSubscription(String subscriptionName) 104 { 105 synchronized(guard) 106 { 107 Iterator it = keysToTopics.values().iterator(); 108 while(it.hasNext()) 109 { 110 SomniTopic topic = (SomniTopic)it.next(); 111 topic.removeDurableSubscriber(subscriptionName); 112 } 113 } 114 } 115 116 } 117 118 138 | Popular Tags |