Coverage Report - org.apache.camel.component.file.FileEndpoint
 
Classes in this File Line Coverage Branch Coverage Complexity
FileEndpoint
70% 
100% 
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.Consumer;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.Producer;
 22  
 import org.apache.camel.component.file.strategy.DefaultFileRenamer;
 23  
 import org.apache.camel.component.file.strategy.DeleteFileProcessStrategy;
 24  
 import org.apache.camel.component.file.strategy.FileProcessStrategy;
 25  
 import org.apache.camel.component.file.strategy.FileProcessStrategySupport;
 26  
 import org.apache.camel.component.file.strategy.NoOpFileProcessStrategy;
 27  
 import org.apache.camel.component.file.strategy.RenameFileProcessStrategy;
 28  
 import org.apache.camel.impl.ScheduledPollEndpoint;
 29  
 import org.apache.commons.logging.Log;
 30  
 import org.apache.commons.logging.LogFactory;
 31  
 
 32  
 import java.io.File;
 33  
 
 34  
 /**
 35  
  * A <a href="http://activemq.apache.org/camel/file.html">File Endpoint</a> for
 36  
  * working with file systems
 37  
  * 
 38  
  * @version $Revision: 523016 $
 39  
  */
 40  12
 public class FileEndpoint extends ScheduledPollEndpoint<FileExchange> {
 41  3
     private static final transient Log LOG = LogFactory.getLog(FileEndpoint.class);
 42  
     private File file;
 43  
     private FileProcessStrategy fileProcessStrategy;
 44  30
     private boolean autoCreate = true;
 45  30
     private boolean lock = true;
 46  
     private boolean delete;
 47  
     private boolean noop;
 48  30
     private boolean append = true;
 49  
     private String moveNamePrefix;
 50  
     private String moveNamePostfix;
 51  30
     private String[] excludedNamePrefixes = {"."};
 52  30
     private String[] excludedNamePostfixes = { FileProcessStrategySupport.DEFAULT_LOCK_FILE_POSTFIX };
 53  30
     private int bufferSize = 128 * 1024;
 54  
 
 55  
     protected FileEndpoint(File file, String endpointUri, FileComponent component) {
 56  30
         super(endpointUri, component);
 57  30
         this.file = file;
 58  30
     }
 59  
 
 60  
     /**
 61  
      * @return a Producer
 62  
      * @throws Exception
 63  
      * @see org.apache.camel.Endpoint#createProducer()
 64  
      */
 65  
     public Producer<FileExchange> createProducer() throws Exception {
 66  15
         Producer<FileExchange> result = new FileProducer(this);
 67  15
         return result;
 68  
     }
 69  
 
 70  
     /**
 71  
      * @param file
 72  
      * @return a Consumer
 73  
      * @throws Exception
 74  
      * @see org.apache.camel.Endpoint#createConsumer(org.apache.camel.Processor)
 75  
      */
 76  
     public Consumer<FileExchange> createConsumer(Processor file) throws Exception {
 77  18
         Consumer<FileExchange> result = new FileConsumer(this, file);
 78  18
         configureConsumer(result);
 79  18
         return result;
 80  
     }
 81  
 
 82  
     /**
 83  
      * @param file
 84  
      * @return a FileExchange
 85  
      * @see org.apache.camel.Endpoint#createExchange()
 86  
      */
 87  
     public FileExchange createExchange(File file) {
 88  39
         return new FileExchange(getContext(), file);
 89  
     }
 90  
 
 91  
     /**
 92  
      * @return an Exchange
 93  
      * @see org.apache.camel.Endpoint#createExchange()
 94  
      */
 95  
     public FileExchange createExchange() {
 96  12
         return createExchange(getFile());
 97  
     }
 98  
 
 99  
     public File getFile() {
 100  62
         if (autoCreate && !file.exists()) {
 101  3
             file.mkdirs();
 102  
         }
 103  62
         return file;
 104  
     }
 105  
 
 106  
     public boolean isSingleton() {
 107  30
         return true;
 108  
     }
 109  
 
 110  
     /**
 111  
      * @return the autoCreate
 112  
      */
 113  
     public boolean isAutoCreate() {
 114  0
         return this.autoCreate;
 115  
     }
 116  
 
 117  
     /**
 118  
      * @param autoCreate the autoCreate to set
 119  
      */
 120  
     public void setAutoCreate(boolean autoCreate) {
 121  0
         this.autoCreate = autoCreate;
 122  0
     }
 123  
 
 124  
     public FileProcessStrategy getFileStrategy() {
 125  27
         if (fileProcessStrategy == null) {
 126  18
             fileProcessStrategy = createFileStrategy();
 127  18
             LOG.debug("" + this + " using strategy: " + fileProcessStrategy);
 128  
         }
 129  27
         return fileProcessStrategy;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Sets the strategy to be used when the file has been processed such as
 134  
      * deleting or renaming it etc.
 135  
      * 
 136  
      * @param fileProcessStrategy the new stategy to use
 137  
      */
 138  
     public void setFileStrategy(FileProcessStrategy fileProcessStrategy) {
 139  0
         this.fileProcessStrategy = fileProcessStrategy;
 140  0
     }
 141  
 
 142  
     public boolean isDelete() {
 143  12
         return delete;
 144  
     }
 145  
 
 146  
     public void setDelete(boolean delete) {
 147  9
         this.delete = delete;
 148  9
     }
 149  
 
 150  
     public boolean isLock() {
 151  12
         return lock;
 152  
     }
 153  
 
 154  
     public void setLock(boolean lock) {
 155  0
         this.lock = lock;
 156  0
     }
 157  
 
 158  
     public String getMoveNamePostfix() {
 159  0
         return moveNamePostfix;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Sets the name postfix appended to moved files. For example to rename all
 164  
      * the files from * to *.done set this value to ".done"
 165  
      * 
 166  
      * @param moveNamePostfix
 167  
      * @see DefaultFileRenamer#setNamePostfix(String)
 168  
      */
 169  
     public void setMoveNamePostfix(String moveNamePostfix) {
 170  0
         this.moveNamePostfix = moveNamePostfix;
 171  0
     }
 172  
 
 173  
     public String getMoveNamePrefix() {
 174  0
         return moveNamePrefix;
 175  
     }
 176  
 
 177  
     /**
 178  
      * Sets the name prefix appended to moved files. For example to move
 179  
      * processed files into a hidden directory called ".camel" set this value to
 180  
      * ".camel/"
 181  
      * 
 182  
      * @see DefaultFileRenamer#setNamePrefix(String)
 183  
      */
 184  
     public void setMoveNamePrefix(String moveNamePrefix) {
 185  3
         this.moveNamePrefix = moveNamePrefix;
 186  3
     }
 187  
 
 188  
     public String[] getExcludedNamePrefixes() {
 189  126
         return excludedNamePrefixes;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Sets the excluded file name prefixes, such as "." for hidden files which
 194  
      * are excluded by default
 195  
      */
 196  
     public void setExcludedNamePrefixes(String[] excludedNamePrefixes) {
 197  0
         this.excludedNamePrefixes = excludedNamePrefixes;
 198  0
     }
 199  
 
 200  
     public String[] getExcludedNamePostfixes() {
 201  111
         return excludedNamePostfixes;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Sets the excluded file name postfixes, such as {@link FileProcessStrategySupport#DEFAULT_LOCK_FILE_POSTFIX}
 206  
      * to ignore lock files by default.
 207  
      */
 208  
     public void setExcludedNamePostfixes(String[] excludedNamePostfixes) {
 209  0
         this.excludedNamePostfixes = excludedNamePostfixes;
 210  0
     }
 211  
 
 212  
     public boolean isNoop() {
 213  47
         return noop;
 214  
     }
 215  
 
 216  
     /**
 217  
      * If set to true then the default {@link FileProcessStrategy} will be to use the
 218  
      * {@link NoOpFileProcessStrategy} to not move or copy processed files
 219  
      * 
 220  
      * @param noop
 221  
      */
 222  
     public void setNoop(boolean noop) {
 223  6
         this.noop = noop;
 224  6
     }
 225  
 
 226  
     public boolean isAppend() {
 227  18
         return append;
 228  
     }
 229  
 
 230  
     /**
 231  
      * When writing do we append to the end of the file, or replace it?
 232  
      * The default is to append
 233  
      *
 234  
      * @param append whether to append (or replace)
 235  
      */
 236  
     public void setAppend(boolean append) {
 237  0
         this.append = append;
 238  0
     }
 239  
 
 240  
     public int getBufferSize() {
 241  18
         return bufferSize;
 242  
     }
 243  
 
 244  
     /**
 245  
      * Sets the buffer size used to read/write files
 246  
      */
 247  
     public void setBufferSize(int bufferSize) {
 248  0
         this.bufferSize = bufferSize;
 249  0
     }
 250  
 
 251  
     /**
 252  
      * A strategy method to lazily create the file strategy
 253  
      */
 254  
     protected FileProcessStrategy createFileStrategy() {
 255  18
         if (isNoop()) {
 256  6
             return new NoOpFileProcessStrategy();
 257  12
         } else if (moveNamePostfix != null || moveNamePrefix != null) {
 258  3
             if (isDelete()) {
 259  0
                 throw new IllegalArgumentException(
 260  
                                                    "You cannot set the deleteFiles property and a moveFilenamePostfix or moveFilenamePrefix");
 261  
             }
 262  3
             return new RenameFileProcessStrategy(isLock(), moveNamePrefix, moveNamePostfix);
 263  9
         } else if (isDelete()) {
 264  3
             return new DeleteFileProcessStrategy(isLock());
 265  
         } else {
 266  6
             return new RenameFileProcessStrategy(isLock());
 267  
         }
 268  
     }
 269  
 }