001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.file.remote;
018
019 import java.util.concurrent.ScheduledExecutorService;
020
021 import org.apache.camel.Exchange;
022 import org.apache.camel.Processor;
023 import org.apache.camel.impl.ScheduledPollConsumer;
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026
027 public abstract class RemoteFileConsumer<T extends RemoteFileExchange> extends ScheduledPollConsumer<T> {
028 protected final transient Log log = LogFactory.getLog(getClass());
029 protected RemoteFileEndpoint<T> endpoint;
030
031 // @deprecated lastPollTime to be removed in Camel 2.0
032 protected long lastPollTime;
033
034 protected boolean recursive;
035 protected String regexPattern;
036 protected boolean setNames = true;
037 protected boolean exclusiveReadLock;
038 protected boolean deleteFile;
039 protected String moveNamePrefix;
040 protected String moveNamePostfix;
041 protected String excludedNamePrefix;
042 protected String excludedNamePostfix;
043 private boolean timestamp;
044
045 public RemoteFileConsumer(RemoteFileEndpoint<T> endpoint, Processor processor) {
046 super(endpoint, processor);
047 this.endpoint = endpoint;
048 }
049
050 public RemoteFileConsumer(RemoteFileEndpoint<T> endpoint, Processor processor,
051 ScheduledExecutorService executor) {
052 super(endpoint, processor, executor);
053 }
054
055 /**
056 * Gets the filename.
057 *
058 * @param file the file object for the given consumer implementation.
059 * @return the filename as String.
060 */
061 protected abstract String getFileName(Object file);
062
063 /**
064 * Is the given file matched to be consumed.
065 */
066 protected boolean isMatched(Object file) {
067 String name = getFileName(file);
068
069 // folders/names starting with dot is always skipped (eg. ".", ".camel", ".camelLock")
070 if (name.startsWith(".")) {
071 return false;
072 }
073
074 if (regexPattern != null && regexPattern.length() > 0) {
075 if (!name.matches(regexPattern)) {
076 return false;
077 }
078 }
079
080 if (excludedNamePrefix != null) {
081 if (name.startsWith(excludedNamePrefix)) {
082 return false;
083 }
084 }
085 if (excludedNamePostfix != null) {
086 if (name.endsWith(excludedNamePostfix)) {
087 return false;
088 }
089 }
090
091 return true;
092 }
093
094 /**
095 * Should the file be moved after consuming?
096 */
097 protected boolean isMoveFile() {
098 return moveNamePostfix != null || moveNamePrefix != null || endpoint.getConfiguration().getExpression() != null;
099 }
100
101 /**
102 * Gets the to filename for moving.
103 *
104 * @param name the original filename
105 * @param exchange the current exchange
106 * @return the move filename
107 */
108 protected String getMoveFileName(String name, Exchange exchange) {
109 // move according to the expression
110 if (endpoint.getConfiguration().getExpression() != null) {
111 Object result = endpoint.getConfiguration().getExpression() .evaluate(exchange);
112 return exchange.getContext().getTypeConverter().convertTo(String.class, result);
113 }
114
115 // move according to the pre and postfix
116 StringBuffer buffer = new StringBuffer();
117 if (moveNamePrefix != null) {
118 buffer.append(moveNamePrefix);
119 }
120 buffer.append(name);
121 if (moveNamePostfix != null) {
122 buffer.append(moveNamePostfix);
123 }
124 return buffer.toString();
125 }
126
127 protected String remoteServer() {
128 return endpoint.getConfiguration().remoteServerInformation();
129 }
130
131 public boolean isRecursive() {
132 return recursive;
133 }
134
135 public void setRecursive(boolean recursive) {
136 this.recursive = recursive;
137 }
138
139 public long getLastPollTime() {
140 return lastPollTime;
141 }
142
143 public void setLastPollTime(long lastPollTime) {
144 this.lastPollTime = lastPollTime;
145 }
146
147 public String getRegexPattern() {
148 return regexPattern;
149 }
150
151 public void setRegexPattern(String regexPattern) {
152 this.regexPattern = regexPattern;
153 }
154
155 public boolean isSetNames() {
156 return setNames;
157 }
158
159 public void setSetNames(boolean setNames) {
160 this.setNames = setNames;
161 }
162
163 public boolean isExclusiveReadLock() {
164 return exclusiveReadLock;
165 }
166
167 public void setExclusiveReadLock(boolean exclusiveReadLock) {
168 this.exclusiveReadLock = exclusiveReadLock;
169 }
170
171 public boolean isDeleteFile() {
172 return deleteFile;
173 }
174
175 public void setDeleteFile(boolean deleteFile) {
176 this.deleteFile = deleteFile;
177 }
178
179 public String getMoveNamePrefix() {
180 return moveNamePrefix;
181 }
182
183 public void setMoveNamePrefix(String moveNamePrefix) {
184 this.moveNamePrefix = moveNamePrefix;
185 }
186
187 public String getMoveNamePostfix() {
188 return moveNamePostfix;
189 }
190
191 public void setMoveNamePostfix(String moveNamePostfix) {
192 this.moveNamePostfix = moveNamePostfix;
193 }
194
195 public String getExcludedNamePrefix() {
196 return excludedNamePrefix;
197 }
198
199 public void setExcludedNamePrefix(String excludedNamePrefix) {
200 this.excludedNamePrefix = excludedNamePrefix;
201 }
202
203 public String getExcludedNamePostfix() {
204 return excludedNamePostfix;
205 }
206
207 public void setExcludedNamePostfix(String excludedNamePostfix) {
208 this.excludedNamePostfix = excludedNamePostfix;
209 }
210
211 /**
212 * @deprecated the timestamp feature will be removed in Camel 2.0
213 */
214 public boolean isTimestamp() {
215 return timestamp;
216 }
217
218 /**
219 * Sets wether polling should use last poll timestamp for filtering only new files.
220 * @deprecated the timestamp feature will be removed in Camel 2.0
221 */
222 public void setTimestamp(boolean timestamp) {
223 this.timestamp = timestamp;
224 }
225 }