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 */ 017package org.apache.camel.component.file.strategy; 018 019import java.util.Map; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.Expression; 023import org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy; 024import org.apache.camel.component.file.GenericFileProcessStrategy; 025import org.apache.camel.util.ObjectHelper; 026 027public 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 (isDelete) { 044 GenericFileDeleteProcessStrategy<T> strategy = new GenericFileDeleteProcessStrategy<>(); 045 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params)); 046 if (preMoveExpression != null) { 047 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<>(); 048 renamer.setExpression(preMoveExpression); 049 strategy.setBeginRenamer(renamer); 050 } 051 if (moveFailedExpression != null) { 052 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<>(); 053 renamer.setExpression(moveFailedExpression); 054 strategy.setFailureRenamer(renamer); 055 } 056 return strategy; 057 } else if (isMove || isNoop) { 058 GenericFileRenameProcessStrategy<T> strategy = new GenericFileRenameProcessStrategy<>(); 059 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params)); 060 if (!isNoop && moveExpression != null) { 061 // move on commit is only possible if not noop 062 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<>(); 063 renamer.setExpression(moveExpression); 064 strategy.setCommitRenamer(renamer); 065 } 066 // both move and noop supports pre move 067 if (moveFailedExpression != null) { 068 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<>(); 069 renamer.setExpression(moveFailedExpression); 070 strategy.setFailureRenamer(renamer); 071 } 072 // both move and noop supports pre move 073 if (preMoveExpression != null) { 074 GenericFileExpressionRenamer<T> renamer = new GenericFileExpressionRenamer<>(); 075 renamer.setExpression(preMoveExpression); 076 strategy.setBeginRenamer(renamer); 077 } 078 return strategy; 079 } else { 080 // default strategy will do nothing 081 GenericFileNoOpProcessStrategy<T> strategy = new GenericFileNoOpProcessStrategy<>(); 082 strategy.setExclusiveReadLockStrategy((GenericFileExclusiveReadLockStrategy<T>) getExclusiveReadLockStrategy(params)); 083 return strategy; 084 } 085 } 086 087 @SuppressWarnings("unchecked") 088 private static <T> GenericFileExclusiveReadLockStrategy<T> getExclusiveReadLockStrategy(Map<String, Object> params) { 089 GenericFileExclusiveReadLockStrategy<T> strategy = (GenericFileExclusiveReadLockStrategy<T>) params.get("exclusiveReadLockStrategy"); 090 if (strategy != null) { 091 return strategy; 092 } 093 094 // no explicit strategy set then fallback to readLock option 095 String readLock = (String) params.get("readLock"); 096 if (ObjectHelper.isNotEmpty(readLock)) { 097 if ("none".equals(readLock) || "false".equals(readLock)) { 098 return null; 099 } else if ("rename".equals(readLock)) { 100 GenericFileRenameExclusiveReadLockStrategy<T> readLockStrategy = new GenericFileRenameExclusiveReadLockStrategy<>(); 101 Long timeout = (Long) params.get("readLockTimeout"); 102 if (timeout != null) { 103 readLockStrategy.setTimeout(timeout); 104 } 105 Boolean readLockMarkerFile = (Boolean) params.get("readLockMarkerFile"); 106 if (readLockMarkerFile != null) { 107 readLockStrategy.setMarkerFiler(readLockMarkerFile); 108 } 109 return readLockStrategy; 110 } 111 } 112 return null; 113 } 114}