Coverage Report - org.apache.camel.component.file.FileConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
FileConsumer
77% 
91% 
0
 
 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;
 18  
 
 19  
 import org.apache.camel.Processor;
 20  
 import org.apache.camel.component.file.strategy.FileProcessStrategy;
 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  
 
 27  
 /**
 28  
  * @version $Revision: 523016 $
 29  
  */
 30  
 public class FileConsumer extends ScheduledPollConsumer<FileExchange> {
 31  3
     private static final transient Log LOG = LogFactory.getLog(FileConsumer.class);
 32  
     private final FileEndpoint endpoint;
 33  18
     private boolean recursive = true;
 34  18
     private String regexPattern = "";
 35  
     private long lastPollTime;
 36  
 
 37  
     public FileConsumer(final FileEndpoint endpoint, Processor processor) {
 38  18
         super(endpoint, processor);
 39  18
         this.endpoint = endpoint;
 40  18
     }
 41  
 
 42  
     protected void poll() throws Exception {
 43  20
         pollFileOrDirectory(endpoint.getFile(), isRecursive());
 44  20
         lastPollTime = System.currentTimeMillis();
 45  20
     }
 46  
 
 47  
     protected void pollFileOrDirectory(File fileOrDirectory, boolean processDir) {
 48  126
         if (!fileOrDirectory.isDirectory()) {
 49  29
             pollFile(fileOrDirectory); // process the file
 50  29
         }
 51  97
         else if (processDir) {
 52  97
             if (isValidFile(fileOrDirectory)) {
 53  82
                 LOG.debug("Polling directory " + fileOrDirectory);
 54  82
                 File[] files = fileOrDirectory.listFiles();
 55  188
                 for (int i = 0; i < files.length; i++) {
 56  106
                     pollFileOrDirectory(files[i], isRecursive()); // self-recursion
 57  
                 }
 58  82
             }
 59  
         }
 60  
         else {
 61  0
             LOG.debug("Skipping directory " + fileOrDirectory);
 62  
         }
 63  126
     }
 64  
 
 65  
     protected void pollFile(final File file) {
 66  29
         if (!file.exists()) {
 67  0
             return;
 68  
         }
 69  29
         if (isValidFile(file)) {
 70  
             // we only care about file modified times if we are not deleting/moving files
 71  29
             if (endpoint.isNoop()) {
 72  11
                 long fileModified = file.lastModified();
 73  11
                 if (fileModified <= lastPollTime) {
 74  2
                     if (LOG.isDebugEnabled()) {
 75  0
                         LOG.debug("Ignoring file: " + file + " as modified time: " + fileModified + " less than last poll time: " + lastPollTime);
 76  
                     }
 77  2
                     return;
 78  
                 }
 79  
             }
 80  
 
 81  27
             FileProcessStrategy processStrategy = endpoint.getFileStrategy();
 82  27
             FileExchange exchange = endpoint.createExchange(file);
 83  
 
 84  
             try {
 85  27
                 if (LOG.isDebugEnabled()) {
 86  0
                     LOG.debug("About to process file:  " + file + " using exchange: " + exchange);
 87  
                 }
 88  27
                 if (processStrategy.begin(endpoint, exchange, file)) {
 89  27
                     getProcessor().process(exchange);
 90  27
                     processStrategy.commit(endpoint, exchange, file);
 91  27
                 }
 92  
                 else {
 93  0
                     if (LOG.isDebugEnabled()) {
 94  0
                         LOG.debug(endpoint + " cannot process file: " + file);
 95  
                     }
 96  
                 }
 97  
             }
 98  0
             catch (Throwable e) {
 99  0
                 handleException(e);
 100  27
             }
 101  
         }
 102  27
     }
 103  
 
 104  
     protected boolean isValidFile(File file) {
 105  126
         boolean result = false;
 106  126
         if (file != null && file.exists()) {
 107  126
             if (isMatched(file)) {
 108  111
                 result = true;
 109  
             }
 110  
         }
 111  126
         return result;
 112  
     }
 113  
 
 114  
     protected boolean isMatched(File file) {
 115  126
         String name = file.getName();
 116  126
         if (regexPattern != null && regexPattern.length() > 0) {
 117  0
             if (!name.matches(getRegexPattern())) {
 118  0
                 return false;
 119  
             }
 120  
         }
 121  126
         String[] prefixes = endpoint.getExcludedNamePrefixes();
 122  126
         if (prefixes != null) {
 123  237
             for (String prefix : prefixes) {
 124  126
                 if (name.startsWith(prefix)) {
 125  15
                     return false;
 126  
                 }
 127  
             }
 128  
         }
 129  111
         String[] postfixes = endpoint.getExcludedNamePostfixes();
 130  111
         if (postfixes != null) {
 131  222
             for (String postfix : postfixes) {
 132  111
                 if (name.endsWith(postfix)) {
 133  0
                     return false;
 134  
                 }
 135  
             }
 136  
         }
 137  111
         return true;
 138  
     }
 139  
 
 140  
     /**
 141  
      * @return the recursive
 142  
      */
 143  
     public boolean isRecursive() {
 144  126
         return this.recursive;
 145  
     }
 146  
 
 147  
     /**
 148  
      * @param recursive the recursive to set
 149  
      */
 150  
     public void setRecursive(boolean recursive) {
 151  0
         this.recursive = recursive;
 152  0
     }
 153  
 
 154  
     /**
 155  
      * @return the regexPattern
 156  
      */
 157  
     public String getRegexPattern() {
 158  0
         return this.regexPattern;
 159  
     }
 160  
 
 161  
     /**
 162  
      * @param regexPattern the regexPattern to set
 163  
      */
 164  
     public void setRegexPattern(String regexPattern) {
 165  0
         this.regexPattern = regexPattern;
 166  0
     }
 167  
 }