[jboss-svn-commits] JBL Code SVN: r13072 - in labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta: tests/src/org/jboss/soa/esb/listeners/gateway and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jul 4 07:22:58 EDT 2007


Author: beve
Date: 2007-07-04 07:22:58 -0400 (Wed, 04 Jul 2007)
New Revision: 13072

Added:
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGateway.java
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGatewayUnitTest.java
Modified:
   labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
Log:
Added the class AbstractPollingGateway, which contains the polling logic that was previously in
the AbstractFileGateway class. AbstractFileGateway now extends this class.
The will let us create other polling gateways without having to duplicate code


Modified: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java	2007-07-04 07:54:40 UTC (rev 13071)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java	2007-07-04 11:22:58 UTC (rev 13072)
@@ -47,9 +47,7 @@
 import org.jboss.soa.esb.listeners.ListenerTagNames;
 import org.jboss.soa.esb.listeners.ListenerUtil;
 import org.jboss.soa.esb.listeners.RegistryUtil;
-import org.jboss.soa.esb.listeners.lifecycle.AbstractThreadedManagedLifecycle;
 import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleException;
-import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleThreadState;
 import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.message.format.MessageFactory;
 import org.jboss.soa.esb.services.registry.RegistryException;
@@ -66,7 +64,7 @@
  * @since Version 4.0
  * 
  */
-public abstract class AbstractFileGateway extends AbstractThreadedManagedLifecycle
+public abstract class AbstractFileGateway extends AbstractPollingGateway
 {
 	abstract File[] getFileList() throws GatewayException;
 
@@ -113,7 +111,7 @@
         /**
          * Execute on the thread.
          */
-        protected void doRun()
+		protected void performGatewayOperation()
         {
 
         	EPR 	replyEpr = null;
@@ -125,13 +123,12 @@
     				+ " started on thread " + Thread.currentThread().getName());
             }
 
-        	do {
-        		File[] fileList;
+			File[] fileList;
 			try {
 				fileList = getFileList();
 			} catch (GatewayException e) {
 				_logger.error("Can't retrieve file list", e);
-				continue;
+				return;
 			}
 
 			for (File fileIn : fileList)
@@ -342,14 +339,6 @@
 					}
 				}
 			}
-		}
-                while (!waitForRunningStateChange(ManagedLifecycleThreadState.STOPPING, _sleepBetweenPolls)) ;
-
-                if (_logger.isDebugEnabled())
-                {
-                    _logger.debug("run() method of " + this.getClass().getSimpleName()
-				+ " finished on thread " + Thread.currentThread().getName());
-                }
 	} // ________________________________
         
         protected File getWorkFileName( File fileIn, String suffix )
@@ -402,7 +391,8 @@
 		_targetServiceName = ListenerUtil.obtainAtt(_config,
 				ListenerTagNames.TARGET_SERVICE_NAME_TAG, null);
 
-		// Polling interval
+		// Polling interval; moved to AbstractPollingInverval
+		/*
 		String sAux = _config
 				.getAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG);
 		
@@ -424,6 +414,7 @@
 					+ ListenerTagNames.POLL_LATENCY_SECS_TAG
 					+ " -  Using default of " + (_sleepBetweenPolls / 1000));
 		}
+		*/
 
 		resolveComposerClass();
 

Added: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGateway.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGateway.java	                        (rev 0)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGateway.java	2007-07-04 11:22:58 UTC (rev 13072)
@@ -0,0 +1,107 @@
+/*
+ * 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 static org.jboss.soa.esb.listeners.ListenerTagNames.POLL_LATENCY_SECS_TAG;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.lifecycle.AbstractThreadedManagedLifecycle;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleThreadState;
+import org.jboss.soa.esb.util.Util;
+
+/**
+ * AbstractPollingGateway
+ * </p>
+ * Implements the behaviour for triggering ...
+ * 
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>				
+ *
+ */
+public abstract class AbstractPollingGateway extends AbstractThreadedManagedLifecycle 
+{
+	private Logger log = Logger.getLogger( AbstractPollingGateway.class );
+	
+	/* polling interval in milliseconds */
+	private long pollingInterval = 10000l;
+	
+	protected AbstractPollingGateway( final ConfigTree configTree ) throws ConfigurationException
+	{
+		super( configTree );
+		checkMyParms( configTree );
+	}
+	
+	@Override
+	protected void doRun()
+	{
+		do
+		{
+			performGatewayOperation();
+		}
+		while (!waitForRunningStateChange(ManagedLifecycleThreadState.STOPPING, pollingInterval )) ;
+		
+        log.debug("doRun() method of " + this.getClass().getSimpleName() + " finished on thread " + Thread.currentThread().getName());
+	}
+	
+	/**
+	 * This method performs whatever needs to be preformed when
+	 * the polling intervall is reached.
+	 * </p>
+	 *
+	 */
+	protected abstract void performGatewayOperation();
+
+	public long getPollIntervalMS()
+	{
+		return pollingInterval;
+	}
+	
+	public long getPollIntervalSec()
+	{
+		return pollingInterval / 1000;
+	}
+	
+	private void checkMyParms( final ConfigTree configTree ) throws ConfigurationException
+	{
+		assert( configTree != null ) : "configTree was null in checkMyParams!";
+		
+		final String pollIntervalSecondStr = configTree.getAttribute( POLL_LATENCY_SECS_TAG );
+		if ( Util.isNullString( pollIntervalSecondStr ) )
+		{
+			log.warn("No value specified for: " + POLL_LATENCY_SECS_TAG + " -  Using default of " + getPollIntervalSec() );
+			return;
+		}
+		
+		try
+		{
+			pollingInterval = 1000 * Long.parseLong( pollIntervalSecondStr );
+			if ( pollingInterval <= 0 )
+				throw new ConfigurationException( "[pollingInterval] must be greater then 0, was : " + pollingInterval );
+		}
+		catch ( final NumberFormatException e)
+		{
+			log.warn("Invalid poll latency: "+ pollIntervalSecondStr + " - keeping default of " + getPollIntervalSec() );
+		}
+	}
+
+}

Added: labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGatewayUnitTest.java
===================================================================
--- labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGatewayUnitTest.java	                        (rev 0)
+++ labs/jbossesb/workspace/dbevenius/wslistener/product/rosetta/tests/src/org/jboss/soa/esb/listeners/gateway/AbstractPollingGatewayUnitTest.java	2007-07-04 11:22:58 UTC (rev 13072)
@@ -0,0 +1,115 @@
+/*
+ * 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 static org.junit.Assert.*;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+import org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleException;
+import org.junit.Test;
+
+/**
+ * Test the AbstractPollingGateway.
+ * </p>
+ * 
+ * @author <a href="daniel.bevenius at redpill.se">Daniel Bevenius</a>				
+ *
+ */
+public class AbstractPollingGatewayUnitTest
+{
+	@SuppressWarnings ( "unused")
+	private Logger log = Logger .getLogger( AbstractPollingGatewayUnitTest.class );
+	
+	@Test ( expected = ConfigurationException.class )
+	public void constructor_pollingIntervalNegative() throws ConfigurationException
+	{
+		long pollInterval = -1000;
+		new MockAbstractPollingGateway( createConfigTree( pollInterval ) );
+	}
+	
+	@Test ( expected = ConfigurationException.class )
+	public void constructor_pollingIntervalNegative_zero() throws ConfigurationException
+	{
+		long pollInterval = 0;
+		new MockAbstractPollingGateway( createConfigTree( pollInterval ) );
+	}
+	
+	@Test 
+	public void constructor_pollingInterval() throws ConfigurationException
+	{
+		long pollIntervalSec = 10;
+		MockAbstractPollingGateway gateway = new MockAbstractPollingGateway( createConfigTree( pollIntervalSec ) );
+		equals ( gateway.getPollIntervalMS() == 1000 );
+	}
+	
+	@Test
+	public void start() throws ConfigurationException, ManagedLifecycleException, InterruptedException
+	{
+		long pollIntervalSec = 10;
+		MockAbstractPollingGateway gateway = new MockAbstractPollingGateway( createConfigTree( pollIntervalSec ) );
+		gateway.initialise();
+		gateway.start();
+		Thread.sleep( 1000 );
+		gateway.stop();
+		assertTrue ( gateway.hasRun );
+	}
+	
+	private ConfigTree createConfigTree( long pollInterval )
+	{
+		ConfigTree tree = new ConfigTree("AbstractPollingGatewayUnitTest");
+		tree.setAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG, String.valueOf( pollInterval ) );
+		return tree;
+	}
+	
+	private static class MockAbstractPollingGateway extends AbstractPollingGateway
+	{
+		private static final long serialVersionUID = 1L;
+
+		private Logger log = Logger .getLogger( MockAbstractPollingGateway.class );
+		
+		private boolean hasRun;
+
+		protected MockAbstractPollingGateway(ConfigTree config) throws ConfigurationException
+		{
+			super( config );
+		}
+
+		protected void doInitialise() throws ManagedLifecycleException { }
+
+		@Override
+		protected void performGatewayOperation()
+		{
+			log.debug( "Performing mock gateway operation" );
+			hasRun = true;
+		}
+
+		public boolean hasRun()
+		{
+			return hasRun;
+		}
+		
+	}
+
+}




More information about the jboss-svn-commits mailing list