Fixes "a bunch of small leaks (closes missing, reported as warnings in Eclipse)" - https://issues.apache.org/jira/browse/OFBIZ-8115

At r1758927 I tried to fixe a bunch of closes missing, reported as warnings in Eclipse

Jacopo noticed and reported on dev ML http://markmail.org/message/b4hvzi7foftfq3j5 2 possible issues:
1) you are adding a close statement to code that already had the close statement in the "finally" block; your modification actually introduces a code pattern that is not correct (if an exception is thrown your close statement are not executed)
2) I suspect that you are closing the socket connection too early in PcChargeApi

1)The best option when closing resources (which implement AutoCloseable) is actually to use the try-with-resources statement. At least when there are no performance issues.
I have fixed the issue (only in DatabaseUtil.java) this way because
* there are not performance issues, we anyway create a Statement in both cases, we don't reuse one.
* The finally part is cleaner

I have also used the try-with-resources statement in other places where it fitted.

There is only 1 place where I was undecided: ScriptUtil.parseScript() which uses GroovyUtil.parseClass().
I put only a logError() there and returned null. 
I did not throw an IOException because else it spreads everywhere in FlexibleStringExpander.
Here introducing Optional would be better, but I have no time or that today.

2) was not a problem


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1758951 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacques Le Roux
2016-09-02 13:18:35 +00:00
parent 8bf34869da
commit 38e9885d3d
9 changed files with 64 additions and 55 deletions
@@ -108,6 +108,7 @@ public final class Debug {
Formatter formatter = new Formatter(sb);
formatter.format(msg, params);
msg = sb.toString();
formatter.close();
}
// log
@@ -18,11 +18,6 @@
*/
package org.apache.ofbiz.base.util;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -31,11 +26,16 @@ import java.util.Map;
import javax.script.ScriptContext;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.cache.UtilCache;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.cache.UtilCache;
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
/**
* Groovy Utilities.
@@ -173,19 +173,28 @@ public class GroovyUtil {
}
}
public static Class<?> loadClass(String path) throws ClassNotFoundException {
return new GroovyClassLoader().loadClass(path);
public static Class<?> loadClass(String path) throws ClassNotFoundException, IOException {
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<?> classLoader = groovyClassLoader.loadClass(path);
groovyClassLoader.close();
return classLoader;
}
public static Class<?> parseClass(InputStream in, String location) throws IOException {
return new GroovyClassLoader().parseClass(UtilIO.readString(in), location);
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<?> classLoader = groovyClassLoader.parseClass(UtilIO.readString(in), location);
groovyClassLoader.close();
return classLoader;
}
public static Class<?> parseClass(InputStream in, String location, GroovyClassLoader groovyClassLoader) throws IOException {
return groovyClassLoader.parseClass(UtilIO.readString(in), location);
}
public static Class<?> parseClass(String text) {
return new GroovyClassLoader().parseClass(text);
public static Class<?> parseClass(String text) throws IOException {
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<?> classLoader = groovyClassLoader.parseClass(text);
groovyClassLoader.close();
return classLoader;
}
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException {
@@ -46,9 +46,9 @@ import javax.script.ScriptException;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.apache.ofbiz.base.location.FlexibleLocation;
import org.apache.ofbiz.base.util.cache.UtilCache;
import org.codehaus.groovy.runtime.InvokerHelper;
/**
* Scripting utility methods. This is a facade class that is used to connect OFBiz to JSR-223 scripting engines.
@@ -408,7 +408,12 @@ public final class ScriptUtil {
public static Class<?> parseScript(String language, String script) {
Class<?> scriptClass = null;
if ("groovy".equals(language)) {
scriptClass = GroovyUtil.parseClass(script);
try {
scriptClass = GroovyUtil.parseClass(script);
} catch (IOException e) {
Debug.logError(e, module);
return null;
}
}
return scriptClass;
}