diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/gosoftware/PcChargeApi.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/gosoftware/PcChargeApi.java index cd924839e3..009573c494 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/gosoftware/PcChargeApi.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/thirdparty/gosoftware/PcChargeApi.java @@ -18,18 +18,17 @@ *******************************************************************************/ package org.apache.ofbiz.accounting.thirdparty.gosoftware; +import java.io.DataInputStream; import java.io.IOException; import java.io.PrintStream; -import java.io.DataInputStream; import java.net.Socket; import javax.xml.parsers.ParserConfigurationException; -import org.apache.ofbiz.base.util.UtilXml; -import org.apache.ofbiz.base.util.ObjectType; -import org.apache.ofbiz.base.util.GeneralException; import org.apache.ofbiz.base.util.Debug; - +import org.apache.ofbiz.base.util.GeneralException; +import org.apache.ofbiz.base.util.ObjectType; +import org.apache.ofbiz.base.util.UtilXml; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.SAXException; @@ -189,6 +188,7 @@ public class PcChargeApi { Socket sock = new Socket(host, port); PrintStream ps = new PrintStream(sock.getOutputStream()); DataInputStream dis = new DataInputStream(sock.getInputStream()); + sock.close(); ps.print(this.toString()); ps.flush(); diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java b/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java index 1ca5942cc2..428557a9bc 100644 --- a/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java +++ b/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java @@ -579,8 +579,7 @@ public class PdfSurveyServices { String pdfFileNameIn = (String)context.get("pdfFileNameIn"); String contentId = (String)context.get("contentId"); if (UtilValidate.isNotEmpty(pdfFileNameIn)) { - try { - FileInputStream fis = new FileInputStream(pdfFileNameIn); + try (FileInputStream fis = new FileInputStream(pdfFileNameIn)) { int c; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((c = fis.read()) != -1) baos.write(c); diff --git a/applications/marketing/src/main/java/org/apache/ofbiz/sfa/vcard/VCard.java b/applications/marketing/src/main/java/org/apache/ofbiz/sfa/vcard/VCard.java index 7c1f88cf11..d1887e5834 100644 --- a/applications/marketing/src/main/java/org/apache/ofbiz/sfa/vcard/VCard.java +++ b/applications/marketing/src/main/java/org/apache/ofbiz/sfa/vcard/VCard.java @@ -31,16 +31,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import ezvcard.Ezvcard; -import ezvcard.io.text.VCardReader; -import ezvcard.parameter.AddressType; -import ezvcard.parameter.TelephoneType; -import ezvcard.parameter.EmailType; -import ezvcard.property.Address; -import ezvcard.property.Email; -import ezvcard.property.FormattedName; -import ezvcard.property.StructuredName; -import ezvcard.property.Telephone; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.FileUtil; import org.apache.ofbiz.base.util.StringUtil; @@ -62,6 +52,17 @@ import org.apache.ofbiz.service.GenericServiceException; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.service.ServiceUtil; +import ezvcard.Ezvcard; +import ezvcard.io.text.VCardReader; +import ezvcard.parameter.AddressType; +import ezvcard.parameter.EmailType; +import ezvcard.parameter.TelephoneType; +import ezvcard.property.Address; +import ezvcard.property.Email; +import ezvcard.property.FormattedName; +import ezvcard.property.StructuredName; +import ezvcard.property.Telephone; + public class VCard { public static final String module = VCard.class.getName(); public static final String resourceError = "MarketingUiLabels"; @@ -71,8 +72,9 @@ public class VCard { * @param dctx * @param context * @return + * @throws IOException */ - public static Map importVCard(DispatchContext dctx, Map context) { + public static Map importVCard(DispatchContext dctx, Map context) throws IOException { LocalDispatcher dispatcher = dctx.getDispatcher(); Delegator delegator = dctx.getDelegator(); Locale locale = (Locale) context.get("locale"); @@ -84,10 +86,9 @@ public class VCard { boolean isGroup = false; List> partiesCreated = new ArrayList>(); List> partiesExist = new ArrayList>(); - String partyName = ""; + String partyName = ""; // TODO this is not used yet - try { - VCardReader vCardReader = new VCardReader(in); + try (VCardReader vCardReader = new VCardReader(in)) { ezvcard.VCard vcard = null; while ((vcard = vCardReader.readNext()) != null) { @@ -162,6 +163,7 @@ public class VCard { } else { //TODO change uncorrect labellisation String emailFormatErrMsg = UtilProperties.getMessage(resourceError, "SfaImportVCardEmailFormatError", locale); + vCardReader.close(); return ServiceUtil.returnError(structuredName.getGiven() + " " + structuredName.getFamily() + " has " + emailFormatErrMsg); } } @@ -215,7 +217,6 @@ public class VCard { resp = dispatcher.runSync("createPartyIdentification", createPartyIdentificationMap); } } - vCardReader.close(); } catch (IOException | GenericEntityException | GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, diff --git a/applications/product/src/main/java/org/apache/ofbiz/product/spreadsheetimport/ImportProductServices.java b/applications/product/src/main/java/org/apache/ofbiz/product/spreadsheetimport/ImportProductServices.java index ecd7aded12..8183adb54c 100644 --- a/applications/product/src/main/java/org/apache/ofbiz/product/spreadsheetimport/ImportProductServices.java +++ b/applications/product/src/main/java/org/apache/ofbiz/product/spreadsheetimport/ImportProductServices.java @@ -28,11 +28,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import org.apache.poi.hssf.usermodel.HSSFCell; -import org.apache.poi.hssf.usermodel.HSSFRow; -import org.apache.poi.hssf.usermodel.HSSFSheet; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.UtilProperties; import org.apache.ofbiz.base.util.UtilValidate; @@ -41,6 +36,11 @@ import org.apache.ofbiz.entity.GenericEntityException; import org.apache.ofbiz.entity.GenericValue; import org.apache.ofbiz.service.DispatchContext; import org.apache.ofbiz.service.ServiceUtil; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ImportProductServices { @@ -59,8 +59,9 @@ public class ImportProductServices { * @param dctx the dispatch context * @param context the context * @return the result of the service execution + * @throws IOException */ - public static Map productImportFromSpreadsheet(DispatchContext dctx, Map context) { + public static Map productImportFromSpreadsheet(DispatchContext dctx, Map context) throws IOException { Delegator delegator = dctx.getDelegator(); Locale locale = (Locale) context.get("locale"); // System.getProperty("user.dir") returns the path upto ofbiz home @@ -110,6 +111,7 @@ public class ImportProductServices { // get first sheet HSSFSheet sheet = wb.getSheetAt(0); + wb.close(); int sheetLastRowNumber = sheet.getLastRowNum(); for (int j = 1; j <= sheetLastRowNumber; j++) { HSSFRow row = sheet.getRow(j); diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/Debug.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/Debug.java index 6e9be0c43b..3b6115dfaf 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/Debug.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/Debug.java @@ -108,6 +108,7 @@ public final class Debug { Formatter formatter = new Formatter(sb); formatter.format(msg, params); msg = sb.toString(); + formatter.close(); } // log diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java index 8c2a3276bf..f0c9bea462 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/GroovyUtil.java @@ -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 context) throws GeneralException { diff --git a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java index 5ef9d2a31d..9d5ab1130c 100644 --- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java +++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/ScriptUtil.java @@ -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; } diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java index 9f76b9c2df..d6242d3bd6 100644 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java +++ b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/DatabaseUtil.java @@ -1809,7 +1809,6 @@ public class DatabaseUtil { } Connection connection = null; - Statement stmt = null; try { connection = getConnection(); @@ -1852,8 +1851,7 @@ public class DatabaseUtil { String sql = sqlBuf.toString(); if (Debug.infoOn()) Debug.logInfo("[addColumn] sql=" + sql, module); - try { - stmt = connection.createStatement(); + try (Statement stmt = connection.createStatement()) { stmt.executeUpdate(sql); } catch (SQLException e) { // if that failed try the alternate syntax real quick @@ -1880,21 +1878,13 @@ public class DatabaseUtil { String sql2 = sql2Buf.toString(); if (Debug.infoOn()) Debug.logInfo("[addColumn] sql failed, trying sql2=" + sql2, module); - try { - stmt = connection.createStatement(); + try (Statement stmt = connection.createStatement()) { stmt.executeUpdate(sql2); } catch (SQLException e2) { // if this also fails report original error, not this error... return "SQL Exception while executing the following:\n" + sql + "\nError was: " + e.toString(); } } finally { - try { - if (stmt != null) { - stmt.close(); - } - } catch (SQLException e) { - Debug.logError(e, module); - } try { if (connection != null) { connection.close(); diff --git a/specialpurpose/ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStore.java b/specialpurpose/ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStore.java index a5316b5698..7e9d312b8d 100644 --- a/specialpurpose/ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStore.java +++ b/specialpurpose/ebaystore/src/main/java/org/apache/ofbiz/ebaystore/EbayStore.java @@ -2263,9 +2263,11 @@ public class EbayStore { // Upload image to ofbiz path /runtime/tmp . ByteBuffer byteWrap = (ByteBuffer) context.get("imageData"); File file = new File(System.getProperty("ofbiz.home"), "runtime" + File.separator + "tmp" + File.separator + imageFileName); - FileChannel wChannel = new FileOutputStream(file, false).getChannel(); + FileOutputStream fileOutputStream = new FileOutputStream(file, false); + FileChannel wChannel = fileOutputStream.getChannel(); wChannel.write(byteWrap); wChannel.close(); + fileOutputStream.close(); // Set path file picture to api and set picture details. String [] pictureFiles = {System.getProperty("ofbiz.home") + File.separator + "runtime" + File.separator + "tmp" + File.separator + imageFileName};