camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r909998 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ test/java/org/apache/camel/component/file/
Date Sun, 14 Feb 2010 11:17:47 GMT
Author: davsclaus
Date: Sun Feb 14 11:17:46 2010
New Revision: 909998

URL: http://svn.apache.org/viewvc?rev=909998&view=rev
Log:
CAMEL-2466: Added option eagerDeleteTargetFile to file component.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java?rev=909998&r1=909997&r2=909998&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
Sun Feb 14 11:17:46 2010
@@ -65,6 +65,7 @@
     protected int maxMessagesPerPoll;
     protected String tempPrefix;
     protected Expression tempFileName;
+    protected boolean eagerDeleteTargetFile = true;
     protected String include;
     protected String exclude;
     protected Expression fileName;
@@ -346,6 +347,14 @@
         this.tempFileName = createFileLanguageExpression(tempFileNameExpression);
     }
 
+    public boolean isEagerDeleteTargetFile() {
+        return eagerDeleteTargetFile;
+    }
+
+    public void setEagerDeleteTargetFile(boolean eagerDeleteTargetFile) {
+        this.eagerDeleteTargetFile = eagerDeleteTargetFile;
+    }
+
     public GenericFileConfiguration getConfiguration() {
         if (configuration == null) {
             configuration = new GenericFileConfiguration();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java?rev=909998&r1=909997&r2=909998&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/GenericFileProducer.java
Sun Feb 14 11:17:46 2010
@@ -98,16 +98,15 @@
                         return;
                     } else if (endpoint.getFileExist() == GenericFileExist.Fail) {
                         throw new GenericFileOperationFailedException("File already exist:
" + target + ". Cannot write new file.");
-                    } else if (endpoint.getFileExist() == GenericFileExist.Override) {
+                    } else if (endpoint.isEagerDeleteTargetFile() && endpoint.getFileExist()
== GenericFileExist.Override) {
                         // we override the target so we do this by deleting it so the temp
file can be renamed later
                         // with success as the existing target file have been deleted
                         if (log.isTraceEnabled()) {
-                            log.trace("Deleting existing file: " + target);
+                            log.trace("Eagerly deleting existing file: " + target);
                         }
                         if (!operations.deleteFile(target)) {
                             throw new GenericFileOperationFailedException("Cannot delete
file: " + target);
                         }
-
                     }
                 }
 
@@ -128,6 +127,21 @@
             // if we did write to a temporary name then rename it to the real
             // name after we have written the file
             if (tempTarget != null) {
+
+                // if we should not eager delete the target file then do it now just before
renaming
+                if (!endpoint.isEagerDeleteTargetFile() && operations.existsFile(target)
+                        && endpoint.getFileExist() == GenericFileExist.Override)
{
+                    // we override the target so we do this by deleting it so the temp file
can be renamed later
+                    // with success as the existing target file have been deleted
+                    if (log.isTraceEnabled()) {
+                        log.trace("Deleting existing file: " + target);
+                    }
+                    if (!operations.deleteFile(target)) {
+                        throw new GenericFileOperationFailedException("Cannot delete file:
" + target);
+                    }
+                }
+
+                // now we are ready to rename the temp file to the target file
                 if (log.isTraceEnabled()) {
                     log.trace("Renaming file: [" + tempTarget + "] to: [" + target + "]");
                 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java?rev=909998&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
Sun Feb 14 11:17:46 2010
@@ -0,0 +1,68 @@
+/**
+ * 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.camel.component.file;
+
+import java.io.File;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+/**
+ * @version $Revision$
+ */
+public class FileEagerDeleteTargetFileTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/eagerdelete");
+        super.setUp();
+        template.sendBodyAndHeader("file://target/eagerdelete", "Hello World", Exchange.FILE_NAME,
"world.txt");
+    }
+
+    public void testEagerDeleteTargetFileTrue() throws Exception {
+        template.sendBodyAndHeader("file://target/eagerdelete?tempFileName=inprogress-${file:name}&eagerDeleteTargetFile=true",
+                "Bye World", Exchange.FILE_NAME, "world.txt");
+
+        Thread.sleep(1000);
+
+        File file = new File("target/eagerdelete/world.txt").getAbsoluteFile();
+        assertTrue("File should exist", file.exists());
+        assertEquals("Bye World", context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testEagerDeleteTargetFileFalse() throws Exception {
+        template.sendBodyAndHeader("file://target/eagerdelete?tempFileName=inprogress-${file:name}&eagerDeleteTargetFile=false",
+                "Bye World", Exchange.FILE_NAME, "world.txt");
+
+        Thread.sleep(1000);
+
+        File file = new File("target/eagerdelete/world.txt").getAbsoluteFile();
+        assertTrue("File should exist", file.exists());
+        assertEquals("Bye World", context.getTypeConverter().convertTo(String.class, file));
+    }
+
+    public void testEagerDeleteTargetFileDefault() throws Exception {
+        template.sendBodyAndHeader("file://target/eagerdelete?tempFileName=inprogress-${file:name}",
+                "Bye World", Exchange.FILE_NAME, "world.txt");
+
+        Thread.sleep(1000);
+
+        File file = new File("target/eagerdelete/world.txt").getAbsoluteFile();
+        assertTrue("File should exist", file.exists());
+        assertEquals("Bye World", context.getTypeConverter().convertTo(String.class, file));
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileEagerDeleteTargetFileTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message