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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Aug 5 23:01:35 EDT 2006


Author: estebanschifman
Date: 2006-08-05 23:01:30 -0400 (Sat, 05 Aug 2006)
New Revision: 5503

Added:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java
Log:
Refactoring of listeners/processors/action classes

Copied: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java (from rev 5471, labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/BetterDirListener.java)
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/BetterDirListener.java	2006-08-04 12:48:02 UTC (rev 5471)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/DirectoryPoller.java	2006-08-06 03:01:30 UTC (rev 5503)
@@ -0,0 +1,175 @@
+/*
+* 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.net.*;
+import java.util.*;
+
+import org.jboss.soa.esb.util.*;
+import org.jboss.soa.esb.actions.AbstractFileAction;
+import org.jboss.soa.esb.helpers.*;
+
+public class DirectoryPoller extends AbstractPoller
+{
+  public static final String FILE_INPUT_DIR 	= "inputDirURI";
+  public static final String FILE_INPUT_SFX 	= "inputSuffix";
+  public static final String FILE_WORK_SFX 		= "workSuffix";
+  public static final String FILE_ERROR_DIR   	= "errorDirURI";
+  public static final String FILE_ERROR_SFX   	= "errorSuffix";
+  public static final String FILE_POST_DIR  	= "postDirURI";
+  public static final String FILE_POST_SFX  	= "postSuffix";
+  public static final String FILE_POST_DEL  	= "postDelete";
+
+  public DirectoryPoller(GpListener p_oDad, DomElement p_oParms) throws Exception
+  {
+	super(p_oDad,p_oParms);
+	checkParms();
+  } //__________________________________
+
+
+    protected File 			m_oInpDir	,m_oErrorDir	,m_oPostDir;
+    protected FileFilter	m_oFFilt;
+    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 array of 3 Files containing:
+     * <p/>[0] renamed file (workSuffix appended to input file name)
+     * <p/>[1] target file name in case actionClass is unable to complete successfuly
+     * <p/>[2] target file name in case actionClass finishes successfuly
+     */
+	@Override
+	public Object preProcess(Object p_o) throws Exception 
+	{
+		if (!(p_o instanceof File))
+			return null;
+		File oF = (File)p_o;
+		File oNameWrk = new File (oF.getParentFile(),oF.getName()+m_sWrkSfx);
+
+
+		if (! oF.renameTo(oNameWrk))
+			return null;
+		AbstractFileAction.Params oCurr = new AbstractFileAction.Params();
+		oCurr.bPostDelete	= m_bPostDel;
+		oCurr.oInpF			= oF;
+		oCurr.oWrkF			= oNameWrk;
+		oCurr.oErrF			= new File (m_oErrorDir	,oF.getName()+m_sErrSfx);
+		oCurr.oDoneF		= new File (m_oPostDir	,oF.getName()+m_sPostSfx);
+
+		return oCurr;
+	} //________________________________
+
+	@Override
+	protected List<Object> pollForCandidates()
+	{
+		File[] oaF = m_oInpDir.listFiles(m_oFFilt);
+		return Arrays.asList((Object[])oaF);
+	} //________________________________
+
+	protected void checkParms() throws Exception
+    { 
+	//  INPUT directory and suffix  (used for FileFilter)
+	  String sInpDir = GpListener.obtainAtt(m_oParms,FILE_INPUT_DIR,null);
+      m_oInpDir = new File(new URI(sInpDir));
+      seeIfOkToWorkOnDir(m_oInpDir);
+
+      m_sInpSfx  = GpListener.obtainAtt(m_oParms,FILE_INPUT_SFX,null);
+      m_sInpSfx  = m_sInpSfx.trim();
+      if (m_sInpSfx.length()<1)
+    	  throw new Exception ("Invalid "+FILE_INPUT_SFX+" attribute");
+	  m_oFFilt = new FileEndsWith(m_sInpSfx);
+
+	//  WORK suffix (will rename in input directory)
+      m_sWrkSfx	= GpListener.obtainAtt(m_oParms,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(m_oParms,FILE_ERROR_DIR,sInpDir);
+      m_oErrorDir = new File(new URI(sErrDir));
+      seeIfOkToWorkOnDir(m_oErrorDir);
+
+      m_sErrSfx  = GpListener.obtainAtt(m_oParms,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(m_oParms,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(m_oParms,FILE_POST_DIR,sInpDir);
+      m_oPostDir = new File(new URI(sPostDir));
+      seeIfOkToWorkOnDir(m_oPostDir);
+      m_sPostSfx  = GpListener.obtainAtt(m_oParms,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+">");
+      }
+
+    } //________________________________
+	
+	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 class FileEndsWith implements FileFilter
+    {
+      String m_sSuffix;
+      FileEndsWith(String p_sEnd) throws Exception
+      {
+        m_sSuffix = p_sEnd;
+        if (EsbUtil.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;
+      } //______________________________
+    } //____________________________________________________
+
+} //____________________________________________________________________________




More information about the jboss-svn-commits mailing list