KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > jms > JmsListenerFactory


1 /*
2  * $Id: JmsListenerFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.service.jms;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.ofbiz.service.GenericServiceException;
34 import org.ofbiz.service.ServiceDispatcher;
35 import org.ofbiz.service.config.ServiceConfigUtil;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilXml;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  * JmsListenerFactory
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class JmsListenerFactory implements Runnable JavaDoc {
49
50     public static final String JavaDoc module = JmsListenerFactory.class.getName();
51
52     public static final String JavaDoc TOPIC_LISTENER_CLASS = "org.ofbiz.service.jms.JmsTopicListener";
53     public static final String JavaDoc QUEUE_LISTENER_CLASS = "org.ofbiz.service.jms.JmsQueueListener";
54
55     protected static Map JavaDoc listeners = new HashMap JavaDoc();
56     protected static Map JavaDoc servers = new HashMap JavaDoc();
57
58     protected ServiceDispatcher dispatcher;
59     protected boolean firstPass = true;
60     protected int loadable = 0;
61     protected int connected = 0;
62     protected Thread JavaDoc thread;
63
64     public JmsListenerFactory(ServiceDispatcher dispatcher) {
65         this.dispatcher = dispatcher;
66         thread = new Thread JavaDoc(this, this.toString());
67         thread.setDaemon(false);
68         thread.start();
69     }
70
71     public void run() {
72         Debug.logInfo("Starting JMS Listener Factory Thread", module);
73         while (firstPass || connected < loadable) {
74             if (Debug.verboseOn()) Debug.logVerbose("First Pass: " + firstPass + " Connected: " + connected + " Available: " + loadable, module);
75             this.loadListeners();
76             firstPass = false;
77             try {
78                 Thread.sleep(20000);
79             } catch (InterruptedException JavaDoc ie) {}
80             continue;
81         }
82         Debug.logInfo("JMS Listener Factory Thread Finished; All listeners connected.", module);
83     }
84
85     // Load the JMS listeners
86
private void loadListeners() {
87         try {
88             Element JavaDoc rootElement = ServiceConfigUtil.getXmlRootElement();
89             NodeList JavaDoc nodeList = rootElement.getElementsByTagName("jms-service");
90
91             if (Debug.verboseOn()) Debug.logVerbose("[ServiceDispatcher] : Loading JMS Listeners.", module);
92             for (int i = 0; i < nodeList.getLength(); i++) {
93                 Element JavaDoc element = (Element JavaDoc) nodeList.item(i);
94                 List JavaDoc serverList = UtilXml.childElementList(element, "server");
95                 Iterator JavaDoc serverIter = serverList.iterator();
96
97                 while (serverIter.hasNext()) {
98                     Element JavaDoc server = (Element JavaDoc) serverIter.next();
99
100                     try {
101                         String JavaDoc listenerEnabled = server.getAttribute("listen");
102
103                         if (listenerEnabled.equalsIgnoreCase("true")) {
104                             // create a server key
105
StringBuffer JavaDoc serverKey = new StringBuffer JavaDoc();
106
107                             serverKey.append(server.getAttribute("jndi-server-name") + ":");
108                             serverKey.append(server.getAttribute("jndi-name") + ":");
109                             serverKey.append(server.getAttribute("topic-queue"));
110                             // store the server element
111
servers.put(serverKey.toString(), server);
112                             // load the listener
113
GenericMessageListener listener = loadListener(serverKey.toString(), server);
114
115                             // store the listener w/ the key
116
if (serverKey.length() > 0 && listener != null)
117                                 listeners.put(serverKey.toString(), listener);
118                         }
119                     } catch (GenericServiceException gse) {
120                         Debug.logVerbose("Cannot load message listener (" + gse.toString() + ").", module);
121                     } catch (Exception JavaDoc e) {
122                         Debug.logError(e, "Uncaught exception.", module);
123                     }
124                 }
125             }
126         } catch (org.ofbiz.base.config.GenericConfigException gce) {
127             Debug.logError(gce, "Cannot get serviceengine.xml root element.", module);
128         } catch (Exception JavaDoc e) {
129             Debug.logError(e, "Uncaught exception.", module);
130         }
131     }
132
133     private GenericMessageListener loadListener(String JavaDoc serverKey, Element JavaDoc server) throws GenericServiceException {
134         String JavaDoc serverName = server.getAttribute("jndi-server-name");
135         String JavaDoc jndiName = server.getAttribute("jndi-name");
136         String JavaDoc queueName = server.getAttribute("topic-queue");
137         String JavaDoc type = server.getAttribute("type");
138         String JavaDoc userName = server.getAttribute("username");
139         String JavaDoc password = server.getAttribute("password");
140         String JavaDoc className = server.getAttribute("listener-class");
141
142         if (className == null || className.length() == 0) {
143             if (type.equals("topic"))
144                 className = JmsListenerFactory.TOPIC_LISTENER_CLASS;
145             else if (type.equals("queue"))
146                 className = JmsListenerFactory.QUEUE_LISTENER_CLASS;
147         }
148
149         GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey);
150
151         if (listener == null) {
152             synchronized (this) {
153                 listener = (GenericMessageListener) listeners.get(serverKey);
154                 if (listener == null) {
155                     ClassLoader JavaDoc cl = this.getClass().getClassLoader();
156                     Class JavaDoc[] paramTypes = new Class JavaDoc[] {ServiceDispatcher.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class, String JavaDoc.class};
157                     Object JavaDoc[] params = new Object JavaDoc[] {dispatcher, serverName, jndiName, queueName, userName, password};
158
159                     try {
160                         Class JavaDoc c = cl.loadClass(className);
161                         Constructor JavaDoc cn = c.getConstructor(paramTypes);
162
163                         listener = (GenericMessageListener) cn.newInstance(params);
164                     } catch (Exception JavaDoc e) {
165                         throw new GenericServiceException(e.getMessage(), e);
166                     }
167                     if (listener != null)
168                         listeners.put(serverKey, listener);
169                     loadable++;
170                 }
171             }
172
173         }
174         if (listener != null && !listener.isConnected()) {
175             listener.load();
176             if (listener.isConnected())
177                 connected++;
178         }
179         return listener;
180     }
181
182     /**
183      * Load a JMS message listener.
184      * @param serverKey Name of the jms-service
185      * @throws GenericServiceException
186      */

187     public void loadListener(String JavaDoc serverKey) throws GenericServiceException {
188         Element JavaDoc server = (Element JavaDoc) servers.get(serverKey);
189
190         if (server == null)
191             throw new GenericServiceException("No listener found with that serverKey.");
192         loadListener(serverKey, server);
193     }
194         
195     /**
196      * Close all the JMS message listeners.
197      * @throws GenericServiceException
198      */

199     public void closeListeners() throws GenericServiceException {
200         loadable = 0;
201         Set JavaDoc listenerKeys = listeners.keySet();
202         Iterator JavaDoc listenerIterator = listenerKeys.iterator();
203         while (listenerIterator.hasNext()) {
204             String JavaDoc serverKey = (String JavaDoc) listenerIterator.next();
205             closeListener(serverKey);
206         }
207     }
208
209     /**
210      * Close a JMS message listener.
211      * @param serverKey Name of the jms-service
212      * @throws GenericServiceException
213      */

214     public void closeListener(String JavaDoc serverKey) throws GenericServiceException {
215         GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey);
216
217         if (listener == null)
218             throw new GenericServiceException("No listener found with that serverKey.");
219         listener.close();
220     }
221
222     /**
223      * Refresh a JMS message listener.
224      * @param serverKey Name of the jms-service
225      * @throws GenericServiceException
226      */

227     public void refreshListener(String JavaDoc serverKey) throws GenericServiceException {
228         GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey);
229
230         if (listener == null)
231             throw new GenericServiceException("No listener found with that serverKey.");
232         listener.refresh();
233     }
234
235     /**
236      * Gets a Map of JMS Listeners.
237      * @return Map of JMS Listeners
238      */

239     public Map JavaDoc getJMSListeners() {
240         return new HashMap JavaDoc(listeners);
241     }
242
243 }
244
Popular Tags