001: /*
002: * $Id: JmsListenerFactory.java,v 1.2 2003/12/06 23:32:11 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.service.jms;
025:
026: import java.lang.reflect.Constructor;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Set;
032:
033: import org.ofbiz.service.GenericServiceException;
034: import org.ofbiz.service.ServiceDispatcher;
035: import org.ofbiz.service.config.ServiceConfigUtil;
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.UtilXml;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.NodeList;
040:
041: /**
042: * JmsListenerFactory
043: *
044: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
045: * @version $Revision: 1.2 $
046: * @since 2.0
047: */
048: public class JmsListenerFactory implements Runnable {
049:
050: public static final String module = JmsListenerFactory.class
051: .getName();
052:
053: public static final String TOPIC_LISTENER_CLASS = "org.ofbiz.service.jms.JmsTopicListener";
054: public static final String QUEUE_LISTENER_CLASS = "org.ofbiz.service.jms.JmsQueueListener";
055:
056: protected static Map listeners = new HashMap();
057: protected static Map servers = new HashMap();
058:
059: protected ServiceDispatcher dispatcher;
060: protected boolean firstPass = true;
061: protected int loadable = 0;
062: protected int connected = 0;
063: protected Thread thread;
064:
065: public JmsListenerFactory(ServiceDispatcher dispatcher) {
066: this .dispatcher = dispatcher;
067: thread = new Thread(this , this .toString());
068: thread.setDaemon(false);
069: thread.start();
070: }
071:
072: public void run() {
073: Debug.logInfo("Starting JMS Listener Factory Thread", module);
074: while (firstPass || connected < loadable) {
075: if (Debug.verboseOn())
076: Debug.logVerbose("First Pass: " + firstPass
077: + " Connected: " + connected + " Available: "
078: + loadable, module);
079: this .loadListeners();
080: firstPass = false;
081: try {
082: Thread.sleep(20000);
083: } catch (InterruptedException ie) {
084: }
085: continue;
086: }
087: Debug
088: .logInfo(
089: "JMS Listener Factory Thread Finished; All listeners connected.",
090: module);
091: }
092:
093: // Load the JMS listeners
094: private void loadListeners() {
095: try {
096: Element rootElement = ServiceConfigUtil.getXmlRootElement();
097: NodeList nodeList = rootElement
098: .getElementsByTagName("jms-service");
099:
100: if (Debug.verboseOn())
101: Debug.logVerbose(
102: "[ServiceDispatcher] : Loading JMS Listeners.",
103: module);
104: for (int i = 0; i < nodeList.getLength(); i++) {
105: Element element = (Element) nodeList.item(i);
106: List serverList = UtilXml.childElementList(element,
107: "server");
108: Iterator serverIter = serverList.iterator();
109:
110: while (serverIter.hasNext()) {
111: Element server = (Element) serverIter.next();
112:
113: try {
114: String listenerEnabled = server
115: .getAttribute("listen");
116:
117: if (listenerEnabled.equalsIgnoreCase("true")) {
118: // create a server key
119: StringBuffer serverKey = new StringBuffer();
120:
121: serverKey.append(server
122: .getAttribute("jndi-server-name")
123: + ":");
124: serverKey.append(server
125: .getAttribute("jndi-name")
126: + ":");
127: serverKey.append(server
128: .getAttribute("topic-queue"));
129: // store the server element
130: servers.put(serverKey.toString(), server);
131: // load the listener
132: GenericMessageListener listener = loadListener(
133: serverKey.toString(), server);
134:
135: // store the listener w/ the key
136: if (serverKey.length() > 0
137: && listener != null)
138: listeners.put(serverKey.toString(),
139: listener);
140: }
141: } catch (GenericServiceException gse) {
142: Debug
143: .logVerbose(
144: "Cannot load message listener ("
145: + gse.toString() + ").",
146: module);
147: } catch (Exception e) {
148: Debug
149: .logError(e, "Uncaught exception.",
150: module);
151: }
152: }
153: }
154: } catch (org.ofbiz.base.config.GenericConfigException gce) {
155: Debug.logError(gce,
156: "Cannot get serviceengine.xml root element.",
157: module);
158: } catch (Exception e) {
159: Debug.logError(e, "Uncaught exception.", module);
160: }
161: }
162:
163: private GenericMessageListener loadListener(String serverKey,
164: Element server) throws GenericServiceException {
165: String serverName = server.getAttribute("jndi-server-name");
166: String jndiName = server.getAttribute("jndi-name");
167: String queueName = server.getAttribute("topic-queue");
168: String type = server.getAttribute("type");
169: String userName = server.getAttribute("username");
170: String password = server.getAttribute("password");
171: String className = server.getAttribute("listener-class");
172:
173: if (className == null || className.length() == 0) {
174: if (type.equals("topic"))
175: className = JmsListenerFactory.TOPIC_LISTENER_CLASS;
176: else if (type.equals("queue"))
177: className = JmsListenerFactory.QUEUE_LISTENER_CLASS;
178: }
179:
180: GenericMessageListener listener = (GenericMessageListener) listeners
181: .get(serverKey);
182:
183: if (listener == null) {
184: synchronized (this ) {
185: listener = (GenericMessageListener) listeners
186: .get(serverKey);
187: if (listener == null) {
188: ClassLoader cl = this .getClass().getClassLoader();
189: Class[] paramTypes = new Class[] {
190: ServiceDispatcher.class, String.class,
191: String.class, String.class, String.class,
192: String.class };
193: Object[] params = new Object[] { dispatcher,
194: serverName, jndiName, queueName, userName,
195: password };
196:
197: try {
198: Class c = cl.loadClass(className);
199: Constructor cn = c.getConstructor(paramTypes);
200:
201: listener = (GenericMessageListener) cn
202: .newInstance(params);
203: } catch (Exception e) {
204: throw new GenericServiceException(e
205: .getMessage(), e);
206: }
207: if (listener != null)
208: listeners.put(serverKey, listener);
209: loadable++;
210: }
211: }
212:
213: }
214: if (listener != null && !listener.isConnected()) {
215: listener.load();
216: if (listener.isConnected())
217: connected++;
218: }
219: return listener;
220: }
221:
222: /**
223: * Load a JMS message listener.
224: * @param serverKey Name of the jms-service
225: * @throws GenericServiceException
226: */
227: public void loadListener(String serverKey)
228: throws GenericServiceException {
229: Element server = (Element) servers.get(serverKey);
230:
231: if (server == null)
232: throw new GenericServiceException(
233: "No listener found with that serverKey.");
234: loadListener(serverKey, server);
235: }
236:
237: /**
238: * Close all the JMS message listeners.
239: * @throws GenericServiceException
240: */
241: public void closeListeners() throws GenericServiceException {
242: loadable = 0;
243: Set listenerKeys = listeners.keySet();
244: Iterator listenerIterator = listenerKeys.iterator();
245: while (listenerIterator.hasNext()) {
246: String serverKey = (String) listenerIterator.next();
247: closeListener(serverKey);
248: }
249: }
250:
251: /**
252: * Close a JMS message listener.
253: * @param serverKey Name of the jms-service
254: * @throws GenericServiceException
255: */
256: public void closeListener(String serverKey)
257: throws GenericServiceException {
258: GenericMessageListener listener = (GenericMessageListener) listeners
259: .get(serverKey);
260:
261: if (listener == null)
262: throw new GenericServiceException(
263: "No listener found with that serverKey.");
264: listener.close();
265: }
266:
267: /**
268: * Refresh a JMS message listener.
269: * @param serverKey Name of the jms-service
270: * @throws GenericServiceException
271: */
272: public void refreshListener(String serverKey)
273: throws GenericServiceException {
274: GenericMessageListener listener = (GenericMessageListener) listeners
275: .get(serverKey);
276:
277: if (listener == null)
278: throw new GenericServiceException(
279: "No listener found with that serverKey.");
280: listener.refresh();
281: }
282:
283: /**
284: * Gets a Map of JMS Listeners.
285: * @return Map of JMS Listeners
286: */
287: public Map getJMSListeners() {
288: return new HashMap(listeners);
289: }
290:
291: }
|