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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Dec 8 12:49:02 EST 2006


Author: estebanschifman
Date: 2006-12-08 12:48:52 -0500 (Fri, 08 Dec 2006)
New Revision: 8148

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/FileHandlerFactory.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/addressing/eprs/FTPEpr.java
   labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java
Log:
FtpFileHandler is now operational - Minor changes to related classes

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-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -24,6 +24,8 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -140,9 +142,22 @@
 		try
 		{
 			File tmpFile = CourierUtil.messageToLocalFile(dir, message);
+
+			Method upload = null;
+			try	  { upload = handler.getClass().getMethod("uploadFile",new Class[] {File.class}); }
+			catch (NoSuchMethodException e) { /* OK- we just won't call it */  }
+			if (null!=upload)
+			{
+				try  { upload.invoke(handler, new Object[] {tmpFile}); }
+				catch (InvocationTargetException e) {throw new CourierException(e);}
+				catch (IllegalAccessException e) 	{throw new CourierException(e);}
+				finally { tmpFile.delete(); }
+			}
+
 			String name	 = message.getHeader().getCall().getMessageID().toString();
 			name += _outputSuffix;
 			handler.renameFile(tmpFile, new File(dir,name));
+			
 			return true;
 		}
 		catch (IOException e) 				{ throw new CourierException(e);}

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerFactory.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerFactory.java	2006-12-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerFactory.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -1,8 +1,28 @@
+/*
+* 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.helpers;
 
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
-
+import org.jboss.soa.esb.addressing.eprs.FTPEpr;
 import org.jboss.soa.esb.addressing.eprs.FileEpr;
 import org.jboss.soa.esb.couriers.CourierException;
 
@@ -14,16 +34,10 @@
 
 	public FileHandlerInterface getFileHandler(FileEpr epr) throws CourierException
 	{
-		try
-		{
-			String protocol = epr.getURL().getProtocol();
-			if ("file".equals(protocol)) 	return new LocalFileHandler(epr);
-			if ("ftp".equals(protocol)) 	return new FtpFileHandler(epr);
-			// Maybe ftps and sftp ?  Wait for Bruno's input
-		}
-		catch (URISyntaxException e) 	{ throw new CourierException(e); }
-		catch (MalformedURLException e) { throw new CourierException(e); }
-		
+		if (epr instanceof FileEpr)	return new LocalFileHandler	((FileEpr)epr);
+		if (epr instanceof FTPEpr) 	return new FtpFileHandler	((FTPEpr)epr);
+		//TODO  Maybe ftps and sftp ?  Wait for Bruno's input
+
 		throw new CourierException("Unable to obtain a file handler for supplied EPR");
 	}
 	

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-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -1,43 +1,148 @@
 package org.jboss.internal.soa.esb.couriers.helpers;
 
 import java.io.File;
-import java.io.Serializable;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
 
-import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.addressing.eprs.FTPEpr;
 import org.jboss.soa.esb.couriers.CourierException;
+import org.jboss.soa.esb.couriers.CourierUtil;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.util.RemoteFileSystem;
+import org.jboss.soa.esb.util.RemoteFileSystemException;
+import org.jboss.soa.esb.util.RemoteFileSystemFactory;
 
 public class FtpFileHandler implements FileHandlerInterface 
 {
 	private FtpFileHandler() {}
-	FtpFileHandler(FileEpr epr) throws CourierException 
+	FtpFileHandler(FTPEpr epr) throws CourierException 
 	{
 		_epr	= epr;
+
+		URL url = null;
+		try { url = _epr.getURL(); }
+		catch (MalformedURLException e) { throw new CourierException(e); }
+		catch (URISyntaxException e)	{ throw new CourierException(e); }
+
+		_server	= url.getHost();
+
+		String[] sa = url.getUserInfo().split(":");
+		if (null==sa)
+			sa = new String[] {"",""};
+		_user		= (sa.length<1) ? "" : sa[0];
+		_passwd	= (sa.length<2) ? "" : sa[1];
+
+		_remoteDir		= url.getFile();
+
+		_port = url.getPort();
+		if (_port < 0)
+			_port = url.getDefaultPort();
+		//TODO  resolve this - Best bet would be 'local dir' accessors in FTPEpr
+		_localDir = "/tmp";
+//		_localDir = _epr.getLocalDir();
+		
+		//TODO  setAscii(boolean)  getAscii() in FTPEpr() ??
+		_isAscii	= true;
+//		_isAscii 	= _epr.getAscii();
+
+		//TODO Once the RemoteFileSystem has a constructor that takes FTPEpr, we can get rid of this kludge
+		configTreeFromEpr();
 	}
 
 	public boolean deleteFile(File file) throws CourierException 
 	{
-		return false;
+		try
+		{
+			getHandler().deleteRemoteFile(file.getName());
+			return true;
+		}
+		catch (Exception e) { throw new CourierException(e); }
 	}
 
 	public byte[] getFileContents(File file) throws CourierException 
 	{
-		return null;
+		try
+		{
+			String name = file.getName();
+			getHandler().downloadFile(name, name);
+			File local = new File(_localDir,name);
+			byte[] ba = CourierUtil.bytesFromLocalFile(local);
+			local.delete();
+			return ba;
+		}
+		catch (Exception e) { throw new CourierException(e); }
 	}
 
+	//  FileCourier will try to invoke the uploadFile(File) method using reflection
+	//  FileHandlers that don't implement it (e.g. LocalFileHandler) will not upload
+	public void uploadFile(File file) throws CourierException 
+	{
+		try
+		{
+			String name = file.getName();
+			getHandler().uploadFile(file, name);
+			file.delete();
+		}
+		catch (Exception e) { throw new CourierException(e); }
+	}
+
 	public File[] getFileList() throws CourierException 
 	{
-		return null;
+		String[] names = null;
+		
+		try { names = getHandler().getFileListFromRemoteDir(_epr.getInputSuffix()); }
+		catch (Exception e)	{ throw new CourierException(e); }
+		
+		if (null==names)
+			return null;
+		File[] files = new File[names.length];
+		int i = 0;
+		for (String file : names)
+			if (null!=file)
+				files[i++] = new File(file);
+		return files;
 	}
 
 	public boolean renameFile(File from, File to) throws CourierException 
 	{
-		return false;
+		try { getHandler().remoteRename(from, to); }
+		catch (Exception e) {throw new CourierException(e); }
+		return true;
 	}
 
-	public boolean writeToFile(File file, Serializable what) throws CourierException 
+	// Because ftp connections are volatile, we are always getting a fresh handle
+	//TODO  there's room for optimization here - not for GA though
+	//TODO  check with Bruno - why is the getInstance() not needed ?  is it OK ?
+	protected RemoteFileSystem getHandler() throws CourierException
 	{
-		return false;
+		try { return RemoteFileSystemFactory.getRemoteFileSystem(_tree,true); }
+		catch (RemoteFileSystemException e) {throw new CourierException(e); }
 	}
+	
+	// this method is only needed until there's an appropriate constructor that takes an EPR
+	// see TODO in constructor
+	private void configTreeFromEpr() throws CourierException
+	{
+		try
+		{
+			_tree = new ConfigTree("mockFromEPR");
+			_tree.setAttribute(RemoteFileSystem.PARMS_FTP_SERVER	,_server);
+			_tree.setAttribute(RemoteFileSystem.PARMS_USER			,_user);
+			_tree.setAttribute(RemoteFileSystem.PARMS_PASSWD		,_passwd);
+			_tree.setAttribute(RemoteFileSystem.PARMS_REMOTE_DIR	,_remoteDir);
+			_tree.setAttribute(RemoteFileSystem.PARMS_PORT			,Integer.toString(_port));
+			_tree.setAttribute(RemoteFileSystem.PARMS_LOCAL_DIR		,_localDir);
+			_tree.setAttribute(RemoteFileSystem.PARMS_ASCII			,Boolean.toString(_isAscii));
+			_tree.setAttribute(RemoteFileSystem.PARMS_PASSIVE		,Boolean.toString(_isPassive));
+		}
+		catch (Exception e) { throw new CourierException(e); }
+	}
 
-	FileEpr _epr;
+	protected FTPEpr 	 _epr;
+	protected ConfigTree _tree;
+	
+	protected String	_server		,_user			,_passwd		,_remoteDir	,_localDir;
+	protected int		_port;
+	protected boolean	_isAscii	,_isPassive;
 }

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-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -86,5 +86,5 @@
       } //______________________________
     } //____________________________________________________
    
-	FileEpr _epr;
+	protected FileEpr _epr;
 }

Modified: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/eprs/FTPEpr.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/eprs/FTPEpr.java	2006-12-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/addressing/eprs/FTPEpr.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -173,7 +173,7 @@
 	
 	public final boolean getPassive () throws URISyntaxException
 	{
-		return getAddr().getExtensionValue(PASSIVE_TAG).equals("true");
+		return "true".equals(getAddr().getExtensionValue(PASSIVE_TAG));
 	}
 	
 	

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-08 16:35:07 UTC (rev 8147)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/couriers/CourierUtil.java	2006-12-08 17:48:52 UTC (rev 8148)
@@ -22,6 +22,7 @@
 
 package org.jboss.soa.esb.couriers;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -48,10 +49,7 @@
 {
 	public static final String CORRELATION_ID_TAG = "messageCorrelationId";
 	
-	private CourierUtil()
-	{
-	}
-
+	private CourierUtil() {}
 	
     public static List<KeyValuePair> propertiesFromSelector(String selector) throws Exception 
     {
@@ -143,4 +141,26 @@
 		courier.deliver(message);
 	}
 
+	public static byte[] bytesFromLocalFile(File from)
+		throws IOException
+	{
+		ObjectInputStream in = new ObjectInputStream(new FileInputStream(from));
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		byte[] buff = new byte[1000];
+		int iQ = 0;
+		while ((iQ=in.read(buff))>=0)
+			if (iQ>0)
+				out.write(buff,0,iQ);
+		in.close();
+		out.close();
+		return out.toByteArray();
+	}
+	
+	public static void bytesToLocalFile(byte[] bytes, File to)
+		throws IOException
+	{
+		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(to));
+		out.write(bytes);
+		out.close();
+	}
 }




More information about the jboss-svn-commits mailing list