| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.file.strategy; |
| 18 |
|
|
| 19 |
|
import java.io.File; |
| 20 |
|
import java.io.IOException; |
| 21 |
|
|
| 22 |
|
import org.apache.camel.component.file.FileEndpoint; |
| 23 |
|
import org.apache.camel.component.file.FileExchange; |
| 24 |
|
import org.apache.commons.logging.Log; |
| 25 |
|
import org.apache.commons.logging.LogFactory; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public class RenameFileProcessStrategy extends FileProcessStrategySupport { |
| 33 |
3 |
private static final transient Log LOG = LogFactory.getLog(RenameFileProcessStrategy.class); |
| 34 |
|
private FileRenamer renamer; |
| 35 |
|
|
| 36 |
|
public RenameFileProcessStrategy() { |
| 37 |
0 |
this(true); |
| 38 |
0 |
} |
| 39 |
|
|
| 40 |
|
public RenameFileProcessStrategy(boolean lock) { |
| 41 |
6 |
this(lock, ".camel/", ""); |
| 42 |
6 |
} |
| 43 |
|
|
| 44 |
|
public RenameFileProcessStrategy(boolean lock, String namePrefix, String namePostfix) { |
| 45 |
9 |
this(lock, new DefaultFileRenamer(namePrefix, namePostfix)); |
| 46 |
9 |
} |
| 47 |
|
|
| 48 |
|
public RenameFileProcessStrategy(boolean lock, FileRenamer renamer) { |
| 49 |
9 |
super(lock); |
| 50 |
9 |
this.renamer = renamer; |
| 51 |
9 |
} |
| 52 |
|
|
| 53 |
|
public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception { |
| 54 |
12 |
File newName = renamer.renameFile(file); |
| 55 |
12 |
newName.getParentFile().mkdirs(); |
| 56 |
|
|
| 57 |
12 |
if (LOG.isDebugEnabled()) { |
| 58 |
0 |
LOG.debug("Renaming file: " + file + " to: " + newName); |
| 59 |
|
} |
| 60 |
12 |
boolean renamed = file.renameTo(newName); |
| 61 |
12 |
if (!renamed) { |
| 62 |
0 |
throw new IOException("Could not rename file from: " + file + " to " + newName); |
| 63 |
|
} |
| 64 |
12 |
super.commit(endpoint, exchange, file); |
| 65 |
12 |
} |
| 66 |
|
|
| 67 |
|
public FileRenamer getRenamer() { |
| 68 |
0 |
return renamer; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public void setRenamer(FileRenamer renamer) { |
| 72 |
0 |
this.renamer = renamer; |
| 73 |
0 |
} |
| 74 |
|
} |