[jboss-svn-commits] JBL Code SVN: r6450 - in labs/jbossesb/trunk/product/core/listeners: . src/org/jboss/soa/esb/actions src/org/jboss/soa/esb/listeners

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Sep 27 16:55:55 EDT 2006


Author: estebanschifman
Date: 2006-09-27 16:55:49 -0400 (Wed, 27 Sep 2006)
New Revision: 6450

Added:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpClientUtil.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpDownloader.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpUploader.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/RemoteDirectoryPoller.java
Modified:
   labs/jbossesb/trunk/product/core/listeners/
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/GpListener.java
Log:
Add bin folder to ignore list
New Ftp classes
Changes in GpListener and Directory poller needed for new classes


Property changes on: labs/jbossesb/trunk/product/core/listeners
___________________________________________________________________
Name: svn:ignore
   + bin


Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpClientUtil.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpClientUtil.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpClientUtil.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -0,0 +1,294 @@
+/*
+* 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.actions;
+
+import com.enterprisedt.net.ftp.*;
+
+import java.io.*;
+import java.util.*;
+
+import org.jboss.soa.esb.helpers.*;
+
+/**
+ * Simplified FTP transfers
+ * <p>Description:  Implements a simple set of FTP functionality
+ * Parameters to establish the FTP connection are provided at construction time
+ * and cannot change during the lifetime of the object
+ * <br/>Hides low level details.  Current implementation is based on the
+ * "Entreprise Distributed Technology edtFTPj" library
+ * but this can be changed with no impact to existing code, just by changing
+ * this class without modifying the signature of it's public methods
+ * </p>
+ */
+
+public class FtpClientUtil
+{
+  public  static final String PARMS_FTP_SERVER = "ftpServer";
+  public  static final String PARMS_USER    = "ftpUser";
+  public  static final String PARMS_PASSWD  = "ftpPassword";
+  public  static final String PARMS_PORT    = "ftpPort";
+  public  static final String PARMS_REMOTE_DIR = "ftpRemoteDir";
+  public  static final String PARMS_LOCAL_DIR  = "ftpLocalDir";
+  public  static final String PARMS_ASCII      = "ftpAscii";
+  public  static final String PARMS_PASSIVE    = "ftpPassive";
+  
+  private static final String TMP_SUFFIX	= ".rosettaPart";
+
+  public enum XFER_TYPE
+  {ascii
+  ,binary
+  };
+
+  private DomElement	m_oParms;
+  private String    	m_sFtpServer  ,m_sUser        ,m_sPasswd;
+  private String    	m_sRemoteDir  ,m_sLocalDir;
+  private int			m_iPort;
+  private boolean		m_bPassive;
+  public  String getRemoteDir()  { return m_sRemoteDir; }
+
+  private FTPClient  	m_oConn = new FTPClient();
+  private FTPTransferType m_oXferType = FTPTransferType.BINARY;
+
+  /**
+   * Checks validity and completeness of parameters, and keeps the info internally
+   * for subsequent FTP requests
+   * @param p_oP DomElement
+   * @throws Exception : if parameters are invalid or incomplete
+   * <li>Parameters: (XML attributes at the root level) </li>
+   * <li> ftpServer = name or IP of FTP server </li>
+   * <li> ftpUser = login ID for server </li>
+   * <li> ftpPassword </li>
+   * <li> localDirURI = absolute path in the local filesystem </li>
+   * <li> remoteDirURI = remote path is relative to ftp user home in remote
+   * computer </li>
+   */
+
+  public FtpClientUtil (DomElement p_oP, boolean p_bConnect) throws Exception
+   { m_oParms = p_oP;
+   	 initialize(p_bConnect);
+   } //_________________________________
+
+  public FtpClientUtil (List<KeyValuePair> attribs, boolean connect) throws Exception
+  {
+	  m_oParms = new DomElement("fromProps");
+	  for (KeyValuePair oCurr : attribs)
+		m_oParms.setAttr(oCurr.getKey(),oCurr.getValue());
+	  initialize(connect);
+  } //__________________________________
+  
+  private void initialize(boolean bConnect) throws Exception
+  {
+	     checkParms();
+	     if (bConnect)
+	     {
+		     m_oConn.setRemoteHost	(m_sFtpServer);
+		     m_oConn.setRemotePort(m_iPort);
+		     m_oConn.connect();
+		     for (int i1=0; i1<10 && ! m_oConn.connected(); i1++)
+		    	 Thread.sleep(200);
+		     if (! m_oConn.connected())
+		    	 throw new Exception("Can't connect to FTP server");
+		     m_oConn.user	      	(m_sUser);
+		     m_oConn.password		(m_sPasswd);
+		     m_oConn.setConnectMode	((m_bPassive) ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE);
+	     }
+  } //__________________________________
+   /**
+    * Terminates ftp session and frees resources
+    * <li>Well behaved programs should make sure to call this method </li>
+    */
+   public void quit ()
+   { if (null != m_oConn)
+      try { m_oConn.quit(); }
+      catch (Exception e) {}
+   } //_________________________________
+
+   /**
+    * Deletes specified file in remote directory
+    * @param p_sFile String : filename to delete.  Method will attempt to delete
+    * file with rightmost node of argument within remote directory specified in 'remoteDirURI'
+    * @throws Exception : if ftp connection cannot be established, or file
+    * cannot be deleted in remote directory
+    */
+   public void deleteRemoteFile (String p_sFile) throws Exception
+   { m_oConn.delete(getRemoteDir()+"/"+new File(p_sFile).getName());
+   } //_________________________________
+
+   public void remoteDelete (File p_oFile) throws Exception
+   { m_oConn.delete(fileToFtpString(p_oFile));
+   } //_________________________________
+
+   /**
+    * Gets the list of files in the remote directory that end with arg0
+    * @param p_sSuffix String : retrieve only files that end with that suffix - all files if null
+    * @throws Exception : if ftp connection cannot be established, or problems encountered
+    */
+   public String[] getFileListFromRemoteDir (String p_sSuffix) throws Exception
+   { 
+	   String sSuffix = (null==p_sSuffix)?"*":"*"+p_sSuffix;
+	   return m_oConn.dir(sSuffix);
+   } //_________________________________
+
+   /**
+    * Change remote directory
+    * @param p_sDir String : directory to set 
+    * @throws Exception : if ftp connection cannot be established, or problems encountered
+    */
+   public void setRemoteDir (String p_sDir) throws Exception
+   {
+	   m_oConn.chdir(p_sDir);
+   } //_________________________________
+
+   /**
+    * Renames specified file in remote directory to specified new name
+    * @param p_sFrom String : filename to rename
+    * @param p_sTo String : new filename
+    * @throws Exception : if ftp connection cannot be established, or file
+    * cannot be renamed to new name in remote directory
+    * <li>Method will attempt to rename file with rightmost node of argument
+    * within remote directory specified in 'remoteDirURI', to new name inside
+    * the SAME remote directory
+    */
+   public void renameInRemoteDir (String p_sFrom, String p_sTo) throws Exception
+   { 
+	 String sRmtFrom = new File(p_sFrom).getName();
+     String sRmtTo   = new File(p_sTo).getName();
+
+     try { m_oConn.rename   (getRemoteDir()+"/"+sRmtFrom,getRemoteDir()+"/"+sRmtTo); }
+     catch (Exception e)
+     {	String sMess = this.getClass().getSimpleName()
+    	 +" can't rename in remote directory <"
+    	 +e.getMessage()+">"
+    	 ;
+    	 throw new Exception(sMess);
+     }
+   } //_________________________________
+
+   public void remoteRename(File p_oFrom, File p_oTo) throws Exception
+   { 
+     try { m_oConn.rename   (fileToFtpString(p_oFrom),fileToFtpString(p_oTo)); }
+     catch (Exception e)
+     {	String sMess = this.getClass().getSimpleName()
+    	 +" can't rename in remote directory <"
+    	 +e.getMessage()+">"
+    	 ;
+    	 throw new Exception(sMess);
+     }
+   } //_________________________________
+
+   /**
+    * Uploads specified file from local directory (localDirURI) to remote
+    * directory (remoteDirURI)
+    * @param p_sFile String : filename to upload
+    * @throws Exception : if ftp connection cannot be established, or file
+    * cannot be uploaded
+    * <li> local file will be renamed during transfer ('.xferNotReady' appended
+    * to name)</li>
+    * <li> upon successful completion. the suffix '.xferDone' will be appended
+    * to the original filename </li>
+    */
+   public void uploadFile (File p_oFile, String p_sRemoteName) throws Exception
+   { 
+	 String sRemoteOK	= getRemoteDir() + "/" + p_sRemoteName;
+     String sRemoteTmp	= sRemoteOK+TMP_SUFFIX;
+     m_oConn.setType(m_oXferType);
+     m_oConn.put(fileToFtpString(p_oFile),sRemoteTmp);
+     m_oConn.rename(sRemoteTmp,sRemoteOK);
+   } //_________________________________
+
+   /**
+    * Downloads specified file from remote directory (remoteDirURI) to local
+    * directory (localDirURI)
+    * @param p_sFile String : filename to download
+    * @throws Exception : if ftp connection cannot be established, or file
+    * cannot be downloaded
+    * <li> local file is assigned a temporary name during transfer  </li>
+    * <li> upon successful completion, local temporary file will be renamed to
+    * name specified in argument, and suffix '.xferDone' will be appended
+    * to the original filename in the remote directory </li>
+    */
+   public void downloadFile (String p_sFile, String p_sFinalName)  throws Exception
+   { 
+	 File oLocalDir = new File(m_sLocalDir);
+     File oLclFile= File.createTempFile("Rosetta_",TMP_SUFFIX,oLocalDir);
+
+     try { oLclFile.delete(); }
+     catch (Exception e) {}
+     m_oConn.setType(m_oXferType);
+     m_oConn.get(fileToFtpString(oLclFile),p_sFile);
+
+     File oNew = new File(oLocalDir,p_sFinalName);
+     if (oNew.exists()) oNew.delete();
+     oLclFile.renameTo(oNew);
+   } //_________________________________
+
+   // Beware !!!  The logic here seems wrong, but it works
+   //  It appears that there's some kind of bug in the edtftpj.jar library
+   //  The !XFER_TYPE.ascii.equals(p_oMode) should NOT be negated
+   //               But it works when negated (newlines from Unix to DOS)
+   private void setXferType (XFER_TYPE p_oMode)
+   {	m_oXferType = !XFER_TYPE.ascii.equals(p_oMode)
+	   		? FTPTransferType.ASCII : FTPTransferType.BINARY;
+   } //_________________________________
+
+   private void checkParms() throws Exception
+   {
+     m_sFtpServer  = m_oParms.getAttr(PARMS_FTP_SERVER);
+     if (null==m_sFtpServer) throw new Exception ("No FTP server specified");
+
+     m_sUser       = m_oParms.getAttr(PARMS_USER);
+     if (null==m_sUser) throw new Exception ("No username specified for FTP");
+
+     m_sPasswd     = m_oParms.getAttr(PARMS_PASSWD);
+     if (null==m_sPasswd) throw new Exception ("No password specified for FTP");
+
+     m_sRemoteDir  = m_oParms.getAttr(PARMS_REMOTE_DIR);
+     if (null==m_sRemoteDir)
+    	 m_sRemoteDir = "";
+     
+     m_sLocalDir  = m_oParms.getAttr(PARMS_LOCAL_DIR);
+     if (null==m_sLocalDir)
+    	 m_sLocalDir = ".";
+     
+     String sAux	= m_oParms.getAttr(PARMS_PORT);
+     m_iPort = (null==sAux) ? 21 : Integer.parseInt(sAux);
+
+     boolean bAscii = false;
+     sAux = m_oParms.getAttr(PARMS_ASCII);
+     if (null!=sAux)
+    	 bAscii = Boolean.parseBoolean(sAux);
+     setXferType((bAscii)?XFER_TYPE.ascii:XFER_TYPE.binary);
+     
+     m_bPassive = false;
+     sAux = m_oParms.getAttr(PARMS_PASSIVE);
+     m_bPassive = (null!=sAux) && Boolean.parseBoolean(sAux);
+     
+     return;
+   } //__________________________________
+
+   public static String fileToFtpString(File p_oF)
+	{
+		return(null==p_oF) ? null
+			: p_oF.toString().replace("\\","/");		
+    } //________________________________
+	
+} //____________________________________________________________________________

Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpDownloader.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpDownloader.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpDownloader.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -0,0 +1,111 @@
+/*
+* 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.actions;
+
+import java.io.*;
+import java.util.List;
+
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.DomElement;
+import org.jboss.soa.esb.helpers.KeyValuePair;
+import org.jboss.soa.esb.listeners.GpListener;
+import org.jboss.soa.esb.listeners.RemoteDirectoryPoller;
+import org.apache.log4j.*;
+
+public class FtpDownloader extends AbstractFileAction 
+{
+	DomElement		_parms;
+	FtpClientUtil 	_ftpClient;
+	String			_localDir;
+	RemoteDirectoryPoller.WorkingFile _workFile;
+	Logger			_logger = Logger.getLogger(this.getClass());
+	
+    /**
+     * Public constructor.
+     * @param actionName Action name.
+     * @param properties Action Properties.
+     * @throws ConfigurationException Action not properly configured.
+     */
+	public FtpDownloader(String actionName, List<KeyValuePair> properties)
+	{	_parms	= new DomElement("fromProps");
+	  	for (KeyValuePair oCurr : properties)
+	  		_parms.setAttr(oCurr.getKey(),oCurr.getValue());
+	} //________________________________
+
+	@Override
+	public Object process(File obj) throws ActionProcessingException
+	{
+        try { _workFile = (RemoteDirectoryPoller.WorkingFile)obj; }
+        catch(ClassCastException e) 
+        {
+        	throw new ActionProcessingException
+        		("Argument must be an instance of "+ _workFile.getClass().getName(),e);
+        }
+
+        try 
+		{
+			_ftpClient = new FtpClientUtil(_parms,true);
+			_localDir = _parms.getAttr(FtpClientUtil.PARMS_LOCAL_DIR);
+			String sFrom = FtpClientUtil.fileToFtpString(_workFile);
+			_ftpClient.downloadFile(sFrom,_workFile.getInputFile().getName());
+
+			if (_workFile.isPostDelete())
+				_ftpClient.remoteDelete(_workFile);
+			else
+				_workFile.remoteRenameToOutput(_ftpClient);
+			
+			GpListener.notifyOK(_parms,getOkNotification(_workFile));
+			return _workFile;
+		}
+        catch (Exception e)
+        {
+			GpListener.notifyError(_parms,e,getErrorNotification(_workFile));
+			try { _workFile.remoteRenameToInput(_ftpClient); }
+			catch (Exception e2) { /* not much we can do if can't rename back to input */}
+        	throw new ActionProcessingException(e);
+		}
+		finally 
+		{	
+			if (null!=_ftpClient)
+				_ftpClient.quit();
+			_ftpClient = null;
+		}
+	} //________________________________
+
+	public Serializable getOkNotification(Object obj) 
+	{
+		return new StringBuilder()
+		.append(_workFile.getInputFile().toString())
+		.append("successfully downloaded to ").append(_localDir)
+		.toString();
+	} //________________________________
+	
+	public Serializable getErrorNotification(Object obj) 
+	{
+		return new StringBuilder()
+		.append("Failed to download ").append(_workFile.getInputFile().toString())
+		.append(" to ").append(_localDir)
+		.toString();
+	} //________________________________
+	
+} //____________________________________________________________________________

Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpUploader.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpUploader.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/actions/FtpUploader.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -0,0 +1,110 @@
+/*
+* 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.actions;
+
+import java.io.*;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.helpers.*;
+import org.jboss.soa.esb.listeners.*;
+
+public class FtpUploader extends AbstractFileAction 
+{
+	DomElement		_parms;
+	FtpClientUtil 	_ftpClient;
+	String			_remoteDir;
+	DirectoryPoller.WorkingFile _workFile;
+	Logger			_logger = Logger.getLogger(this.getClass());
+
+	/**
+     * Public constructor.
+     * @param actionName String - Action name.
+     * @param properties List<KeyValuePair> - Action Properties.
+     * @throws ConfigurationException Action not properly configured.
+     */
+	public FtpUploader(String actionName, List<KeyValuePair> properties)
+	{	_parms	= new DomElement("fromProps");
+	  	for (KeyValuePair oCurr : properties)
+	  		_parms.setAttr(oCurr.getKey(),oCurr.getValue());
+	} //________________________________
+
+	@Override
+	public Object process(File obj) throws ActionProcessingException
+	{
+        try { _workFile = (DirectoryPoller.WorkingFile)obj; }
+        catch(ClassCastException e) 
+        {
+        	throw new ActionProcessingException
+        		("Argument must be an instance of "+ _workFile.getClass().getName(),e);
+        }
+
+        try 
+		{
+        	System.out.println("FTP uploading "+obj);
+			_ftpClient = new FtpClientUtil(_parms,true);
+			_remoteDir = _parms.getAttr(FtpClientUtil.PARMS_REMOTE_DIR);
+			_ftpClient.uploadFile(_workFile,_workFile.getInputFile().getName());
+
+			if (_workFile.isPostDelete())
+				_workFile.delete();
+			else
+				_workFile.renameToOutputFile();
+			
+			GpListener.notifyOK(_parms,getOkNotification(_workFile));
+			return _workFile;
+		}
+        catch (Exception e)
+        {
+			GpListener.notifyError(_parms,e,getErrorNotification(_workFile));
+			_workFile.renameToError();
+        	throw new ActionProcessingException(e);
+		}
+		finally 
+		{	
+			if (null!=_ftpClient)
+				_ftpClient.quit();
+			_ftpClient = null;
+		}
+	} //________________________________
+
+	public Serializable getOkNotification(Object obj) 
+	{
+		return new StringBuilder()
+		.append(_workFile.getInputFile().toString())
+		.append("successfully uploaded to ").append(_remoteDir)
+		.append(" on ").append(_parms.getAttr(FtpClientUtil.PARMS_FTP_SERVER))
+		.toString();
+	} //________________________________
+	
+	public Serializable getErrorNotification(Object obj) 
+	{
+		return new StringBuilder()
+		.append("Failed to upload ").append(_workFile.getInputFile().toString())
+		.append(" to ").append(_remoteDir)
+		.append(" on ").append(_parms.getAttr(FtpClientUtil.PARMS_FTP_SERVER))
+		.toString();
+	} //________________________________
+	
+} //____________________________________________________________________________

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -228,6 +228,7 @@
         private static final long serialVersionUID = 1L;
 
         private boolean postDelete;
+        public boolean isPostDelete() { return postDelete; }
 
         private File inputFile, errorFile, outputFile;
 
@@ -239,11 +240,11 @@
             super(parentFile, filename);
         }
 
-        private boolean renameToError() {
+        public boolean renameToError() {
             return renameTo(errorFile);
         }
 
-        private boolean renameToOutputFile() {
+        public boolean renameToOutputFile() {
             return renameTo(outputFile);
         }
         

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/GpListener.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/GpListener.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/GpListener.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -92,7 +92,6 @@
 	;
 
 	public static final String PARM_RELOAD_SECS = "parameterReloadSecs";
-
 	public static final String PARM_END_TIME = "endTime";
 
 	// Attribute name that denotes listener class to be instantiated in a child
@@ -100,15 +99,11 @@
 	// This attribute is not in the root node but in first level child
 	// DomElements
 	public static final String PARM_LISTENER_CLASS = "listenerClass";
-
 	public static final String PARM_ACTIONS = "actions";
-
 	public static final String PARM_MAX_THREADS = "maxThreads";
-
 	public static final String CHLD_EMAIL_PARMS = "EmailProperties";
 
 	private String m_sParmsName;
-
 	private DomElement m_oParms;
 
 	private HashMap<String, Object> m_oAtts;
@@ -248,17 +243,18 @@
 		commandQueue = createCommandQueue(p_oP);
 
 		// Open the command queue...
-		commandQueue.open(p_oP);
+		if (null!=commandQueue)
+			commandQueue.open(p_oP);
 
 		// if PARM_RELOAD_SECS not set, and no command queue
 		// then reload every 10 minutes
 		// If there is a command queue, run until command is received
 		String sRldSecs = p_oP.getAttr(PARM_RELOAD_SECS);
-		m_lNextReload = (null != sRldSecs) ? System.currentTimeMillis() + 1000
-				* Long.parseLong(sRldSecs)
-				: (null == commandQueue) ? Long.MAX_VALUE : System
-						.currentTimeMillis()
-						+ m_iDfltReloadMillis;
+		m_lNextReload = (null != sRldSecs) 
+				? System.currentTimeMillis() + 1000 * Long.parseLong(sRldSecs)
+				: (null == commandQueue) 
+						? Long.MAX_VALUE 
+						: System.currentTimeMillis() + m_iDfltReloadMillis;
 
 		// if PARM_END_TIME not set try to run forever
 		// not a good practice if command queue is not set
@@ -288,12 +284,14 @@
 			try {
 				return (CommandQueue) Class.forName(commandQueueClass).newInstance();
 			} catch (Exception e) {
-				m_oLogger.error("Failed to instantiate CommandQueue ["+ commandQueueClass + "].  Defaulting to the JMS Command Queue", e);
+				m_oLogger.error("Failed to instantiate CommandQueue ["+ commandQueueClass + "].  Defaulting to no Command Queue", e);
 			}
 		}
+			
+		return null;
 		
 		// Default command queue...
-		return new JmsCommandQueue();
+//		return new JmsCommandQueue();
 	}
 
 	/**

Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/RemoteDirectoryPoller.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/RemoteDirectoryPoller.java	2006-09-27 20:53:40 UTC (rev 6449)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/RemoteDirectoryPoller.java	2006-09-27 20:55:49 UTC (rev 6450)
@@ -0,0 +1,259 @@
+/*
+* 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.*;
+import java.util.*;
+
+import org.jboss.soa.esb.actions.*;
+import org.jboss.soa.esb.helpers.*;
+
+import org.apache.log4j.*;
+
+public class RemoteDirectoryPoller extends AbstractPoller
+{
+  public static final String FILE_INPUT_DIR 	= "inputDir";
+  public static final String FILE_INPUT_SFX 	= "inputSuffix";
+  public static final String FILE_WORK_SFX 		= "workSuffix";
+  public static final String FILE_ERROR_DIR   	= "errorDir";
+  public static final String FILE_ERROR_SFX   	= "errorSuffix";
+  public static final String FILE_POST_DIR  	= "postDir";
+  public static final String FILE_POST_SFX  	= "postSuffix";
+  public static final String FILE_POST_DEL  	= "postDelete";
+  
+  private DomElement _params;
+  private Logger	_logger = Logger.getLogger(this.getClass());
+  FtpClientUtil		_ftpClient;
+
+  public RemoteDirectoryPoller(GpListener p_oDad, DomElement p_oParms,ActionDefinitionFactory actionDF)
+  	throws Exception
+  {
+	super(p_oDad,p_oParms,actionDF);
+	_params	= p_oParms;
+	checkMyParms();
+  } //__________________________________
+
+
+    protected File 			m_oInpDir	,m_oErrorDir	,m_oPostDir;
+    protected String 		m_sInpSfx	,m_sWrkSfx		,m_sErrSfx	,m_sPostSfx;
+    protected boolean		m_bPostDel;
+
+    /**
+     * 
+     * @param p_o Object - Must be a File representing the file that has to be processed
+     * @return Object - an instance of the internal WorkingFile class
+     */
+	@Override
+	public Object preProcess(Object p_o) 
+	{
+		if (!(p_o instanceof File))
+			return null;
+		File oF = (File)p_o;
+		WorkingFile oCurr =  new WorkingFile(oF,m_sWrkSfx,m_bPostDel);		
+		oCurr.errorFile		= new File (m_oErrorDir	,oF.getName()+m_sErrSfx);
+		oCurr.outputFile	= new File (m_oPostDir	,oF.getName()+m_sPostSfx);
+
+		try
+		{
+			_ftpClient	= new FtpClientUtil(_params,true);
+			_ftpClient.remoteRename(oF,oCurr);
+		}
+		catch (Exception e)
+		{
+			_logger.error("Can't FTP rename",e);
+			return null;
+		}
+		finally 
+		{
+			if (null!=_ftpClient)
+				_ftpClient.quit();
+			_ftpClient = null;
+		}
+
+		return oCurr;
+	} //________________________________
+
+	@Override
+	protected List<Object> pollForCandidates()
+	{
+		List<Object> oRet = new ArrayList<Object>();
+		FtpClientUtil _ftpClient = null;
+		try
+		{	
+			_ftpClient = new FtpClientUtil(_params,true);
+			_ftpClient.setRemoteDir(FtpClientUtil.fileToFtpString(m_oInpDir));
+			String[] sa = _ftpClient.getFileListFromRemoteDir(m_sInpSfx);
+			if (null!=sa)
+				for (String sCurr : sa)
+					oRet.add(new File(m_oInpDir,sCurr));
+		}
+		catch (Exception e)
+		{
+			_logger.error("Problems with FTP",e);
+		}
+		finally
+		{
+			if (null!=_ftpClient)
+				_ftpClient.quit();
+			_ftpClient = null;
+		}
+		return oRet;
+		
+	} //________________________________
+
+	protected void checkMyParms() throws Exception
+    { 
+	//  INPUT directory and suffix  (used for FileFilter)
+	  String sInpDir = GpListener.obtainAtt(_params,FILE_INPUT_DIR,null);
+      m_oInpDir = new File(sInpDir);
+
+      m_sInpSfx  = GpListener.obtainAtt(_params,FILE_INPUT_SFX,null);
+      m_sInpSfx  = m_sInpSfx.trim();
+      if (m_sInpSfx.length()<1)
+    	  throw new Exception ("Invalid "+FILE_INPUT_SFX+" attribute");
+
+	//  WORK suffix (will rename in input directory)
+      m_sWrkSfx	= GpListener.obtainAtt(_params,FILE_WORK_SFX,".esbWork").trim();
+      if (m_sWrkSfx.length()<1)
+    	  throw new Exception ("Invalid "+FILE_WORK_SFX+" attribute");
+      if (m_sInpSfx.equals(m_sWrkSfx))
+    	  throw new Exception("Work suffix must differ from input suffix <"+m_sWrkSfx+">");
+
+    //    ERROR directory and suffix (defaults to input dir and ".esbError" suffix)
+      String sErrDir = GpListener.obtainAtt(_params,FILE_ERROR_DIR,sInpDir);
+      m_oErrorDir = new File(sErrDir);
+
+      m_sErrSfx  = GpListener.obtainAtt(_params,FILE_ERROR_SFX,".esbError").trim();
+      if (m_sErrSfx.length()<1)
+    	  throw new Exception ("Invalid "+FILE_ERROR_SFX+" attribute");
+      if (m_oErrorDir.equals(m_oInpDir) && m_sInpSfx.equals(m_sErrSfx))
+    	  throw new Exception("Error suffix must differ from input suffix <"+m_sErrSfx+">");
+
+
+   //    Do users wish to delete files that were processed OK ?
+      String sPostDel = GpListener.obtainAtt(_params,FILE_POST_DEL,"false").trim();
+      m_bPostDel = Boolean.parseBoolean(sPostDel);
+      if (m_bPostDel)
+    	  return;
+
+    //    POST (done) directory and suffix (defaults to input dir and ".esbDone" suffix)
+      String sPostDir = GpListener.obtainAtt(_params,FILE_POST_DIR,sInpDir);
+      m_oPostDir = new File(sPostDir);
+      m_sPostSfx  = GpListener.obtainAtt(_params,FILE_POST_SFX,".esbDone").trim();
+      if (m_oPostDir.equals(m_oInpDir))
+      {	if (m_sPostSfx.length()<1)
+    	  throw new Exception ("Invalid "+FILE_POST_SFX+" attribute");
+      	if (m_sPostSfx.equals(m_sInpSfx))
+    	  throw new Exception("Post process suffix must differ from input suffix <"+m_sPostSfx+">");
+      }
+      
+      
+      FtpClientUtil _ftpClient = new FtpClientUtil(_params,false);
+      _ftpClient.quit();
+
+    } //________________________________
+	
+
+    /* (non-Javadoc)
+     * @see org.jboss.soa.esb.listeners.AbstractListener#close()
+     */
+    @Override
+    protected void close() { }
+
+    /* (non-Javadoc)
+     * @see org.jboss.soa.esb.listeners.AbstractListener#processingError(java.lang.Object, org.jboss.soa.esb.actions.ActionProcessor, java.lang.Throwable)
+     */
+    @Override
+    protected void processingError(Object initialMessage, ActionProcessor processor, Throwable error) 
+    {
+        @SuppressWarnings("unused") 
+        WorkingFile workingFile = (WorkingFile) initialMessage;
+    }
+
+    /* (non-Javadoc)
+     * @see org.jboss.soa.esb.listeners.AbstractListener#processingComplete(java.lang.Object)
+     */
+    @Override
+    protected void processingComplete(Object initialMessage) 
+    {
+        @SuppressWarnings("unused") 
+        WorkingFile workingFile = (WorkingFile) initialMessage;
+    }
+
+    /**
+     * Working file.
+     * <p/>
+     * Once the remote directory poller picks up on an input file, it immediately tries to rename
+     *  it to a working file in order to avoid a situation where the file gets processed again.
+     * 
+     * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+     * @since Version 4.0
+     */
+    public static class WorkingFile extends File 
+    {
+        private static final long serialVersionUID = 1L;
+
+        private boolean postDelete;
+        private File inputFile, errorFile, outputFile;
+
+        private WorkingFile(File pFile,String pWrkSfx, boolean pPostDelete) 
+        {
+        	super(pFile.getParentFile(), pFile.getName() + pWrkSfx);
+        	inputFile	= pFile;
+        }
+        /**
+         * Get the File instance representing the original input file.
+         * @return Original input file.
+         */
+        public File getInputFile() { return inputFile; }
+	/**
+	 * is this working file to be deleted after successful processing ? 
+	 * @return boolean - true if this file can be deleted
+	 */
+        public boolean isPostDelete() { return postDelete; }
+
+        public boolean localRenameToInput() { return super.renameTo(inputFile); }
+        public void remoteRenameToInput(FtpClientUtil util) throws Exception 
+        {
+            	util.remoteRename(this,inputFile);
+        }
+
+        public boolean localRenameToError() { return super.renameTo(errorFile); }
+        public void remoteRenameToError(FtpClientUtil util) throws Exception 
+        {
+            	util.remoteRename(this,errorFile);
+        }
+
+        public boolean localRenameToOutput() { return renameTo(outputFile); }
+        public void remoteRenameToOutput(FtpClientUtil util) throws Exception
+        {
+        	util.remoteRename(this,outputFile);
+        }
+        
+        public boolean localDelete() 	{return delete(); }
+        public void	   remoteDelete(FtpClientUtil util) throws Exception
+        	{util.deleteRemoteFile(this.toString()); }
+        
+    }
+} //____________________________________________________________________________




More information about the jboss-svn-commits mailing list