Coverage Report - org.apache.camel.component.file.strategy.FileProcessStrategySupport
 
Classes in this File Line Coverage Branch Coverage Complexity
FileProcessStrategySupport
74% 
100% 
1.778
 
 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.RandomAccessFile;
 21  
 import java.nio.channels.Channel;
 22  
 import java.nio.channels.FileChannel;
 23  
 import java.nio.channels.FileLock;
 24  
 
 25  
 import org.apache.camel.component.file.FileEndpoint;
 26  
 import org.apache.camel.component.file.FileExchange;
 27  
 import org.apache.camel.util.ExchangeHelper;
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 /**
 32  
  * @version $Revision: 1.1 $
 33  
  */
 34  
 public abstract class FileProcessStrategySupport implements FileProcessStrategy {
 35  
     public static final String DEFAULT_LOCK_FILE_POSTFIX = ".cameLock";
 36  
     
 37  3
     private static final transient Log LOG = LogFactory.getLog(FileProcessStrategySupport.class);
 38  
     private boolean lockFile;
 39  
     private FileRenamer lockFileRenamer;
 40  
 
 41  
     protected FileProcessStrategySupport() {
 42  0
         this(true);
 43  0
     }
 44  
 
 45  
     protected FileProcessStrategySupport(boolean lockFile) {
 46  18
         this(lockFile, new DefaultFileRenamer(null, DEFAULT_LOCK_FILE_POSTFIX));
 47  18
     }
 48  
 
 49  18
     protected FileProcessStrategySupport(boolean lockFile, FileRenamer lockFileRenamer) {
 50  18
         this.lockFile = lockFile;
 51  18
         this.lockFileRenamer = lockFileRenamer;
 52  18
     }
 53  
 
 54  
     public boolean begin(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
 55  27
         if (isLockFile()) {
 56  18
             File newFile = lockFileRenamer.renameFile(file);
 57  18
             String lockFileName = newFile.getAbsolutePath();
 58  18
             if (LOG.isDebugEnabled()) {
 59  0
                 LOG.debug("Locking the file: " + file + " using the lock file name: " + lockFileName);
 60  
             }
 61  
 
 62  18
             FileChannel channel = new RandomAccessFile(lockFileName, "rw").getChannel();
 63  18
             FileLock lock = channel.lock();
 64  18
             if (lock != null) {
 65  18
                 exchange.setProperty("org.apache.camel.fileChannel", channel);
 66  18
                 exchange.setProperty("org.apache.camel.file.lock", lock);
 67  18
                 exchange.setProperty("org.apache.camel.file.lock.name", lockFileName);
 68  18
                 return true;
 69  
             }
 70  0
             return false;
 71  
         }
 72  9
         return true;
 73  
     }
 74  
 
 75  
     public void commit(FileEndpoint endpoint, FileExchange exchange, File file) throws Exception {
 76  27
         if (isLockFile()) {
 77  18
             Channel channel = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.fileChannel", Channel.class);
 78  18
             String lockfile = ExchangeHelper.getMandatoryProperty(exchange, "org.apache.camel.file.lock.name", String.class);
 79  18
             if (LOG.isDebugEnabled()) {
 80  0
                 LOG.debug("Unlocking file: " + file);
 81  
             }
 82  18
             channel.close();
 83  18
             File lock = new File(lockfile);
 84  18
             lock.delete();
 85  
         }
 86  27
     }
 87  
 
 88  
     public boolean isLockFile() {
 89  54
         return lockFile;
 90  
     }
 91  
 
 92  
     public void setLockFile(boolean lockFile) {
 93  0
         this.lockFile = lockFile;
 94  0
     }
 95  
 
 96  
     public FileRenamer getLockFileRenamer() {
 97  0
         return lockFileRenamer;
 98  
     }
 99  
 
 100  
     public void setLockFileRenamer(FileRenamer lockFileRenamer) {
 101  0
         this.lockFileRenamer = lockFileRenamer;
 102  0
     }
 103  
 }