[jboss-svn-commits] JBL Code SVN: r8347 - in labs/jbossesb/trunk/product/core/rosetta: src/org/jboss/internal/soa/esb/couriers tests/src/org/jboss/internal/soa/esb/couriers/tests

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Dec 15 10:23:32 EST 2006


Author: mark.little at jboss.com
Date: 2006-12-15 10:23:26 -0500 (Fri, 15 Dec 2006)
New Revision: 8347

Added:
   labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/CourierIntegrationTest.java
Modified:
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java
Log:
Added courier integration test on behalf of Esteban.

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-15 15:16:48 UTC (rev 8346)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-15 15:23:26 UTC (rev 8347)
@@ -45,189 +45,250 @@
 import org.jboss.soa.esb.util.Util;
 
 /**
- * Internal implementation of a courier to handle Messages in a filesystem 
- * <p/> Intended to cater for local file system, remote via ftp, ftps or sftp
- * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * Internal implementation of a courier to handle Messages in a filesystem <p/>
+ * Intended to cater for local file system, remote via ftp, ftps or sftp
+ * 
+ * @author <a
+ *         href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
  * @since Version 4.0
- *
+ * 
  */
-public class FileCourier implements PickUpOnlyCourier, DeliverOnlyCourier 
+public class FileCourier implements PickUpOnlyCourier, DeliverOnlyCourier
 {
 	/**
 	 * disable public default constructor
 	 */
-	protected FileCourier() { }
+	protected FileCourier()
+	{
+	}
 
 	/**
-	 * package protected constructor - 
-	 * Objects of this class should only be instantiated by internal implementations
+	 * package protected constructor - Objects of this class should only be
+	 * instantiated by internal implementations
+	 * 
 	 * @param epr
 	 */
-	FileCourier(FileEpr epr) throws CourierException { this(epr,false); }
+	FileCourier(FileEpr epr) throws CourierException
+	{
+		this(epr, false);
+	}
 
 	/**
-	 * package protected constructor - 
-	 * Objects of this class should only be instantiated by internal implementations
+	 * package protected constructor - Objects of this class should only be
+	 * instantiated by internal implementations
+	 * 
 	 * @param epr
 	 * @param receiverOnly
 	 */
 	FileCourier(FileEpr epr, boolean receiverOnly) throws CourierException
 	{
 		_receiverOnly = receiverOnly;
-		_epr		= epr;
+		_epr = epr;
 		checkEprParms();
-    } //________________________________
-	
+	} // ________________________________
+
 	/**
 	 * See if we have everything we need in the EPR
+	 * 
 	 * @throws CourierException
 	 */
 	protected void checkEprParms() throws CourierException
 	{
 		_outputSuffix = null;
-		try { _outputSuffix = _epr.getPostSuffix(); }
-		catch (URISyntaxException e) { _logger.warn("Malformed EPR"); }
-		
+		try
+		{
+			_outputSuffix = _epr.getPostSuffix();
+		}
+		catch (URISyntaxException e)
+		{
+			_logger.warn("Malformed EPR");
+		}
+
 		if (Util.isNullString(_outputSuffix))
 		{
 			_outputSuffix = ".esbMessage";
-			_logger.debug("No suffix for delivered messages - using default of <"+_outputSuffix+">");
+			_logger
+					.debug("No suffix for delivered messages - using default of <"
+							+ _outputSuffix + ">");
 		}
-		
+
 		// Certain things can only be checked in local filesystem
 		try
 		{
-			_url	= _epr.getURL();
-			FileHandler handler = FileHandlerFactory.getInstance().getFileHandler(_epr);
+			_url = _epr.getURL();
+			FileHandler handler = FileHandlerFactory.getInstance()
+					.getFileHandler(_epr);
 			if (handler instanceof LocalFileHandler)
 			{
-				_localFhandler = (LocalFileHandler)handler;  
+				_localFhandler = (LocalFileHandler) handler;
 				File file = new File(_url.getFile());
 				if ((!_receiverOnly) && (!file.isDirectory()))
-						throw new CourierException("File for deliver EPR must be a directory (file name will be MessageID)");
+					throw new CourierException(
+							"File for deliver EPR must be a directory (file name will be MessageID)");
 
-				File directory = (file.isDirectory()) ? file : file.getParentFile();
-				if (! directory.canRead())
-					throw new CourierException("Can't read directory "+directory.toString());
-				// need to write even if it's readOnly - file will be renamed during xfer
-				if (! directory.canWrite())
-					throw new CourierException("Can't write in directory "+directory.toString());
+				File directory = (file.isDirectory()) ? file : file
+						.getParentFile();
+				if (!directory.canRead())
+					throw new CourierException("Can't read directory "
+							+ directory.toString());
+				// need to write even if it's readOnly - file will be renamed
+				// during xfer
+				if (!directory.canWrite())
+					throw new CourierException("Can't write in directory "
+							+ directory.toString());
 
 				return;
 			}
 		}
-		catch (MalformedURLException e) {throw new CourierException(e); }
-		catch (URISyntaxException e) 	{throw new CourierException(e); }
-    } //________________________________
-	
+		catch (MalformedURLException e)
+		{
+			throw new CourierException(e);
+		}
+		catch (URISyntaxException e)
+		{
+			throw new CourierException(e);
+		}
+	} // ________________________________
+
 	/**
 	 * package the ESB message in a File
-	 * @param message Message - the message to deliver 
+	 * 
+	 * @param message
+	 *            Message - the message to deliver
 	 * @return boolean - the result of the delivery
-	 * @throws CourierException - if problems were encountered
+	 * @throws CourierException -
+	 *             if problems were encountered
 	 */
-	public boolean deliver(Message message) throws CourierException 
+	public boolean deliver(Message message) throws CourierException
 	{
 		if (_receiverOnly)
 			throw new CourierException("This is a pickUp-only Courier");
 
-		if (null==message)
+		if (null == message)
 			return false;
 
-		// FileHandler is durable only for local filesystem (see checkEprParms())
-		FileHandler handler = (null!=_localFhandler) ? _localFhandler
+		// FileHandler is durable only for local filesystem (see
+		// checkEprParms())
+		FileHandler handler = (null != _localFhandler) ? _localFhandler
 				: FileHandlerFactory.getInstance().getFileHandler(_epr);
-		if (null==handler)
-			throw new CourierException("Can't find appropriate file handler for "+_url.toString());
+		if (null == handler)
+			throw new CourierException(
+					"Can't find appropriate file handler for "
+							+ _url.toString());
 
 		File tmpFile = null;
 
 		if (handler instanceof LocalFileHandler)
 			try
 			{
-				File dir = new File(_url.getFile()); 
+				File dir = new File(_url.getFile());
 				tmpFile = CourierUtil.messageToLocalFile(dir, message);
-	
-				String name	 = message.getHeader().getCall().getMessageID().toString();
+
+				String name = message.getHeader().getCall().getMessageID()
+						.toString();
 				name += _outputSuffix;
-				handler.renameFile(tmpFile, new File(dir,name));
-				
+				handler.renameFile(tmpFile, new File(dir, name));
+
 				return true;
 			}
-			catch (IOException e) 				{ throw new CourierException(e);}
-			catch (ParserConfigurationException e){ throw new CourierException(e);}
-			catch (URISyntaxException e)		{ throw new CourierException(e);}
+			catch (IOException e)
+			{
+				throw new CourierException(e);
+			}
+			catch (ParserConfigurationException e)
+			{
+				throw new CourierException(e);
+			}
+			catch (URISyntaxException e)
+			{
+				throw new CourierException(e);
+			}
 
 		try
 		{
-			Method upload = handler.getClass().getMethod("uploadFile",new Class[] {File.class}); 
+			Method upload = handler.getClass().getMethod("uploadFile",
+					new Class[]
+					{ File.class });
 
-			String sDir = ModulePropertyManager.getPropertyManager(ModulePropertyManager.TRANSPORTS_MODULE)
-				.getProperty(Environment.FTP_LOCALDIR, DEFAULT_TMP);
-			File dir = new File(sDir); 
+			String sDir = ModulePropertyManager.getPropertyManager(
+					ModulePropertyManager.TRANSPORTS_MODULE).getProperty(
+					Environment.FTP_LOCALDIR, DEFAULT_TMP);
+			File dir = new File(sDir);
 			tmpFile = CourierUtil.messageToLocalFile(dir, message);
-			String name	 = message.getHeader().getCall().getMessageID().toString();
+			String name = message.getHeader().getCall().getMessageID()
+					.toString();
 			name += _outputSuffix;
 
-			File messageFile = new File(dir,name);  
+			File messageFile = new File(dir, name);
 			tmpFile.renameTo(messageFile);
-			tmpFile	= messageFile;
-			
-			upload.invoke(handler, new Object[] {messageFile});
+			tmpFile = messageFile;
+
+			upload.invoke(handler, new Object[]
+			{ messageFile });
 			return true;
 		}
-		catch (Exception e)				{throw new CourierException(e);}
-		finally 
+		catch (Exception e)
 		{
-			if (null!=tmpFile)
-				tmpFile.delete(); 
+			e.printStackTrace();
+			
+			throw new CourierException(e);
 		}
+		finally
+		{
+			if (null != tmpFile)
+				tmpFile.delete();
+		}
 
+	} // ________________________________
 
-			
-	} //________________________________
-	
-	public Message pickup(long millis) throws CourierException 
+	public Message pickup(long millis) throws CourierException
 	{
 		Message result = null;
-		long limit = System.currentTimeMillis() + ((millis < 100) ? 100 : millis);
+		long limit = System.currentTimeMillis()
+				+ ((millis < 100) ? 100 : millis);
 		do
 		{
-			FileHandler handler = (null!=_localFhandler) ? _localFhandler
+			FileHandler handler = (null != _localFhandler) ? _localFhandler
 					: FileHandlerFactory.getInstance().getFileHandler(_epr);
 			File[] files = handler.getFileList();
-			if (null!=files && files.length>0)
+			if (null != files && files.length > 0)
 			{
-				File input 	= files[0];
-				File work	= workFile(input);
+				File input = files[0];
+				File work = workFile(input);
 				handler.renameFile(input, work);
-				try 
+				try
 				{
-					result = readOneMessage (handler,work);
+					result = readOneMessage(handler, work);
 				}
-				catch (Exception e) 
+				catch (Exception e)
 				{
-					if (null==errorFile(input))
+					if (null == errorFile(input))
 						handler.deleteFile(work);
 					else
 						handler.renameFile(work, errorFile(input));
 					continue;
 				}
 				File done = postFile(input);
-				if (null==done)
+				if (null == done)
 					handler.deleteFile(work);
 				else
 					handler.renameFile(work, done);
 				return result;
 			}
-			try { Thread.sleep(200); }
-			catch (InterruptedException e) {	return null;}
-		}
-		while (System.currentTimeMillis() <= limit);
+			try
+			{
+				Thread.sleep(200);
+			}
+			catch (InterruptedException e)
+			{
+				return null;
+			}
+		} while (System.currentTimeMillis() <= limit);
 		return null;
-    } //________________________________
-	
-	private Message readOneMessage(FileHandler handler, File work) throws Exception
+	} // ________________________________
+
+	private Message readOneMessage(FileHandler handler, File work)
+			throws Exception
 	{
 		if (handler instanceof LocalFileHandler)
 			return CourierUtil.messageFromLocalFile(work);
@@ -235,98 +296,144 @@
 		File tmpFile = null;
 		try
 		{
-			Method download = handler.getClass().getMethod("downloadFile",new Class[] {File.class}); 
-			tmpFile  = (File)download.invoke(handler, new Object[] {work});
+			Method download = handler.getClass().getMethod("downloadFile",
+					new Class[]
+					{ File.class });
+			tmpFile = (File) download.invoke(handler, new Object[]
+			{ work });
 			return CourierUtil.messageFromLocalFile(tmpFile);
 		}
-		catch (Exception e)				{throw new CourierException(e);}
-		finally 
+		catch (Exception e)
 		{
-			if (null!=tmpFile)
-				tmpFile.delete(); 
+			throw new CourierException(e);
 		}
-    } //________________________________
-	
+		finally
+		{
+			if (null != tmpFile)
+				tmpFile.delete();
+		}
+	} // ________________________________
+
 	protected File workFile(File input)
 	{
 		String sfx = null;
-		try { sfx = _epr.getWorkSuffix(); }
-		catch (URISyntaxException e) 
-			{ _logger.warn("Malformed EPR",e); }
+		try
+		{
+			sfx = _epr.getWorkSuffix();
+		}
+		catch (URISyntaxException e)
+		{
+			_logger.warn("Malformed EPR", e);
+		}
 
 		if (Util.isNullString(sfx))
 		{
 			sfx = ".esbInProcess";
-			_logger.debug("No valid work suffix found in EPR - using default of "+sfx);
+			_logger
+					.debug("No valid work suffix found in EPR - using default of "
+							+ sfx);
 		}
-		return new File(input.getParent(),input.getName()+sfx);
-    } //________________________________
-	
+		return new File(input.getParent(), input.getName() + sfx);
+	} // ________________________________
+
 	protected File errorFile(File input)
 	{
-		try 
+		try
 		{
 			if (_epr.getErrorDelete())
 				return null;
 		}
-		catch (Exception e) { _logger.warn("Problems in FileEpr",e); }
+		catch (Exception e)
+		{
+			_logger.warn("Problems in FileEpr", e);
+		}
 
 		String sfx = null;
-		try { sfx = _epr.getErrorSuffix(); }
-		catch (URISyntaxException e) 
-			{ _logger.warn("Malformed EPR",e); }
+		try
+		{
+			sfx = _epr.getErrorSuffix();
+		}
+		catch (URISyntaxException e)
+		{
+			_logger.warn("Malformed EPR", e);
+		}
 
 		if (Util.isNullString(sfx))
 		{
 			sfx = ".esbERROR";
-			_logger.debug("No valid work suffix found in EPR - using default of "+sfx);
+			_logger
+					.debug("No valid work suffix found in EPR - using default of "
+							+ sfx);
 		}
-		return new File(input.getParent(),input.getName()+sfx);
-    } //________________________________
-	
+		return new File(input.getParent(), input.getName() + sfx);
+	} // ________________________________
+
 	protected File postFile(File input)
 	{
-		try 
+		try
 		{
 			if (_epr instanceof FTPEpr || _epr.getPostDelete())
 				return null;
 		}
-		catch (Exception e) { _logger.warn("Problems in FileEpr",e); }
+		catch (Exception e)
+		{
+			_logger.warn("Problems in FileEpr", e);
+		}
 
 		String inputDir = input.getParent();
 		String dir = null;
-		try { dir = _epr.getPostDirectory(); }
-		catch (URISyntaxException e) 
-			{ _logger.warn("Malformed EPR",e); }
+		try
+		{
+			dir = _epr.getPostDirectory();
+		}
+		catch (URISyntaxException e)
+		{
+			_logger.warn("Malformed EPR", e);
+		}
 		if (Util.isNullString(dir))
 		{
 			dir = inputDir;
-			_logger.debug("No valid post process directory found in EPR - using same as input ("+dir+")");
+			_logger
+					.debug("No valid post process directory found in EPR - using same as input ("
+							+ dir + ")");
 		}
 
 		String sfx = null;
-		try { sfx = _epr.getPostSuffix(); }
-		catch (URISyntaxException e) 
-			{ _logger.warn("Malformed EPR",e); }
+		try
+		{
+			sfx = _epr.getPostSuffix();
+		}
+		catch (URISyntaxException e)
+		{
+			_logger.warn("Malformed EPR", e);
+		}
 		if (Util.isNullString(sfx))
 		{
 			if (dir.equals(inputDir))
 			{
 				sfx = ".esbProcessed";
-				_logger.debug("No valid post suffix found in EPR - using default of "+sfx);
+				_logger
+						.debug("No valid post suffix found in EPR - using default of "
+								+ sfx);
 			}
 		}
 
-		return new File(dir,input.getName()+sfx);
-    } //________________________________
-	
-	protected static final String DEFAULT_TMP = System.getProperty("java.io.tmpdir");
+		return new File(dir, input.getName() + sfx);
+	} // ________________________________
 
-	protected String			_outputSuffix;
-	protected URL				_url;
-	protected boolean			_receiverOnly;	  
-    protected FileEpr			_epr;
-    protected LocalFileHandler	_localFhandler;
-    protected static Logger		_logger = Logger.getLogger(FileCourier.class);
+	protected static final String DEFAULT_TMP = System
+			.getProperty("java.io.tmpdir");
 
-} //____________________________________________________________________________
+	protected String _outputSuffix;
+
+	protected URL _url;
+
+	protected boolean _receiverOnly;
+
+	protected FileEpr _epr;
+
+	protected LocalFileHandler _localFhandler;
+
+	protected static Logger _logger = Logger.getLogger(FileCourier.class);
+
+} // ____________________________________________________________________________

Added: labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/CourierIntegrationTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/CourierIntegrationTest.java	2006-12-15 15:16:48 UTC (rev 8346)
+++ labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/CourierIntegrationTest.java	2006-12-15 15:23:26 UTC (rev 8347)
@@ -0,0 +1,119 @@
+/*
+ * 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.io.File;
+import java.net.URI;
+import java.net.URL;
+import java.util.UUID;
+
+import junit.framework.Assert;
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.eprs.FTPEpr;
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.addressing.eprs.HTTPEpr;
+import org.jboss.soa.esb.common.tests.BaseTest;
+import org.jboss.soa.esb.couriers.Courier;
+import org.jboss.soa.esb.couriers.CourierFactory;
+import org.jboss.soa.esb.couriers.CourierUtil;
+import org.jboss.soa.esb.couriers.TwoWayCourier;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.message.format.MessageType;
+import org.junit.Test;
+
+/**
+ * Tests for internal FtpFileHandler class
+ * 
+ * @since Version 4.0
+ * 
+ */
+public class CourierIntegrationTest extends BaseTest
+{
+	public static junit.framework.Test suite()
+	{
+		return new JUnit4TestAdapter(CourierIntegrationTest.class);
+	}
+	
+	@Test
+	public void testFileDeliver () throws Exception
+	{
+		deliver("file://"+System.getProperty("java.io.tmpdir"));
+	}
+	
+	@Test
+	public void testFTPDeliver () throws Exception
+	{
+		deliver("ftp://"+getFtpUser()+"@"+getFtpHostname()+"/"+getFtpDir());
+	}
+	
+	@Test
+	public void pickupFile () throws Exception
+	{
+		pickup("file://"+System.getProperty("java.io.tmpdir"));
+	}
+	
+	@Test
+	public void pickupFTP () throws Exception
+	{
+		pickup("ftp://"+getFtpUser()+"@"+getFtpHostname()+"/"+getFtpDir());
+	}
+	
+	private final FileEpr getEpr(String sUrl) throws Exception
+	{
+		String protocol = new URL(sUrl).getProtocol();
+
+		if ("ftp".equals(protocol))
+			return new FTPEpr(sUrl);
+		if ("file".equals(protocol))
+			return new FileEpr(sUrl);
+
+		throw new IllegalArgumentException("Not a recognised protocol!");
+	}
+	
+	private final void pickup (String url) throws Exception
+	{
+		FileEpr epr = getEpr(url);
+		epr.setInputSuffix(".esbMessage");
+		TwoWayCourier courier = CourierFactory.getPickupCourier(epr);
+
+		Message message = null;
+		while (null != (message = courier.pickup(100)))
+			System.out.println(message.getHeader().getCall().getMessageID());
+	}
+
+	private final void deliver (String url) throws Exception
+	{
+		Call call = new Call(getEpr(url));
+		call.setMessageID(new URI(UUID.randomUUID().toString()));
+
+		Message message = MessageFactory.getInstance().getMessage();
+		message.getHeader().setCall(call);
+		message.getBody().setContents("Hello World".getBytes());
+		Courier courier = CourierFactory.getCourier(call.getTo());
+
+		courier.deliver(message);
+	}
+}




More information about the jboss-svn-commits mailing list