[jboss-svn-commits] JBL Code SVN: r8046 - in labs/jbossesb/workspace/b_georges: product/core/listeners/src/org/jboss/soa/esb/listeners/gateway qa/build
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Dec 4 08:22:51 EST 2006
Author: b_georges
Date: 2006-12-04 08:22:43 -0500 (Mon, 04 Dec 2006)
New Revision: 8046
Added:
labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java
Removed:
labs/jbossesb/workspace/b_georges/qa/build/junit/
Modified:
labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListener.java
Log:
Added Esteban input
Added: labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
===================================================================
--- labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2006-12-04 13:13:06 UTC (rev 8045)
+++ labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2006-12-04 13:22:43 UTC (rev 8046)
@@ -0,0 +1,355 @@
+/*
+ * 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 java.io.File;
+import java.io.FileFilter;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.Collection;
+
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.addressing.EPR;
+import org.jboss.soa.esb.couriers.Courier;
+import org.jboss.soa.esb.couriers.CourierException;
+import org.jboss.soa.esb.couriers.CourierFactory;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+import org.jboss.soa.esb.services.registry.RegistryException;
+import org.jboss.soa.esb.util.Util;
+
+/**
+ * Base class for all file gateways: local filesystem, ftp, sftp and ftps.
+ * <p/>Implementations for file manipulation (getFileList, getFileContents, renameFile and deleteFile)
+ * must be provided by factory
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * @since Version 4.0
+ *
+ */
+public abstract class AbstractFileGateway implements Runnable
+{
+ abstract File[] getFileList(String suffix) throws GatewayException;
+ abstract byte[] getFileContents(File file) throws GatewayException;
+ abstract boolean renameFile(File from, File to) throws GatewayException;
+ abstract boolean deleteFile(File file) throws GatewayException;
+ abstract void seeIfOkToWorkOnDir (File p_oDir) throws GatewayException;
+ abstract void getDefaultComposer () throws GatewayException;
+
+ protected AbstractFileGateway() {}
+ protected AbstractFileGateway(GatewayListenerController commandListener, ConfigTree config)
+ throws Exception
+ {
+ _config = config;
+ _controller = commandListener;
+ _sleepBetweenPolls = 10000; // milliseconds
+ checkMyParms();
+ } // __________________________________
+
+ public void run()
+ {
+ _logger.debug("run() method of "+this.getClass().getSimpleName()
+ +" started on thread "+Thread.currentThread().getName());
+ if (null!=_serviceName)
+ try { _controller.register(_config,_myEpr); }
+ catch (RegistryException e1)
+ { _logger.warn("unable to register service",e1); }
+
+ boolean bSleep = false;
+ while (_controller.continueLooping())
+ {
+ // only sleep in between - not the first time
+ if (bSleep)
+ { long lUntil = System.currentTimeMillis()+_sleepBetweenPolls;
+ while (System.currentTimeMillis() < lUntil)
+ {
+ try { Thread.sleep(1000); }
+ catch (InterruptedException e) { lUntil = 0; }
+ if (! _controller.continueLooping())
+ break;
+ }
+ }
+ else
+ bSleep = true;
+ if (! _controller.continueLooping())
+ break;
+
+ File[] fileList;
+ try {fileList = getFileList(_inputSuffix); }
+ catch (GatewayException e)
+ {
+ _logger.error("Can't retrieve file list",e);
+ continue;
+ }
+
+ for (File fileIn : fileList)
+ {
+ // Try to rename - if unsuccessful, somebody else got it first
+ File fileWork = new File(fileIn.getParent(),fileIn.getName()+_workingSuffix);
+ try
+ {
+ if (! renameFile(fileIn,fileWork))
+ continue;
+ }
+ catch (GatewayException e)
+ {
+ _logger.error("Problems renaming file "+fileIn+" to "+fileWork);
+ continue;
+ }
+
+
+ Throwable thrown = null;
+ String text = null;
+ try
+ {
+ Object obj = _processMethod.invoke(_composer,new Object[] {fileWork} );
+ if (null==obj)
+ {
+ _logger.warn("Action class method <"+_processMethod.getName()+"> returned a null object");
+ continue;
+ }
+ boolean bSent = false;
+ for (EPR current : _targetEprs)
+ {
+ _courier = CourierFactory.getCourier(current);
+ if (_courier.deliver((org.jboss.soa.esb.message.Message)obj))
+ {
+ bSent = true;
+ break;
+ }
+ }
+ if (! bSent)
+ {
+ text = "Target service <"+_targetServiceCategory+","+_targetServiceName+"> is not registered";
+ thrown = new Exception(text);
+ }
+ }
+
+ catch (InvocationTargetException e)
+ {
+ thrown = e;
+ text = "Problems invoking method <"+_processMethod.getName()+">";
+
+ }
+ catch (IllegalAccessException e)
+ {
+ thrown = e;
+ text = "Problems invoking method <"+_processMethod.getName()+">";
+ }
+ catch (ClassCastException e)
+ {
+ thrown = e;
+ text = "Action class method <"+_processMethod.getName()+"> returned a non Message object";
+ }
+ catch (CourierException e)
+ {
+ thrown = e;
+ if (null!=_courier)
+ text = "Courier <"+_courier.getClass().getName()+".deliver(Message) FAILED";
+ else
+ text = "NULL courier can't deliver Message";
+ }
+
+ if (null==thrown)
+ {
+ File fileOK = new File(_postProcessDirectory,fileIn.getName()+_postProcessSuffix);
+ if (_deleteAfterOK)
+ try { deleteFile(fileWork); }
+ catch (GatewayException e)
+ {
+ _logger.error("File "+fileIn+" has been processed and renamed to "+fileWork
+ +", but there were problems deleting it from the input directory ",e);
+ }
+ else
+ try
+ {
+ deleteFile(fileOK);
+ renameFile(fileWork,fileOK);
+ }
+ catch(GatewayException e)
+ {
+ _logger.error("File "+fileIn+" has been processed and renamed to "+fileWork
+ +", but there were problems renaming it to "+fileOK,e);
+ }
+ }
+ else
+ {
+ thrown.printStackTrace();
+ _logger.error(text,thrown);
+ File fileError = new File(_errorDirectory,fileIn.getName()+_errorSuffix);
+ try
+ {
+ deleteFile(fileError);
+ renameFile(fileWork,fileError);
+ }
+ catch(GatewayException e)
+ {
+ _logger.error("Problems renaming file "+fileWork+" to "+fileError,e);
+ }
+ }
+ }
+ }
+
+ if (null!=_serviceName)
+ try { _controller.unRegister(_serviceCategory, _serviceName,_myEpr); }
+ catch (RegistryException e1){ _logger.warn("unable to unRegister service",e1); }
+
+ _logger.debug("run() method of "+this.getClass().getSimpleName()
+ +" finished on thread "+Thread.currentThread().getName());
+ } // ________________________________
+
+ /**
+ * Check for mandatory and optional attributes in parameter tree
+ *
+ * @throws Exception -
+ * if mandatory atts are not right or actionClass not in
+ * classpath
+ */
+ protected void checkMyParms() throws Exception
+ {
+ // Third arg is null - Exception will be thrown if attribute is not found
+ _targetServiceCategory = _controller.obtainAtt(_config, ListenerTagNames.TARGET_SERVICE_CATEGORY_TAG, null);
+ _targetServiceName = _controller.obtainAtt(_config, ListenerTagNames.TARGET_SERVICE_NAME_TAG, null);
+ _targetEprs = _controller.getEprs(_targetServiceCategory,_targetServiceName);
+ if (null==_targetEprs || _targetEprs.size()<1)
+ throw new ConfigurationException("EPR <"+_targetServiceName+"> not found in registry");
+
+ // Polling interval
+ String sAux = _config.getAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG);
+ if (! Util.isNullString(sAux))
+ try { _sleepBetweenPolls = 1000 * Long.parseLong(sAux); }
+ catch (NumberFormatException e)
+ { _logger.warn("Invalid poll latency - keeping default of "+(_sleepBetweenPolls/1000)); }
+ else
+ {
+ _logger.warn("No value specified for: "+ListenerTagNames.POLL_LATENCY_SECS_TAG
+ +" - Using default of "+(_sleepBetweenPolls/1000));
+ }
+
+ resolveComposerClass();
+
+ // INPUT directory and suffix (used for FileFilter)
+ String sInpDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_INPUT_DIR_TAG,null);
+ _inputDirectory = fileFromString(sInpDir);
+ seeIfOkToWorkOnDir(_inputDirectory);
+
+ _inputSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_INPUT_SFX_TAG,null);
+ _inputSuffix = _inputSuffix.trim();
+ if (_inputSuffix.length()<1)
+ throw new Exception ("Invalid "+ListenerTagNames.FILE_INPUT_SFX_TAG+" attribute");
+
+ // WORK suffix (will rename in input directory)
+ _workingSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_WORK_SFX_TAG,".esbWork").trim();
+ if (_workingSuffix.length()<1)
+ throw new Exception ("Invalid "+ListenerTagNames.FILE_WORK_SFX_TAG+" attribute");
+ if (_inputSuffix.equals(_workingSuffix))
+ throw new Exception("Work suffix must differ from input suffix <"+_workingSuffix+">");
+
+ // ERROR directory and suffix (defaults to input dir and ".esbError" suffix)
+ String sErrDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_ERROR_DIR_TAG,sInpDir);
+ _errorDirectory = fileFromString(sErrDir);
+ seeIfOkToWorkOnDir(_errorDirectory);
+
+ _errorSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_ERROR_SFX_TAG,".esbError").trim();
+ if (_errorSuffix.length()<1)
+ throw new Exception ("Invalid "+ListenerTagNames.FILE_ERROR_SFX_TAG+" attribute");
+ if (_errorDirectory.equals(_inputDirectory) && _inputSuffix.equals(_errorSuffix))
+ throw new Exception("Error suffix must differ from input suffix <"+_errorSuffix+">");
+
+
+ // Do users wish to delete files that were processed OK ?
+ String sPostDel = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_DEL_TAG,"false").trim();
+ _deleteAfterOK = Boolean.parseBoolean(sPostDel);
+ if (_deleteAfterOK)
+ return;
+
+ // POST (done) directory and suffix (defaults to input dir and ".esbDone" suffix)
+ String sPostDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_DIR_TAG,sInpDir);
+ _postProcessDirectory = fileFromString(sPostDir);
+ seeIfOkToWorkOnDir(_postProcessDirectory);
+ _postProcessSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_SFX_TAG,".esbDone").trim();
+ if (_postProcessDirectory.equals(_inputDirectory))
+ { if (_postProcessSuffix.length()<1)
+ throw new Exception ("Invalid "+ListenerTagNames.FILE_POST_SFX_TAG+" attribute");
+ if (_postProcessSuffix.equals(_inputSuffix))
+ throw new Exception("Post process suffix must differ from input suffix <"+_postProcessSuffix+">");
+ }
+ } //________________________________
+
+ protected void resolveComposerClass() throws Exception
+ {
+ // Look for first "action" element - only first one will be used
+ String tagName = ListenerTagNames.ACTION_ELEMENT_TAG;
+ ConfigTree actionElement = _config.getFirstChild(tagName);
+ String sProcessMethod = null;
+ if (null!=actionElement)
+ { // class attribute
+ _composerName = _controller.obtainAtt(actionElement,ListenerTagNames.ACTION_CLASS_TAG,null);
+ _composerClass = Class.forName(_composerName);
+ Constructor oConst = _composerClass.getConstructor(new Class[] {ConfigTree.class});
+ _composer= oConst.newInstance(_config);
+ tagName = ListenerTagNames.PROCESS_METHOD_TAG;
+ sProcessMethod = _controller.obtainAtt(_config,tagName,tagName);
+ }
+ else
+ {
+ getDefaultComposer();
+ sProcessMethod = "process";
+ }
+
+ _processMethod = _composerClass.getMethod(sProcessMethod,new Class[] {Object.class});
+ } //________________________________
+
+ private File fileFromString(String file)
+ {
+ try { return new File(new URI(file)); }
+ catch(Exception e) { return new File(file); }
+ } //________________________________
+
+ protected final static Logger _logger = Logger.getLogger(AbstractFileGateway.class);
+
+ protected ConfigTree _config;
+ protected GatewayListenerController _controller;
+ protected long _sleepBetweenPolls; // milliseconds
+
+ protected String _serviceCategory, _serviceName;
+ protected String _targetServiceCategory ,_targetServiceName;
+ protected EPR _myEpr;
+ protected Collection<EPR> _targetEprs;
+
+ protected String _composerName;
+ protected Class _composerClass;
+ protected Object _composer;
+ protected Method _processMethod;
+
+ protected Courier _courier;
+
+ protected boolean _deleteAfterOK;
+ protected File _inputDirectory ,_errorDirectory ,_postProcessDirectory;
+ protected String _inputSuffix ,_postProcessSuffix
+ ,_workingSuffix ,_errorSuffix
+ ;
+ protected FileFilter _fileFilter;
+} //____________________________________________________________________________
Modified: labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListener.java
===================================================================
--- labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListener.java 2006-12-04 13:13:06 UTC (rev 8045)
+++ labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FileGatewayListener.java 2006-12-04 13:22:43 UTC (rev 8046)
@@ -1,403 +1,162 @@
-/*
- * 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 java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileFilter;
-import java.io.FileInputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.net.URI;
-import java.util.Collection;
-
-import org.apache.log4j.Logger;
-import org.jboss.soa.esb.ConfigurationException;
-import org.jboss.soa.esb.addressing.EPR;
-import org.jboss.soa.esb.couriers.Courier;
-import org.jboss.soa.esb.couriers.CourierException;
-import org.jboss.soa.esb.couriers.CourierFactory;
-import org.jboss.soa.esb.helpers.ConfigTree;
-import org.jboss.soa.esb.listeners.ListenerTagNames;
-import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.message.format.MessageFactory;
-import org.jboss.soa.esb.services.registry.RegistryException;
-import org.jboss.soa.esb.util.Util;
-
-/**
- *
- * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
- * @since Version 4.0
- *
- */
-public class FileGatewayListener implements Runnable
-{
-
- public FileGatewayListener(GatewayListenerController commandListener, ConfigTree config)
- throws Exception
- {
- _config = config;
- _controller = commandListener;
- _sleepBetweenPolls = 10000; // milliseconds
- checkMyParms();
- } // __________________________________
-
- public void run()
- {
- _logger.debug("run() method of "+this.getClass().getSimpleName()
- +" started on thread "+Thread.currentThread().getName());
- if (null!=_serviceName)
- try { _controller.register(_config,_myEpr); }
- catch (RegistryException e1)
- { _logger.warn("unable to register service",e1); }
-
- boolean bSleep = false;
- while (_controller.continueLooping())
- {
- // only sleep in between - not the first time
- if (bSleep)
- { long lUntil = System.currentTimeMillis()+_sleepBetweenPolls;
- while (System.currentTimeMillis() < lUntil)
- {
- try { Thread.sleep(1000); }
- catch (InterruptedException e) { lUntil = 0; }
- if (! _controller.continueLooping())
- break;
- }
- }
- else
- bSleep = true;
- if (! _controller.continueLooping())
- break;
-
- for (File fileIn : _inputDirectory.listFiles(_fileFilter))
- {
- // Try to rename - if unsuccessful, somebody else got it first
- File fileWork = new File(fileIn.getParent(),fileIn.getName()+_workingSuffix);
- if (! fileIn.renameTo(fileWork))
- continue;
-
- Throwable thrown = null;
- String text = null;
- try
- {
- Object obj = _processMethod.invoke(_composer,new Object[] {fileWork} );
- if (null==obj)
- {
- _logger.warn("Action class method <"+_processMethod.getName()+"> returned a null object");
- continue;
- }
- boolean bSent = false;
- for (EPR current : _targetEprs)
- {
- _courier = CourierFactory.getCourier(current);
- if (_courier.deliver((org.jboss.soa.esb.message.Message)obj))
- {
- bSent = true;
- break;
- }
- }
- if (! bSent)
- {
- text = "Target service <"+_targetServiceCategory+","+_targetServiceName+"> is not registered";
- thrown = new Exception(text);
- }
- }
-
- catch (InvocationTargetException e)
- {
- thrown = e;
- text = "Problems invoking method <"+_processMethod.getName()+">";
-
- }
- catch (IllegalAccessException e)
- {
- thrown = e;
- text = "Problems invoking method <"+_processMethod.getName()+">";
- }
- catch (ClassCastException e)
- {
- thrown = e;
- text = "Action class method <"+_processMethod.getName()+"> returned a non Message object";
- }
- catch (CourierException e)
- {
- thrown = e;
- if (null!=_courier)
- text = "Courier <"+_courier.getClass().getName()+".deliver(Message) FAILED";
- else
- text = "NULL courier can't deliver Message";
- }
-
- if (null==thrown)
- {
- File fileOK = new File(_postProcessDirectory,fileIn.getName()+_postProcessSuffix);
- if (_deleteAfterOK)
- fileWork.delete();
- else
- {
- fileOK.delete();
- fileWork.renameTo(fileOK);
- }
- }
- else
- {
- thrown.printStackTrace();
- _logger.error(text,thrown);
- File fileError = new File(_errorDirectory,fileIn.getName()+_errorSuffix);
- fileWork.renameTo(fileError);
- }
- }
- }
-
- if (null!=_serviceName)
- try { _controller.unRegister(_serviceCategory, _serviceName,_myEpr); }
- catch (RegistryException e1){ _logger.warn("unable to unRegister service",e1); }
- _logger.debug("run() method of "+this.getClass().getSimpleName()
- +" finished on thread "+Thread.currentThread().getName());
-
- } // ________________________________
-
- /**
- * Check for mandatory and optional attributes in parameter tree
- *
- * @throws Exception -
- * if mandatory atts are not right or actionClass not in
- * classpath
- */
- protected void checkMyParms() throws Exception
- {
- // Third arg is null - Exception will be thrown if attribute is not found
- _targetServiceCategory = _controller.obtainAtt(_config, ListenerTagNames.TARGET_SERVICE_CATEGORY_TAG, null);
- _targetServiceName = _controller.obtainAtt(_config, ListenerTagNames.TARGET_SERVICE_NAME_TAG, null);
- _targetEprs = _controller.getEprs(_targetServiceCategory,_targetServiceName);
- if (null==_targetEprs || _targetEprs.size()<1)
- throw new ConfigurationException("EPR <"+_targetServiceName+"> not found in registry");
-
- // Polling interval
- String sAux = _config.getAttribute(ListenerTagNames.POLL_LATENCY_SECS_TAG);
- if (! Util.isNullString(sAux))
- try { _sleepBetweenPolls = 1000 * Long.parseLong(sAux); }
- catch (NumberFormatException e)
- { _logger.warn("Invalid poll latency - keeping default of "+(_sleepBetweenPolls/1000)); }
- else
- {
- _logger.warn("No value specified for: "+ListenerTagNames.POLL_LATENCY_SECS_TAG
- +" - Using default of "+(_sleepBetweenPolls/1000));
- }
-
- resolveComposerClass();
-
- // INPUT directory and suffix (used for FileFilter)
- String sInpDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_INPUT_DIR_TAG,null);
- _inputDirectory = fileFromString(sInpDir);
- seeIfOkToWorkOnDir(_inputDirectory);
-
- _inputSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_INPUT_SFX_TAG,null);
- _inputSuffix = _inputSuffix.trim();
- if (_inputSuffix.length()<1)
- throw new Exception ("Invalid "+ListenerTagNames.FILE_INPUT_SFX_TAG+" attribute");
- _fileFilter = new FileEndsWith(_inputSuffix);
-
- // WORK suffix (will rename in input directory)
- _workingSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_WORK_SFX_TAG,".esbWork").trim();
- if (_workingSuffix.length()<1)
- throw new Exception ("Invalid "+ListenerTagNames.FILE_WORK_SFX_TAG+" attribute");
- if (_inputSuffix.equals(_workingSuffix))
- throw new Exception("Work suffix must differ from input suffix <"+_workingSuffix+">");
- if (null==_config.getAttribute(ListenerTagNames.FILE_WORK_SFX_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_WORK_SFX_TAG
- +" - Using default of '"+_workingSuffix+"'");
-
- // ERROR directory and suffix (defaults to input dir and ".esbError" suffix)
- String sErrDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_ERROR_DIR_TAG,sInpDir);
- _errorDirectory = fileFromString(sErrDir);
- if (null==_config.getAttribute(ListenerTagNames.FILE_ERROR_DIR_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_ERROR_DIR_TAG
- +" - Using input directory ("+sErrDir+")");
- seeIfOkToWorkOnDir(_errorDirectory);
-
- _errorSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_ERROR_SFX_TAG,".esbError").trim();
- if (_errorSuffix.length()<1)
- throw new Exception ("Invalid "+ListenerTagNames.FILE_ERROR_SFX_TAG+" attribute");
- if (null==_config.getAttribute(ListenerTagNames.FILE_ERROR_SFX_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_ERROR_SFX_TAG
- +" - Using default of '"+_errorSuffix+"'");
- if (_errorDirectory.equals(_inputDirectory) && _inputSuffix.equals(_errorSuffix))
- throw new Exception("Error suffix must differ from input suffix <"+_errorSuffix+">");
-
-
- // Do users wish to delete files that were processed OK ?
- String sPostDel = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_DEL_TAG,"false").trim();
- if (null==_config.getAttribute(ListenerTagNames.FILE_POST_DEL_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_POST_DEL_TAG
- +" - Using default of 'false'");
- _deleteAfterOK = Boolean.parseBoolean(sPostDel);
- if (_deleteAfterOK)
- return;
-
- // POST (done) directory and suffix (defaults to input dir and ".esbDone" suffix)
- String sPostDir = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_DIR_TAG,sInpDir);
- if (null==_config.getAttribute(ListenerTagNames.FILE_POST_DIR_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_POST_DIR_TAG
- +" - Using default of '"+sPostDir+"'");
- _postProcessDirectory = fileFromString(sPostDir);
- seeIfOkToWorkOnDir(_postProcessDirectory);
- _postProcessSuffix = _controller.obtainAtt(_config,ListenerTagNames.FILE_POST_SFX_TAG,".esbDone").trim();
- if (null==_config.getAttribute(ListenerTagNames.FILE_POST_SFX_TAG))
- _logger.warn("No value specified for: "+ListenerTagNames.FILE_POST_SFX_TAG
- +" - Using default of '"+_postProcessSuffix+"'");
-
- if (_postProcessDirectory.equals(_inputDirectory))
- { if (_postProcessSuffix.length()<1)
- throw new Exception ("Invalid "+ListenerTagNames.FILE_POST_SFX_TAG+" attribute");
- if (_postProcessSuffix.equals(_inputSuffix))
- throw new Exception("Post process suffix must differ from input suffix <"+_postProcessSuffix+">");
- }
- } //________________________________
-
- protected void resolveComposerClass() throws Exception
- {
- // Look for first "action" element - only first one will be used
- String tagName = ListenerTagNames.ACTION_ELEMENT_TAG;
- ConfigTree actionElement = _config.getFirstChild(tagName);
- String sProcessMethod = null;
- if (null!=actionElement)
- { // class attribute
- _composerName = _controller.obtainAtt(actionElement,ListenerTagNames.ACTION_CLASS_TAG,null);
- _composerClass = Class.forName(_composerName);
- Constructor oConst = _composerClass.getConstructor(new Class[] {ConfigTree.class});
- _composer= oConst.newInstance(_config);
- tagName = ListenerTagNames.PROCESS_METHOD_TAG;
- sProcessMethod = _controller.obtainAtt(_config,tagName,tagName);
- }
- else
- {
- _composerName = PackageFileContents.class.getName();
- _composerClass= PackageFileContents.class;
- _composer = new PackageFileContents();
- sProcessMethod = "process";
- _logger.warn("No <"+ListenerTagNames.ACTION_ELEMENT_TAG+"> element found in cofiguration"
- +" - Using default composer class : "+_composerName);
- }
-
- _processMethod = _composerClass.getMethod(sProcessMethod,new Class[] {Object.class});
- } //________________________________
-
- protected void seeIfOkToWorkOnDir (File p_oDir) throws Exception
- {
- if (! p_oDir.exists())
- throw new Exception ("Directory "+p_oDir.toString()+" not found");
- if (!p_oDir.isDirectory())
- throw new Exception(p_oDir.toString()+" is not a directory");
- if (!p_oDir.canRead())
- throw new Exception("Can't read directory "+p_oDir.toString());
- if (! p_oDir.canWrite())
- throw new Exception ("Can't write/rename in directory "+p_oDir.toString());
- } //________________________________
-
- private File fileFromString(String file)
- {
- try { return new File(new URI(file)); }
- catch(Exception e) { return new File(file); }
- } //________________________________
-
- private class FileEndsWith implements FileFilter
- {
- String m_sSuffix;
- FileEndsWith(String p_sEnd) throws Exception
- {
- m_sSuffix = p_sEnd;
- if (Util.isNullString(m_sSuffix))
- throw new Exception("Must specify file extension");
- } //______________________________
-
- public boolean accept(File p_f)
- { return (p_f.isFile())
- ? p_f.toString().endsWith(m_sSuffix)
- : false;
- } //______________________________
- } //____________________________________________________
-
-/**
- * Default gateway action for files
- * <p/>It will just drop the file contents into a Message
- * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
- * @since Version 4.0
- *
- */
- private static class PackageFileContents
- {
- public Message process (Object obj) throws Exception
- {
- if (! (obj instanceof File))
- throw new Exception ("Object must be instance of File");
-
- Message message = MessageFactory.getInstance().getMessage();
- message.getBody().setContents(getFileContent((File)obj));
- return message;
- }
-
- private byte[] getFileContent(File file) throws Exception
- {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] ba = new byte[1000];
- int iQread;
-
- FileInputStream inp = new FileInputStream(file);
- while (-1!= (iQread=inp.read(ba)))
- if (iQread > 0)
- out.write(ba,0,iQread);
- inp.close();
-
- out.close();
- return out.toByteArray();
- }
- } //____________________________________________________
-
- protected final static Logger _logger = Logger.getLogger(FileGatewayListener.class);
-
- protected ConfigTree _config;
- protected GatewayListenerController _controller;
- protected long _sleepBetweenPolls; // milliseconds
-
- protected String _serviceCategory, _serviceName;
- protected String _targetServiceCategory ,_targetServiceName;
- protected EPR _myEpr;
- protected Collection<EPR> _targetEprs;
-
- protected String _composerName;
- protected Class _composerClass;
- protected Object _composer;
- protected Method _processMethod;
-
- protected Courier _courier;
-
- protected boolean _deleteAfterOK;
- protected File _inputDirectory ,_errorDirectory ,_postProcessDirectory;
- protected String _inputSuffix ,_postProcessSuffix
- ,_workingSuffix ,_errorSuffix
- ;
- protected FileFilter _fileFilter;
-} //____________________________________________________________________________
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.jboss.soa.esb.util.Util;
+
+public class AnewFileGatewayListener extends AbstractFileGateway
+{
+ protected AnewFileGatewayListener() {}
+ public AnewFileGatewayListener(GatewayListenerController commandListener, ConfigTree config)
+ throws Exception
+ {
+ super(commandListener,config);
+ _fileFilter = new FileEndsWith(_inputSuffix);
+ }
+
+ protected void checkMyParms() throws Exception
+ {
+ super.checkMyParms();
+ }
+
+ @Override
+ protected void seeIfOkToWorkOnDir (File p_oDir) throws GatewayException
+ {
+ if (! p_oDir.exists())
+ throw new GatewayException ("Directory "+p_oDir.toString()+" not found");
+ if (!p_oDir.isDirectory())
+ throw new GatewayException(p_oDir.toString()+" is not a directory");
+ if (!p_oDir.canRead())
+ throw new GatewayException("Can't read directory "+p_oDir.toString());
+ if (! p_oDir.canWrite())
+ throw new GatewayException ("Can't write/rename in directory "+p_oDir.toString());
+ } //________________________________
+
+ @Override
+ boolean deleteFile(File file) throws GatewayException
+ {
+ return file.delete();
+ }
+
+
+ @Override
+ byte[] getFileContents(File file) throws GatewayException
+ {
+ try
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] ba = new byte[1000];
+ int iQread;
+ FileInputStream inp = new FileInputStream(file);
+ while (-1!= (iQread=inp.read(ba)))
+ if (iQread > 0)
+ out.write(ba,0,iQread);
+ inp.close();
+ out.close();
+ return out.toByteArray();
+ }
+ catch (IOException e) { throw new GatewayException(e); }
+ }
+
+
+ @Override
+ File[] getFileList(String suffix) throws GatewayException
+ {
+ return _inputDirectory.listFiles(_fileFilter);
+ }
+
+
+ @Override
+ boolean renameFile(File from, File to) throws GatewayException
+ {
+ return from.renameTo(to);
+ }
+
+ @Override
+ void getDefaultComposer() throws GatewayException
+ {
+ _composerName = PackageFileContents.class.getName();
+ _composerClass= PackageFileContents.class;
+ _composer = new PackageFileContents();
+ _logger.warn("No <"+ListenerTagNames.ACTION_ELEMENT_TAG+"> element found in cofiguration"
+ +" - Using default composer class : "+_composerName);
+ }
+//______________________________________________________________________________
+ /**
+ * Default gateway action for files
+ * <p/>It will just drop the file contents into a Message
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * @since Version 4.0
+ *
+ */
+ public class PackageFileContents
+ {
+ public Message process (Object obj) throws Exception
+ {
+ if (! (obj instanceof File))
+ throw new Exception ("Object must be instance of File");
+
+ Message message = MessageFactory.getInstance().getMessage();
+ message.getBody().setContents(getFileContent((File)obj));
+ return message;
+ }
+
+ private byte[] getFileContent(File file) throws Exception
+ { return getFileContents(file); }
+ } //____________________________________________________
+
+ /**
+ * Simple file filter for local filesystem
+ * Will accept only files that end with the String supplied at constructor time
+ *
+ */
+ private class FileEndsWith implements FileFilter
+ {
+ String m_sSuffix;
+ FileEndsWith(String p_sEnd) throws Exception
+ {
+ m_sSuffix = p_sEnd;
+ if (Util.isNullString(m_sSuffix))
+ throw new Exception("Must specify file extension");
+ } //______________________________
+
+ public boolean accept(File p_f)
+ { return (p_f.isFile())
+ ? p_f.toString().endsWith(m_sSuffix)
+ : false;
+ } //______________________________
+ } //____________________________________________________
+
+ private FileFilter _fileFilter;
+
+}
Added: labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java
===================================================================
--- labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java 2006-12-04 13:13:06 UTC (rev 8045)
+++ labs/jbossesb/workspace/b_georges/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java 2006-12-04 13:22:43 UTC (rev 8046)
@@ -0,0 +1,161 @@
+/*
+ * 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 java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.util.FtpClientUtil;
+import org.jboss.soa.esb.util.Util;
+
+public class FtpGatewayListener extends AnewFileGatewayListener
+{
+ protected FtpGatewayListener() {}
+ public FtpGatewayListener(GatewayListenerController commandListener, ConfigTree config)
+ throws Exception
+ {
+ super(commandListener,config);
+ }
+
+ protected void checkMyParms() throws Exception
+ {
+ super.checkMyParms();
+
+
+ FtpClientUtil ftpClient = new FtpClientUtil(_config,false);
+ ftpClient.quit();
+
+ // Copy FTP parameters to be passed to the action class (inside the WorkingFile class)
+ // This is a kludge - we have to get back to this (ES)
+ String[] sa = new String[]
+ {FtpClientUtil.PARMS_FTP_SERVER
+ ,FtpClientUtil.PARMS_USER
+ ,FtpClientUtil.PARMS_PASSWD
+ ,FtpClientUtil.PARMS_PASSIVE
+ ,FtpClientUtil.PARMS_PORT
+ };
+ for (String sProp : sa)
+ {
+ String sVal = _config.getAttribute(sProp);
+ if (!Util.isNullString(sVal))
+ _ftpArguments.put(sProp,sVal);
+ } }
+
+ @Override
+ protected void seeIfOkToWorkOnDir (File p_oDir) throws GatewayException
+ {
+ // Bruno: is there any way that the remote directory exists, that we can
+ // read it, and that that we can write on it ?
+ // please see the AnewFileGatewayListener class to see what the method does there
+ } //________________________________
+
+ @Override
+ boolean deleteFile(File file) throws GatewayException
+ {
+ // Bruno: please check this and the rest of the methods.
+ // I just copied the RemoteDirectoryPoller, but I know you have been working on this
+ // so what I copied here could be stale
+ FtpClientUtil ftpClient=null;
+ try
+ {
+ ftpClient = new FtpClientUtil(_config,true);
+ ftpClient.deleteRemoteFile(file.toString());
+ return true;
+ }
+ catch (Exception e) { throw new GatewayException(e); }
+ finally
+ {
+ if (null!=ftpClient)
+ ftpClient.quit();
+ }
+ }
+
+
+ @Override
+ byte[] getFileContents(File file) throws GatewayException
+ {
+ FtpClientUtil ftpClient=null;
+ try
+ {
+ File temp = File.createTempFile("FTPdown",".tmp");
+ temp.delete();
+ ftpClient = new FtpClientUtil(_config,true);
+ ftpClient.downloadFile(file.toString(), temp.toString());
+ return getFileContents(temp);
+ }
+ catch (Exception e) { throw new GatewayException(e); }
+ finally
+ {
+ if (null!=ftpClient)
+ ftpClient.quit();
+ }
+ }
+
+
+ @Override
+ File[] getFileList(String suffix) throws GatewayException
+ {
+ FtpClientUtil ftpClient = null;
+ try
+ {
+ ftpClient = new FtpClientUtil(_config,true);
+ ftpClient.setRemoteDir(FtpClientUtil.fileToFtpString(_inputDirectory));
+ String[] sa = ftpClient.getFileListFromRemoteDir(_inputSuffix);
+ File[] oaRet = new File[(null==sa)?0:sa.length];
+ int i1 = 0;
+ if (null!=sa)
+ for (String sCurr : sa)
+ oaRet[i1++] = new File(sCurr);
+ return oaRet;
+ }
+ catch (Exception e) { throw new GatewayException(e); }
+ finally
+ {
+ if (null!=ftpClient)
+ ftpClient.quit();
+ }
+ }
+
+
+ @Override
+ boolean renameFile(File from, File to) throws GatewayException
+ {
+ FtpClientUtil ftpClient=null;
+ try
+ {
+ ftpClient = new FtpClientUtil(_config,true);
+ ftpClient.renameInRemoteDir(from.toString(), to.toString());
+ return true;
+ }
+ catch (Exception e) { throw new GatewayException(e); }
+ finally
+ {
+ if (null!=ftpClient)
+ ftpClient.quit();
+ }
+ }
+
+ protected Map<String,String> _ftpArguments = new HashMap<String, String>();
+}
More information about the jboss-svn-commits
mailing list