[jboss-svn-commits] JBL Code SVN: r7296 - labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Nov 1 18:00:33 EST 2006


Author: jokum
Date: 2006-11-01 18:00:30 -0500 (Wed, 01 Nov 2006)
New Revision: 7296

Added:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/HttpListener.java
Log:
Implemented a HttpListener using JBoss Remoting

Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/HttpListener.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/HttpListener.java	2006-11-01 22:59:58 UTC (rev 7295)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/message/HttpListener.java	2006-11-01 23:00:30 UTC (rev 7296)
@@ -0,0 +1,252 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.listeners.message;
+
+import java.net.MalformedURLException;
+
+import javax.management.MBeanServer;
+
+import org.apache.log4j.Logger;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionUtils;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerInitialisationException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.message.format.MessageType;
+
+/**
+ * Http listener implementation using the JBoss Remoting channel.
+ * 
+ * The listener will listen to messages on the configured listenHttpUrl. If this
+ * is not set, the default http://localhost:5400 will be used.
+ * 
+ * Sample listener Configuration:
+ * 
+ * <HttpListener listenerClass="org.jboss.soa.esb.listeners.message.HttpListener" actions="HttpInvocationToFile"/>
+ * 
+ * <pre>
+ *  &lt;HttpListener listenerClass=&quot;org.jboss.soa.esb.listeners.message.HttpListener&quot;&gt; 
+ *  	&lt;action class=&quotorg.jboss.soa.esb.actions.routing.EchoRouter&quot;/&gt;     
+ *  &lt;/HttpListener&gt;
+ * </pre>
+ * 
+ * @author <a href="mailto:johan.kumps at telenet.be">Johan Kumps</a>
+ * 
+ */
+public class HttpListener extends AbstractPassiveListener implements
+		ServerInvocationHandler {
+
+	/* The logger for this class */
+	protected Logger logger = Logger.getLogger(HttpListener.class);
+
+	/* Boolean indicating whether the info logging level is enabled */
+	protected boolean info = this.logger.isInfoEnabled();
+
+	/* The url this listener will listen on */
+	private static final String LISTEN_HTTP_URL = "listenHttpUrl";
+
+	/* The url to listen on */
+	public String listenHttpUrl = null;
+
+	/* The default transport this listener will listen on */
+	private static final String transport = "http";
+
+	/* The default hostname this listener will listen on */
+	private static final String host = "localhost";
+
+	/* The default port this listener will listen on */
+	private static final int port = 5400;
+
+	/**
+	 * Constructor initialising this HttpListener
+	 * 
+	 * @param commandListener
+	 * @param listenerConfig
+	 * @param actionDefinitionFactory
+	 * @throws Exception
+	 */
+	public HttpListener(EsbListenerController esbListenerController,
+			ConfigTree configuration) throws ConfigurationException,
+			ListenerInitialisationException {
+		super(esbListenerController, configuration);
+
+		this.checkParams();
+		this.initServer();
+
+	}
+
+	/*
+	 * Method being invoked when a request comes in.
+	 * 
+	 * (non-Javadoc)
+	 * 
+	 * @see org.jboss.remoting.ServerInvocationHandler#invoke(org.jboss.remoting.InvocationRequest)
+	 */
+	@SuppressWarnings("unchecked")
+	public Object invoke(InvocationRequest invocationRequest) throws Throwable {
+		// Retrieving the real payload of this invocationRequest
+		Object payload = invocationRequest.getParameter();
+
+		if (this.logger.isInfoEnabled()) {
+			this.logger
+					.info("HttpInvocationListener is invoked...The given payload is : "
+							+ payload);
+		}
+		// Message creation...
+		Message msg = MessageFactory.getInstance().getMessage(
+				MessageType.JAVA_SERIALIZED);
+		ActionUtils.setTaskObject(msg,payload);
+		
+		// Start the action processing pipeline
+		ActionProcessingPipeline pipelineRunner = new ActionProcessingPipeline(
+				msg, this._config);
+		this.pipelineExecutorPool.submit(pipelineRunner);
+		
+		return payload;
+	}
+
+	/**
+	 * Adds a callback handler that will listen for callbacks from the server
+	 * invoker handler.
+	 * 
+	 * @param callbackHandler
+	 */
+	public void addListener(InvokerCallbackHandler callbackHandler) {
+		// NO OP as do not handling callback listeners in this example
+	}
+
+	/**
+	 * Removes the callback handler that was listening for callbacks from the
+	 * server invoker handler.
+	 * 
+	 * @param callbackHandler
+	 */
+	public void removeListener(InvokerCallbackHandler callbackHandler) {
+		// NO OP as do not handling callback listeners in this example
+	}
+
+	/**
+	 * set the mbean server that the handler can reference
+	 * 
+	 * @param server
+	 */
+	public void setMBeanServer(MBeanServer server) {
+		// NO OP as do not need reference to MBeanServer for this handler
+	}
+
+	/**
+	 * set the invoker that owns this handler
+	 * 
+	 * @param invoker
+	 */
+	public void setInvoker(ServerInvoker invoker) {
+		// NO OP as do not need reference back to the server invoker
+	}
+
+	/**
+	 * Method getting the url this HttpListener instance is listening on
+	 * 
+	 * @return the current listenHttpUrl
+	 */
+	public String getListenHttpUrl() {
+		return listenHttpUrl;
+	}
+
+	/**
+	 * Method setting the listenHttpUrl property to listen on
+	 * 
+	 * @param listenHttpUrl
+	 *            the listenHttpUrl to be used by this HttpListener instance
+	 */
+	public void setListenHttpUrl(String listenHttpUrl) {
+		this.listenHttpUrl = listenHttpUrl;
+	}
+
+	/**
+	 * Check for mandatory and optional attributes in parameter tree
+	 */
+	private void checkParams() throws ConfigurationException {
+		// listener url
+		this.listenHttpUrl = this._config.getAttribute(LISTEN_HTTP_URL);
+		if (this.listenHttpUrl == null) {
+			this.listenHttpUrl = this.getDefaultListenHttpUrl();
+			if (this.logger.isInfoEnabled()){
+				this.logger.info("Attribute listenHttpUrl has not been set on the HttpListener. Using default " + this.getDefaultListenHttpUrl());
+			}
+		}
+	}
+
+	/**
+	 * Method returning the default listenHttpUrl for this HttpListener instance
+	 * 
+	 * @return the default listen url
+	 */
+	private String getDefaultListenHttpUrl() {
+		return HttpListener.transport + "://" + HttpListener.host + ":"
+				+ HttpListener.port;
+	}
+
+	/**
+	 * Method initialising the remoting deamon
+	 * 
+	 * @throws ListenerInitialisationException
+	 * 
+	 * @throws Exception
+	 *             when something goes wrong during remoting deamon startup
+	 */
+	private void initServer() throws ListenerInitialisationException {
+
+		try {
+			InvokerLocator locator = new InvokerLocator(this.listenHttpUrl);
+			if (this.logger.isInfoEnabled()) {
+				this.logger
+						.info("Starting remoting server with locator uri of: "
+								+ this.listenHttpUrl);
+			}
+			Connector connector = new Connector(locator);
+			connector.create();
+
+			connector.addInvocationHandler("HttpInvocationHandler", this);
+
+			// Starting the server deamon
+			connector.start();
+
+			if (this.logger.isInfoEnabled()) {
+				this.logger.info("HttpListener deamon started successfully!");
+			}
+		} catch (MalformedURLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (Exception exc) {
+			throw new ListenerInitialisationException(exc);
+		}
+
+	}
+
+}




More information about the jboss-svn-commits mailing list