KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
7 import java.util.Collections JavaDoc;
8
9 import javax.naming.NamingException JavaDoc;
10 import javax.naming.Context JavaDoc;
11
12 import javax.jms.JMSException JavaDoc;
13 import javax.jms.Message JavaDoc;
14
15 import net.walend.somnifugi.channel.ChannelFactory;
16 import net.walend.somnifugi.channel.FanOutFactory;
17
18 /**
19 SomniTopicCache holds maps of names to topics. SomnifugiJMS uses it internally to create new and store existing topics for SomniJNDIByPass and SomniTopicFactory to hand out. Use those objects to get Topics.
20
21 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
22  */

23
24 class SomniTopicCache
25 {
26     public static final SomniTopicCache IT = new SomniTopicCache();
27
28     private Map JavaDoc<String JavaDoc,SomniTopic> keysToTopics = new HashMap JavaDoc<String JavaDoc,SomniTopic>();
29
30     private final Object JavaDoc guard = new Object JavaDoc();
31
32     private SomniTopicCache()
33     {}
34
35     SomniTopic getTopic(String JavaDoc key,Context JavaDoc 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 JavaDoc> factory = ChannelFactoryCache.IT.getChannelFactory(key,context,false);
49                     FanOutFactory<Message JavaDoc> 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 JavaDoc ne)
58         {
59             throw new SomniNamingException(ne);
60         }
61     }
62
63     void putTemporaryTopic(SomniTemporaryTopic topic)
64         throws JMSException JavaDoc
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 JavaDoc 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 JavaDoc key)
88     {
89         synchronized(guard)
90             {
91                 return keysToTopics.containsKey(key);
92             }
93     }
94
95     Set JavaDoc<String JavaDoc> getNames()
96     {
97         synchronized(guard)
98         {
99             return Collections.unmodifiableSet(keysToTopics.keySet());
100         }
101     }
102
103     void endDurableSubscription(String JavaDoc subscriptionName)
104     {
105         synchronized(guard)
106             {
107                 Iterator JavaDoc 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 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
119 All rights reserved.
120
121 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
122
123 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
124
125 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.
126
127 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.
128
129 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
130
131 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.
132 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.
133
134 =================================================================================
135
136 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>.
137  */

138
Popular Tags