Coverage Report - org.apache.camel.component.file.FileConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
FileConsumer
62% 
90% 
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 org.apache.camel.Processor;
 21  
 import org.apache.camel.impl.ScheduledPollConsumer;
 22  
 import org.apache.commons.logging.Log;
 23  
 import org.apache.commons.logging.LogFactory;
 24  
 
 25  
 import java.io.File;
 26  
 import java.io.IOException;
 27  
 import java.io.RandomAccessFile;
 28  
 import java.nio.channels.FileChannel;
 29  
 
 30  
 /**
 31  
  * @version $Revision: 523016 $
 32  
  */
 33  
 public class FileConsumer extends ScheduledPollConsumer<FileExchange> {
 34  1
     private static final transient Log log = LogFactory.getLog(FileConsumer.class);
 35  
     private final FileEndpoint endpoint;
 36  1
     private boolean recursive = true;
 37  1
     private boolean attemptFileLock = false;
 38  1
     private String regexPattern = "";
 39  1
     private long lastPollTime = 0l;
 40  
 
 41  
     public FileConsumer(final FileEndpoint endpoint, Processor processor) {
 42  1
         super(endpoint, processor);
 43  1
         this.endpoint = endpoint;
 44  1
     }
 45  
 
 46  
     protected void poll() throws Exception {
 47  1
         pollFileOrDirectory(endpoint.getFile(), isRecursive());
 48  1
         lastPollTime = System.currentTimeMillis();
 49  1
     }
 50  
 
 51  
     protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) {
 52  796
         if (!fileOrDirectory.isDirectory()) {
 53  710
             pollFile(fileOrDirectory); // process the file
 54  710
         }
 55  86
         else if (processDir) {
 56  86
             log.debug("Polling directory " + fileOrDirectory);
 57  86
             File[] files = fileOrDirectory.listFiles();
 58  881
             for (int i = 0; i < files.length; i++) {
 59  795
                 pollFileOrDirectory(files[i], isRecursive()); // self-recursion
 60  
             }
 61  86
         }
 62  
         else {
 63  0
             log.debug("Skipping directory " + fileOrDirectory);
 64  
         }
 65  796
     }
 66  
 
 67  
     protected void pollFile(final File file) {
 68  710
         if (file.exists() && file.lastModified() > lastPollTime) {
 69  710
             if (isValidFile(file)) {
 70  710
                 processFile(file);
 71  
             }
 72  
         }
 73  710
     }
 74  
 
 75  
     protected void processFile(File file) {
 76  
         try {
 77  710
                         getProcessor().process(endpoint.createExchange(file));
 78  0
                 } catch (Throwable e) {
 79  0
                         handleException(e);
 80  710
                 }
 81  710
     }
 82  
 
 83  
     protected boolean isValidFile(File file) {
 84  710
         boolean result = false;
 85  710
         if (file != null && file.exists()) {
 86  710
             if (isMatched(file)) {
 87  710
                 if (isAttemptFileLock()) {
 88  0
                     FileChannel fc = null;
 89  
                     try {
 90  0
                         fc = new RandomAccessFile(file, "rw").getChannel();
 91  0
                         fc.lock();
 92  0
                         result = true;
 93  
                     }
 94  0
                     catch (Throwable e) {
 95  0
                         log.debug("Failed to get the lock on file: " + file, e);
 96  
                     }
 97  
                     finally {
 98  0
                         if (fc != null) {
 99  
                             try {
 100  0
                                 fc.close();
 101  
                             }
 102  0
                             catch (IOException e) {
 103  0
                             }
 104  0
                         }
 105  0
                     }
 106  0
                 }
 107  
                 else {
 108  710
                     result = true;
 109  
                 }
 110  
             }
 111  
         }
 112  710
         return result;
 113  
     }
 114  
 
 115  
     protected boolean isMatched(File file) {
 116  710
         boolean result = true;
 117  710
         if (regexPattern != null && regexPattern.length() > 0) {
 118  0
             result = file.getName().matches(getRegexPattern());
 119  
         }
 120  710
         return result;
 121  
     }
 122  
 
 123  
     /**
 124  
      * @return the recursive
 125  
      */
 126  
     public boolean isRecursive() {
 127  796
         return this.recursive;
 128  
     }
 129  
 
 130  
     /**
 131  
      * @param recursive the recursive to set
 132  
      */
 133  
     public void setRecursive(boolean recursive) {
 134  0
         this.recursive = recursive;
 135  0
     }
 136  
 
 137  
     /**
 138  
      * @return the attemptFileLock
 139  
      */
 140  
     public boolean isAttemptFileLock() {
 141  710
         return this.attemptFileLock;
 142  
     }
 143  
 
 144  
     /**
 145  
      * @param attemptFileLock the attemptFileLock to set
 146  
      */
 147  
     public void setAttemptFileLock(boolean attemptFileLock) {
 148  0
         this.attemptFileLock = attemptFileLock;
 149  0
     }
 150  
 
 151  
     /**
 152  
      * @return the regexPattern
 153  
      */
 154  
     public String getRegexPattern() {
 155  0
         return this.regexPattern;
 156  
     }
 157  
 
 158  
     /**
 159  
      * @param regexPattern the regexPattern to set
 160  
      */
 161  
     public void setRegexPattern(String regexPattern) {
 162  0
         this.regexPattern = regexPattern;
 163  0
     }
 164  
 }