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