Author: markt Date: Wed Mar 26 16:00:10 2014 New Revision: 1581900 URL: http://svn.apache.org/r1581900 Log: More try-with-resources
Modified: tomcat/trunk/TOMCAT-NEXT.txt tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java Modified: tomcat/trunk/TOMCAT-NEXT.txt URL: http://svn.apache.org/viewvc/tomcat/trunk/TOMCAT-NEXT.txt?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/TOMCAT-NEXT.txt (original) +++ tomcat/trunk/TOMCAT-NEXT.txt Wed Mar 26 16:00:10 2014 @@ -214,8 +214,8 @@ but possibly 7.1.x). - Use of try with resources - Started. - javax.* complete - - o.a.[catalina to el ] complete - - o.a.jasper in progress + - o.a.[catalina to jasper ] complete + - o.a.juli in progress - remainder TODO - Catching multiple exceptions - Started 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=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java (original) +++ tomcat/trunk/java/org/apache/catalina/startup/ContextConfig.java Wed Mar 26 16:00:10 2014 @@ -1655,21 +1655,21 @@ public class ContextConfig implements Li protected void processResourceJARs(Set<WebXml> fragments) { for (WebXml fragment : fragments) { URL url = fragment.getURL(); - Jar jar = null; try { if ("jar".equals(url.getProtocol())) { - jar = JarFactory.newInstance(url); - jar.nextEntry(); - String entryName = jar.getEntryName(); - while (entryName != null) { - if (entryName.startsWith("META-INF/resources/")) { - context.getResources().createWebResourceSet( - WebResourceRoot.ResourceSetType.RESOURCE_JAR, - "/", url, "/META-INF/resources"); - break; - } + try (Jar jar = JarFactory.newInstance(url)) { jar.nextEntry(); - entryName = jar.getEntryName(); + String entryName = jar.getEntryName(); + while (entryName != null) { + if (entryName.startsWith("META-INF/resources/")) { + context.getResources().createWebResourceSet( + WebResourceRoot.ResourceSetType.RESOURCE_JAR, + "/", url, "/META-INF/resources"); + break; + } + jar.nextEntry(); + entryName = jar.getEntryName(); + } } } else if ("file".equals(url.getProtocol())) { File file = new File(url.toURI()); @@ -1686,10 +1686,6 @@ public class ContextConfig implements Li } catch (URISyntaxException e) { log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName())); - } finally { - if (jar != null) { - jar.close(); - } } } } @@ -1937,11 +1933,7 @@ public class ContextConfig implements Li protected void processAnnotationsJar(URL url, WebXml fragment, boolean handlesTypesOnly) { - Jar jar = null; - - try { - jar = JarFactory.newInstance(url); - + try (Jar jar = JarFactory.newInstance(url)) { jar.nextEntry(); String entryName = jar.getEntryName(); while (entryName != null) { @@ -1962,10 +1954,6 @@ public class ContextConfig implements Li } } catch (IOException e) { log.error(sm.getString("contextConfig.jarFile", url), e); - } finally { - if (jar != null) { - jar.close(); - } } } Modified: tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/Compiler.java Wed Mar 26 16:00:10 2014 @@ -173,7 +173,6 @@ public abstract class Compiler { ctxt.checkOutputDir(); String javaFileName = ctxt.getServletJavaFileName(); - ServletWriter writer = null; try { /* * The setting of isELIgnored changes the behaviour of the parser @@ -207,11 +206,10 @@ public abstract class Compiler { if (ctxt.isPrototypeMode()) { // generate prototype .java file for the tag file - writer = setupContextWriter(javaFileName); - Generator.generate(writer, this, pageNodes); - writer.close(); - writer = null; - return null; + try (ServletWriter writer = setupContextWriter(javaFileName)) { + Generator.generate(writer, this, pageNodes); + return null; + } } // Validate and process attributes - don't re-validate the @@ -248,10 +246,9 @@ public abstract class Compiler { ELFunctionMapper.map(pageNodes); // generate servlet .java file - writer = setupContextWriter(javaFileName); - Generator.generate(writer, this, pageNodes); - writer.close(); - writer = null; + try (ServletWriter writer = setupContextWriter(javaFileName)) { + Generator.generate(writer, this, pageNodes); + } // The writer is only used during the compile, dereference // it in the JspCompilationContext when done to allow it @@ -265,14 +262,6 @@ public abstract class Compiler { } } catch (Exception e) { - if (writer != null) { - try { - writer.close(); - writer = null; - } catch (Exception e1) { - // do nothing - } - } // Remove the generated .java file File file = new File(javaFileName); if (file.exists()) { @@ -283,14 +272,6 @@ public abstract class Compiler { } } throw e; - } finally { - if (writer != null) { - try { - writer.close(); - } catch (Exception e2) { - // do nothing - } - } } // JSR45 Support Modified: tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/JDTCompiler.java Wed Mar 26 16:00:10 2014 @@ -105,14 +105,10 @@ public class JDTCompiler extends org.apa @Override public char[] getContents() { char[] result = null; - FileInputStream is = null; - InputStreamReader isr = null; - Reader reader = null; - try { - is = new FileInputStream(sourceFile); - isr = new InputStreamReader(is, - ctxt.getOptions().getJavaEncoding()); - reader = new BufferedReader(isr); + try (FileInputStream is = new FileInputStream(sourceFile); + InputStreamReader isr = new InputStreamReader( + is, ctxt.getOptions().getJavaEncoding()); + Reader reader = new BufferedReader(isr)) { char[] chars = new char[8192]; StringBuilder buf = new StringBuilder(); int count; @@ -124,22 +120,6 @@ public class JDTCompiler extends org.apa buf.getChars(0, result.length, result, 0); } catch (IOException e) { log.error("Compilation error", e); - } finally { - if (reader != null) { - try { - reader.close(); - } catch (IOException ioe) {/*Ignore*/} - } - if (isr != null) { - try { - isr.close(); - } catch (IOException ioe) {/*Ignore*/} - } - if (is != null) { - try { - is.close(); - } catch (IOException exc) {/*Ignore*/} - } } return result; } @@ -204,17 +184,17 @@ public class JDTCompiler extends org.apa private NameEnvironmentAnswer findType(String className) { - InputStream is = null; - try { - if (className.equals(targetClassName)) { - ICompilationUnit compilationUnit = - new CompilationUnit(sourceFile, className); - return - new NameEnvironmentAnswer(compilationUnit, null); - } - String resourceName = + if (className.equals(targetClassName)) { + ICompilationUnit compilationUnit = + new CompilationUnit(sourceFile, className); + return + new NameEnvironmentAnswer(compilationUnit, null); + } + + String resourceName = className.replace('.', '/') + ".class"; - is = classLoader.getResourceAsStream(resourceName); + + try (InputStream is = classLoader.getResourceAsStream(resourceName)) { if (is != null) { byte[] classBytes; byte[] buf = new byte[8192]; @@ -237,14 +217,6 @@ public class JDTCompiler extends org.apa log.error("Compilation error", exc); } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) { log.error("Compilation error", exc); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException exc) { - // Ignore - } - } } return null; } Modified: tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/JavacErrorDetail.java Wed Mar 26 16:00:10 2014 @@ -86,71 +86,52 @@ public class JavacErrorDetail { // be modified (corrected) during the execution of this method if (jspBeginLineNum > 0 && ctxt != null) { - InputStream is = null; - FileInputStream fis = null; - - try { + try (InputStream is = ctxt.getResourceAsStream(jspFileName)) { // Read both files in, so we can inspect them - is = ctxt.getResourceAsStream(jspFileName); String[] jspLines = readFile(is); - fis = new FileInputStream(ctxt.getServletJavaFileName()); - String[] javaLines = readFile(fis); + try (FileInputStream fis = new FileInputStream(ctxt.getServletJavaFileName())) { + String[] javaLines = readFile(fis); - if (jspLines.length < jspBeginLineNum) { - // Avoid ArrayIndexOutOfBoundsException - // Probably bug 48498 but could be some other cause - jspExtract = Localizer.getMessage("jsp.error.bug48498"); - return; - } + if (jspLines.length < jspBeginLineNum) { + // Avoid ArrayIndexOutOfBoundsException + // Probably bug 48498 but could be some other cause + jspExtract = Localizer.getMessage("jsp.error.bug48498"); + return; + } - // If the line contains the opening of a multi-line scriptlet - // block, then the JSP line number we got back is probably - // faulty. Scan forward to match the java line... - if (jspLines[jspBeginLineNum-1].lastIndexOf("<%") > - jspLines[jspBeginLineNum-1].lastIndexOf("%>")) { - String javaLine = javaLines[javaLineNum-1].trim(); - - for (int i=jspBeginLineNum-1; i<jspLines.length; i++) { - if (jspLines[i].indexOf(javaLine) != -1) { - // Update jsp line number - jspBeginLineNum = i+1; - break; + // If the line contains the opening of a multi-line scriptlet + // block, then the JSP line number we got back is probably + // faulty. Scan forward to match the java line... + if (jspLines[jspBeginLineNum-1].lastIndexOf("<%") > + jspLines[jspBeginLineNum-1].lastIndexOf("%>")) { + String javaLine = javaLines[javaLineNum-1].trim(); + + for (int i=jspBeginLineNum-1; i<jspLines.length; i++) { + if (jspLines[i].indexOf(javaLine) != -1) { + // Update jsp line number + jspBeginLineNum = i+1; + break; + } } } - } - // copy out a fragment of JSP to display to the user - StringBuilder fragment = new StringBuilder(1024); - int startIndex = Math.max(0, jspBeginLineNum-1-3); - int endIndex = Math.min( - jspLines.length-1, jspBeginLineNum-1+3); - - for (int i=startIndex;i<=endIndex; ++i) { - fragment.append(i+1); - fragment.append(": "); - fragment.append(jspLines[i]); - fragment.append(Constants.NEWLINE); + // copy out a fragment of JSP to display to the user + StringBuilder fragment = new StringBuilder(1024); + int startIndex = Math.max(0, jspBeginLineNum-1-3); + int endIndex = Math.min( + jspLines.length-1, jspBeginLineNum-1+3); + + for (int i=startIndex;i<=endIndex; ++i) { + fragment.append(i+1); + fragment.append(": "); + fragment.append(jspLines[i]); + fragment.append(Constants.NEWLINE); + } + jspExtract = fragment.toString(); } - jspExtract = fragment.toString(); - } catch (IOException ioe) { // Can't read files - ignore - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException ioe) { - // Ignore - } - } - if (fis != null) { - try { - fis.close(); - } catch (IOException ioe) { - // Ignore - } - } } } this.jspBeginLineNum = jspBeginLineNum; Modified: tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/ParserController.java Wed Mar 26 16:00:10 2014 @@ -220,22 +220,13 @@ class ParserController implements TagCon isEncodingSpecifiedInProlog, isBomPresent); } else { // Standard syntax - InputStreamReader inStreamReader = null; - try { - inStreamReader = JspUtil.getReader(absFileName, sourceEnc, - jar, ctxt, err, skip); + try (InputStreamReader inStreamReader = JspUtil.getReader( + absFileName, sourceEnc, jar, ctxt, err, skip);) { JspReader jspReader = new JspReader(ctxt, absFileName, inStreamReader, err); parsedPage = Parser.parse(this, jspReader, parent, isTagFile, directiveOnly, jar, sourceEnc, jspConfigPageEnc, isDefaultPageEncoding, isBomPresent); - } finally { - if (inStreamReader != null) { - try { - inStreamReader.close(); - } catch (Exception any) { - } - } } } Modified: tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/ServletWriter.java Wed Mar 26 16:00:10 2014 @@ -24,7 +24,8 @@ import java.io.PrintWriter; * @author Anil K. Vijendran * @author Kin-man Chung */ -public class ServletWriter { +public class ServletWriter implements AutoCloseable { + private static final int TAB_WIDTH = 2; private static final String SPACES = " "; @@ -43,6 +44,7 @@ public class ServletWriter { this.writer = writer; } + @Override public void close() { writer.close(); } Modified: tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java (original) +++ tomcat/trunk/java/org/apache/jasper/servlet/JspCServletContext.java Wed Mar 26 16:00:10 2014 @@ -344,23 +344,12 @@ public class JspCServletContext implemen throw new MalformedURLException("Path '" + path + "' does not start with '/'"); URL url = new URL(myResourceBaseURL, path.substring(1)); - InputStream is = null; - try { - is = url.openStream(); + try (InputStream is = url.openStream()) { } catch (Throwable t) { ExceptionUtils.handleThrowable(t); url = null; - } finally { - if (is != null) { - try { - is.close(); - } catch (Throwable t2) { - ExceptionUtils.handleThrowable(t2); - } - } } return url; - } Modified: tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java (original) +++ tomcat/trunk/java/org/apache/jasper/servlet/TldScanner.java Wed Mar 26 16:00:10 2014 @@ -278,9 +278,9 @@ public class TldScanner { jarFound = true; } boolean found = false; - Jar jar = JarFactory.newInstance(urlConn.getURL()); - URL jarURL = jar.getJarFileURL(); - try { + URL jarURL = null; + try (Jar jar = JarFactory.newInstance(urlConn.getURL())) { + jarURL = jar.getJarFileURL(); jar.nextEntry(); for (String entryName = jar.getEntryName(); entryName != null; @@ -298,8 +298,6 @@ public class TldScanner { throw new IOException(e); } } - } finally { - jar.close(); } if (found) { tldFound = true; Modified: tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java?rev=1581900&r1=1581899&r2=1581900&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/scan/Jar.java Wed Mar 26 16:00:10 2014 @@ -29,7 +29,7 @@ import java.net.URL; * based {@link java.net.URL}s, {@link java.util.jar.JarFile} creates a copy of the JAR in the * temporary directory so {@link java.util.jar.JarInputStream} is faster. */ -public interface Jar { +public interface Jar extends AutoCloseable { /** * Obtain the URL for accessing the JAR file. @@ -70,6 +70,7 @@ public interface Jar { /** * Close any resources associated with this JAR. */ + @Override void close(); /** --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org