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.Collections ; 7 8 import javax.naming.Context ; 9 import javax.naming.NamingException ; 10 11 import javax.jms.JMSException ; 12 import javax.jms.Message ; 13 14 import net.walend.somnifugi.channel.ChannelFactory; 15 16 21 22 class SomniQueueCache 23 { 24 public static final SomniQueueCache IT = new SomniQueueCache(); 25 26 private Map <String ,SomniQueue> keysToQueues = new HashMap <String ,SomniQueue>(); 27 28 private final Object guard = new Object (); 29 30 private SomniQueueCache() 31 {} 32 33 SomniQueue getQueue(String key,Context context) 34 throws SomniNamingException 35 { 36 synchronized(guard) 37 { 38 if(containsQueue(key)) 39 { 40 return (SomniQueue)keysToQueues.get(key); 41 } 42 else 43 { 44 ChannelFactory<Message > factory = ChannelFactoryCache.IT.getChannelFactory(key,context,true); 45 SomniQueue queue = new SomniQueue(key,factory,context); 46 keysToQueues.put(key,queue); 47 SomniLogger.IT.config("Added "+queue.getName()+" Queue."); 48 return queue; 49 } 50 } 51 } 52 53 void putTemporaryQueue(SomniTemporaryQueue queue) 54 throws JMSException 55 { 56 if(!containsQueue(queue.getQueueName())) 57 { 58 keysToQueues.put(queue.getQueueName(),queue); 59 SomniLogger.IT.config("Added "+queue.getQueueName()+" Queue."); 60 } 61 else 62 { 63 throw new SomniTempDestinationNameException(queue.getQueueName()); 64 } 65 } 66 67 SomniQueue removeQueue(String key) 68 { 69 synchronized(guard) 70 { 71 SomniLogger.IT.config("Removed "+key+" Queue."); 72 73 return (SomniQueue)keysToQueues.remove(key); 75 } 76 } 77 78 boolean containsQueue(String key) 79 { 80 synchronized(guard) 81 { 82 return keysToQueues.containsKey(key); 83 } 84 } 85 86 Set <String > getNames() 87 { 88 synchronized(guard) 89 { 90 return Collections.unmodifiableSet(keysToQueues.keySet()); 91 } 92 } 93 } 94 95 115 | Popular Tags |