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.io.IOException;
021
022 import org.apache.camel.component.file.FileEndpoint;
023 import org.apache.camel.component.file.FileExchange;
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026
027 /**
028 * A strategy to rename a file
029 *
030 * @version $Revision: 723860 $
031 */
032 public class RenameFileProcessStrategy extends FileProcessStrategySupport {
033 private static final transient Log LOG = LogFactory.getLog(RenameFileProcessStrategy.class);
034 private FileRenamer beginRenamer;
035 private FileRenamer commitRenamer;
036
037 public RenameFileProcessStrategy() {
038 this(true);
039 }
040
041 public RenameFileProcessStrategy(boolean lock) {
042 this(lock, ".camel/", "");
043 }
044
045 public RenameFileProcessStrategy(boolean lock, String namePrefix, String namePostfix) {
046 this(lock, new DefaultFileRenamer(namePrefix, namePostfix), null);
047 }
048
049 public RenameFileProcessStrategy(boolean lock, String namePrefix, String namePostfix, String preNamePrefix, String preNamePostfix) {
050 this(lock, new DefaultFileRenamer(namePrefix, namePostfix), new DefaultFileRenamer(preNamePrefix, preNamePostfix));
051 }
052
053 public RenameFileProcessStrategy(boolean lock, FileRenamer commitRenamer, FileRenamer beginRenamer) {
054 super(lock);
055 this.commitRenamer = commitRenamer;
056 this.beginRenamer = beginRenamer;
057 }
058
059 @Override
060 public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
061 boolean answer = super.begin(endpoint, exchange, file);
062
063 if (beginRenamer != null) {
064 File newName = beginRenamer.renameFile(exchange, file);
065 // deleting any existing files before renaming
066 File to = renameFile(file, newName);
067 exchange.setFile(to);
068 }
069
070 return answer;
071 }
072
073 @Override
074 public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
075 File newName = commitRenamer.renameFile(exchange, file);
076 renameFile(file, newName);
077
078 // must commit to release the lock
079 super.commit(endpoint, exchange, file);
080 }
081
082 private static File renameFile(File from, File to) throws IOException {
083 // deleting any existing files before renaming
084 if (to.exists()) {
085 to.delete();
086 }
087
088 // make parent folder if missing
089 File parent = to.getParentFile();
090 if (!parent.exists()) {
091 if (LOG.isDebugEnabled()) {
092 LOG.debug("Creating directory: " + parent);
093 }
094 boolean mkdir = parent.mkdirs();
095 if (!mkdir) {
096 throw new IOException("Can not create directory: " + parent);
097 }
098 }
099
100 if (LOG.isDebugEnabled()) {
101 LOG.debug("Renaming file: " + from + " to: " + to);
102 }
103 boolean renamed = from.renameTo(to);
104 if (!renamed) {
105 throw new IOException("Can not rename file from: " + from + " to: " + to);
106 }
107
108 return to;
109 }
110
111 public FileRenamer getBeginRenamer() {
112 return beginRenamer;
113 }
114
115 public void setBeginRenamer(FileRenamer beginRenamer) {
116 this.beginRenamer = beginRenamer;
117 }
118
119 public FileRenamer getCommitRenamer() {
120 return commitRenamer;
121 }
122
123 public void setCommitRenamer(FileRenamer commitRenamer) {
124 this.commitRenamer = commitRenamer;
125 }
126
127 }