Coverage Report - org.apache.camel.component.file.FileProducer
 
Classes in this File Line Coverage Branch Coverage Complexity
FileProducer
73% 
90% 
3.667
 
 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.Exchange;
 20  
 import org.apache.camel.Producer;
 21  
 import org.apache.camel.impl.DefaultProducer;
 22  
 import org.apache.camel.util.ExchangeHelper;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 import java.io.File;
 27  
 import java.io.FileOutputStream;
 28  
 import java.io.IOException;
 29  
 import java.io.InputStream;
 30  
 import java.io.RandomAccessFile;
 31  
 import java.nio.ByteBuffer;
 32  
 import java.nio.channels.FileChannel;
 33  
 
 34  
 /**
 35  
  * A {@link Producer} implementation for File
 36  
  *
 37  
  * @version $Revision: 523016 $
 38  
  */
 39  0
 public class FileProducer extends DefaultProducer {
 40  3
     private static final transient Log LOG = LogFactory.getLog(FileProducer.class);
 41  
     private final FileEndpoint endpoint;
 42  
 
 43  
     public FileProducer(FileEndpoint endpoint) {
 44  15
         super(endpoint);
 45  15
         this.endpoint = endpoint;
 46  15
     }
 47  
 
 48  
     public FileEndpoint getEndpoint() {
 49  36
         return (FileEndpoint) super.getEndpoint();
 50  
     }
 51  
 
 52  
     /**
 53  
      * @param exchange
 54  
      * @see org.apache.camel.Processor#process(Exchange)
 55  
      */
 56  
     public void process(Exchange exchange) throws Exception {
 57  18
         process(endpoint.toExchangeType(exchange));
 58  18
     }
 59  
 
 60  
     public void process(FileExchange exchange) throws Exception {
 61  18
         InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
 62  18
         File file = createFileName(exchange);
 63  18
         buildDirectory(file);
 64  18
         if (LOG.isDebugEnabled()) {
 65  0
             LOG.debug("About to write to: " + file + " from exchange: " + exchange);
 66  
         }
 67  18
         FileChannel fc = null;
 68  
         try {
 69  18
             if (getEndpoint().isAppend()) {
 70  18
                 fc = new RandomAccessFile(file, "rw").getChannel();
 71  18
                 fc.position(fc.size());
 72  18
             }
 73  
             else {
 74  0
                 fc = new FileOutputStream(file).getChannel();
 75  
             }
 76  18
             int size = getEndpoint().getBufferSize();
 77  18
             byte[] buffer = new byte[size];
 78  18
             ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
 79  
             while (true) {
 80  18
                 int count = in.read(buffer);
 81  18
                 if (count <= 0) {
 82  0
                     break;
 83  
                 }
 84  18
                 else if (count < size) {
 85  18
                     byteBuffer = ByteBuffer.wrap(buffer, 0, count);
 86  18
                     fc.write(byteBuffer);
 87  18
                     break;
 88  
                 }
 89  
                 else {
 90  0
                     fc.write(byteBuffer);
 91  
                 }
 92  0
             }
 93  
         }
 94  
         finally {
 95  18
             if (in != null) {
 96  
                 try {
 97  18
                     in.close();
 98  
                 }
 99  0
                 catch (IOException e) {
 100  0
                     LOG.warn("Failed to close input: " + e, e);
 101  18
                 }
 102  
             }
 103  18
             if (fc != null) {
 104  
                 try {
 105  18
                     fc.close();
 106  
                 }
 107  0
                 catch (IOException e) {
 108  0
                     LOG.warn("Failed to close output: " + e, e);
 109  18
                 }
 110  0
             }
 111  0
         }
 112  
         /*
 113  
         ByteBuffer payload = exchange.getIn().getBody(ByteBuffer.class);
 114  
         if (payload == null) {
 115  
             InputStream in = ExchangeHelper.getMandatoryInBody(exchange, InputStream.class);
 116  
             payload = ExchangeHelper.convertToMandatoryType(exchange, ByteBuffer.class, in);
 117  
         }
 118  
         payload.flip();
 119  
         File file = createFileName(exchange);
 120  
         buildDirectory(file);
 121  
         if (LOG.isDebugEnabled()) {
 122  
             LOG.debug("Creating file: " + file);
 123  
         }
 124  
         FileChannel fc = null;
 125  
         try {
 126  
             if (getEndpoint().isAppend()) {
 127  
                 fc = new RandomAccessFile(file, "rw").getChannel();
 128  
                 fc.position(fc.size());
 129  
             }
 130  
             else {
 131  
                 fc = new FileOutputStream(file).getChannel();
 132  
             }
 133  
             fc.write(payload);
 134  
         }
 135  
         catch (Throwable e) {
 136  
             LOG.error("Failed to write to File: " + file, e);
 137  
         }
 138  
         finally {
 139  
             if (fc != null) {
 140  
                 fc.close();
 141  
             }
 142  
         }
 143  
         */
 144  18
     }
 145  
 
 146  
     protected File createFileName(FileExchange exchange) {
 147  18
         String fileName = exchange.getIn().getMessageId();
 148  
 
 149  18
         File endpointFile = endpoint.getFile();
 150  18
         String name = exchange.getIn().getHeader(FileComponent.HEADER_FILE_NAME, String.class);
 151  18
         if (name != null) {
 152  0
             File answer = new File(endpointFile, name);
 153  0
             if (answer.isDirectory()) {
 154  0
                 return new File(answer, fileName);
 155  
             }
 156  
             else {
 157  0
                 return answer;
 158  
             }
 159  
         }
 160  18
         if (endpointFile != null && endpointFile.isDirectory()) {
 161  18
             return new File(endpointFile, fileName);
 162  
         }
 163  
         else {
 164  0
             return new File(fileName);
 165  
         }
 166  
     }
 167  
 
 168  
     private void buildDirectory(File file) {
 169  18
         String dirName = file.getAbsolutePath();
 170  18
         int index = dirName.lastIndexOf(File.separatorChar);
 171  18
         if (index > 0) {
 172  18
             dirName = dirName.substring(0, index);
 173  18
             File dir = new File(dirName);
 174  18
             dir.mkdirs();
 175  
         }
 176  18
     }
 177  
 }