From 176a0d77522e6c69099f01cff0a1c88568fabc19 Mon Sep 17 00:00:00 2001 From: Sebastian Peters Date: Mon, 19 Nov 2018 22:13:06 +0100 Subject: [PATCH] Fix: Resources should be closed Java 7 introduced the try-with-resources statement, which implicitly closes Closeables. Fixes sonar rule squid:S2095 --- .../xwork2/util/ClassPathFinder.java | 3 +-- .../xwork2/util/ResolverUtil.java | 10 +++------- .../util/classloader/JarResourceStore.java | 3 +-- .../util/location/LocatableProperties.java | 18 ++++++++--------- .../xwork2/util/location/LocationImpl.java | 6 ++---- .../struts2/jasper/compiler/SmapUtil.java | 20 +++++++++---------- .../struts2/osgi/host/BaseOsgiHost.java | 19 +++++++++--------- .../spring/StrutsSpringObjectFactory.java | 15 ++++++++------ 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/util/ClassPathFinder.java b/core/src/main/java/com/opensymphony/xwork2/util/ClassPathFinder.java index 8cb69cbef..2bcbfb0bc 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/ClassPathFinder.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/ClassPathFinder.java @@ -92,8 +92,7 @@ public class ClassPathFinder { } File entry = new File(entryURI); if (entry.isFile() && entry.toString().endsWith(".jar")) { - try { - ZipInputStream zip = new ZipInputStream(new FileInputStream(entry)); + try(ZipInputStream zip = new ZipInputStream(new FileInputStream(entry))) { for (ZipEntry zipEntry = zip.getNextEntry(); zipEntry != null; zipEntry = zip.getNextEntry()) { boolean doesMatch = patternMatcher.match(new HashMap(), zipEntry.getName(), compiledPattern); if (doesMatch) { diff --git a/core/src/main/java/com/opensymphony/xwork2/util/ResolverUtil.java b/core/src/main/java/com/opensymphony/xwork2/util/ResolverUtil.java index 279461850..81e5e61cc 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/ResolverUtil.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/ResolverUtil.java @@ -430,19 +430,15 @@ public class ResolverUtil { * @param jarfile the jar file to be examined for classes */ private void loadImplementationsInJar(Test test, String parent, File jarfile) { - - try { + try(JarInputStream jarStream = new JarInputStream(new FileInputStream(jarfile))) { JarEntry entry; - JarInputStream jarStream = new JarInputStream(new FileInputStream(jarfile)); - - while ( (entry = jarStream.getNextJarEntry() ) != null) { + while ((entry = jarStream.getNextJarEntry() ) != null) { String name = entry.getName(); if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) { addIfMatching(test, name); } } - } - catch (IOException ioe) { + } catch (IOException ioe) { LOG.error("Could not search jar file '" + jarfile + "' for classes matching criteria: " + test + " due to an IOException", ioe); } diff --git a/core/src/main/java/com/opensymphony/xwork2/util/classloader/JarResourceStore.java b/core/src/main/java/com/opensymphony/xwork2/util/classloader/JarResourceStore.java index 742018641..ce3b327fc 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/classloader/JarResourceStore.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/classloader/JarResourceStore.java @@ -37,8 +37,7 @@ public class JarResourceStore extends AbstractResourceStore { public byte[] read(String pResourceName) { InputStream in = null; - try { - ZipFile jarFile = new ZipFile(file); + try(ZipFile jarFile = new ZipFile(file)) { ZipEntry entry = jarFile.getEntry(pResourceName); //read into byte array diff --git a/core/src/main/java/com/opensymphony/xwork2/util/location/LocatableProperties.java b/core/src/main/java/com/opensymphony/xwork2/util/location/LocatableProperties.java index d96445147..9c79adc5f 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/location/LocatableProperties.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/location/LocatableProperties.java @@ -51,16 +51,16 @@ public class LocatableProperties extends Properties implements Locatable { @Override public void load(InputStream in) throws IOException { - Reader reader = new InputStreamReader(in); - PropertiesReader pr = new PropertiesReader(reader); - while (pr.nextProperty()) { - String name = pr.getPropertyName(); - String val = pr.getPropertyValue(); - int line = pr.getLineNumber(); - String desc = convertCommentsToString(pr.getCommentLines()); + try (PropertiesReader pr = new PropertiesReader(new InputStreamReader(in))) { + while (pr.nextProperty()) { + String name = pr.getPropertyName(); + String val = pr.getPropertyValue(); + int line = pr.getLineNumber(); + String desc = convertCommentsToString(pr.getCommentLines()); - Location loc = new LocationImpl(desc, location.getURI(), line, 0); - setProperty(name, val, loc); + Location loc = new LocationImpl(desc, location.getURI(), line, 0); + setProperty(name, val, loc); + } } } diff --git a/core/src/main/java/com/opensymphony/xwork2/util/location/LocationImpl.java b/core/src/main/java/com/opensymphony/xwork2/util/location/LocationImpl.java index d61eff1e4..298b34b3f 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/location/LocationImpl.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/location/LocationImpl.java @@ -154,10 +154,8 @@ public class LocationImpl implements Location, Serializable { public List getSnippet(int padding) { List snippet = new ArrayList<>(); if (getLineNumber() > 0) { - try { - InputStream in = new URL(getURI()).openStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); - + try (InputStream in = new URL(getURI()).openStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in));) { int lineno = 0; int errno = getLineNumber(); String line; diff --git a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/SmapUtil.java b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/SmapUtil.java index 17c752454..57d3c16e2 100644 --- a/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/SmapUtil.java +++ b/plugins/embeddedjsp/src/main/java/org/apache/struts2/jasper/compiler/SmapUtil.java @@ -264,9 +264,9 @@ public class SmapUtil { addSDE(); // write result - FileOutputStream outStream = new FileOutputStream(outClassFile); - outStream.write(gen, 0, genPos); - outStream.close(); + try(FileOutputStream outStream = new FileOutputStream(outClassFile)) { + outStream.write(gen, 0, genPos); + } } SDEInstaller(File inClassFile, File attrFile, File outClassFile) @@ -275,14 +275,14 @@ public class SmapUtil { } static byte[] readWhole(File input) throws IOException { - FileInputStream inStream = new FileInputStream(input); - int len = (int)input.length(); - byte[] bytes = new byte[len]; - if (inStream.read(bytes, 0, len) != len) { - throw new IOException("expected size: " + len); + try (FileInputStream inStream = new FileInputStream(input)) { + int len = (int) input.length(); + byte[] bytes = new byte[len]; + if (inStream.read(bytes, 0, len) != len) { + throw new IOException("expected size: " + len); + } + return bytes; } - inStream.close(); - return bytes; } void addSDE() throws UnsupportedEncodingException, IOException { diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/host/BaseOsgiHost.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/host/BaseOsgiHost.java index a29374658..a27b7bd4c 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/host/BaseOsgiHost.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/host/BaseOsgiHost.java @@ -270,16 +270,17 @@ public abstract class BaseOsgiHost implements OsgiHost { if ("jar".equals(url.getProtocol())) { try { FileManager fileManager = ServletActionContext.getContext().getInstance(FileManagerFactory.class).getFileManager(); - JarFile jarFile = new JarFile(new File(fileManager.normalizeToFileProtocol(url).toURI())); - Manifest manifest = jarFile.getManifest(); - if (manifest != null) { - String version = manifest.getMainAttributes().getValue("Bundle-Version"); - if (StringUtils.isNotBlank(version)) { - return getVersionFromString(version); + try (JarFile jarFile = new JarFile(new File(fileManager.normalizeToFileProtocol(url).toURI()))) { + Manifest manifest = jarFile.getManifest(); + if (manifest != null) { + String version = manifest.getMainAttributes().getValue("Bundle-Version"); + if (StringUtils.isNotBlank(version)) { + return getVersionFromString(version); + } + } else { + // try to get the version from the file name + return getVersionFromString(jarFile.getName()); } - } else { - //try to get the version from the file name - return getVersionFromString(jarFile.getName()); } } catch (Exception e) { LOG.error("Unable to extract version from [{}], defaulting to '1.0.0'", url.toExternalForm()); diff --git a/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java b/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java index 157518c27..6d2bfac15 100644 --- a/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java +++ b/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java @@ -104,14 +104,17 @@ public class StrutsSpringObjectFactory extends SpringObjectFactory { //prevent class caching useClassCache = false; - ClassReloadingXMLWebApplicationContext reloadingContext = (ClassReloadingXMLWebApplicationContext) appContext; - reloadingContext.setupReloading(watchList.split(","), acceptClasses, servletContext, "true".equals(reloadConfig)); - LOG.info("Class reloading is enabled. Make sure this is not used on a production environment!\n{}", watchList); + try (ClassReloadingXMLWebApplicationContext reloadingContext = (ClassReloadingXMLWebApplicationContext) appContext) { + reloadingContext.setupReloading(watchList.split(","), acceptClasses, servletContext, + "true".equals(reloadConfig)); + LOG.info("Class reloading is enabled. Make sure this is not used on a production environment!\n{}", + watchList); - setClassLoader(reloadingContext.getReloadingClassLoader()); + setClassLoader(reloadingContext.getReloadingClassLoader()); - //we need to reload the context, so our isntance of the factory is picked up - reloadingContext.refresh(); + // we need to reload the context, so our isntance of the factory is picked up + reloadingContext.refresh(); + } } this.setApplicationContext(appContext);