mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
Fix: Resources should be closed
Java 7 introduced the try-with-resources statement, which implicitly closes Closeables. Fixes sonar rule squid:S2095
This commit is contained in:
@@ -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<String, String>(), zipEntry.getName(), compiledPattern);
|
||||
if (doesMatch) {
|
||||
|
||||
@@ -430,19 +430,15 @@ public class ResolverUtil<T> {
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -154,10 +154,8 @@ public class LocationImpl implements Location, Serializable {
|
||||
public List<String> getSnippet(int padding) {
|
||||
List<String> 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;
|
||||
|
||||
+10
-10
@@ -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 {
|
||||
|
||||
@@ -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());
|
||||
|
||||
+9
-6
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user