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.strategy;
018
019 import java.io.File;
020 import java.util.Map;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.Expression;
024 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
025 import org.apache.camel.component.file.GenericFileProcessStrategy;
026 import org.apache.camel.spi.Language;
027 import org.apache.camel.util.ObjectHelper;
028
029 public final class FileProcessStrategyFactory {
030
031 private FileProcessStrategyFactory() {
032 }
033
034 public static GenericFileProcessStrategy createGenericFileProcessStrategy(CamelContext context, Map<String, Object> params) {
035
036 // We assume a value is present only if its value not null for String and 'true' for boolean
037 boolean isNoop = params.get("noop") != null;
038 boolean isDelete = params.get("delete") != null;
039 Expression moveExpression = (Expression) params.get("move");
040 Expression preMoveExpression = (Expression) params.get("preMove");
041
042 if (isNoop) {
043 GenericFileNoOpProcessStrategy<File> strategy = new GenericFileNoOpProcessStrategy<File>();
044 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
045 return strategy;
046 } else if (isDelete) {
047 GenericFileDeleteProcessStrategy<File> strategy = new GenericFileDeleteProcessStrategy<File>();
048 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
049 return strategy;
050 } else if (moveExpression != null || preMoveExpression != null) {
051 GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
052 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
053 if (moveExpression != null) {
054 GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
055 renamer.setExpression(moveExpression);
056 strategy.setCommitRenamer(renamer);
057 }
058 if (preMoveExpression != null) {
059 GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
060 renamer.setExpression(preMoveExpression);
061 strategy.setBeginRenamer(renamer);
062 }
063 return strategy;
064 } else {
065 // default strategy will move files in a .camel/ subfolder where the file was consumed
066 GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
067 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
068 // use context to lookup language to let it be loose coupled
069 Language language = context.resolveLanguage("file");
070 Expression expression = language.createExpression("${file:parent}/.camel/${file:onlyname}");
071 strategy.setCommitRenamer(new GenericFileExpressionRenamer<File>(expression));
072 return strategy;
073 }
074 }
075
076 @SuppressWarnings("unchecked")
077 private static GenericFileExclusiveReadLockStrategy<File> getExclusiveReadLockStrategy(Map<String, Object> params) {
078 GenericFileExclusiveReadLockStrategy strategy = (GenericFileExclusiveReadLockStrategy) params.get("exclusiveReadLockStrategy");
079 if (strategy != null) {
080 return strategy;
081 }
082
083 // no explicit stategy set then fallback to readLock option
084 String readLock = (String) params.get("readLock");
085 if (ObjectHelper.isNotEmpty(readLock)) {
086 if ("none".equals(readLock) || "false".equals(readLock)) {
087 return null;
088 } else if ("fileLock".equals(readLock)) {
089 GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new FileLockExclusiveReadLockStrategy();
090 Long timeout = (Long) params.get("readLockTimeout");
091 if (timeout != null) {
092 readLockStrategy.setTimeout(timeout);
093 }
094 return readLockStrategy;
095 } else if ("rename".equals(readLock)) {
096 GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<File>();
097 Long timeout = (Long) params.get("readLockTimeout");
098 if (timeout != null) {
099 readLockStrategy.setTimeout(timeout);
100 }
101 return readLockStrategy;
102 } else if ("markerFile".equals(readLock)) {
103 return new MarkerFileExclusiveReadLockStrategy();
104 }
105 }
106
107 return null;
108 }
109 }