cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r729855 - in /cocoon/cocoon3/trunk: cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/ cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/
Date Mon, 29 Dec 2008 10:38:09 GMT
Author: reinhard
Date: Mon Dec 29 02:38:09 2008
New Revision: 729855

URL: http://svn.apache.org/viewvc?rev=729855&view=rev
Log:
. close URLConnections in a finally clause
. URLConnectionsUtils.toSAX always closes the input stream that it uses
. javadocs

Modified:
    cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/URLConnectionUtils.java
    cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
    cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/ServletServiceTransformer.java

Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/URLConnectionUtils.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/URLConnectionUtils.java?rev=729855&r1=729854&r2=729855&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/URLConnectionUtils.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/URLConnectionUtils.java
Mon Dec 29 02:38:09 2008
@@ -31,40 +31,63 @@
 
     private static final Log LOG = LogFactory.getLog(URLConnectionUtils.class);
 
+    /**
+     * Stream a {@link URLConnection} into an {@link XMLConsumer} by using
+     * {@link XMLUtils#toSax(InputStream, XMLConsumer)}. The {@link URLConnection} is closed
after streaming.
+     * 
+     * @param The {@link URLConnection} to be streamed.
+     * @param An {@link XMLConsumer} as target.
+     */
     public static void toSax(URLConnection urlConnection, XMLConsumer xmlConsumer) {
         try {
             InputStream inputStream = urlConnection.getInputStream();
             XMLUtils.toSax(inputStream, xmlConsumer);
-            inputStream.close();
         } catch (PipelineException e) {
             throw e;
         } catch (Exception e) {
             throw new ProcessingException("Can't parse url connection " + urlConnection.getURL(),
e);
+        } finally {
+            closeQuietly(urlConnection);
         }
     }
 
+    /**
+     * Close a {@link URLConnection} quietly and take care of all the exception handling.
+     * 
+     * @param The {@link URLConnection} to be closed.
+     */
     public static void closeQuietly(URLConnection urlConnection) {
         if (urlConnection.getDoInput()) {
+            InputStream inputStream = null;
             try {
-                InputStream inputStream = urlConnection.getInputStream();
-
-                if (inputStream != null) {
-                    inputStream.close();
-                }
+                inputStream = urlConnection.getInputStream();
             } catch (IOException e) {
                 LOG.warn("Can't close input stream from " + urlConnection.getURL(), e);
+            } finally {
+                if (inputStream != null) {
+                    try {
+                        inputStream.close();
+                    } catch (IOException e) {
+                        LOG.warn("Can't close input stream from " + urlConnection.getURL(),
e);
+                    }
+                }
             }
         }
 
         if (urlConnection.getDoOutput()) {
+            OutputStream outputStream = null;
             try {
-                OutputStream outputStream = urlConnection.getOutputStream();
-
-                if (outputStream != null) {
-                    outputStream.close();
-                }
+                outputStream = urlConnection.getOutputStream();
             } catch (IOException e) {
                 LOG.warn("Can't close output stream to " + urlConnection.getURL(), e);
+            } finally {
+                if (outputStream != null) {
+                    try {
+                        outputStream.close();
+                    } catch (IOException e) {
+                        LOG.warn("Can't close input stream to " + urlConnection.getURL(),
e);
+                    }
+                }
             }
         }
     }

Modified: cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java?rev=729855&r1=729854&r2=729855&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-pipeline/src/main/java/org/apache/cocoon/pipeline/util/XMLUtils.java
Mon Dec 29 02:38:09 2008
@@ -39,6 +39,14 @@
     private static final SAXTransformerFactory SAX_TRANSFORMER_FACTORY = (SAXTransformerFactory)
TransformerFactory
             .newInstance();
 
+    /**
+     * Use a SAX parser and read the content of an {@link InputStream} into an {@link XMLConsumer}.
+     * 
+     * @param The {@link InputStream} to be parsed.
+     * @param The target {@link XMLConsumer}.
+     * @throws An IOException if the {@link InputStream} can't be used.
+     * @throws SAXException if the {@link InputStream} can't be parsed.
+     */
     public static void toSax(final InputStream inputStream, final XMLConsumer xmlConsumer)
throws IOException,
             SAXException {
         XMLReader xmlReader;
@@ -48,9 +56,6 @@
             xmlReader.setContentHandler(xmlConsumer);
             xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", xmlConsumer);
         } catch (SAXException e) {
-            // FIXME We have to do something with exception handling. Throwing
-            // plain RuntimeExceptions makes it difficult to react on different
-            // error conditions in your code.
             throw new ProcessingException("Cannot create and prepare an XMLReader.", e);
         }
 

Modified: cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/ServletServiceTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/ServletServiceTransformer.java?rev=729855&r1=729854&r2=729855&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/ServletServiceTransformer.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-servlet/src/main/java/org/apache/cocoon/servlet/ssf/ServletServiceTransformer.java
Mon Dec 29 02:38:09 2008
@@ -55,10 +55,7 @@
         } catch (IOException e) {
             throw new ProcessingException("Can't stream SaxBuffer into the output stream
of the URL "
                     + this.getUrlConnection().getURL());
-        } finally {
-            URLConnectionUtils.closeQuietly(this.getUrlConnection());
         }
-
     }
 
     private URLConnection getUrlConnection() {



Mime
View raw message