You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2017/11/30 11:25:58 UTC

[myfaces-tobago] 03/03: TOBAGO-1782: Clean up * using "try with resources" (new with Java 7)

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit 024dee4088e9ca835cb9bcf990235bfbd6dcc1d9
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu Nov 30 12:20:44 2017 +0100

    TOBAGO-1782: Clean up
    * using "try with resources" (new with Java 7)
---
 .../myfaces/tobago/context/TobagoBundle.java       |  8 ++---
 .../apache/myfaces/tobago/facelets/Resource.java   | 14 +++-----
 .../tobago/internal/config/TobagoConfigParser.java |  7 +---
 .../internal/config/TobagoConfigVersion.java       |  7 +---
 .../myfaces/tobago/internal/util/IoUtils.java      | 39 ----------------------
 .../myfaces/tobago/servlet/ResourceServlet.java    | 28 ++++++++--------
 .../apache/myfaces/tobago/util/WebXmlUtils.java    |  8 +----
 .../webapp/TobagoResponseWriterUnitTest.java       | 12 +++----
 8 files changed, 29 insertions(+), 94 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoBundle.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoBundle.java
index f3422be..899db42 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoBundle.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/context/TobagoBundle.java
@@ -124,13 +124,9 @@ public class TobagoBundle extends ResourceBundle {
         if (reload) {
           connection.setUseCaches(false);
         }
-        final InputStream stream = connection.getInputStream();
-        if (stream == null) {
-          return null;
+        try (final BufferedInputStream bis = new BufferedInputStream(connection.getInputStream())) {
+          bundle = new XmlTobagoBundle(bis);
         }
-        final BufferedInputStream bis = new BufferedInputStream(stream);
-        bundle = new XmlTobagoBundle(bis);
-        bis.close();
 
         return bundle;
       }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/Resource.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/Resource.java
index a6b109c..a8d07d5 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/Resource.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/facelets/Resource.java
@@ -87,15 +87,11 @@ public final class Resource {
     final Object ctx = externalContext.getContext();
     if (ctx instanceof ServletContext) {
       final ServletContext servletContext = (ServletContext) ctx;
-      final InputStream stream = servletContext.getResourceAsStream(path);
-      if (stream != null) {
-        try {
-          stream.close();
-        } catch (final IOException e) {
-          // Ignore here, since we donnot wanted to read from this
-          // resource anyway
-        }
-        return true;
+      try (final InputStream stream = servletContext.getResourceAsStream(path)) {
+        return stream != null;
+      } catch (final IOException e) {
+        // Ignore here, since we donnot wanted to read from this
+        // resource anyway
       }
     }
     return false;
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
index 880ebd8..8821def 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigParser.java
@@ -23,7 +23,6 @@ import org.apache.myfaces.tobago.context.ThemeImpl;
 import org.apache.myfaces.tobago.context.ThemeScript;
 import org.apache.myfaces.tobago.context.ThemeStyle;
 import org.apache.myfaces.tobago.exception.TobagoConfigurationException;
-import org.apache.myfaces.tobago.internal.util.IoUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.xml.sax.Attributes;
@@ -135,17 +134,13 @@ public class TobagoConfigParser extends TobagoConfigEntityResolver {
       validate(url, version);
     }
 
-    InputStream inputStream = null;
-    try {
-      inputStream = url.openStream();
+    try (final InputStream inputStream = url.openStream()) {
       final SAXParserFactory factory = SAXParserFactory.newInstance();
       if (!version.isSchema()) {
         factory.setValidating(true);
       }
       final SAXParser saxParser = factory.newSAXParser();
       saxParser.parse(inputStream, this);
-    } finally {
-      IoUtils.closeQuietly(inputStream);
     }
     return tobagoConfig;
   }
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigVersion.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigVersion.java
index 89e77fa..f4bf524 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigVersion.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/config/TobagoConfigVersion.java
@@ -19,7 +19,6 @@
 
 package org.apache.myfaces.tobago.internal.config;
 
-import org.apache.myfaces.tobago.internal.util.IoUtils;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
@@ -40,14 +39,10 @@ public class TobagoConfigVersion extends TobagoConfigEntityResolver {
   public TobagoConfigVersion(final URL url) throws ParserConfigurationException, SAXException, IOException {
 
     // simple reading with no validation, at this time
-    InputStream inputStream = null;
-    try {
-      inputStream = url.openStream();
+    try (final InputStream inputStream = url.openStream()) {
       final SAXParserFactory factory = SAXParserFactory.newInstance();
       final SAXParser saxParser = factory.newSAXParser();
       saxParser.parse(inputStream, this);
-    } finally {
-      IoUtils.closeQuietly(inputStream);
     }
   }
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/IoUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/IoUtils.java
deleted file mode 100644
index fad8f3b..0000000
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/IoUtils.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.myfaces.tobago.internal.util;
-
-import java.io.Closeable;
-import java.io.IOException;
-
-public final class IoUtils {
-
-  private IoUtils() {
-  }
-
-  public static void closeQuietly(final Closeable closeable) {
-    try {
-      if (closeable != null) {
-        closeable.close();
-      }
-    } catch (final IOException exception) {
-      // ignore
-    }
-  }
-}
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
index a6c3136..95a1878 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/servlet/ResourceServlet.java
@@ -20,7 +20,6 @@
 package org.apache.myfaces.tobago.servlet;
 
 import org.apache.myfaces.tobago.config.TobagoConfig;
-import org.apache.myfaces.tobago.internal.util.IoUtils;
 import org.apache.myfaces.tobago.internal.util.MimeTypeUtils;
 import org.apache.myfaces.tobago.internal.util.ResponseUtils;
 import org.slf4j.Logger;
@@ -127,18 +126,7 @@ public class ResourceServlet extends HttpServlet {
       return;
     }
 
-    InputStream inputStream = null;
-    try {
-      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-
-      // meta inf (like in servlet 3.0)
-      inputStream = classLoader.getResourceAsStream("META-INF/resources/" + resource);
-
-      // "normal" classpath
-      if (inputStream == null) {
-        inputStream = classLoader.getResourceAsStream(resource);
-      }
-
+    try (final InputStream inputStream = locateResource(resource)) {
       if (inputStream != null) {
         copy(inputStream, response.getOutputStream());
       } else {
@@ -146,11 +134,21 @@ public class ResourceServlet extends HttpServlet {
         LOG.warn(message);
         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
       }
-    } finally {
-      IoUtils.closeQuietly(inputStream);
     }
   }
 
+  private InputStream locateResource(String resource) {
+    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    InputStream inputStream;// meta inf (like in servlet 3.0)
+    inputStream = classLoader.getResourceAsStream("META-INF/resources/" + resource);
+
+    // "normal" classpath
+    if (inputStream == null) {
+      inputStream = classLoader.getResourceAsStream(resource);
+    }
+    return inputStream;
+  }
+
   @Override
   protected long getLastModified(final HttpServletRequest request) {
     if (expires != null) {
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/util/WebXmlUtils.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/util/WebXmlUtils.java
index 9105de4..11cbb1d 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/util/WebXmlUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/util/WebXmlUtils.java
@@ -151,17 +151,11 @@ public class WebXmlUtils {
     if (url != null) {
       final URLConnection connection = url.openConnection();
       connection.setUseCaches(false);
-      InputStream input = null;
 
-      try {
-        input = connection.getInputStream();
+      try (final InputStream input = connection.getInputStream()) {
         final Document document = documentBuilder.parse(input);
         document.getDocumentElement().normalize();
         return document;
-      } finally {
-        if (input != null) {
-          input.close();
-        }
       }
     } else {
       return null;
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java
index 98b7c86..ce43019 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/webapp/TobagoResponseWriterUnitTest.java
@@ -132,12 +132,12 @@ public class TobagoResponseWriterUnitTest extends AbstractJsfTestCase {
 
   @Test
   public void testNonUtf8() throws IOException {
-    final TobagoResponseWriter writer1 = new HtmlResponseWriter(stringWriter, "", "ISO-8859-1");
-    writer1.startElement(HtmlElements.INPUT);
-    writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", true);
-    writer1.writeAttribute(HtmlAttributes.READONLY, true);
-    writer1.endElement(HtmlElements.INPUT);
-    writer1.close();
+    try (final TobagoResponseWriter writer1 = new HtmlResponseWriter(stringWriter, "", "ISO-8859-1")) {
+      writer1.startElement(HtmlElements.INPUT);
+      writer1.writeAttribute(HtmlAttributes.VALUE, "Gutschein über 100 €.", true);
+      writer1.writeAttribute(HtmlAttributes.READONLY, true);
+      writer1.endElement(HtmlElements.INPUT);
+    }
     Assert.assertEquals("\n<input value='Gutschein &uuml;ber 100 &euro;.' readonly='readonly'>",
         stringWriter.toString());
   }

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.