| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.component.file; |
| 18 |
|
|
| 19 |
|
import org.apache.camel.Processor; |
| 20 |
|
import org.apache.camel.component.file.strategy.FileProcessStrategy; |
| 21 |
|
import org.apache.camel.impl.ScheduledPollConsumer; |
| 22 |
|
import org.apache.commons.logging.Log; |
| 23 |
|
import org.apache.commons.logging.LogFactory; |
| 24 |
|
|
| 25 |
|
import java.io.File; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
public class FileConsumer extends ScheduledPollConsumer<FileExchange> { |
| 31 |
3 |
private static final transient Log LOG = LogFactory.getLog(FileConsumer.class); |
| 32 |
|
private final FileEndpoint endpoint; |
| 33 |
18 |
private boolean recursive = true; |
| 34 |
18 |
private String regexPattern = ""; |
| 35 |
|
private long lastPollTime; |
| 36 |
|
|
| 37 |
|
public FileConsumer(final FileEndpoint endpoint, Processor processor) { |
| 38 |
18 |
super(endpoint, processor); |
| 39 |
18 |
this.endpoint = endpoint; |
| 40 |
18 |
} |
| 41 |
|
|
| 42 |
|
protected void poll() throws Exception { |
| 43 |
20 |
pollFileOrDirectory(endpoint.getFile(), isRecursive()); |
| 44 |
20 |
lastPollTime = System.currentTimeMillis(); |
| 45 |
20 |
} |
| 46 |
|
|
| 47 |
|
protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) { |
| 48 |
126 |
if (!fileOrDirectory.isDirectory()) { |
| 49 |
29 |
pollFile(fileOrDirectory); |
| 50 |
29 |
} |
| 51 |
97 |
else if (processDir) { |
| 52 |
97 |
if (isValidFile(fileOrDirectory)) { |
| 53 |
82 |
LOG.debug("Polling directory " + fileOrDirectory); |
| 54 |
82 |
File[] files = fileOrDirectory.listFiles(); |
| 55 |
188 |
for (int i = 0; i < files.length; i++) { |
| 56 |
106 |
pollFileOrDirectory(files[i], isRecursive()); |
| 57 |
|
} |
| 58 |
82 |
} |
| 59 |
|
} |
| 60 |
|
else { |
| 61 |
0 |
LOG.debug("Skipping directory " + fileOrDirectory); |
| 62 |
|
} |
| 63 |
126 |
} |
| 64 |
|
|
| 65 |
|
protected void pollFile(final File file) { |
| 66 |
29 |
if (!file.exists()) { |
| 67 |
0 |
return; |
| 68 |
|
} |
| 69 |
29 |
if (isValidFile(file)) { |
| 70 |
|
|
| 71 |
29 |
if (endpoint.isNoop()) { |
| 72 |
11 |
long fileModified = file.lastModified(); |
| 73 |
11 |
if (fileModified <= lastPollTime) { |
| 74 |
2 |
if (LOG.isDebugEnabled()) { |
| 75 |
0 |
LOG.debug("Ignoring file: " + file + " as modified time: " + fileModified + " less than last poll time: " + lastPollTime); |
| 76 |
|
} |
| 77 |
2 |
return; |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
|
| 81 |
27 |
FileProcessStrategy processStrategy = endpoint.getFileStrategy(); |
| 82 |
27 |
FileExchange exchange = endpoint.createExchange(file); |
| 83 |
|
|
| 84 |
|
try { |
| 85 |
27 |
if (LOG.isDebugEnabled()) { |
| 86 |
0 |
LOG.debug("About to process file: " + file + " using exchange: " + exchange); |
| 87 |
|
} |
| 88 |
27 |
if (processStrategy.begin(endpoint, exchange, file)) { |
| 89 |
27 |
getProcessor().process(exchange); |
| 90 |
27 |
processStrategy.commit(endpoint, exchange, file); |
| 91 |
27 |
} |
| 92 |
|
else { |
| 93 |
0 |
if (LOG.isDebugEnabled()) { |
| 94 |
0 |
LOG.debug(endpoint + " cannot process file: " + file); |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
0 |
catch (Throwable e) { |
| 99 |
0 |
handleException(e); |
| 100 |
27 |
} |
| 101 |
|
} |
| 102 |
27 |
} |
| 103 |
|
|
| 104 |
|
protected boolean isValidFile(File file) { |
| 105 |
126 |
boolean result = false; |
| 106 |
126 |
if (file != null && file.exists()) { |
| 107 |
126 |
if (isMatched(file)) { |
| 108 |
111 |
result = true; |
| 109 |
|
} |
| 110 |
|
} |
| 111 |
126 |
return result; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
protected boolean isMatched(File file) { |
| 115 |
126 |
String name = file.getName(); |
| 116 |
126 |
if (regexPattern != null && regexPattern.length() > 0) { |
| 117 |
0 |
if (!name.matches(getRegexPattern())) { |
| 118 |
0 |
return false; |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
126 |
String[] prefixes = endpoint.getExcludedNamePrefixes(); |
| 122 |
126 |
if (prefixes != null) { |
| 123 |
237 |
for (String prefix : prefixes) { |
| 124 |
126 |
if (name.startsWith(prefix)) { |
| 125 |
15 |
return false; |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
111 |
String[] postfixes = endpoint.getExcludedNamePostfixes(); |
| 130 |
111 |
if (postfixes != null) { |
| 131 |
222 |
for (String postfix : postfixes) { |
| 132 |
111 |
if (name.endsWith(postfix)) { |
| 133 |
0 |
return false; |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
111 |
return true; |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
public boolean isRecursive() { |
| 144 |
126 |
return this.recursive; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
public void setRecursive(boolean recursive) { |
| 151 |
0 |
this.recursive = recursive; |
| 152 |
0 |
} |
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
public String getRegexPattern() { |
| 158 |
0 |
return this.regexPattern; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
public void setRegexPattern(String regexPattern) { |
| 165 |
0 |
this.regexPattern = regexPattern; |
| 166 |
0 |
} |
| 167 |
|
} |