Author: markt
Date: Wed Mar 26 14:33:44 2014
New Revision: 1581864

URL: http://svn.apache.org/r1581864
Log:
More try-with-resources

Modified:
    tomcat/trunk/TOMCAT-NEXT.txt
    tomcat/trunk/java/org/apache/catalina/startup/Catalina.java
    tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
    tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java
    tomcat/trunk/java/org/apache/catalina/startup/HostConfig.java
    tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java

Modified: tomcat/trunk/TOMCAT-NEXT.txt
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/TOMCAT-NEXT.txt?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/TOMCAT-NEXT.txt (original)
+++ tomcat/trunk/TOMCAT-NEXT.txt Wed Mar 26 14:33:44 2014
@@ -214,7 +214,7 @@ but possibly 7.1.x).
     - Use of try with resources
       - Started.
         - javax.* complete
-        - o.a.catalina.[ant to session] complete
+        - o.a.catalina.[ant to startup] complete
         - remainder TODO
     - Catching multiple exceptions
       - Started

Modified: tomcat/trunk/java/org/apache/catalina/startup/Catalina.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Catalina.java?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/Catalina.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/Catalina.java Wed Mar 26 
14:33:44 2014
@@ -420,29 +420,19 @@ public class Catalina {
         }
 
         Server s = getServer();
-        if( s == null ) {
+        if (s == null) {
             // Create and execute our Digester
             Digester digester = createStopDigester();
             File file = configFile();
-            FileInputStream fis = null;
-            try {
+            try (FileInputStream fis = new FileInputStream(file)) {
                 InputSource is =
                     new InputSource(file.toURI().toURL().toString());
-                fis = new FileInputStream(file);
                 is.setByteStream(fis);
                 digester.push(this);
                 digester.parse(is);
             } catch (Exception e) {
                 log.error("Catalina.stop: ", e);
                 System.exit(1);
-            } finally {
-                if (fis != null) {
-                    try {
-                        fis.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
             }
         } else {
             // Server object already present. Must be running as a service
@@ -457,11 +447,8 @@ public class Catalina {
         // Stop the existing server
         s = getServer();
         if (s.getPort()>0) {
-            Socket socket = null;
-            OutputStream stream = null;
-            try {
-                socket = new Socket(s.getAddress(), s.getPort());
-                stream = socket.getOutputStream();
+            try (Socket socket = new Socket(s.getAddress(), s.getPort());
+                    OutputStream stream = socket.getOutputStream()) {
                 String shutdown = s.getShutdown();
                 for (int i = 0; i < shutdown.length(); i++) {
                     stream.write(shutdown.charAt(i));
@@ -476,21 +463,6 @@ public class Catalina {
             } catch (IOException e) {
                 log.error("Catalina.stop: ", e);
                 System.exit(1);
-            } finally {
-                if (stream != null) {
-                    try {
-                        stream.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
-                if (socket != null) {
-                    try {
-                        socket.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
             }
         } else {
             log.error(sm.getString("catalina.stopServer"));
@@ -509,7 +481,6 @@ public class Catalina {
         initDirs();
 
         // Before digester - it may be needed
-
         initNaming();
 
         // Create and execute our Digester
@@ -544,7 +515,7 @@ public class Catalina {
 
         // This should be included in catalina.jar
         // Alternative: don't bother with xml, just create it manually.
-        if( inputStream==null ) {
+        if (inputStream == null) {
             try {
                 inputStream = getClass().getClassLoader()
                         .getResourceAsStream("server-embed.xml");
@@ -609,14 +580,12 @@ public class Catalina {
             } else {
                 log.error("Catalina.start", e);
             }
-
         }
 
         long t2 = System.nanoTime();
         if(log.isInfoEnabled()) {
             log.info("Initialization processed in " + ((t2 - t1) / 1000000) + 
" ms");
         }
-
     }
 
 

Modified: tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Wed Mar 26 
14:33:44 2014
@@ -1899,9 +1899,7 @@ public class ContextConfig implements Li
             }
         } else if (webResource.isFile() &&
                 webResource.getName().endsWith(".class")) {
-            InputStream is = null;
-            try {
-                is = webResource.getInputStream();
+            try (InputStream is = webResource.getInputStream()) {
                 processAnnotationsStream(is, fragment, handlesTypesOnly);
             } catch (IOException e) {
                 log.error(sm.getString("contextConfig.inputStreamWebResource",
@@ -1909,14 +1907,6 @@ public class ContextConfig implements Li
             } catch (ClassFormatException e) {
                 log.error(sm.getString("contextConfig.inputStreamWebResource",
                         webResource.getWebappPath()),e);
-            } finally {
-                if (is != null) {
-                    try {
-                        is.close();
-                    } catch (Throwable t) {
-                        ExceptionUtils.handleThrowable(t);
-                    }
-                }
             }
         }
     }
@@ -1948,7 +1938,6 @@ public class ContextConfig implements Li
             boolean handlesTypesOnly) {
 
         Jar jar = null;
-        InputStream is;
 
         try {
             jar = JarFactory.newInstance(url);
@@ -1957,9 +1946,7 @@ public class ContextConfig implements Li
             String entryName = jar.getEntryName();
             while (entryName != null) {
                 if (entryName.endsWith(".class")) {
-                    is = null;
-                    try {
-                        is = jar.getEntryInputStream();
+                    try (InputStream is = jar.getEntryInputStream()) {
                         processAnnotationsStream(
                                 is, fragment, handlesTypesOnly);
                     } catch (IOException e) {
@@ -1968,14 +1955,6 @@ public class ContextConfig implements Li
                     } catch (ClassFormatException e) {
                         log.error(sm.getString("contextConfig.inputStreamJar",
                                 entryName, url),e);
-                    } finally {
-                        if (is != null) {
-                            try {
-                                is.close();
-                            } catch (IOException ioe) {
-                                // Ignore
-                            }
-                        }
                     }
                 }
                 jar.nextEntry();
@@ -2001,9 +1980,7 @@ public class ContextConfig implements Li
                         new File(file,dir), fragment, handlesTypesOnly);
             }
         } else if (file.canRead() && file.getName().endsWith(".class")) {
-            FileInputStream fis = null;
-            try {
-                fis = new FileInputStream(file);
+            try (FileInputStream fis = new FileInputStream(file)) {
                 processAnnotationsStream(fis, fragment, handlesTypesOnly);
             } catch (IOException e) {
                 log.error(sm.getString("contextConfig.inputStreamFile",
@@ -2011,14 +1988,6 @@ public class ContextConfig implements Li
             } catch (ClassFormatException e) {
                 log.error(sm.getString("contextConfig.inputStreamFile",
                         file.getAbsolutePath()),e);
-            } finally {
-                if (fis != null) {
-                    try {
-                        fis.close();
-                    } catch (Throwable t) {
-                        ExceptionUtils.handleThrowable(t);
-                    }
-                }
             }
         }
     }
@@ -2182,13 +2151,11 @@ public class ContextConfig implements Li
     private void populateJavaClassCache(String className) {
         if (!javaClassCache.containsKey(className)) {
             String name = className.replace('.', '/') + ".class";
-            InputStream is =
-                    
context.getLoader().getClassLoader().getResourceAsStream(name);
-            if (is == null) {
-                return;
-            }
-            ClassParser parser = new ClassParser(is, null);
-            try {
+            try (InputStream is = 
context.getLoader().getClassLoader().getResourceAsStream(name)) {
+                if (is == null) {
+                    return;
+                }
+                ClassParser parser = new ClassParser(is, null);
                 JavaClass clazz = parser.parse();
                 populateJavaClassCache(clazz.getClassName(), clazz);
             } catch (ClassFormatException e) {
@@ -2197,12 +2164,6 @@ public class ContextConfig implements Li
             } catch (IOException e) {
                 log.debug(sm.getString("contextConfig.invalidSciHandlesTypes",
                         className), e);
-            } finally {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    // ignore
-                }
             }
         }
     }

Modified: tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/ExpandWar.java Wed Mar 26 
14:33:44 2014
@@ -14,8 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-
 package org.apache.catalina.startup;
 
 import java.io.BufferedOutputStream;
@@ -35,7 +33,6 @@ import java.util.zip.ZipException;
 import org.apache.catalina.Host;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
@@ -92,11 +89,9 @@ public class ExpandWar {
         }
         JarURLConnection juc = (JarURLConnection) war.openConnection();
         juc.setUseCaches(false);
-        JarFile jarFile = null;
-        InputStream input = null;
+
         boolean success = false;
-        try {
-            jarFile = juc.getJarFile();
+        try (JarFile jarFile = juc.getJarFile()) {
             Enumeration<JarEntry> jarEntries = jarFile.entries();
             while (jarEntries.hasMoreElements()) {
                 JarEntry jarEntry = jarEntries.nextElement();
@@ -123,20 +118,20 @@ public class ExpandWar {
                 if (name.endsWith("/")) {
                     continue;
                 }
-                input = jarFile.getInputStream(jarEntry);
 
-                if(null == input)
-                    throw new 
ZipException(sm.getString("expandWar.missingJarEntry", jarEntry.getName()));
 
-                // Bugzilla 33636
-                expand(input, expandedFile);
-                long lastModified = jarEntry.getTime();
-                if ((lastModified != -1) && (lastModified != 0)) {
-                    expandedFile.setLastModified(lastModified);
+                try (InputStream input = jarFile.getInputStream(jarEntry)) {
+                    if (null == input)
+                        throw new 
ZipException(sm.getString("expandWar.missingJarEntry",
+                                jarEntry.getName()));
+
+                    // Bugzilla 33636
+                    expand(input, expandedFile);
+                    long lastModified = jarEntry.getTime();
+                    if ((lastModified != -1) && (lastModified != 0)) {
+                        expandedFile.setLastModified(lastModified);
+                    }
                 }
-
-                input.close();
-                input = null;
             }
             success = true;
         } catch (IOException e) {
@@ -147,27 +142,10 @@ public class ExpandWar {
                 // clean
                 deleteDir(docBase);
             }
-            if (input != null) {
-                try {
-                    input.close();
-                } catch (IOException ioe) {
-                    // Ignore
-                }
-                input = null;
-            }
-            if (jarFile != null) {
-                try {
-                    jarFile.close();
-                } catch (IOException ioe) {
-                    // Ignore
-                }
-                jarFile = null;
-            }
         }
 
         // Return the absolute path to our new document base directory
-        return (docBase.getAbsolutePath());
-
+        return docBase.getAbsolutePath();
     }
 
 
@@ -184,8 +162,7 @@ public class ExpandWar {
      * @exception IOException if an input/output error was encountered
      *            during validation
      */
-    public static void validate(Host host, URL war, String pathname)
-        throws IOException {
+    public static void validate(Host host, URL war, String pathname) throws 
IOException {
 
         File docBase = new File(host.getAppBaseFile(), pathname);
 
@@ -196,9 +173,7 @@ public class ExpandWar {
         }
         JarURLConnection juc = (JarURLConnection) war.openConnection();
         juc.setUseCaches(false);
-        JarFile jarFile = null;
-        try {
-            jarFile = juc.getJarFile();
+        try (JarFile jarFile = juc.getJarFile()) {
             Enumeration<JarEntry> jarEntries = jarFile.entries();
             while (jarEntries.hasMoreElements()) {
                 JarEntry jarEntry = jarEntries.nextElement();
@@ -216,15 +191,6 @@ public class ExpandWar {
             }
         } catch (IOException e) {
             throw e;
-        } finally {
-            if (jarFile != null) {
-                try {
-                    jarFile.close();
-                } catch (Throwable t) {
-                    ExceptionUtils.handleThrowable(t);
-                }
-                jarFile = null;
-            }
         }
     }
 
@@ -256,34 +222,16 @@ public class ExpandWar {
             if (fileSrc.isDirectory()) {
                 result = copy(fileSrc, fileDest);
             } else {
-                FileChannel ic = null;
-                FileChannel oc = null;
-                try {
-                    ic = (new FileInputStream(fileSrc)).getChannel();
-                    oc = (new FileOutputStream(fileDest)).getChannel();
+                try (FileChannel ic = (new 
FileInputStream(fileSrc)).getChannel();
+                        FileChannel oc = (new 
FileOutputStream(fileDest)).getChannel()) {
                     ic.transferTo(0, ic.size(), oc);
                 } catch (IOException e) {
-                    log.error(sm.getString
-                            ("expandWar.copy", fileSrc, fileDest), e);
+                    log.error(sm.getString("expandWar.copy", fileSrc, 
fileDest), e);
                     result = false;
-                } finally {
-                    if (ic != null) {
-                        try {
-                            ic.close();
-                        } catch (IOException e) {
-                        }
-                    }
-                    if (oc != null) {
-                        try {
-                            oc.close();
-                        } catch (IOException e) {
-                        }
-                    }
                 }
             }
         }
         return result;
-
     }
 
 
@@ -298,6 +246,7 @@ public class ExpandWar {
         return delete(dir, true);
     }
 
+
     /**
      * Delete the specified directory, including all of its contents and
      * sub-directories recursively.
@@ -335,6 +284,7 @@ public class ExpandWar {
         return deleteDir(dir, true);
     }
 
+
     /**
      * Delete the specified directory, including all of its contents and
      * sub-directories recursively.
@@ -371,7 +321,6 @@ public class ExpandWar {
         }
 
         return result;
-
     }
 
 
@@ -383,12 +332,9 @@ public class ExpandWar {
      *
      * @exception IOException if an input/output error occurs
      */
-    private static void expand(InputStream input, File file)
-        throws IOException {
-        BufferedOutputStream output = null;
-        try {
-            output =
-                new BufferedOutputStream(new FileOutputStream(file));
+    private static void expand(InputStream input, File file) throws 
IOException {
+        try (BufferedOutputStream output =
+                new BufferedOutputStream(new FileOutputStream(file))) {
             byte buffer[] = new byte[2048];
             while (true) {
                 int n = input.read(buffer);
@@ -396,16 +342,6 @@ public class ExpandWar {
                     break;
                 output.write(buffer, 0, n);
             }
-        } finally {
-            if (output != null) {
-                try {
-                    output.close();
-                } catch (IOException e) {
-                    // Ignore
-                }
-            }
         }
     }
-
-
 }

Modified: tomcat/trunk/java/org/apache/catalina/startup/HostConfig.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/HostConfig.java?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/HostConfig.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/HostConfig.java Wed Mar 26 
14:33:44 2014
@@ -761,19 +761,12 @@ public class HostConfig
      */
     protected void deployWAR(ContextName cn, File war) {
 
-        // Checking for a nested /META-INF/context.xml
-        JarFile jar = null;
-        InputStream istream = null;
-        FileOutputStream fos = null;
-        BufferedOutputStream ostream = null;
-
         File xml = new File(host.getAppBaseFile(),
                 cn.getBaseName() + "/META-INF/context.xml");
 
         boolean xmlInWar = false;
         JarEntry entry = null;
-        try {
-            jar = new JarFile(war);
+        try (JarFile jar = new JarFile(war)) {
             entry = jar.getJarEntry(Constants.ApplicationContextXml);
             if (entry != null) {
                 xmlInWar = true;
@@ -782,14 +775,6 @@ public class HostConfig
             /* Ignore */
         } finally {
             entry = null;
-            if (jar != null) {
-                try {
-                    jar.close();
-                } catch (IOException ioe) {
-                    // Ignore;
-                }
-                jar = null;
-            }
         }
 
         Context context = null;
@@ -812,12 +797,11 @@ public class HostConfig
                 context.setConfigFile(xml.toURI().toURL());
             } else if (deployXML && xmlInWar) {
                 synchronized (digesterLock) {
-                    try {
-                        jar = new JarFile(war);
-                        entry =
-                            jar.getJarEntry(Constants.ApplicationContextXml);
-                        istream = jar.getInputStream(entry);
-                        context = (Context) digester.parse(istream);
+                    try (JarFile jar = new JarFile(war)) {
+                        entry = 
jar.getJarEntry(Constants.ApplicationContextXml);
+                        try (InputStream istream = jar.getInputStream(entry)) {
+                            context = (Context) digester.parse(istream);
+                        }
                     } catch (Exception e) {
                         log.error(sm.getString(
                                 "hostConfig.deployDescriptor.error",
@@ -829,23 +813,7 @@ public class HostConfig
                         context.setConfigFile(new URL("jar:" +
                                 war.toURI().toString() + "!/" +
                                 Constants.ApplicationContextXml));
-                        if (istream != null) {
-                            try {
-                                istream.close();
-                            } catch (IOException e) {
-                                /* Ignore */
-                            }
-                            istream = null;
-                        }
                         entry = null;
-                        if (jar != null) {
-                            try {
-                                jar.close();
-                            } catch (IOException e) {
-                                /* Ignore */
-                            }
-                            jar = null;
-                        }
                         digester.reset();
                     }
                 }
@@ -884,58 +852,23 @@ public class HostConfig
                 xml = new File(host.getConfigBaseFile(),
                         cn.getBaseName() + ".xml");
                 entry = null;
-                try {
-                    jar = new JarFile(war);
-                    entry =
-                        jar.getJarEntry(Constants.ApplicationContextXml);
-                    istream = jar.getInputStream(entry);
-
-                    fos = new FileOutputStream(xml);
-                    ostream = new BufferedOutputStream(fos, 1024);
-                    byte buffer[] = new byte[1024];
-                    while (true) {
-                        int n = istream.read(buffer);
-                        if (n < 0) {
-                            break;
+                try (JarFile jar = new JarFile(war)) {
+                    entry = jar.getJarEntry(Constants.ApplicationContextXml);
+                    try (InputStream istream = jar.getInputStream(entry);
+                            FileOutputStream fos = new FileOutputStream(xml);
+                            BufferedOutputStream ostream = new 
BufferedOutputStream(fos, 1024)) {
+                        byte buffer[] = new byte[1024];
+                        while (true) {
+                            int n = istream.read(buffer);
+                            if (n < 0) {
+                                break;
+                            }
+                            ostream.write(buffer, 0, n);
                         }
-                        ostream.write(buffer, 0, n);
+                        ostream.flush();
                     }
-                    ostream.flush();
                 } catch (IOException e) {
                     /* Ignore */
-                } finally {
-                    if (ostream != null) {
-                        try {
-                            ostream.close();
-                        } catch (IOException ioe) {
-                            // Ignore
-                        }
-                        ostream = null;
-                    }
-                    if (fos != null) {
-                        try {
-                            fos.close();
-                        } catch (IOException ioe) {
-                            // Ignore
-                        }
-                        fos = null;
-                    }
-                    if (istream != null) {
-                        try {
-                            istream.close();
-                        } catch (IOException ioe) {
-                            // Ignore
-                        }
-                        istream = null;
-                    }
-                    if (jar != null) {
-                        try {
-                            jar.close();
-                        } catch (IOException ioe) {
-                            // Ignore;
-                        }
-                        jar = null;
-                    }
                 }
             }
         }
@@ -1089,24 +1022,10 @@ public class HostConfig
                 }
 
                 if (copyThisXml) {
-                    InputStream is = null;
-                    OutputStream os = null;
-                    try {
-                        is = new FileInputStream(xml);
-                        os = new FileOutputStream(xmlCopy);
+                    try (InputStream is = new FileInputStream(xml);
+                            OutputStream os = new FileOutputStream(xmlCopy)) {
                         IOTools.flow(is, os);
                         // Don't catch IOE - let the outer try/catch handle it
-                    } finally {
-                        try {
-                            if (is != null) is.close();
-                        } catch (IOException e){
-                            // Ignore
-                        }
-                        try {
-                            if (os != null) os.close();
-                        } catch (IOException e){
-                            // Ignore
-                        }
                     }
                     context.setConfigFile(xmlCopy.toURI().toURL());
                 } else {

Modified: tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java?rev=1581864&r1=1581863&r2=1581864&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java Wed Mar 26 
14:33:44 2014
@@ -1094,9 +1094,7 @@ public class Tomcat {
 
     private URL getWebappConfigFileFromJar(File docBase, String url) {
         URL result = null;
-        JarFile jar = null;
-        try {
-            jar = new JarFile(docBase);
+        try (JarFile jar = new JarFile(docBase)) {
             JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
             if (entry != null) {
                 result = new URL("jar:" + docBase.toURI().toString() + "!/"
@@ -1105,14 +1103,6 @@ public class Tomcat {
         } catch (IOException e) {
             Logger.getLogger(getLoggerName(getHost(), url)).log(Level.WARNING,
                     "Unable to determine web application context.xml " + 
docBase, e);
-        } finally {
-            if (jar != null) {
-                try {
-                    jar.close();
-                } catch (IOException e) {
-                    // ignore
-                }
-            }
         }
         return result;
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to