Coverage Report - org.mule.util.DefaultStreamCloserService
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultStreamCloserService
0%
0/19
0%
0/6
3.4
DefaultStreamCloserService$CoreStreamTypesCloser
0%
0/20
0%
0/22
3.4
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.util;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.util.StreamCloser;
 11  
 import org.mule.api.util.StreamCloserService;
 12  
 
 13  
 import java.io.IOException;
 14  
 import java.io.InputStream;
 15  
 import java.util.Iterator;
 16  
 
 17  
 import javax.xml.transform.sax.SAXSource;
 18  
 import javax.xml.transform.stream.StreamSource;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 import org.xml.sax.InputSource;
 23  
 
 24  
 /**
 25  
  * Closes streams of different types by looking up available {@link StreamCloser}'s
 26  
  * from the Mule registry.
 27  
  */
 28  0
 public class DefaultStreamCloserService implements StreamCloserService
 29  
 {
 30  
 
 31  0
     private static final Log log = LogFactory.getLog(DefaultStreamCloserService.class);
 32  
 
 33  
     private MuleContext muleContext;
 34  0
     private StreamCloser coreStreamTypesCloser = new CoreStreamTypesCloser();
 35  
 
 36  
     public void closeStream(Object stream)
 37  
     {
 38  
         try
 39  
         {
 40  0
             if (coreStreamTypesCloser.canClose(stream.getClass()))
 41  
             {
 42  0
                 coreStreamTypesCloser.close(stream);
 43  
             }
 44  
             else
 45  
             {
 46  0
                 Iterator closers = muleContext.getRegistry().lookupObjects(StreamCloser.class).iterator();
 47  0
                 while (closers.hasNext())
 48  
                 {
 49  0
                     StreamCloser closer = (StreamCloser) closers.next();
 50  0
                     if (closer.canClose(stream.getClass()))
 51  
                     {
 52  0
                         closer.close(stream);
 53  
                     }
 54  
                     else
 55  
                     {
 56  0
                         log.debug("Unable to find an StreamCloser for the stream type: " + stream.getClass()
 57  
                                   + ", the stream: " + stream + " will not be closed.");
 58  
                     }
 59  0
                 }
 60  
             }
 61  
         }
 62  0
         catch (Exception e)
 63  
         {
 64  0
             log.debug("Exception closing stream: " + stream, e);
 65  0
         }
 66  
 
 67  0
     }
 68  
 
 69  
     public void setMuleContext(MuleContext context)
 70  
     {
 71  0
         muleContext = context;
 72  0
     }
 73  
 
 74  0
     static class CoreStreamTypesCloser implements StreamCloser
 75  
     {
 76  
 
 77  
         public boolean canClose(Class streamType)
 78  
         {
 79  0
             return InputStream.class.isAssignableFrom(streamType)
 80  
                    || InputSource.class.isAssignableFrom(streamType)
 81  
                    || StreamSource.class.isAssignableFrom(streamType)
 82  
                    || (SAXSource.class.isAssignableFrom(streamType) && !streamType.getName().endsWith(
 83  
                        "StaxSource"));
 84  
         }
 85  
 
 86  
         public void close(Object stream) throws IOException
 87  
         {
 88  0
             if (stream instanceof InputStream)
 89  
             {
 90  
                 try
 91  
                 {
 92  0
                     ((InputStream) stream).close();
 93  
                 }
 94  0
                 catch (IOException e)
 95  
                 {
 96  
                     // no-op
 97  0
                 }
 98  
             }
 99  0
             else if (stream instanceof InputSource)
 100  
             {
 101  0
                 closeInputSourceStream((InputSource) stream);
 102  
             }
 103  0
             else if (stream instanceof SAXSource)
 104  
             {
 105  0
                 closeInputSourceStream(((SAXSource) stream).getInputSource());
 106  
             }
 107  0
             else if (stream instanceof StreamSource)
 108  
             {
 109  
                 try
 110  
                 {
 111  0
                     ((StreamSource) stream).getInputStream().close();
 112  
                 }
 113  0
                 catch (IOException e)
 114  
                 {
 115  0
                 }
 116  
             }
 117  0
         }
 118  
 
 119  
         private void closeInputSourceStream(InputSource payload) throws IOException
 120  
         {
 121  0
             if (payload.getByteStream() != null)
 122  
             {
 123  0
                 payload.getByteStream().close();
 124  
             }
 125  0
             else if (payload.getCharacterStream() != null)
 126  
             {
 127  0
                 payload.getCharacterStream().close();
 128  
             }
 129  0
         }
 130  
         
 131  
     }
 132  
 
 133  
 }