KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > SomniQueueCache


1 package net.walend.somnifugi;
2
3 import java.util.Set JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Collections JavaDoc;
7
8 import javax.naming.Context JavaDoc;
9 import javax.naming.NamingException JavaDoc;
10
11 import javax.jms.JMSException JavaDoc;
12 import javax.jms.Message JavaDoc;
13
14 import net.walend.somnifugi.channel.ChannelFactory;
15
16 /**
17 SomniQueueCache holds maps of names to queues. SomnifugiJMS uses it internally to create new and store existing queues for SomniJNDIByPass and SomniQueueFactory to hand out. Use those objects to get Queues.
18
19 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
20  */

21
22 class SomniQueueCache
23 {
24     public static final SomniQueueCache IT = new SomniQueueCache();
25
26     private Map JavaDoc<String JavaDoc,SomniQueue> keysToQueues = new HashMap JavaDoc<String JavaDoc,SomniQueue>();
27
28     private final Object JavaDoc guard = new Object JavaDoc();
29
30     private SomniQueueCache()
31     {}
32
33     SomniQueue getQueue(String JavaDoc key,Context JavaDoc 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 JavaDoc> 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 JavaDoc
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 JavaDoc key)
68     {
69         synchronized(guard)
70             {
71                 SomniLogger.IT.config("Removed "+key+" Queue.");
72
73                 //todo log any remaining messages??
74
return (SomniQueue)keysToQueues.remove(key);
75             }
76     }
77
78     boolean containsQueue(String JavaDoc key)
79     {
80         synchronized(guard)
81             {
82                 return keysToQueues.containsKey(key);
83             }
84     }
85     
86     Set JavaDoc<String JavaDoc> getNames()
87     {
88         synchronized(guard)
89         {
90             return Collections.unmodifiableSet(keysToQueues.keySet());
91         }
92     }
93 }
94
95 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
96 All rights reserved.
97
98 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
99
100 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
101
102 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
103
104 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
105
106 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
107
108 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
110
111 =================================================================================
112
113 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
114  */

115
Popular Tags