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.Expression;
023    import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy;
024    import org.apache.camel.component.file.GenericFileProcessStrategy;
025    import org.apache.camel.language.simple.FileLanguage;
026    import org.apache.camel.util.ObjectHelper;
027    
028    public final class FileProcessStrategyFactory {
029    
030        private FileProcessStrategyFactory() {
031        }
032    
033        public static GenericFileProcessStrategy createGenericFileProcessStrategy(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<File> strategy = new GenericFileNoOpProcessStrategy<File>();
043                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
044                return strategy;
045            } else if (isDelete) {
046                GenericFileDeleteProcessStrategy<File> strategy = new GenericFileDeleteProcessStrategy<File>();
047                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
048                return strategy;
049            } else if (moveExpression != null || preMoveExpression != null) {
050                GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
051                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
052                if (moveExpression != null) {
053                    GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
054                    renamer.setExpression(moveExpression);
055                    strategy.setCommitRenamer(renamer);
056                }
057                if (preMoveExpression != null) {
058                    GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
059                    renamer.setExpression(preMoveExpression);
060                    strategy.setBeginRenamer(renamer);
061                }
062                return strategy;
063            } else {
064                // default strategy will move files in a .camel/ subfolder where the file was consumed
065                GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
066                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
067                Expression exp = FileLanguage.file("${file:parent}/.camel/${file:onlyname}");
068                strategy.setCommitRenamer(new GenericFileExpressionRenamer<File>(exp));
069                return strategy;
070            }
071        }
072    
073        @SuppressWarnings("unchecked")
074        private static GenericFileExclusiveReadLockStrategy<File> getExclusiveReadLockStrategy(Map<String, Object> params) {
075            GenericFileExclusiveReadLockStrategy strategy = (GenericFileExclusiveReadLockStrategy) params.get("exclusiveReadLockStrategy");
076            if (strategy != null) {
077                return strategy;
078            }
079    
080            // no explicit stategy set then fallback to readLock option
081            String readLock = (String) params.get("readLock");
082            if (ObjectHelper.isNotEmpty(readLock)) {
083                if ("none".equals(readLock) || "false".equals(readLock)) {
084                    return null;
085                } else if ("fileLock".equals(readLock)) {
086                    GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new FileLockExclusiveReadLockStrategy();
087                    Long timeout = (Long) params.get("readLockTimeout");
088                    if (timeout != null) {
089                        readLockStrategy.setTimeout(timeout);
090                    }
091                    return readLockStrategy;
092                } else if ("rename".equals(readLock)) {
093                    GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<File>();
094                    Long timeout = (Long) params.get("readLockTimeout");
095                    if (timeout != null) {
096                        readLockStrategy.setTimeout(timeout);
097                    }
098                    return readLockStrategy;
099                } else if ("markerFile".equals(readLock)) {
100                    return new MarkerFileExclusiveReadLockStrategy();
101                }
102            }
103    
104            return null;
105        }
106    }