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