servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r564081 - in /incubator/servicemix/trunk: common/servicemix-components/src/main/java/org/apache/servicemix/components/file/ deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/ deployables/bindingcomponent...
Date Thu, 09 Aug 2007 03:30:58 GMT
Author: gertv
Date: Wed Aug  8 20:30:56 2007
New Revision: 564081

URL: http://svn.apache.org/viewvc?view=rev&rev=564081
Log:
Fix for SM-990: FilePoller with Archiving

Added:
    incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
Modified:
    incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/file/FilePoller.java
    incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java

Modified: incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/file/FilePoller.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/file/FilePoller.java?view=diff&rev=564081&r1=564080&r2=564081
==============================================================================
--- incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/file/FilePoller.java
(original)
+++ incubator/servicemix/trunk/common/servicemix-components/src/main/java/org/apache/servicemix/components/file/FilePoller.java
Wed Aug  8 20:30:56 2007
@@ -21,10 +21,12 @@
 import org.apache.servicemix.components.util.DefaultFileMarshaler;
 import org.apache.servicemix.components.util.FileMarshaler;
 import org.apache.servicemix.components.util.PollingComponentSupport;
+import org.apache.servicemix.jbi.util.FileUtil;
 
 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet;
 
 import javax.jbi.JBIException;
+import javax.jbi.management.DeploymentException;
 import javax.jbi.messaging.InOnly;
 import javax.jbi.messaging.NormalizedMessage;
 import java.io.BufferedInputStream;
@@ -45,6 +47,7 @@
 public class FilePoller extends PollingComponentSupport {
     private static final Log log = LogFactory.getLog(FilePoller.class);
 
+    private File archive;
     private File file;
     private FileFilter filter;
     private boolean deleteFile = true;
@@ -117,6 +120,19 @@
     public void setMarshaler(FileMarshaler marshaler) {
         this.marshaler = marshaler;
     }
+    
+    public File getArchive() {
+        return archive;
+    }
+    
+    /**
+     * Configure a directory to archive files before deleting them.
+     * 
+     * @param archive the archive directory
+     */
+    public void setArchive(File archive) {
+        this.archive = archive;
+    }
 
     /**
      * The set of FTPFiles that this component is currently working on
@@ -136,6 +152,17 @@
         if (isAutoCreateDirectory() && !file.exists()) {
             file.mkdirs();
         }
+        if (archive != null) {
+            if (!deleteFile) {
+                throw new DeploymentException("Archive shouldn't be specified unless deleteFile='true'");
+            }
+            if (isAutoCreateDirectory() && !archive.exists()) {
+                archive.mkdirs();
+            }
+            if (!archive.isDirectory()) {
+                throw new DeploymentException("Archive should refer to a directory");
+            }
+        }
         super.init();
     }
 
@@ -182,8 +209,12 @@
             if (aFile.exists()) {
                 processFile(aFile);
                 if (isDeleteFile()) {
-                    if (!aFile.delete()) {
-                        throw new IOException("Could not delete file " + aFile);
+                    if (archive != null) {
+                        FileUtil.moveFile(aFile, archive);
+                    } else {
+                        if (!aFile.delete()) {
+                            throw new IOException("Could not delete file " + aFile);
+                        }
                     }
                 }
             }

Modified: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java?view=diff&rev=564081&r1=564080&r2=564081
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java
(original)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/main/java/org/apache/servicemix/file/FilePollerEndpoint.java
Wed Aug  8 20:30:56 2007
@@ -39,6 +39,7 @@
 import org.apache.servicemix.common.endpoints.PollingEndpoint;
 import org.apache.servicemix.components.util.DefaultFileMarshaler;
 import org.apache.servicemix.components.util.FileMarshaler;
+import org.apache.servicemix.jbi.util.FileUtil;
 import org.apache.servicemix.locks.LockManager;
 import org.apache.servicemix.locks.impl.SimpleLockManager;
 
@@ -58,6 +59,7 @@
     private boolean deleteFile = true;
     private boolean recursive = true;
     private boolean autoCreateDirectory = true;
+    private File archive;
     private FileMarshaler marshaler = new DefaultFileMarshaler();
     private LockManager lockManager;
 
@@ -84,6 +86,17 @@
         if (isAutoCreateDirectory() && !file.exists()) {
             file.mkdirs();
         }
+        if (archive != null) {
+            if (!deleteFile) {
+                throw new DeploymentException("Archive shouldn't be specified unless deleteFile='true'");
+            }
+            if (isAutoCreateDirectory() && !archive.exists()) {
+                archive.mkdirs();
+            }
+            if (!archive.isDirectory()) {
+                throw new DeploymentException("Archive should refer to a directory");
+            }
+        }
         if (lockManager == null) {
             lockManager = createLockManager();
         }
@@ -168,6 +181,19 @@
     public void setMarshaler(FileMarshaler marshaler) {
         this.marshaler = marshaler;
     }
+    
+    public File getArchive() {
+        return archive;
+    }
+    
+    /**
+     * Configure a directory to archive files before deleting them.
+     * 
+     * @param archive the archive directory
+     */
+    public void setArchive(File archive) {
+        this.archive = archive;
+    }
 
     // Implementation methods
     //-------------------------------------------------------------------------
@@ -227,8 +253,12 @@
                 processFile(aFile);
                 unlock = false;
                 if (isDeleteFile()) {
-                    if (!aFile.delete()) {
-                        throw new IOException("Could not delete file " + aFile);
+                    if (archive != null) {
+                        FileUtil.moveFile(aFile, archive);
+                    } else {
+                        if (!aFile.delete()) {
+                            throw new IOException("Could not delete file " + aFile);
+                        }
                     }
                     unlock = true;
                 }

Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java?view=auto&rev=564081
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
(added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-file/src/test/java/org/apache/servicemix/file/FilePollerEndpointTest.java
Wed Aug  8 20:30:56 2007
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.file;
+
+import java.io.File;
+
+import javax.jbi.management.DeploymentException;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+public class FilePollerEndpointTest extends TestCase {
+    
+    private static final File DATA = new File("target/test/data");
+    private static final File ARCHIVE = new File("target/test/archive");
+    private FilePollerEndpoint endpoint;
+
+    @Override
+    protected void setUp() throws Exception {
+        endpoint = new FilePollerEndpoint();
+        endpoint.setTargetService(new QName("urn:test", "service"));
+    }
+    
+    public void testValidateNoFile() throws Exception {
+        try {
+            endpoint.validate();
+            fail("validate() should throw an exception when file has not been set");
+        } catch (DeploymentException e) {
+            //test succeeds
+        }
+    }
+
+    public void testValidateArchiveNoDirectory() throws Exception {
+        endpoint.setFile(DATA);
+        File archive = null;
+        try {
+            archive = File.createTempFile("servicemix", "test");
+            endpoint.setArchive(archive);
+            endpoint.validate();
+            fail("validate() should throw an exception when archive doesn't refer to a directory");
+        } catch (DeploymentException e) {
+            //test succeeds
+        } finally {
+            if (archive != null) {
+                archive.delete();
+            }
+        }
+    }
+    
+    public void testValidateArchiveWithoutDelete() throws Exception {
+        endpoint.setFile(DATA);
+        endpoint.setArchive(ARCHIVE);
+        endpoint.setDeleteFile(false);
+        try {
+            endpoint.validate();
+            fail("validate() should throw an exception when archive was set without delete");
+        } catch (DeploymentException e) {
+            //test succeeds
+        }
+    }
+}



Mime
View raw message