[jboss-svn-commits] JBL Code SVN: r8131 - in labs/jbossesb/trunk/product/core/rosetta: src/org/jboss/internal/soa/esb/couriers src/org/jboss/internal/soa/esb/couriers/helpers src/org/jboss/soa/esb/couriers tests/src/org/jboss/internal/soa/esb tests/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
Thu Dec 7 17:39:57 EST 2006


Author: estebanschifman
Date: 2006-12-07 17:39:46 -0500 (Thu, 07 Dec 2006)
New Revision: 8131

Added:
   labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/
   labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/
   labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java
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/helpers/FileHandlerInterface.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java
Log:
More work on FileCourier and couriers.helpers package

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-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -23,14 +23,20 @@
 package org.jboss.internal.soa.esb.couriers;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
+import java.net.URL;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.log4j.Logger;
 import org.jboss.internal.soa.esb.couriers.helpers.FileHandlerFactory;
 import org.jboss.internal.soa.esb.couriers.helpers.FileHandlerInterface;
+import org.jboss.internal.soa.esb.couriers.helpers.LocalFileHandler;
 import org.jboss.soa.esb.addressing.eprs.FileEpr;
 import org.jboss.soa.esb.couriers.CourierException;
+import org.jboss.soa.esb.couriers.CourierUtil;
 import org.jboss.soa.esb.message.Message;
 
 public class FileCourier implements PickUpOnlyCourier, DeliverOnlyCourier 
@@ -61,19 +67,20 @@
     } //________________________________
 	
 	/**
-	 * See if we have everythin we need in the EPR
+	 * See if we have everything we need in the EPR
 	 * @throws CourierException
 	 */
 	protected void checkEprParms() throws CourierException
 	{
-		//TODO need to add more logic here
-		
-		// Certain things can only be done in local filesystem
+		// Certain things can only be checked in local filesystem
 		try
 		{
-			if ("file".equals(_epr.getURL().getProtocol()))
+			_url	= _epr.getURL();
+			FileHandlerInterface handler = FileHandlerFactory.getInstance().getFileHandler(_epr);
+			if (handler instanceof LocalFileHandler)
 			{
-				File file = new File(_epr.getURL().getFile());
+				_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)");
 
@@ -84,7 +91,7 @@
 				if (! directory.canWrite())
 					throw new CourierException("Can't write in directory "+directory.toString());
 
-				_handler = FileHandlerFactory.getInstance().getFileHandler(_epr);
+				return;
 			}
 		}
 		catch (MalformedURLException e) {throw new CourierException(e); }
@@ -100,24 +107,67 @@
 	public boolean deliver(Message message) throws CourierException 
 	{
 		if (_receiverOnly)
-			throw new CourierException("This is a read-only Courier");
+			throw new CourierException("This is a pickUp-only Courier");
 
 		if (null==message)
 			return false;
-		
-		//TODO  come back here once FileHandlerInterface is done
-		return false;
+
+		// FileHandler is durable only for local filesystem (see checkEprParms())
+		FileHandlerInterface handler = (null!=_localFhandler) ? _localFhandler
+				: FileHandlerFactory.getInstance().getFileHandler(_epr);
+		if (null==handler)
+			throw new CourierException("Can't find appropriate file handler for "+_url.toString());
+
+		File dir = new File(_url.getFile());
+		try
+		{
+			File tmpFile = CourierUtil.messageToLocalFile(dir, message);
+			String name	 = message.getHeader().getCall().getMessageID().toString();
+			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);}
+			
 	} //________________________________
 	
 	public Message pickup(long millis) throws CourierException 
 	{
-		//TODO  come back here once FileHandlerInterface is done
+		Message result = null;
+		long limit = System.currentTimeMillis() + ((millis < 100) ? 100 : millis);
+		do
+		{
+			FileHandlerInterface handler = (null!=_localFhandler) ? _localFhandler
+					: FileHandlerFactory.getInstance().getFileHandler(_epr);
+			File[] files = handler.getFileList();
+			if (null!=files && files.length>0)
+			{
+				File input 	= files[0];
+				File work	= new File(input.getParent(),input.getName()+".esbInProcess");
+				if (handler.renameFile(input, work))
+					try { result = CourierUtil.messageFromLocalFile(work); }
+					catch (Exception e) 
+					{
+						File error = new File(input.getParent(),input.getName()+".esbError");
+						handler.renameFile(work, error);
+						continue;
+					}
+					File done	= new File(input.getParent(),input.getName()+".esbDone");
+					handler.renameFile(work, done);
+					return result;
+			}
+			try { Thread.sleep(200); }
+			catch (InterruptedException e) {	return null;}
+		}
+		while (System.currentTimeMillis() <= limit);
 		return null;
     } //________________________________
-
+	
+	protected URL				_url;
 	protected boolean			_receiverOnly;	  
     protected FileEpr			_epr;
-    protected FileHandlerInterface _handler;
+    protected LocalFileHandler	_localFhandler;
     protected Logger			_logger = Logger.getLogger(FileCourier.class);
 
 } //____________________________________________________________________________

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerInterface.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerInterface.java	2006-12-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerInterface.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -23,11 +23,12 @@
 package org.jboss.internal.soa.esb.couriers.helpers;
 
 import java.io.File;
+
 import org.jboss.soa.esb.couriers.CourierException;
 
 public interface FileHandlerInterface 
 {
-	abstract File[]  getFileList(String suffix)		throws CourierException;
+	abstract File[]  getFileList()					throws CourierException;
 	abstract byte[]	 getFileContents(File file)		throws CourierException;
 	abstract boolean renameFile(File from, File to) throws CourierException;
 	abstract boolean deleteFile(File file)			throws CourierException;    	

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java	2006-12-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -1,6 +1,7 @@
 package org.jboss.internal.soa.esb.couriers.helpers;
 
 import java.io.File;
+import java.io.Serializable;
 
 import org.jboss.soa.esb.addressing.eprs.FileEpr;
 import org.jboss.soa.esb.couriers.CourierException;
@@ -23,7 +24,7 @@
 		return null;
 	}
 
-	public File[] getFileList(String suffix) throws CourierException 
+	public File[] getFileList() throws CourierException 
 	{
 		return null;
 	}
@@ -33,5 +34,10 @@
 		return false;
 	}
 
+	public boolean writeToFile(File file, Serializable what) throws CourierException 
+	{
+		return false;
+	}
+
 	FileEpr _epr;
 }

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java	2006-12-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -46,7 +46,7 @@
 		catch (Exception e)				{throw new CourierException(e);}
 	}
 
-	public File[] getFileList(String suffix) throws CourierException 
+	public File[] getFileList() throws CourierException 
 	{
 		try
 		{

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java	2006-12-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -22,15 +22,27 @@
 
 package org.jboss.soa.esb.couriers;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.jboss.soa.esb.addressing.EPR;
 import org.jboss.soa.esb.addressing.eprs.JMSEpr;
 import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.message.Message;
 import org.jboss.soa.esb.util.Util;
+import org.xml.sax.SAXException;
 
 public class CourierUtil
 {
@@ -45,7 +57,8 @@
     {
 		// No problem if selector is null - everything in queue will be returned
 		List<KeyValuePair> oRet = new ArrayList<KeyValuePair>();
-		if (! Util.isNullString(selector)) {
+		if (! Util.isNullString(selector)) 
+		{
 			for (String sCurr : selector.split(",")) {
 				String[] sa = sCurr.split("=");
 				if (sa.length!=2
@@ -100,4 +113,34 @@
     	}
     	return replyToEpr;
     }
+    
+	public static File messageToLocalFile(File directory, Message message)
+		throws IOException, ParserConfigurationException
+	{
+		File tmpFile = File.createTempFile("EsbFileCourier_", ".part",directory);
+		Serializable serial = Util.serialize(message);
+		ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream(tmpFile));
+		writer.writeObject(serial);
+		writer.close();
+		return tmpFile;
+	}
+
+	public static Message messageFromLocalFile(File from)
+		throws FileNotFoundException,IOException,ClassNotFoundException, ClassCastException
+			,ParserConfigurationException, SAXException 
+	{
+		ObjectInputStream reader = new ObjectInputStream(new FileInputStream(from));
+		Serializable serial = (Serializable)reader.readObject();
+		reader.close();
+		return Util.deserialize(serial);
+	}
+	
+	public static void deliverMessage(Message message)
+		throws URISyntaxException, CourierException
+	{
+		EPR toEpr = message.getHeader().getCall().getTo();
+		Courier courier = CourierFactory.getCourier(toEpr);
+		courier.deliver(message);
+	}
+
 }

Added: labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java	2006-12-07 19:48:05 UTC (rev 8130)
+++ labs/jbossesb/trunk/product/core/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/tests/FileCourierUnitTest.java	2006-12-07 22:39:46 UTC (rev 8131)
@@ -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.internal.soa.esb.couriers.tests;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.net.URI;
+import java.util.UUID;
+
+import junit.framework.Assert;
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.addressing.Call;
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+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.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FileCourierUnitTest 
+{
+	private static Logger _logger		= Logger.getLogger(FileCourierUnitTest.class);
+	private static final String DEFAULT_TEMP =
+		(System.getProperty("os.name").toLowerCase().contains("window")) ? "/temp" : "/tmp";
+	private static final String TMP_DIR = System.getProperty("java.io.tmpdir",DEFAULT_TEMP);
+	
+	static File _tmpDir = new File(TMP_DIR);
+	static URI _uid;
+	static File _theFile;
+	
+	@BeforeClass
+	public static void setUp() throws Exception
+	{
+//		_logger.setLevel(Level.DEBUG);
+		_logger.debug("tmp directory = <"+_tmpDir+">");
+		_uid = new URI(UUID.randomUUID().toString());
+		_theFile = new File(_tmpDir,_uid.toString());
+		_theFile.delete();
+	}
+	
+	@AfterClass
+	public static void tearDown() throws Exception
+	{
+		FileFilter ff = new FileFilter() 
+		{
+				public boolean accept(File file)
+				{
+					return (file.getName().startsWith(_uid.toString()));
+				}
+			};
+		for (File file:_tmpDir.listFiles(ff))
+		{
+			_logger.debug("delete of "+file.toString()+" = "+file.delete());
+		}
+	}
+
+	public static junit.framework.Test suite() {
+		return new JUnit4TestAdapter(FileCourierUnitTest.class);
+	}
+
+	@Test
+	public void testStoreRetrieve() throws Exception 
+    {
+		String contents = "This is the text that travels all around";
+		FileEpr toEpr = new FileEpr("file://"+TMP_DIR);
+		Message msg = MessageFactory.getInstance().getMessage();
+		msg.getBody().setContents(contents.getBytes());
+
+		Call call = new Call(toEpr);
+		call.setMessageID(_uid);
+		msg.getHeader().setCall(call);
+
+		CourierUtil.deliverMessage(msg);
+		Assert.assertTrue(_theFile.exists());
+		_logger.info("Message file "+_theFile.toString()+" successfully created");
+				
+		FileEpr fromEpr = new FileEpr(toEpr.getURL());
+		fromEpr.setInputSuffix(_uid.toString());
+		
+		TwoWayCourier pickUp = CourierFactory.getPickupCourier(fromEpr);
+		Message retrieved = pickUp.pickup(1000);
+		Assert.assertFalse("Null message retrieved",null==retrieved);
+		
+		String back = new String(retrieved.getBody().getContents());
+		Assert.assertEquals(contents,back);
+		_logger.info("Contents of retrieved msg equal original text <"+back+">");
+		
+    }
+
+}




More information about the jboss-svn-commits mailing list