[jboss-svn-commits] JBL Code SVN: r15646 - in labs/jbossesb/workspace/bramley/product/rosetta: src/org/jboss/soa/esb/addressing/eprs and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Oct 8 10:03:36 EDT 2007
Author: mark.little at jboss.com
Date: 2007-10-08 10:03:35 -0400 (Mon, 08 Oct 2007)
New Revision: 15646
Added:
labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/InVMCourier.java
labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/addressing/eprs/InVMEpr.java
labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/InVMCourierUnitTest.java
labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/ListenerManagerInVMUnitTest.java
labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/GatewayInVMServiceUnitTest.java
labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/listenerInVM.xml
labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/listenerInVM.xml
Modified:
labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/TwoWayCourierImpl.java
labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/listeners/ListenerUtil.java
Log:
Added InVM transport.
Added: labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/InVMCourier.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/InVMCourier.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/InVMCourier.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,178 @@
+/*
+ * 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.internal.soa.esb.couriers;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Vector;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.MalformedEPRException;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.couriers.CourierException;
+import org.jboss.soa.esb.couriers.CourierTimeoutException;
+import org.jboss.soa.esb.message.Message;
+
+public class InVMCourier implements PickUpOnlyCourier, DeliverOnlyCourier
+{
+ /**
+ * disable public default constructor
+ */
+ protected InVMCourier()
+ {
+ }
+
+ /**
+ * package protected constructor - Objects of this class should only be
+ * instantiated by internal implementations
+ *
+ * @param epr
+ */
+ public InVMCourier(InVMEpr epr) throws CourierException,
+ MalformedEPRException
+ {
+ this(epr, false);
+ }
+
+ /**
+ * package protected constructor - Objects of this class should only be
+ * instantiated by internal implementations
+ *
+ * @param epr
+ * @param receiverOnly
+ */
+ public InVMCourier(InVMEpr epr, boolean receiverOnly)
+ throws CourierException, MalformedEPRException
+ {
+ _receiverOnly = receiverOnly;
+ _epr = epr;
+ }
+
+ /**
+ * package the ESB message into the queue
+ *
+ * @param message
+ * Message - the message to deliverAsync
+ * @return boolean - the result of the delivery
+ * @throws CourierException -
+ * if problems were encountered
+ */
+
+ public boolean deliver(Message message) throws CourierException,
+ MalformedEPRException
+ {
+ if (_receiverOnly)
+ throw new CourierException("This is a pickUp-only Courier");
+
+ if (message == null)
+ return false;
+
+ Vector<Message> serviceQueue;
+
+ synchronized (_queue)
+ {
+ serviceQueue = _queue.get(_epr.getServiceId());
+
+ if (serviceQueue == null)
+ {
+ serviceQueue = new Vector<Message>();
+ _queue.put(_epr.getServiceId(), serviceQueue);
+
+ _queue.notify();
+ }
+ }
+
+ synchronized (serviceQueue)
+ {
+ serviceQueue.add(message);
+ }
+
+ return true;
+ }
+
+ public Message pickup(long millis) throws CourierException,
+ CourierTimeoutException
+ {
+ long limit = ((millis < 100) ? 100 : millis);
+ long startTime = System.currentTimeMillis();
+ Vector serviceQueue;
+
+ do
+ {
+ synchronized (_queue)
+ {
+ serviceQueue = _queue.get(_epr.getServiceId());
+
+ if (serviceQueue == null) // no queue, so no messages, so
+ // let's wait.
+ {
+ try
+ {
+ _queue.wait(limit);
+ }
+ catch (InterruptedException ex)
+ {
+ }
+ }
+ }
+
+ if (serviceQueue != null)
+ {
+ synchronized (serviceQueue)
+ {
+ /*
+ * If queue size is 0 then let's keep waiting.
+ */
+
+ if (serviceQueue.size() > 0)
+ {
+ Message result = (Message) serviceQueue.remove(0);
+
+ if (result != null)
+ return result;
+ }
+ }
+ }
+
+ } while (System.currentTimeMillis() - startTime < millis);
+
+ return null;
+ }
+
+ public void cleanup()
+ {
+ // when can we remove queue entry?
+
+ // in vm is slightly different to other transports because of gc/memory leak issues.
+ }
+
+ protected URL _url;
+
+ protected boolean _receiverOnly;
+
+ protected InVMEpr _epr;
+
+ protected static Logger _logger = Logger.getLogger(InVMCourier.class);
+
+ static private HashMap<String, Vector<Message>> _queue = new HashMap<String, Vector<Message>>();
+}
Modified: labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/TwoWayCourierImpl.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/TwoWayCourierImpl.java 2007-10-08 13:55:23 UTC (rev 15645)
+++ labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/internal/soa/esb/couriers/TwoWayCourierImpl.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -30,6 +30,7 @@
import org.jboss.soa.esb.addressing.EPR;
import org.jboss.soa.esb.addressing.MalformedEPRException;
import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
import org.jboss.soa.esb.addressing.eprs.JDBCEpr;
import org.jboss.soa.esb.addressing.eprs.JMSEpr;
import org.jboss.soa.esb.couriers.CourierException;
@@ -130,6 +131,8 @@
return new FileCourier((FileEpr) epr, pickUpOnly);
if (epr instanceof JDBCEpr)
return new SqlTableCourier((JDBCEpr) epr, pickUpOnly);
+ if (epr instanceof InVMEpr)
+ return new InVMCourier((InVMEpr) epr, pickUpOnly);
// TODO the following is necessary because EPR
// serialization/deserialization loses type
@@ -147,6 +150,8 @@
return new JmsCourier(new JMSEpr(epr), pickUpOnly);
if (addr.startsWith(JDBCEpr.JDBC_PROTOCOL))
return new SqlTableCourier(new JDBCEpr(epr), pickUpOnly);
+ if (addr.startsWith(InVMEpr.INVM_PROTOCOL))
+ return new InVMCourier(new InVMEpr(epr), pickUpOnly);
// TODO magic strings
if (addr.startsWith("file://") || addr.startsWith("ftp://")
|| addr.startsWith("sftp://") || addr.startsWith("ftps://"))
Added: labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/addressing/eprs/InVMEpr.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/addressing/eprs/InVMEpr.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/addressing/eprs/InVMEpr.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,122 @@
+package org.jboss.soa.esb.addressing.eprs;
+
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006,
+ * @author mark.little at jboss.com
+ */
+
+
+/**
+ * This class represents the endpoint reference for services.
+ */
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.XMLUtil;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/**
+ * A helper class for using in-VM communication.
+ *
+ * EPR: local://servicename
+ *
+ * @author marklittle
+ *
+ */
+
+public class InVMEpr extends EPR
+{
+ public static final String INVM_PROTOCOL = "local";
+ public static final String SERVICE_TAG = "serviceId";
+
+ public InVMEpr (EPR epr)
+ {
+ copy(epr);
+ }
+
+ public InVMEpr (EPR epr, Element header)
+ {
+ copy(epr);
+
+ NodeList nl = header.getChildNodes();
+
+ for (int i = 0; i < nl.getLength(); i++)
+ {
+ try
+ {
+ String prefix = nl.item(i).getPrefix();
+ String tag = nl.item(i).getLocalName();
+
+ if ((prefix != null) && (prefix.equals(XMLUtil.JBOSSESB_PREFIX)))
+ {
+ if (tag != null)
+ {
+ if (tag.equals(SERVICE_TAG))
+ getAddr().addExtension(SERVICE_TAG, nl.item(i).getTextContent());
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ public InVMEpr (URI uri) throws URISyntaxException
+ {
+ super(uri);
+
+ String serviceId = uri.getHost();
+
+ if (serviceId == null)
+ throw new URISyntaxException(uri.toString(), "No serviceId specified!");
+ else
+ getAddr().addExtension(SERVICE_TAG, serviceId);
+ }
+
+ public String getServiceId ()
+ {
+ return getAddr().getExtensionValue(SERVICE_TAG);
+ }
+
+ public static URI type ()
+ {
+ return _type;
+ }
+
+ private static URI _type;
+
+ static
+ {
+ try
+ {
+ _type = new URI("urn:jboss/esb/epr/type/invm");
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+
+ throw new ExceptionInInitializerError(ex.toString());
+ }
+ }
+}
\ No newline at end of file
Modified: labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/listeners/ListenerUtil.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/listeners/ListenerUtil.java 2007-10-08 13:55:23 UTC (rev 15645)
+++ labs/jbossesb/workspace/bramley/product/rosetta/src/org/jboss/soa/esb/listeners/ListenerUtil.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -24,6 +24,7 @@
import java.io.File;
import java.net.MalformedURLException;
+import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
@@ -38,6 +39,7 @@
import org.jboss.soa.esb.addressing.EPR;
import org.jboss.soa.esb.addressing.eprs.FTPEpr;
import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
import org.jboss.soa.esb.addressing.eprs.JDBCEpr;
import org.jboss.soa.esb.addressing.eprs.JMSEpr;
import org.jboss.soa.esb.helpers.ConfigTree;
@@ -57,7 +59,7 @@
/**
* For unittest/quickstart use, to deliver a message. This method closes all
- * JmsCOnnectionPools.
+ * JmsConnectionPools.
*
* @param message
* @param category
@@ -92,6 +94,8 @@
return fileEprFromElement(tree);
if ("jdbc".equals(protocol))
return jdbcEprFromElement(tree);
+ if (protocol.equals("local"))
+ return inVMEprFromElement(tree);
}
catch (Exception e)
{
@@ -148,6 +152,30 @@
}
} // ________________________________
+ public static InVMEpr inVMEprFromElement (ConfigTree tree) throws ConfigurationException
+ {
+ try
+ {
+ String uriString = tree.getRequiredAttribute(ListenerTagNames.URL_TAG);
+ URI uri = new URI(uriString);
+ String protocol = uri.getScheme();
+
+ if (protocol.equals("local"))
+ {
+ return new InVMEpr(uri);
+ }
+ else
+ throw new ConfigurationException("Not an InVMEpr");
+ }
+ catch (URISyntaxException ex)
+ {
+ throw new ConfigurationException(ex);
+ }
+ catch (ConfigurationException ex)
+ {
+ throw ex;
+ }
+ }
public static FileEpr fileEprFromElement(ConfigTree tree) throws ConfigurationException
{
try
Added: labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/InVMCourierUnitTest.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/InVMCourierUnitTest.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/InVMCourierUnitTest.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,247 @@
+/*
+ * 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.internal.soa.esb.couriers.tests;
+
+import java.net.URI;
+
+import junit.framework.Assert;
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.internal.soa.esb.couriers.InVMCourier;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.common.ModulePropertyManager;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class InVMCourierUnitTest
+{
+ public static junit.framework.Test suite()
+ {
+ return new JUnit4TestAdapter(InVMCourierUnitTest.class);
+ }
+
+ @Test
+ public void testUnthreadedDeliver() throws Exception
+ {
+ InVMEpr epr = new InVMEpr(new URI("local://serviceid1"));
+ Producer producer = new Producer(epr);
+ Consumer consumer = new Consumer(epr);
+
+ producer.run();
+ consumer.run();
+
+ Assert.assertEquals(consumer.valid(), true);
+ }
+
+ @Test
+ public void testThreadedDeliver() throws Exception
+ {
+ System.err.println("**1");
+ InVMEpr epr = new InVMEpr(new URI("local://serviceid2"));
+ Producer producer = new Producer(epr);
+ Consumer consumer = new Consumer(epr);
+
+ producer.start();
+ consumer.start();
+
+ try
+ {
+ synchronized (condition())
+ {
+ condition().wait();
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+
+ Assert.assertEquals(consumer.valid(), true);
+ }
+
+ @Test
+ public void testDelayedThreadedDeliver() throws Exception
+ {
+ System.err.println("**2");
+ InVMEpr epr = new InVMEpr(new URI("local://serviceid3"));
+ Producer producer = new Producer(epr);
+ Consumer consumer = new Consumer(epr);
+
+ consumer.start();
+
+ try
+ {
+ Thread.currentThread().sleep(500);
+ }
+ catch (Exception ex)
+ {
+ }
+
+ producer.start();
+
+ try
+ {
+ synchronized (condition())
+ {
+ condition().wait();
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+
+ Assert.assertEquals(consumer.valid(), true);
+ }
+
+ @Test
+ public void testThreadedNullDeliver() throws Exception
+ {
+ System.err.println("**3");
+ InVMEpr epr = new InVMEpr(new URI("local://serviceid4"));
+ Consumer consumer = new Consumer(epr);
+
+ consumer.start();
+
+ try
+ {
+ Thread.currentThread().sleep(500);
+ }
+ catch (Exception ex)
+ {
+ }
+
+ try
+ {
+ synchronized (condition())
+ {
+ condition().wait();
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+
+ Assert.assertEquals(consumer.valid(), false);
+ }
+
+ public static Object condition ()
+ {
+ return _condition;
+ }
+
+ public static final int ITERATIONS = 10;
+
+ private static Object _condition = new Object();
+}
+
+class Producer extends Thread
+{
+ public Producer (InVMEpr epr)
+ {
+ _epr = epr;
+ }
+
+ public void run ()
+ {
+ try
+ {
+ InVMCourier courier = new InVMCourier(_epr);
+ int number = 0;
+ int iterations = InVMCourierUnitTest.ITERATIONS;
+
+ while (number < iterations)
+ {
+ Message msg = MessageFactory.getInstance().getMessage();
+
+ msg.getBody().add(new String("Message: "+number++));
+
+ courier.deliver(msg);
+
+ System.err.println("Delivered "+msg);
+
+ Thread.yield();
+ }
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+
+ private InVMEpr _epr;
+}
+
+class Consumer extends Thread
+{
+ public Consumer (InVMEpr epr)
+ {
+ _epr = epr;
+ }
+
+ public void run ()
+ {
+ try
+ {
+ InVMCourier courier = new InVMCourier(_epr);
+ int i;
+
+ for (i = 0; i < InVMCourierUnitTest.ITERATIONS; i++)
+ {
+ Message msg = courier.pickup(1000);
+
+ System.err.println("Received: "+msg);
+
+ if (msg == null)
+ return;
+ else
+ Thread.yield();
+ }
+
+ if (i == InVMCourierUnitTest.ITERATIONS)
+ _valid = true;
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ finally
+ {
+ synchronized (InVMCourierUnitTest.condition())
+ {
+ InVMCourierUnitTest.condition().notify();
+ }
+ }
+ }
+
+ public boolean valid ()
+ {
+ return _valid;
+ }
+
+ private boolean _valid = false;
+ private InVMEpr _epr;
+}
\ No newline at end of file
Added: labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/ListenerManagerInVMUnitTest.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/ListenerManagerInVMUnitTest.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/ListenerManagerInVMUnitTest.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,144 @@
+/*
+ * 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;
+
+import java.io.FileInputStream;
+import java.net.URI;
+import java.util.List;
+import java.util.UUID;
+
+import org.jboss.internal.soa.esb.couriers.DeliverOnlyCourier;
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.couriers.CourierFactory;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycle;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleController;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.body.content.BytesBody;
+import org.jboss.soa.esb.message.format.MessageFactory;
+
+public class ListenerManagerInVMUnitTest extends ListenerManagerBaseTest
+{
+ public ListenerManagerInVMUnitTest()
+ {
+ _file = "listenerInVM.xml";
+ }
+
+ public void testListener()
+ {
+ _logger.info("___Test for InVM starting ____________________");
+
+ try
+ {
+ oneTest();
+ }
+ catch (Exception ex)
+ {
+ _logger.error(ex.toString(), ex);
+ ex.printStackTrace();
+ fail();
+ }
+
+ _logger.info("___Test for InVM finished ____________________");
+ }
+
+ public void setUp()
+ {
+ // initialize registry
+ runBeforeAllTests();
+ }
+
+ public void tearDown()
+ {
+ runAfterAllTests();
+ }
+
+ // TODO more refactoring, since oneTest is duplicated a lot!
+
+ protected void oneTest() throws Exception
+ {
+ // Write wome messages to EPR obtained from configuration file
+ String configFile = getClass().getResource(_file).getFile();
+ ConfigTree tree = ConfigTree.fromInputStream(new FileInputStream(
+ configFile));
+ ConfigTree eprElement = tree.getAllChildren()[0].getFirstChild("EPR");
+ EPR toEPR = ListenerUtil.assembleEpr(eprElement);
+
+ if (!(toEPR instanceof InVMEpr))
+ fail();
+
+ String THE_TEXT = "___Config=" + _file + "___ Message Content ___";
+
+ int howMany = 10; // how many messages do you want to send before the
+ // listener comes up
+
+ DeliverOnlyCourier sender = CourierFactory.getCourier(toEPR);
+
+ Message message = MessageFactory.getInstance().getMessage();
+ message.getHeader().setCall(new Call(toEPR));
+ message.getBody().add(BytesBody.BYTES_LOCATION, THE_TEXT.getBytes());
+
+ for (int i1 = 0; i1 < howMany; i1++)
+ {
+ URI uri = new URI(UUID.randomUUID().toString());
+ message.getHeader().getCall().setMessageID(uri);
+ sender.deliver(message);
+ }
+
+ // launch listener manager in a child thread
+ final ConfigTree newTree = ConfigTree
+ .fromInputStream(new FileInputStream(configFile));
+
+ final List<ManagedLifecycle> instances = LifecycleUtil
+ .getListeners(newTree);
+
+ final ManagedLifecycleController controller = new ManagedLifecycleController(
+ instances);
+ controller.start();
+
+ _logger.debug(" All child listeners ready");
+
+ // JUST FOR THIS TEST:
+ // Give your listener some time to process queued messages (see howMany
+ // above)
+ // Time allowed, and maxThreads in config file will impact how many
+ // messages
+ // will be processed, and how many will remain unprocessed
+
+ for (int count = 0; count < howMany; count++)
+ {
+ final String response = getMessage(10000);
+ assertNotNull("getMessage timeout", response);
+ assertEquals(THE_TEXT, response);
+ }
+
+ _logger.debug("going to stop");
+ controller.stop();
+ _logger.debug("back from stop");
+ }
+
+ protected String _file;
+
+}
Added: labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/GatewayInVMServiceUnitTest.java
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/GatewayInVMServiceUnitTest.java (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/GatewayInVMServiceUnitTest.java 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,253 @@
+/*
+ * 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.gateway;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.List;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.common.tests.BaseTest;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.LifecycleUtil;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+import org.jboss.soa.esb.listeners.ListenerUtil;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycle;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleController;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.testutils.FileUtil;
+import org.jboss.soa.esb.testutils.HsqldbUtil;
+import org.jboss.soa.esb.testutils.TestEnvironmentUtil;
+
+public class GatewayInVMServiceUnitTest extends BaseTest
+{
+ private Logger log = Logger.getLogger(GatewayInVMServiceUnitTest.class);
+
+ public GatewayInVMServiceUnitTest()
+ {
+ }
+
+ public void setUp()
+ {
+ runBeforeAllTests();
+ }
+
+ public void tearDown()
+ {
+ runAfterAllTests();
+ }
+
+ public void testGatewayInVMServiceInvocation() throws Exception
+ {
+ ConfigTree tree = new ConfigTree("test");
+ final File tmpDir = new File(System.getProperty("user.dir"));
+ final String tmpDirForm = tmpDir.toURL().toExternalForm();
+
+ tree.setAttribute("inputDir", tmpDirForm);
+ tree.setAttribute("target-service-category", "Example12345");
+ tree.setAttribute("target-service-name", "Test12345");
+ tree.setAttribute("gatewayClass",
+ "org.jboss.soa.esb.listeners.gateway.FileGatewayListener");
+ tree.setAttribute("inputSuffix", "dummy");
+ tree.setAttribute("workSuffix", "work");
+ tree.setAttribute("postDelete", "true");
+ tree.setAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG, "abcd");
+
+ FileGatewayListener gateway = new FileGatewayListener(tree);
+
+ String configFile = getClass().getResource("listenerInVM.xml")
+ .getFile();
+ ConfigTree lTree = ConfigTree.fromInputStream(new FileInputStream(
+ configFile));
+ ConfigTree eprElement = lTree.getAllChildren()[0].getFirstChild("EPR");
+ EPR toEPR = ListenerUtil.assembleEpr(eprElement);
+
+ if (!(toEPR instanceof InVMEpr))
+ fail();
+
+ final List<ManagedLifecycle> instances = LifecycleUtil
+ .getListeners(lTree);
+
+ final ManagedLifecycleController controller = new ManagedLifecycleController(
+ instances);
+ controller.start();
+
+ gateway.initialise();
+ gateway.start();
+ gateway.onSchedule();
+
+ File testFile = null;
+
+ try
+ {
+ testFile = File.createTempFile("foo", "testFile");
+
+ String testString = "Hello World";
+ FileOutputStream stream = new FileOutputStream(testFile);
+
+ stream.write(testString.getBytes());
+ stream.close();
+ }
+ catch (Throwable ex)
+ {
+ log.error(ex);
+
+ fail();
+ }
+ finally
+ {
+ testFile.delete();
+ }
+
+ gateway.stop();
+ gateway.destroy();
+
+ controller.stop();
+
+ assertTrue(MockMessageAwareAction.received());
+ }
+
+ protected void runBeforeAllTests()
+ {
+ try
+ {
+ TestEnvironmentUtil.setESBPropertiesFileToUse("product",
+ "../product");
+ // Set the juddi properties file in System so juddi will pick it up
+ // later and use the test values.
+ String juddiPropertiesFile = "/org/jboss/soa/esb/listeners/juddi-unittest.properties";
+ System.setProperty("juddi.propertiesFile", juddiPropertiesFile);
+ // Read this properties file to get the db connection string
+ Properties props = new Properties();
+ InputStream inStream = Class.class
+ .getResourceAsStream(juddiPropertiesFile);
+
+ props.load(inStream);
+ String mDbDriver = props.getProperty("juddi.jdbcDriver");
+ String mDbUrl = props.getProperty("juddi.jdbcUrl");
+ String mDbUsername = props.getProperty("juddi.jdbcUsername");
+ String mDbPassword = props.getProperty("juddi.jdbcPassword");
+
+ String database = "not tested yet";
+ if ("org.hsqldb.jdbcDriver".equals(mDbDriver))
+ {
+ database = "hsqldb";
+ // Bring up hsql on default port 9001
+ HsqldbUtil.startHsqldb(TestEnvironmentUtil.getUserDir(
+ "product", "../product")
+ + "/build/hsqltestdb", "juddi");
+ }
+ else if ("com.mysql.jdbc.Driver".equals(mDbDriver))
+ {
+ database = "mysql";
+ } // add and test your own database..
+
+ // Get the registry-schema create scripts
+ String sqlDir = TestEnvironmentUtil.getUserDir("product",
+ "../product")
+ + "/install/jUDDI-registry/sql/" + database + "/";
+ // Drop what is there now, if exists. We want to start fresh.
+ String sqlDropCmd = FileUtil.readTextFile(new File(sqlDir
+ + "drop_database.sql"));
+ String resource = "juddi-sql/" + database + "/create_database.sql";
+ InputStream is = Thread.currentThread().getContextClassLoader()
+ .getResourceAsStream(resource);
+ String sqlCreateCmd = FileUtil.readStream(is);
+ String sqlInsertPubCmd = FileUtil.readTextFile(new File(sqlDir
+ + "import.sql"));
+
+ try
+ {
+ Class.forName(mDbDriver);
+ }
+ catch (Exception e)
+ {
+ log.error(
+ "ERROR: failed to load " + database + " JDBC driver.",
+ e);
+ return;
+ }
+
+ con = DriverManager.getConnection(mDbUrl, mDbUsername, mDbPassword);
+ Statement stmnt = con.createStatement();
+ stmnt.execute(sqlDropCmd);
+ stmnt.execute(sqlCreateCmd);
+ stmnt.execute(sqlInsertPubCmd);
+ stmnt.close();
+ }
+ catch (Throwable e)
+ {
+ log.error("We should stop testing, since we don't have a db.", e);
+ assertTrue(false);
+ }
+ }
+
+ protected final void runAfterAllTests()
+ {
+ try
+ {
+ Thread.sleep(1000);
+ Statement stmnt = con.createStatement();
+
+ stmnt.execute("SHUTDOWN");
+ stmnt.close();
+
+ con.close();
+ }
+ catch (Exception ex)
+ {
+ log.error(ex);
+ }
+ }
+
+ private Connection con;
+
+ public static class MockMessageAwareAction
+ {
+ public MockMessageAwareAction(ConfigTree config)
+ {
+ }
+
+ public Message notifyTest (Message message) throws Exception
+ {
+ _received = true;
+
+ return message;
+ }
+
+ public static final boolean received ()
+ {
+ return _received;
+ }
+
+ private static boolean _received;
+ }
+}
Added: labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/listenerInVM.xml
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/listenerInVM.xml (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/listenerInVM.xml 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,20 @@
+<DummyTester parameterReloadSecs="180">
+ <DummyActionConfig
+ service-category="Example"
+ service-name="Test12345"
+ service-description="My Dummy Service Name InVM"
+ listenerClass="org.jboss.soa.esb.listeners.message.MessageAwareListener"
+ maxThreads="10"
+ >
+ <EPR
+ URL="local://serviceid67890"
+ />
+
+ <action class="org.jboss.soa.esb.listeners.gateway.GatewayInVMServiceUnitTest$MockMessageAwareAction" process="notifyTest" />
+ <action class="org.jboss.soa.esb.actions.Notifier" okMethod="notifyOK">
+ <NotificationList type="OK">
+ <target class="NotifyConsole" />
+ </NotificationList>
+ </action>
+ </DummyActionConfig>
+</DummyTester>
Added: labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/listenerInVM.xml
===================================================================
--- labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/listenerInVM.xml (rev 0)
+++ labs/jbossesb/workspace/bramley/product/rosetta/tests/src/org/jboss/soa/esb/listeners/listenerInVM.xml 2007-10-08 14:03:35 UTC (rev 15646)
@@ -0,0 +1,20 @@
+<DummyTester parameterReloadSecs="180">
+ <DummyActionConfig
+ service-category="CatgFileEsbListener"
+ service-name="testInVMListener"
+ service-description="My Dummy Service Name InVM"
+ listenerClass="org.jboss.soa.esb.listeners.message.MessageAwareListener"
+ maxThreads="10"
+ >
+ <EPR
+ URL="local://serviceid12345"
+ />
+
+ <action class="org.jboss.soa.esb.listeners.ListenerManagerBaseTest$MockMessageAwareAction" process="notifyTest" />
+ <action class="org.jboss.soa.esb.actions.Notifier" okMethod="notifyOK">
+ <NotificationList type="OK">
+ <target class="NotifyConsole" />
+ </NotificationList>
+ </action>
+ </DummyActionConfig>
+</DummyTester>
More information about the jboss-svn-commits
mailing list