View Javadoc

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  public class DefaultStreamCloserService implements StreamCloserService
33  {
34  
35      private static final Log log = LogFactory.getLog(DefaultStreamCloserService.class);
36  
37      private MuleContext muleContext;
38      private StreamCloser coreStreamTypesCloser = new CoreStreamTypesCloser();
39  
40      public void closeStream(Object stream)
41      {
42          try
43          {
44              if (coreStreamTypesCloser.canClose(stream.getClass()))
45              {
46                  coreStreamTypesCloser.close(stream);
47              }
48              else
49              {
50                  Iterator closers = muleContext.getRegistry().lookupObjects(StreamCloser.class).iterator();
51                  while (closers.hasNext())
52                  {
53                      StreamCloser closer = (StreamCloser) closers.next();
54                      if (closer.canClose(stream.getClass()))
55                      {
56                          closer.close(stream);
57                      }
58                      else
59                      {
60                          log.debug("Unable to find an StreamCloser for the stream type: " + stream.getClass()
61                                    + ", the stream: " + stream + " will not be closed.");
62                      }
63                  }
64              }
65          }
66          catch (Exception e)
67          {
68              log.debug("Exception closing stream: " + stream, e);
69          }
70  
71      }
72  
73      public void setMuleContext(MuleContext context)
74      {
75          muleContext = context;
76      }
77  
78      static class CoreStreamTypesCloser implements StreamCloser
79      {
80  
81          public boolean canClose(Class streamType)
82          {
83              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              if (stream instanceof InputStream)
93              {
94                  try
95                  {
96                      ((InputStream) stream).close();
97                  }
98                  catch (IOException e)
99                  {
100                     // no-op
101                 }
102             }
103             else if (stream instanceof InputSource)
104             {
105                 closeInputSourceStream((InputSource) stream);
106             }
107             else if (stream instanceof SAXSource)
108             {
109                 closeInputSourceStream(((SAXSource) stream).getInputSource());
110             }
111             else if (stream instanceof StreamSource)
112             {
113                 try
114                 {
115                     ((StreamSource) stream).getInputStream().close();
116                 }
117                 catch (IOException e)
118                 {
119                 }
120             }
121         }
122 
123         private void closeInputSourceStream(InputSource payload) throws IOException
124         {
125             if (payload.getByteStream() != null)
126             {
127                 payload.getByteStream().close();
128             }
129             else if (payload.getCharacterStream() != null)
130             {
131                 payload.getCharacterStream().close();
132             }
133         }
134         
135     }
136 
137 }