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 GenericFileProcessStrategy 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 boolean isNoop = params.get("noop") != null;
037 boolean isDelete = params.get("delete") != null;
038 Expression moveExpression = (Expression) params.get("move");
039 Expression preMoveExpression = (Expression) params.get("preMove");
040
041 if (isNoop) {
042 GenericFileNoOpProcessStrategy strategy = new GenericFileNoOpProcessStrategy();
043 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
044 return strategy;
045 } else if (isDelete) {
046 GenericFileDeleteProcessStrategy strategy = new GenericFileDeleteProcessStrategy();
047 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
048 return strategy;
049 } else if (moveExpression != null || preMoveExpression != null) {
050 GenericFileRenameProcessStrategy strategy = new GenericFileRenameProcessStrategy();
051 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
052 if (moveExpression != null) {
053 GenericFileExpressionRenamer renamer = new GenericFileExpressionRenamer();
054 renamer.setExpression(moveExpression);
055 strategy.setCommitRenamer(renamer);
056 }
057 if (preMoveExpression != null) {
058 GenericFileExpressionRenamer renamer = new GenericFileExpressionRenamer();
059 renamer.setExpression(preMoveExpression);
060 strategy.setBeginRenamer(renamer);
061 }
062 return strategy;
063 } else {
064 // default strategy will do nothing
065 GenericFileNoOpProcessStrategy strategy = new GenericFileNoOpProcessStrategy();
066 strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
067 return strategy;
068 }
069 }
070
071 private static GenericFileExclusiveReadLockStrategy getExclusiveReadLockStrategy(Map<String, Object> params) {
072 GenericFileExclusiveReadLockStrategy strategy = (GenericFileExclusiveReadLockStrategy) params.get("exclusiveReadLockStrategy");
073 if (strategy != null) {
074 return strategy;
075 }
076
077 // no explicit stategy set then fallback to readLock option
078 String readLock = (String) params.get("readLock");
079 if (ObjectHelper.isNotEmpty(readLock)) {
080 if ("none".equals(readLock) || "false".equals(readLock)) {
081 return null;
082 } else if ("rename".equals(readLock)) {
083 GenericFileRenameExclusiveReadLockStrategy readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy();
084 Long timeout = (Long) params.get("readLockTimeout");
085 if (timeout != null) {
086 readLockStrategy.setTimeout(timeout);
087 }
088 return readLockStrategy;
089 }
090 }
091
092 return null;
093 }
094 }