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<File> 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            Expression moveExpression = (Expression) params.get("move");
038            Expression moveFailedExpression = (Expression) params.get("moveFailed");
039            Expression preMoveExpression = (Expression) params.get("preMove");
040            boolean isNoop = params.get("noop") != null;
041            boolean isDelete = params.get("delete") != null;
042            boolean isMove = moveExpression != null || preMoveExpression != null || moveFailedExpression != null;
043    
044            if (isNoop) {
045                GenericFileNoOpProcessStrategy<File> strategy = new GenericFileNoOpProcessStrategy<File>();
046                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
047                return strategy;
048            } else if (isDelete) {
049                GenericFileDeleteProcessStrategy<File> strategy = new GenericFileDeleteProcessStrategy<File>();
050                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
051                return strategy;
052            } else if (isMove) {
053                GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
054                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
055                if (moveExpression != null) {
056                    GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
057                    renamer.setExpression(moveExpression);
058                    strategy.setCommitRenamer(renamer);
059                } else {
060                    strategy.setCommitRenamer(getDefaultCommitRenamer(context));
061                }
062                if (preMoveExpression != null) {
063                    GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
064                    renamer.setExpression(preMoveExpression);
065                    strategy.setBeginRenamer(renamer);
066                }
067                if (moveFailedExpression != null) {
068                    GenericFileExpressionRenamer<File> renamer = new GenericFileExpressionRenamer<File>();
069                    renamer.setExpression(moveFailedExpression);
070                    strategy.setFailureRenamer(renamer);
071                }
072                return strategy;
073            } else {
074                // default strategy will move files in a .camel/ subfolder where the file was consumed
075                GenericFileRenameProcessStrategy<File> strategy = new GenericFileRenameProcessStrategy<File>();
076                strategy.setExclusiveReadLockStrategy(getExclusiveReadLockStrategy(params));
077                strategy.setCommitRenamer(getDefaultCommitRenamer(context));
078                return strategy;
079            }
080        }
081    
082        private static GenericFileExpressionRenamer<File> getDefaultCommitRenamer(CamelContext context) {
083            // use context to lookup language to let it be loose coupled
084            Language language = context.resolveLanguage("file");
085            Expression expression = language.createExpression("${file:parent}/.camel/${file:onlyname}");
086            return new GenericFileExpressionRenamer<File>(expression);
087        }
088    
089        @SuppressWarnings("unchecked")
090        private static GenericFileExclusiveReadLockStrategy<File> getExclusiveReadLockStrategy(Map<String, Object> params) {
091            GenericFileExclusiveReadLockStrategy strategy = (GenericFileExclusiveReadLockStrategy) params.get("exclusiveReadLockStrategy");
092            if (strategy != null) {
093                return strategy;
094            }
095    
096            // no explicit stategy set then fallback to readLock option
097            String readLock = (String) params.get("readLock");
098            if (ObjectHelper.isNotEmpty(readLock)) {
099                if ("none".equals(readLock) || "false".equals(readLock)) {
100                    return null;
101                } else if ("fileLock".equals(readLock)) {
102                    GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new FileLockExclusiveReadLockStrategy();
103                    Long timeout = (Long) params.get("readLockTimeout");
104                    if (timeout != null) {
105                        readLockStrategy.setTimeout(timeout);
106                    }
107                    return readLockStrategy;
108                } else if ("rename".equals(readLock)) {
109                    GenericFileExclusiveReadLockStrategy<File> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<File>();
110                    Long timeout = (Long) params.get("readLockTimeout");
111                    if (timeout != null) {
112                        readLockStrategy.setTimeout(timeout);
113                    }
114                    return readLockStrategy;
115                } else if ("changed".equals(readLock)) {
116                    GenericFileExclusiveReadLockStrategy readLockStrategy = new FileChangedExclusiveReadLockStrategy();
117                    Long timeout = (Long) params.get("readLockTimeout");
118                    if (timeout != null) {
119                        readLockStrategy.setTimeout(timeout);
120                    }
121                    return readLockStrategy;
122                } else if ("markerFile".equals(readLock)) {
123                    return new MarkerFileExclusiveReadLockStrategy();
124                }
125            }
126    
127            return null;
128        }
129    }