Author: davsclaus
Date: Sat Oct 1 15:58:30 2011
New Revision: 1178035
URL: http://svn.apache.org/viewvc?rev=1178035&view=rev
Log:
CAMEL-4505: Added option copyAndDeleteOnRenameFail to file component.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java
- copied, changed from r1177936,
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=1178035&r1=1178034&r2=1178035&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
Sat Oct 1 15:58:30 2011
@@ -33,6 +33,7 @@ public class FileEndpoint extends Generi
private FileOperations operations = new FileOperations(this);
private File file;
+ private boolean copyAndDeleteOnRenameFail = true;
public FileEndpoint() {
// use marker file as default exclusive read locks
@@ -135,4 +136,11 @@ public class FileEndpoint extends Generi
return FileUtil.isAbsolute(new File(name));
}
+ public boolean isCopyAndDeleteOnRenameFail() {
+ return copyAndDeleteOnRenameFail;
+ }
+
+ public void setCopyAndDeleteOnRenameFail(boolean
copyAndDeleteOnRenameFail) {
+ this.copyAndDeleteOnRenameFail = copyAndDeleteOnRenameFail;
+ }
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java?rev=1178035&r1=1178034&r2=1178035&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileOperations.java
Sat Oct 1 15:58:30 2011
@@ -62,7 +62,11 @@ public class FileOperations implements G
public boolean renameFile(String from, String to) throws
GenericFileOperationFailedException {
File file = new File(from);
File target = new File(to);
- return FileUtil.renameFile(file, target);
+ try {
+ return FileUtil.renameFile(file, target,
endpoint.isCopyAndDeleteOnRenameFail());
+ } catch (IOException e) {
+ throw new GenericFileOperationFailedException("Error renaming file
from " + from + " to " + to, e);
+ }
}
public boolean existsFile(String name) throws
GenericFileOperationFailedException {
@@ -234,10 +238,10 @@ public class FileOperations implements G
}
}
- private boolean writeFileByLocalWorkPath(File source, File file) {
+ private boolean writeFileByLocalWorkPath(File source, File file) throws
IOException {
LOG.trace("Using local work file being renamed from: {} to: {}",
source, file);
- return FileUtil.renameFile(source, file);
+ return FileUtil.renameFile(source, file,
endpoint.isCopyAndDeleteOnRenameFail());
}
private void writeFileByFile(File source, File target) throws IOException {
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java?rev=1178035&r1=1178034&r2=1178035&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultHeaderFilterStrategy.java
Sat Oct 1 15:58:30 2011
@@ -92,7 +92,6 @@ public class DefaultHeaderFilterStrategy
public String getOutFilterPattern() {
return outFilterPattern == null ? null : outFilterPattern.pattern();
}
-
/**
* Sets the "out" direction filter regular expression {@link Pattern}. The
@@ -162,7 +161,7 @@ public class DefaultHeaderFilterStrategy
}
/**
- * Gets the isLowercase property which is a boolean to determinte
+ * Gets the isLowercase property which is a boolean to determine
* whether header names should be converted to lowercase before
* checking it the filter Set. It does not affect filtering using
* regular expression pattern.
@@ -172,7 +171,7 @@ public class DefaultHeaderFilterStrategy
}
/**
- * Sets the isLowercase property which is a boolean to determinte
+ * Sets the isLowercase property which is a boolean to determine
* whether header names should be converted to lowercase before
* checking it the filter Set. It does not affect filtering using
* regular expression pattern.
@@ -235,4 +234,5 @@ public class DefaultHeaderFilterStrategy
return false;
}
+
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=1178035&r1=1178034&r2=1178035&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
Sat Oct 1 15:58:30 2011
@@ -305,7 +305,16 @@ public final class FileUtil {
}
}
- public static boolean renameFile(File from, File to) {
+ /**
+ * Renames a file.
+ *
+ * @param from the from file
+ * @param to the to file
+ * @param copyAndDeleteOnRenameFail whether to fallback and do copy and
delete, if renameTo fails
+ * @return <tt>true</tt> if the file was renamed, otherwise <tt>false</tt>
+ * @throws java.io.IOException is thrown if error renaming file
+ */
+ public static boolean renameFile(File from, File to, boolean
copyAndDeleteOnRenameFail) throws IOException {
// do not try to rename non existing files
if (!from.exists()) {
return false;
@@ -333,18 +342,14 @@ public final class FileUtil {
// we could not rename using renameTo, so lets fallback and do a
copy/delete approach.
// for example if you move files between different file systems (linux
-> windows etc.)
- if (!renamed) {
+ if (!renamed && copyAndDeleteOnRenameFail) {
// now do a copy and delete as all rename attempts failed
- try {
- LOG.debug("Cannot rename file from: {} to: {}, will now use a
copy/delete approach instead", from, to);
- copyFile(from, to);
- if (!deleteFile(from)) {
- LOG.warn("Renaming file from: {} to: {} failed due cannot
delete from file: {} after copy succeeded", new Object[]{from, to, from});
- renamed = false;
- }
+ LOG.debug("Cannot rename file from: {} to: {}, will now use a
copy/delete approach instead", from, to);
+ copyFile(from, to);
+ if (!deleteFile(from)) {
+ throw new IOException("Renaming file from: " + from + " to: "
+ to + " failed due cannot delete from file: " + from + " after copy
succeeded");
+ } else {
renamed = true;
- } catch (IOException e) {
- LOG.debug("Error renaming file from: " + from + " to: " + to +
" using copy/delete", e);
}
}
Copied:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java
(from r1177936,
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveTest.java&r1=1177936&r2=1178035&rev=1178035&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest.java
Sat Oct 1 15:58:30 2011
@@ -16,68 +16,23 @@
*/
package org.apache.camel.component.file;
-import java.io.File;
-
-import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
/**
* @version
*/
-public class FileConsumerPreMoveTest extends ContextTestSupport {
-
- @Override
- protected void setUp() throws Exception {
- deleteDirectory("target/premove");
- super.setUp();
- }
-
- public void testPreMove() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedMessageCount(1);
-
- template.sendBodyAndHeader("file://target/premove", "Hello World",
Exchange.FILE_NAME, "hello.txt");
-
- assertMockEndpointsSatisfied();
- }
-
- public void testPreMoveSameFileTwice() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:result");
- mock.expectedBodiesReceived("Hello World");
-
- template.sendBodyAndHeader("file://target/premove", "Hello World",
Exchange.FILE_NAME, "hello.txt");
-
- assertMockEndpointsSatisfied();
- oneExchangeDone.matchesMockWaitTime();
-
- // reset and drop the same file again
- mock.reset();
- mock.expectedBodiesReceived("Hello Again World");
-
- template.sendBodyAndHeader("file://target/premove", "Hello Again
World", Exchange.FILE_NAME, "hello.txt");
- assertMockEndpointsSatisfied();
- }
+public class FileConsumerPreMoveCopyAndDeleteOnRenameFailFalseTest extends
FileConsumerPreMoveTest {
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
-
from("file://target/premove?preMove=work/work-${file:name}&initialDelay=0&delay=100")
+
from("file://target/premove?preMove=work/work-${file:name}&initialDelay=0&delay=100©AndDeleteOnRenameFail=false")
.process(new MyPreMoveCheckerProcessor())
.to("mock:result");
}
};
}
- public static class MyPreMoveCheckerProcessor implements Processor {
-
- public void process(Exchange exchange) throws Exception {
- File pre = new
File("target/premove/work/work-hello.txt").getAbsoluteFile();
- assertTrue("Pre move file should exist", pre.exists());
- }
- }
}