Coverage Report - org.apache.camel.component.file.strategy.RenameFileProcessStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
RenameFileProcessStrategy
68% 
100% 
1.429
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.component.file.strategy;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.IOException;
 21  
 
 22  
 import org.apache.camel.component.file.FileEndpoint;
 23  
 import org.apache.camel.component.file.FileExchange;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 /**
 28  
  * A strategy to rename a file
 29  
  * 
 30  
  * @version $Revision: 1.1 $
 31  
  */
 32  
 public class RenameFileProcessStrategy extends FileProcessStrategySupport {
 33  3
     private static final transient Log LOG = LogFactory.getLog(RenameFileProcessStrategy.class);
 34  
     private FileRenamer renamer;
 35  
 
 36  
     public RenameFileProcessStrategy() {
 37  0
         this(true);
 38  0
     }
 39  
 
 40  
     public RenameFileProcessStrategy(boolean lock) {
 41  6
         this(lock, ".camel/", "");
 42  6
     }
 43  
 
 44  
     public RenameFileProcessStrategy(boolean lock, String namePrefix, String namePostfix) {
 45  9
         this(lock, new DefaultFileRenamer(namePrefix, namePostfix));
 46  9
     }
 47  
 
 48  
     public RenameFileProcessStrategy(boolean lock, FileRenamer renamer) {
 49  9
         super(lock);
 50  9
         this.renamer = renamer;
 51  9
     }
 52  
 
 53  
     public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
 54  12
         File newName = renamer.renameFile(file);
 55  12
         newName.getParentFile().mkdirs();
 56  
 
 57  12
         if (LOG.isDebugEnabled()) {
 58  0
             LOG.debug("Renaming file: " + file + " to: " + newName);
 59  
         }
 60  12
         boolean renamed = file.renameTo(newName);
 61  12
         if (!renamed) {
 62  0
             throw new IOException("Could not rename file from: " + file + " to " + newName);
 63  
         }
 64  12
         super.commit(endpoint, exchange, file);
 65  12
     }
 66  
 
 67  
     public FileRenamer getRenamer() {
 68  0
         return renamer;
 69  
     }
 70  
 
 71  
     public void setRenamer(FileRenamer renamer) {
 72  0
         this.renamer = renamer;
 73  0
     }
 74  
 }