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.util.Map;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.Expression;
023 import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
024 import org.apache.camel.component.file.GenericFileProcessStrategy;
025 import org.apache.camel.util.ObjectHelper;
026
027 public final class GenericFileProcessStrategyFactory {
028
029 private GenericFileProcessStrategyFactory() {
030 }
031
032 @SuppressWarnings("unchecked")
033 public static <T> GenericFileProcessStrategy<T> createGenericFileProcessStrategy(CamelContext context, Map<String, Object> params) {
034
035 // We assume a value is present only if its value not null for String and 'true' for boolean
036 Expression moveExpression = (Expression) params.get("move");
037 Expression moveFailedExpression = (Expression) params.get("moveFailed");
038 Expression preMoveExpression = (Expression) params.get("preMove");
039 boolean isNoop = params.get("noop") != null;
040 boolean isDelete = params.get("delete") != null;
041 boolean isMove = moveExpression != null || preMoveExpression != null || moveFailedExpression != null;
042
043 if (isNoop) {
044 GenericFileNoOpProcessStrategy<T> strategy = new GenericFileNoOpProcessStrategy<T>();
045 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params));
046 return strategy;
047 } else if (isDelete) {
048 GenericFileDeleteProcessStrategy<T> strategy = new GenericFileDeleteProcessStrategy<T>();
049 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params));
050 return strategy;
051 } else if (isMove) {
052 GenericFileRenameProcessStrategy<T> strategy = new GenericFileRenameProcessStrategy<T>();
053 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params));
054 if (moveExpression != null) {
055 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<T>();
056 renamer.setExpression(moveExpression);
057 strategy.setCommitRenamer(renamer);
058 }
059 if (moveFailedExpression != null) {
060 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<T>();
061 renamer.setExpression(moveFailedExpression);
062 strategy.setFailureRenamer(renamer);
063 }
064 if (preMoveExpression != null) {
065 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<T>();
066 renamer.setExpression(preMoveExpression);
067 strategy.setBeginRenamer(renamer);
068 }
069 return strategy;
070 } else {
071 // default strategy will do nothing
072 GenericFileNoOpProcessStrategy<T> strategy = new GenericFileNoOpProcessStrategy<T>();
073 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params));
074 return strategy;
075 }
076 }
077
078 @SuppressWarnings("unchecked")
079 private static <T> GenericFileExclusiveReadLockStrategy<T> getExclusiveReadLockStrategy(Map<String, Object> params) {
080 GenericFileExclusiveReadLockStrategy<T> strategy = (GenericFileExclusiveReadLockStrategy<T>) params.get("exclusiveReadLockStrategy");
081 if (strategy != null) {
082 return strategy;
083 }
084
085 // no explicit stategy set then fallback to readLock option
086 String readLock = (String) params.get("readLock");
087 if (ObjectHelper.isNotEmpty(readLock)) {
088 if ("none".equals(readLock) || "false".equals(readLock)) {
089 return null;
090 } else if ("rename".equals(readLock)) {
091 GenericFileRenameExclusiveReadLockStrategy<T> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<T>();
092 Long timeout = (Long) params.get("readLockTimeout");
093 if (timeout != null) {
094 readLockStrategy.setTimeout(timeout);
095 }
096 return readLockStrategy;
097 }
098 }
099 return null;
100 }
101 }