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