[jboss-svn-commits] JBL Code SVN: r8115 - in labs/jbossesb/trunk/product/core: listeners/src/org/jboss/soa/esb/listeners/gateway rosetta/src/org/jboss/internal/soa/esb/couriers rosetta/src/org/jboss/internal/soa/esb/couriers/helpers rosetta/src/org/jboss/soa/esb/common
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Dec 7 08:35:54 EST 2006
Author: estebanschifman
Date: 2006-12-07 08:35:45 -0500 (Thu, 07 Dec 2006)
New Revision: 8115
Added:
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AnewFileGatewayListener.java
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java
labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayException.java
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/
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/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/common/TagNames.java
Modified:
labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java
Log:
Add classes for new Couriers (Still incomplete)
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2006-12-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AbstractFileGateway.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -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;
+} //____________________________________________________________________________
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AnewFileGatewayListener.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AnewFileGatewayListener.java 2006-12-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/AnewFileGatewayListener.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +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.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/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java 2006-12-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/FtpGatewayListener.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -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>();
+}
Added: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayException.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayException.java 2006-12-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayException.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,13 @@
+package org.jboss.soa.esb.listeners.gateway;
+
+import org.jboss.soa.esb.BaseException;
+
+public class GatewayException extends BaseException
+{
+ private static final long serialVersionUID = 1L;
+ public GatewayException() { super(); }
+ public GatewayException(Throwable cause) { super(cause); }
+ public GatewayException(String message) { super(message); }
+ public GatewayException(String message, Throwable cause)
+ { super(message, cause); }
+}
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 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/FileCourier.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -23,69 +23,70 @@
package org.jboss.internal.soa.esb.couriers;
import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
-import javax.xml.parsers.ParserConfigurationException;
-
import org.apache.log4j.Logger;
import org.jboss.soa.esb.addressing.eprs.FileEpr;
import org.jboss.soa.esb.couriers.CourierException;
import org.jboss.soa.esb.message.Message;
-import org.jboss.soa.esb.util.Util;
public class FileCourier implements PickUpOnlyCourier, DeliverOnlyCourier
{
/**
- * disable default constructor
+ * disable public default constructor
*/
- private FileCourier() { }
+ protected FileCourier() { }
/**
- * package protected constructor - Objects of Courier should only be instantiated by the Factory
+ * package protected constructor -
+ * Objects of this class should only be instantiated by internal implementations
* @param epr
*/
FileCourier(FileEpr epr) throws CourierException { this(epr,false); }
/**
- * package protected constructor - Objects of Courier should only be instantiated by the Factory
+ * package protected constructor -
+ * Objects of this class should only be instantiated by internal implementations
* @param epr
+ * @param receiverOnly
*/
- FileCourier(FileEpr epr, boolean isReceiver) throws CourierException
+ FileCourier(FileEpr epr, boolean receiverOnly) throws CourierException
{
- _isReceiver = isReceiver;
+ _receiverOnly = receiverOnly;
_epr = epr;
- try
- {
- _directory = new File(epr.getURL().getFile());
- if (! _directory.isDirectory())
- throw new CourierException("URL for a File EPR must be a directory");
-
- if (_isReceiver)
- checkRead();
- else
- checkWrite();
-
- }
- catch(MalformedURLException e) { throw new CourierException(e); }
- catch(URISyntaxException e) { throw new CourierException(e); }
+ checkEprParms();
} //________________________________
-
- private void checkRead () throws CourierException
+ /**
+ * See if we have everythin we need in the EPR
+ * @throws CourierException
+ */
+ protected void checkEprParms() throws CourierException
{
- if (! _directory.canRead())
- throw new CourierException("Can't read directory "+_directory.toString());
- }
+ //TODO need to add more logic here
+
+ // Certain things can only be done in local filesystem
+ try
+ {
+ if ("file".equals(_epr.getURL().getProtocol()))
+ {
+ File file = new File(_epr.getURL().getFile());
+ if ((!_receiverOnly) && (!file.isDirectory()))
+ throw new CourierException("File for deliver EPR must be a directory (file name will be MessageID)");
- private void checkWrite () throws CourierException
- {
- if (! _directory.canWrite())
- throw new CourierException("Can't write in directory "+_directory.toString());
- }
+ File directory = (file.isDirectory()) ? file : file.getParentFile();
+ if (! directory.canRead())
+ throw new CourierException("Can't read directory "+directory.toString());
+ // need to write even if it's readOnly - file will be renamed during xfer
+ if (! directory.canWrite())
+ throw new CourierException("Can't write in directory "+directory.toString());
+
+ }
+ }
+ catch (MalformedURLException e) {throw new CourierException(e); }
+ catch (URISyntaxException e) {throw new CourierException(e); }
+ } //________________________________
/**
* package the ESB message in a File
@@ -95,39 +96,24 @@
*/
public boolean deliver(Message message) throws CourierException
{
- if (_isReceiver)
+ if (_receiverOnly)
throw new CourierException("This is a read-only Courier");
if (null==message)
return false;
- try
- {
-
- String messageId = message.getHeader().getCall().getMessageID().toString();
- File outFile = new File(_directory,messageId);
- ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(outFile));
- stream.writeObject(Util.serialize(message));
- stream.close();
- return true;
- }
- catch (MalformedURLException e) { throw new CourierException(e); }
- catch (URISyntaxException e) { throw new CourierException(e); }
- catch (IOException e) { throw new CourierException(e); }
- catch (ParserConfigurationException e) { throw new CourierException(e); }
+ //TODO come back here once FileHandlerInterface is done
+ return false;
} //________________________________
public Message pickup(long millis) throws CourierException
{
- if (! _isReceiver)
- throw new CourierException("This is an outgoing-only Courier");
-
+ //TODO come back here once FileHandlerInterface is done
return null;
} //________________________________
- protected boolean _isReceiver;
+ protected boolean _receiverOnly;
protected FileEpr _epr;
- protected File _directory;
protected Logger _logger = Logger.getLogger(FileCourier.class);
} //____________________________________________________________________________
Added: 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-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerFactory.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,28 @@
+package org.jboss.internal.soa.esb.couriers.helpers;
+
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.couriers.CourierException;
+
+public class FileHandlerFactory
+{
+ private static final FileHandlerFactory _instance = new FileHandlerFactory();
+ public FileHandlerFactory getInstance() { return _instance; }
+ 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); }
+
+ throw new CourierException("Unable to obtain a file handler for supplied EPR");
+ }
+
+}
Added: 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 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FileHandlerInterface.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,36 @@
+/*
+ * 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.io.File;
+import org.jboss.soa.esb.couriers.CourierException;
+
+public interface FileHandlerInterface
+{
+ abstract File[] getFileList(String suffix) 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;
+ abstract void seeIfOkToWorkOnDir (File p_oDir) throws CourierException;
+
+}
Added: 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 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/FtpFileHandler.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,41 @@
+package org.jboss.internal.soa.esb.couriers.helpers;
+
+import java.io.File;
+
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.couriers.CourierException;
+
+public class FtpFileHandler implements FileHandlerInterface
+{
+ private FtpFileHandler() {}
+ FtpFileHandler(FileEpr epr) throws CourierException
+ {
+ _epr = epr;
+ }
+
+ public boolean deleteFile(File file) throws CourierException
+ {
+ return false;
+ }
+
+ public byte[] getFileContents(File file) throws CourierException
+ {
+ return null;
+ }
+
+ public File[] getFileList(String suffix) throws CourierException
+ {
+ return null;
+ }
+
+ public boolean renameFile(File from, File to) throws CourierException
+ {
+ return false;
+ }
+
+ public void seeIfOkToWorkOnDir(File p_oDir) throws CourierException
+ {
+ }
+
+ FileEpr _epr;
+}
Added: 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 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/internal/soa/esb/couriers/helpers/LocalFileHandler.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,100 @@
+package org.jboss.internal.soa.esb.couriers.helpers;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+
+import org.jboss.soa.esb.addressing.eprs.FileEpr;
+import org.jboss.soa.esb.couriers.CourierException;
+import org.jboss.soa.esb.util.Util;
+
+public class LocalFileHandler implements FileHandlerInterface
+{
+ private LocalFileHandler() {}
+ LocalFileHandler(FileEpr epr) {_epr = epr; }
+
+ public boolean deleteFile(File file) throws CourierException
+ {
+ return file.delete();
+ }
+
+ public byte[] getFileContents(File file) throws CourierException
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] ba = new byte[1000];
+ int iQread;
+
+ try
+ {
+ 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(FileNotFoundException e) {throw new CourierException(e);}
+ catch (IOException e) {throw new CourierException(e);}
+
+ // Just in case... (comment next line out to see if you're missing something)
+ catch (Exception e) {throw new CourierException(e);}
+ }
+
+ public File[] getFileList(String suffix) throws CourierException
+ {
+ try
+ {
+ File dir = new File(_epr.getURL().getFile());
+ if (! dir.isDirectory())
+ throw new CourierException("Can't get file list if URL is not a directory");
+
+ FileFilter filter = new FileEndsWith(_epr.getInputSuffix());
+ return dir.listFiles(filter);
+ }
+ catch (URISyntaxException e) {throw new CourierException(e);}
+ catch (MalformedURLException e) {throw new CourierException(e);}
+ // Just in case... (comment next line out to see if you're missing something)
+ catch (Exception e) {throw new CourierException(e);}
+ }
+
+ public boolean renameFile(File from, File to) throws CourierException
+ {
+ try { return from.renameTo(to); }
+ catch (Exception e) {throw new CourierException(e);}
+ }
+
+ public void seeIfOkToWorkOnDir(File dir) throws CourierException
+ {
+ if (! dir.isDirectory())
+ throw new CourierException(dir.toString()+" is not a directory");
+ if (! dir.canRead())
+ throw new CourierException("Unable to read from directory "+dir.toString());
+ if (! dir.canWrite())
+ throw new CourierException("Unable to write on directory "+dir.toString());
+ }
+
+ private class FileEndsWith implements FileFilter
+ {
+ String m_sSuffix;
+ FileEndsWith(String p_sEnd) throws CourierException
+ {
+ m_sSuffix = p_sEnd;
+ if (Util.isNullString(m_sSuffix))
+ throw new CourierException("A file suffix (or full Message id) must be specified for pickup");
+ } //______________________________
+
+ public boolean accept(File p_f)
+ { return (p_f.isFile())
+ ? p_f.toString().endsWith(m_sSuffix)
+ : false;
+ } //______________________________
+ } //____________________________________________________
+
+ FileEpr _epr;
+}
Added: labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/common/TagNames.java
===================================================================
--- labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/common/TagNames.java 2006-12-07 13:34:48 UTC (rev 8114)
+++ labs/jbossesb/trunk/product/core/rosetta/src/org/jboss/soa/esb/common/TagNames.java 2006-12-07 13:35:45 UTC (rev 8115)
@@ -0,0 +1,38 @@
+/*
+ * 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.common;
+
+/**
+ * Base class to hold commonly used tagnames.
+ * <br/>Semantics of what these tags hold will be context sensitive
+ *
+ * @author <a href="mailto:schifest at heuristica.com.ar">schifest at heuristica.com.ar</a>
+ * @since Version 4.0
+ *
+ */
+public class TagNames
+{
+ /** Common **/
+ public static final String URL_TAG = "url";
+ public static final String USERNAME_TAG = "username";
+ public static final String PASSWORD_TAG = "password";
+}
More information about the jboss-svn-commits
mailing list