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
021 import org.apache.camel.component.file.FileExchange;
022
023 /**
024 * Camel default file renamer.
025 *
026 * @version $Revision: 687736 $
027 */
028 public class DefaultFileRenamer implements FileRenamer {
029
030 private static final boolean ON_WINDOWS = System.getProperty("os.name").startsWith("Windows");
031
032 private String namePrefix;
033 private String namePostfix;
034
035 public DefaultFileRenamer() {
036 }
037
038 public DefaultFileRenamer(String namePrefix, String namePostfix) {
039 this.namePrefix = namePrefix;
040 this.namePostfix = namePostfix;
041 }
042
043 public File renameFile(FileExchange exchange, File file) {
044 File parent = file.getParentFile();
045 String name = renameFileName(file);
046
047 if (ON_WINDOWS && (name.indexOf(":") >= 0 || name.startsWith("//"))) {
048 return new File(name);
049 }
050 return new File(parent, name);
051 }
052
053 public String getNamePostfix() {
054 return namePostfix;
055 }
056
057 /**
058 * Sets the name postfix appended to moved files. For example
059 * to rename all the files from * to *.done set this value to ".done"
060 */
061 public void setNamePostfix(String namePostfix) {
062 this.namePostfix = namePostfix;
063 }
064
065 public String getNamePrefix() {
066 return namePrefix;
067 }
068
069 /**
070 * Sets the name prefix appended to moved files. For example
071 * to move processed files into a hidden directory called ".camel"
072 * set this value to ".camel/"
073 */
074 public void setNamePrefix(String namePrefix) {
075 this.namePrefix = namePrefix;
076 }
077
078 protected String renameFileName(File file) {
079 StringBuffer buffer = new StringBuffer();
080 if (namePrefix != null) {
081 buffer.append(namePrefix);
082 }
083 buffer.append(file.getName());
084 if (namePostfix != null) {
085 buffer.append(namePostfix);
086 }
087 return buffer.toString();
088 }
089 }