Coverage Report - org.apache.camel.component.file.FileConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
FileConsumer
0% 
0% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.file;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.IOException;
 22  
 import java.io.RandomAccessFile;
 23  
 import java.nio.channels.FileChannel;
 24  
 import java.util.concurrent.ScheduledExecutorService;
 25  
 import org.apache.camel.Processor;
 26  
 import org.apache.camel.impl.PollingConsumer;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 /**
 31  
  * @version $Revision: 523016 $
 32  
  */
 33  
 public class FileConsumer extends PollingConsumer<FileExchange>{
 34  0
     private static final transient Log log=LogFactory.getLog(FileConsumer.class);
 35  
 
 36  
     private final FileEndpoint endpoint;
 37  0
     private boolean recursive=true;
 38  0
     private boolean attemptFileLock=false;
 39  0
     private String regexPattern="";
 40  0
     private long lastPollTime=0l;
 41  
 
 42  
     public FileConsumer(final FileEndpoint endpoint,Processor<FileExchange> processor,ScheduledExecutorService executor){
 43  0
         super(endpoint,processor,executor);
 44  0
         this.endpoint=endpoint;
 45  0
     }
 46  0
 
 47  0
     protected void poll() throws Exception{
 48  0
         pollFileOrDirectory(endpoint.getFile(),isRecursive());
 49  0
         lastPollTime=System.currentTimeMillis();
 50  0
     }
 51  
 
 52  
     protected void pollFileOrDirectory(File fileOrDirectory,boolean processDir){
 53  0
         if(!fileOrDirectory.isDirectory()){
 54  0
             pollFile(fileOrDirectory); // process the file
 55  0
         }else if(processDir){
 56  0
             log.debug("Polling directory "+fileOrDirectory);
 57  0
             File[] files=fileOrDirectory.listFiles();
 58  0
             for(int i=0;i<files.length;i++){
 59  0
                 pollFileOrDirectory(files[i],isRecursive()); // self-recursion
 60  0
             }
 61  0
         }else{
 62  0
             log.debug("Skipping directory "+fileOrDirectory);
 63  
         }
 64  0
     }
 65  
 
 66  0
     protected void pollFile(final File file){
 67  0
         if(file.exists()&&file.lastModified()>lastPollTime){
 68  0
             if(isValidFile(file)){
 69  0
                 processFile(file);
 70  0
             }
 71  0
         }
 72  0
     }
 73  0
 
 74  
     protected void processFile(File file){
 75  0
         getProcessor().process(endpoint.createExchange(file));
 76  0
     }
 77  0
 
 78  
     protected boolean isValidFile(File file){
 79  0
         boolean result=false;
 80  0
         if(file!=null&&file.exists()){
 81  0
             if(isMatched(file)){
 82  0
                 if(isAttemptFileLock()){
 83  0
                     FileChannel fc=null;
 84  0
                     try{
 85  0
                         fc=new RandomAccessFile(file,"rw").getChannel();
 86  0
                         fc.lock();
 87  0
                         result=true;
 88  0
                     }catch(Throwable e){
 89  0
                         log.debug("Failed to get the lock on file: " + file,e);
 90  
                     }finally{
 91  0
                         if(fc!=null){
 92  0
                             try{
 93  0
                                 fc.close();
 94  0
                             }catch(IOException e){
 95  0
                             }
 96  0
                         }
 97  0
                     }
 98  0
                 }else{
 99  0
                     result=true;
 100  0
                 }
 101  0
             }
 102  0
         }
 103  0
         return result;
 104  0
     }
 105  0
 
 106  0
     protected boolean isMatched(File file){
 107  0
         boolean result=true;
 108  0
         if(regexPattern!=null&&regexPattern.length()>0){
 109  0
             result=file.getName().matches(getRegexPattern());
 110  
         }
 111  0
         return result;
 112  0
     }
 113  0
 
 114  0
     /**
 115  0
      * @return the recursive
 116  
      */
 117  
     public boolean isRecursive(){
 118  0
         return this.recursive;
 119  0
     }
 120  
 
 121  
     /**
 122  
      * @param recursive the recursive to set
 123  0
      */
 124  0
     public void setRecursive(boolean recursive){
 125  0
         this.recursive=recursive;
 126  0
     }
 127  0
 
 128  
     /**
 129  
      * @return the attemptFileLock
 130  
      */
 131  
     public boolean isAttemptFileLock(){
 132  0
         return this.attemptFileLock;
 133  
     }
 134  0
 
 135  
     /**
 136  
      * @param attemptFileLock the attemptFileLock to set
 137  
      */
 138  
     public void setAttemptFileLock(boolean attemptFileLock){
 139  0
         this.attemptFileLock=attemptFileLock;
 140  0
     }
 141  0
 
 142  0
     /**
 143  
      * @return the regexPattern
 144  
      */
 145  
     public String getRegexPattern(){
 146  0
         return this.regexPattern;
 147  
     }
 148  0
 
 149  
     /**
 150  
      * @param regexPattern the regexPattern to set
 151  
      */
 152  
     public void setRegexPattern(String regexPattern){
 153  0
         this.regexPattern=regexPattern;
 154  0
     }
 155  0
 }