| 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.Consumer; |
| 20 |
|
import org.apache.camel.Processor; |
| 21 |
|
import org.apache.camel.Producer; |
| 22 |
|
import org.apache.camel.component.file.strategy.DefaultFileRenamer; |
| 23 |
|
import org.apache.camel.component.file.strategy.DeleteFileProcessStrategy; |
| 24 |
|
import org.apache.camel.component.file.strategy.FileProcessStrategy; |
| 25 |
|
import org.apache.camel.component.file.strategy.FileProcessStrategySupport; |
| 26 |
|
import org.apache.camel.component.file.strategy.NoOpFileProcessStrategy; |
| 27 |
|
import org.apache.camel.component.file.strategy.RenameFileProcessStrategy; |
| 28 |
|
import org.apache.camel.impl.ScheduledPollEndpoint; |
| 29 |
|
import org.apache.commons.logging.Log; |
| 30 |
|
import org.apache.commons.logging.LogFactory; |
| 31 |
|
|
| 32 |
|
import java.io.File; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
12 |
public class FileEndpoint extends ScheduledPollEndpoint<FileExchange> { |
| 41 |
3 |
private static final transient Log LOG = LogFactory.getLog(FileEndpoint.class); |
| 42 |
|
private File file; |
| 43 |
|
private FileProcessStrategy fileProcessStrategy; |
| 44 |
30 |
private boolean autoCreate = true; |
| 45 |
30 |
private boolean lock = true; |
| 46 |
|
private boolean delete; |
| 47 |
|
private boolean noop; |
| 48 |
30 |
private boolean append = true; |
| 49 |
|
private String moveNamePrefix; |
| 50 |
|
private String moveNamePostfix; |
| 51 |
30 |
private String[] excludedNamePrefixes = {"."}; |
| 52 |
30 |
private String[] excludedNamePostfixes = { FileProcessStrategySupport.DEFAULT_LOCK_FILE_POSTFIX }; |
| 53 |
30 |
private int bufferSize = 128 * 1024; |
| 54 |
|
|
| 55 |
|
protected FileEndpoint(File file, String endpointUri, FileComponent component) { |
| 56 |
30 |
super(endpointUri, component); |
| 57 |
30 |
this.file = file; |
| 58 |
30 |
} |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public Producer<FileExchange> createProducer() throws Exception { |
| 66 |
15 |
Producer<FileExchange> result = new FileProducer(this); |
| 67 |
15 |
return result; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
public Consumer<FileExchange> createConsumer(Processor file) throws Exception { |
| 77 |
18 |
Consumer<FileExchange> result = new FileConsumer(this, file); |
| 78 |
18 |
configureConsumer(result); |
| 79 |
18 |
return result; |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
public FileExchange createExchange(File file) { |
| 88 |
39 |
return new FileExchange(getContext(), file); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
public FileExchange createExchange() { |
| 96 |
12 |
return createExchange(getFile()); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
public File getFile() { |
| 100 |
62 |
if (autoCreate && !file.exists()) { |
| 101 |
3 |
file.mkdirs(); |
| 102 |
|
} |
| 103 |
62 |
return file; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
public boolean isSingleton() { |
| 107 |
30 |
return true; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
public boolean isAutoCreate() { |
| 114 |
0 |
return this.autoCreate; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
public void setAutoCreate(boolean autoCreate) { |
| 121 |
0 |
this.autoCreate = autoCreate; |
| 122 |
0 |
} |
| 123 |
|
|
| 124 |
|
public FileProcessStrategy getFileStrategy() { |
| 125 |
27 |
if (fileProcessStrategy == null) { |
| 126 |
18 |
fileProcessStrategy = createFileStrategy(); |
| 127 |
18 |
LOG.debug("" + this + " using strategy: " + fileProcessStrategy); |
| 128 |
|
} |
| 129 |
27 |
return fileProcessStrategy; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
public void setFileStrategy(FileProcessStrategy fileProcessStrategy) { |
| 139 |
0 |
this.fileProcessStrategy = fileProcessStrategy; |
| 140 |
0 |
} |
| 141 |
|
|
| 142 |
|
public boolean isDelete() { |
| 143 |
12 |
return delete; |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
public void setDelete(boolean delete) { |
| 147 |
9 |
this.delete = delete; |
| 148 |
9 |
} |
| 149 |
|
|
| 150 |
|
public boolean isLock() { |
| 151 |
12 |
return lock; |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
public void setLock(boolean lock) { |
| 155 |
0 |
this.lock = lock; |
| 156 |
0 |
} |
| 157 |
|
|
| 158 |
|
public String getMoveNamePostfix() { |
| 159 |
0 |
return moveNamePostfix; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
public void setMoveNamePostfix(String moveNamePostfix) { |
| 170 |
0 |
this.moveNamePostfix = moveNamePostfix; |
| 171 |
0 |
} |
| 172 |
|
|
| 173 |
|
public String getMoveNamePrefix() { |
| 174 |
0 |
return moveNamePrefix; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
|
| 184 |
|
public void setMoveNamePrefix(String moveNamePrefix) { |
| 185 |
3 |
this.moveNamePrefix = moveNamePrefix; |
| 186 |
3 |
} |
| 187 |
|
|
| 188 |
|
public String[] getExcludedNamePrefixes() { |
| 189 |
126 |
return excludedNamePrefixes; |
| 190 |
|
} |
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
|
| 195 |
|
|
| 196 |
|
public void setExcludedNamePrefixes(String[] excludedNamePrefixes) { |
| 197 |
0 |
this.excludedNamePrefixes = excludedNamePrefixes; |
| 198 |
0 |
} |
| 199 |
|
|
| 200 |
|
public String[] getExcludedNamePostfixes() { |
| 201 |
111 |
return excludedNamePostfixes; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
public void setExcludedNamePostfixes(String[] excludedNamePostfixes) { |
| 209 |
0 |
this.excludedNamePostfixes = excludedNamePostfixes; |
| 210 |
0 |
} |
| 211 |
|
|
| 212 |
|
public boolean isNoop() { |
| 213 |
47 |
return noop; |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
public void setNoop(boolean noop) { |
| 223 |
6 |
this.noop = noop; |
| 224 |
6 |
} |
| 225 |
|
|
| 226 |
|
public boolean isAppend() { |
| 227 |
18 |
return append; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
public void setAppend(boolean append) { |
| 237 |
0 |
this.append = append; |
| 238 |
0 |
} |
| 239 |
|
|
| 240 |
|
public int getBufferSize() { |
| 241 |
18 |
return bufferSize; |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
|
| 245 |
|
|
| 246 |
|
|
| 247 |
|
public void setBufferSize(int bufferSize) { |
| 248 |
0 |
this.bufferSize = bufferSize; |
| 249 |
0 |
} |
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
protected FileProcessStrategy createFileStrategy() { |
| 255 |
18 |
if (isNoop()) { |
| 256 |
6 |
return new NoOpFileProcessStrategy(); |
| 257 |
12 |
} else if (moveNamePostfix != null || moveNamePrefix != null) { |
| 258 |
3 |
if (isDelete()) { |
| 259 |
0 |
throw new IllegalArgumentException( |
| 260 |
|
"You cannot set the deleteFiles property and a moveFilenamePostfix or moveFilenamePrefix"); |
| 261 |
|
} |
| 262 |
3 |
return new RenameFileProcessStrategy(isLock(), moveNamePrefix, moveNamePostfix); |
| 263 |
9 |
} else if (isDelete()) { |
| 264 |
3 |
return new DeleteFileProcessStrategy(isLock()); |
| 265 |
|
} else { |
| 266 |
6 |
return new RenameFileProcessStrategy(isLock()); |
| 267 |
|
} |
| 268 |
|
} |
| 269 |
|
} |