1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.file; |
19 |
|
|
20 |
|
import org.apache.camel.Processor; |
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 |
|
import java.io.IOException; |
27 |
|
import java.io.RandomAccessFile; |
28 |
|
import java.nio.channels.FileChannel; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
public class FileConsumer extends ScheduledPollConsumer<FileExchange> { |
34 |
1 |
private static final transient Log log = LogFactory.getLog(FileConsumer.class); |
35 |
|
private final FileEndpoint endpoint; |
36 |
1 |
private boolean recursive = true; |
37 |
1 |
private boolean attemptFileLock = false; |
38 |
1 |
private String regexPattern = ""; |
39 |
1 |
private long lastPollTime = 0l; |
40 |
|
|
41 |
|
public FileConsumer(final FileEndpoint endpoint, Processor processor) { |
42 |
1 |
super(endpoint, processor); |
43 |
1 |
this.endpoint = endpoint; |
44 |
1 |
} |
45 |
|
|
46 |
|
protected void poll() throws Exception { |
47 |
1 |
pollFileOrDirectory(endpoint.getFile(), isRecursive()); |
48 |
1 |
lastPollTime = System.currentTimeMillis(); |
49 |
1 |
} |
50 |
|
|
51 |
|
protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) { |
52 |
796 |
if (!fileOrDirectory.isDirectory()) { |
53 |
710 |
pollFile(fileOrDirectory); |
54 |
710 |
} |
55 |
86 |
else if (processDir) { |
56 |
86 |
log.debug("Polling directory " + fileOrDirectory); |
57 |
86 |
File[] files = fileOrDirectory.listFiles(); |
58 |
881 |
for (int i = 0; i < files.length; i++) { |
59 |
795 |
pollFileOrDirectory(files[i], isRecursive()); |
60 |
|
} |
61 |
86 |
} |
62 |
|
else { |
63 |
0 |
log.debug("Skipping directory " + fileOrDirectory); |
64 |
|
} |
65 |
796 |
} |
66 |
|
|
67 |
|
protected void pollFile(final File file) { |
68 |
710 |
if (file.exists() && file.lastModified() > lastPollTime) { |
69 |
710 |
if (isValidFile(file)) { |
70 |
710 |
processFile(file); |
71 |
|
} |
72 |
|
} |
73 |
710 |
} |
74 |
|
|
75 |
|
protected void processFile(File file) { |
76 |
|
try { |
77 |
710 |
getProcessor().process(endpoint.createExchange(file)); |
78 |
0 |
} catch (Throwable e) { |
79 |
0 |
handleException(e); |
80 |
710 |
} |
81 |
710 |
} |
82 |
|
|
83 |
|
protected boolean isValidFile(File file) { |
84 |
710 |
boolean result = false; |
85 |
710 |
if (file != null && file.exists()) { |
86 |
710 |
if (isMatched(file)) { |
87 |
710 |
if (isAttemptFileLock()) { |
88 |
0 |
FileChannel fc = null; |
89 |
|
try { |
90 |
0 |
fc = new RandomAccessFile(file, "rw").getChannel(); |
91 |
0 |
fc.lock(); |
92 |
0 |
result = true; |
93 |
|
} |
94 |
0 |
catch (Throwable e) { |
95 |
0 |
log.debug("Failed to get the lock on file: " + file, e); |
96 |
|
} |
97 |
|
finally { |
98 |
0 |
if (fc != null) { |
99 |
|
try { |
100 |
0 |
fc.close(); |
101 |
|
} |
102 |
0 |
catch (IOException e) { |
103 |
0 |
} |
104 |
0 |
} |
105 |
0 |
} |
106 |
0 |
} |
107 |
|
else { |
108 |
710 |
result = true; |
109 |
|
} |
110 |
|
} |
111 |
|
} |
112 |
710 |
return result; |
113 |
|
} |
114 |
|
|
115 |
|
protected boolean isMatched(File file) { |
116 |
710 |
boolean result = true; |
117 |
710 |
if (regexPattern != null && regexPattern.length() > 0) { |
118 |
0 |
result = file.getName().matches(getRegexPattern()); |
119 |
|
} |
120 |
710 |
return result; |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
public boolean isRecursive() { |
127 |
796 |
return this.recursive; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
public void setRecursive(boolean recursive) { |
134 |
0 |
this.recursive = recursive; |
135 |
0 |
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
public boolean isAttemptFileLock() { |
141 |
710 |
return this.attemptFileLock; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
public void setAttemptFileLock(boolean attemptFileLock) { |
148 |
0 |
this.attemptFileLock = attemptFileLock; |
149 |
0 |
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
public String getRegexPattern() { |
155 |
0 |
return this.regexPattern; |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
public void setRegexPattern(String regexPattern) { |
162 |
0 |
this.regexPattern = regexPattern; |
163 |
0 |
} |
164 |
|
} |