KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.walend.somnifugi;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.lang.reflect.InvocationTargetException JavaDoc;
5
6 import java.util.Map JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Hashtable JavaDoc;
9
10 import javax.naming.Context JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12
13 import javax.jms.Message JavaDoc;
14
15 import net.walend.somnifugi.channel.ChannelFactory;
16 import net.walend.somnifugi.channel.FanOutFactory;
17
18 import net.walend.somnifugi.juc.SimpleFanOutFactory;
19
20 /**
21 ChannelFactoryCache holds a map of Context names to ChannelFactories.
22
23 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
24  */

25
26 class ChannelFactoryCache
27 {
28     private static final String JavaDoc DEFAULTFACTORYCLASSNAMEPROP = SomniProperties.DEFAULT+"."+SomniProperties.CHANNELFACTORYCLASSNAMEPROP;
29     
30     public static final ChannelFactoryCache IT = new ChannelFactoryCache();
31
32     private Map JavaDoc<String JavaDoc,ChannelFactory<Message JavaDoc>> contextNamesToChannelFactories = new HashMap JavaDoc<String JavaDoc,ChannelFactory<Message JavaDoc>>();
33     private Map JavaDoc<String JavaDoc,FanOutFactory<Message JavaDoc>> contextNamesToFanOutFactories = new HashMap JavaDoc<String JavaDoc,FanOutFactory<Message JavaDoc>>();
34
35     private final Object JavaDoc guard = new Object JavaDoc();
36
37     private ChannelFactoryCache()
38     {}
39
40     ChannelFactory<Message JavaDoc> getChannelFactory(String JavaDoc name,Context JavaDoc context,boolean isQueue)
41         throws SomniNamingException
42     {
43         try
44         {
45             synchronized(guard)
46             {
47                 if(containsChannelFactory(name))
48                 {
49                     return contextNamesToChannelFactories.get(name);
50                 }
51                 else
52                 {
53                     ChannelFactory<Message JavaDoc> channelFactory = createChannelFactory(name,context.getEnvironment(),isQueue);
54     
55                     contextNamesToChannelFactories.put(name,channelFactory);
56                     SomniLogger.IT.config("Added "+name+" ChannelFactory.");
57                     return channelFactory;
58                 }
59             }
60         }
61         catch(NamingException JavaDoc ne)
62         {
63             throw new SomniNamingException(ne);
64         }
65     }
66 @SuppressWarnings JavaDoc("unchecked")
67     private ChannelFactory<Message JavaDoc> createChannelFactory(String JavaDoc destName,Hashtable JavaDoc env,boolean isQueue)
68         throws SomniNamingException
69     {
70         String JavaDoc channelFactoryClassNameKey = destName+"."+SomniProperties.CHANNELFACTORYCLASSNAMEPROP;
71
72         String JavaDoc className = (String JavaDoc)env.get(channelFactoryClassNameKey);
73         if(className == null)
74         {
75             className = (String JavaDoc)env.get(DEFAULTFACTORYCLASSNAMEPROP);
76         }
77         if(className == null)
78         {
79             if(isQueue)
80             {
81                 className = SomniProperties.DEFAULTQUEUECHANNELFACTORYCLASSNAME;
82             }
83             else
84             {
85                 className = SomniProperties.DEFAULTTOPICCHANNELFACTORYCLASSNAME;
86             }
87         }
88
89         try
90         {
91             Class JavaDoc<ChannelFactory<Message JavaDoc>> channelFactoryClass = (Class JavaDoc<ChannelFactory<Message JavaDoc>>)Class.forName(className);
92             ChannelFactory<Message JavaDoc> factory = (ChannelFactory<Message JavaDoc>)channelFactoryClass.newInstance();
93             
94             return factory;
95         }
96         catch(ClassNotFoundException JavaDoc cnfe)
97         {
98             throw new SomniNamingException("Looking for "+className,cnfe);
99         }
100         catch(InstantiationException JavaDoc ie)
101         {
102             throw new SomniNamingException("Instantiating "+className,ie);
103         }
104         catch(IllegalAccessException JavaDoc iae)
105         {
106             throw new SomniNamingException("Instantiating "+className,iae);
107         }
108     }
109     
110     private boolean containsChannelFactory(String JavaDoc key)
111     {
112         synchronized(guard)
113         {
114             return contextNamesToChannelFactories.containsKey(key);
115         }
116     }
117     @SuppressWarnings JavaDoc("unchecked")
118     FanOutFactory<Message JavaDoc> getFanOutFactory(String JavaDoc name,Context JavaDoc context)
119         throws SomniNamingException,NamingException JavaDoc
120     {
121         synchronized(guard)
122         {
123             if(containsFanOutFactory(name))
124             {
125                 return contextNamesToFanOutFactories.get(name);
126             }
127             else
128             {
129                 Hashtable JavaDoc<?,?> props = context.getEnvironment();
130
131                 String JavaDoc fanOutFactoryClassName = SimpleFanOutFactory.class.getName();
132                 
133                 String JavaDoc fanOutFactoryClassNameKey = SomniProperties.DEFAULT+"."+SomniProperties.FANOUTFACTORYCLASSNAMEPROP;
134                 if(props.containsKey(fanOutFactoryClassNameKey))
135                 {
136                     fanOutFactoryClassName = (String JavaDoc)props.get(fanOutFactoryClassNameKey);
137                 }
138                 
139                 fanOutFactoryClassNameKey = name+"."+SomniProperties.FANOUTFACTORYCLASSNAMEPROP;
140                 if(props.containsKey(fanOutFactoryClassNameKey))
141                 {
142                     fanOutFactoryClassName = (String JavaDoc)props.get(fanOutFactoryClassNameKey);
143                 }
144                 try
145                 {
146                     Class JavaDoc fanOutFactoryClass = Class.forName(fanOutFactoryClassName);
147                     
148                     Constructor JavaDoc fanOutConstructor = fanOutFactoryClass.getConstructor();
149         
150                     FanOutFactory<Message JavaDoc> fanOutFactory = (FanOutFactory<Message JavaDoc>)fanOutConstructor.newInstance();
151         
152                     contextNamesToFanOutFactories.put(name,fanOutFactory);
153                     SomniLogger.IT.config("Added "+name+" FanOutFactory.");
154                     return fanOutFactory;
155                 }
156                 catch(ClassNotFoundException JavaDoc cnfe)
157                 {
158                     throw new SomniNamingException(cnfe);
159                 }
160                 catch(IllegalAccessException JavaDoc cnfe)
161                 {
162                     throw new SomniNamingException(cnfe);
163                 }
164                 catch(NoSuchMethodException JavaDoc nsme)
165                 {
166                     throw new SomniNamingException(nsme);
167                 }
168                 catch(InvocationTargetException JavaDoc ite)
169                 {
170                     throw new SomniNamingException(ite);
171                 }
172                 catch(InstantiationException JavaDoc ie)
173                 {
174                     throw new SomniNamingException(ie);
175                 }
176             }
177         }
178     }
179     
180     private boolean containsFanOutFactory(String JavaDoc name)
181     {
182         synchronized(guard)
183         {
184             return contextNamesToFanOutFactories.containsKey(name);
185         }
186     }
187 }
188
189 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
190 All rights reserved.
191
192 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
193
194 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
195
196 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.
197
198 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.
199
200 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
201
202 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.
203 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.
204
205 =================================================================================
206
207 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>.
208  */

209
Popular Tags