svn commit: r1817566 - in /ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data: EntityDataLoadContainer.java EntityDataServices.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1817566 - in /ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data: EntityDataLoadContainer.java EntityDataServices.java

mbrohl
Author: mbrohl
Date: Fri Dec  8 21:02:21 2017
New Revision: 1817566

URL: http://svn.apache.org/viewvc?rev=1817566&view=rev
Log:
Improved: Fixing defects reported by FindBugs, package
org.apache.ofbiz.entityext.data.
(OFBIZ-9721)

The changes for EntityDataLoadContainer had to be applied manually
because of changes made in between.

Thanks Julian Leichert for reporting and providing the patch.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataLoadContainer.java
    ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataLoadContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataLoadContainer.java?rev=1817566&r1=1817565&r2=1817566&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataLoadContainer.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataLoadContainer.java Fri Dec  8 21:02:21 2017
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.TreeSet;
@@ -561,7 +562,7 @@ public class EntityDataLoadContainer imp
     private List<URL> retrieveDataUrlsFromDirectory(String directory) {
         return Optional.ofNullable(directory)
                 .map(dir -> Arrays.asList(new File(dir).listFiles()).stream()
-                        .filter(file -> file.getName().toLowerCase().endsWith(".xml"))
+                        .filter(file -> file.getName().toLowerCase(Locale.getDefault()).endsWith(".xml"))
                         .map(file -> UtilURL.fromFilename(file.getPath()))
                         .collect(Collectors.toList()))
                 .orElse(new ArrayList<URL>());

Modified: ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java?rev=1817566&r1=1817565&r2=1817566&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entityext/src/main/java/org/apache/ofbiz/entityext/data/EntityDataServices.java Fri Dec  8 21:02:21 2017
@@ -20,9 +20,10 @@ package org.apache.ofbiz.entityext.data;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -36,6 +37,7 @@ import org.apache.ofbiz.base.crypto.DesC
 import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.FileUtil;
 import org.apache.ofbiz.base.util.GeneralException;
+import org.apache.ofbiz.base.util.UtilIO;
 import org.apache.ofbiz.base.util.UtilMisc;
 import org.apache.ofbiz.base.util.UtilProperties;
 import org.apache.ofbiz.base.util.UtilURL;
@@ -173,7 +175,7 @@ public class EntityDataServices {
         if (listFile.exists()) {
             BufferedReader reader = null;
             try {
-                reader = new BufferedReader(new FileReader(listFile));
+                reader = new BufferedReader(new InputStreamReader(new FileInputStream(listFile), UtilIO.getUtf8()));
             } catch (FileNotFoundException e) {
                 Debug.logError(e, module);
             }
@@ -201,10 +203,13 @@ public class EntityDataServices {
                 Debug.logInfo("Read file list : " + fileList.size() + " entities.", module);
             }
         } else {
-            for (File file: root.listFiles()) {
-                String fileName = file.getName();
-                if (!fileName.startsWith("_") && fileName.endsWith(".txt")) {
-                    fileList.add(file);
+            File[] files = root.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    String fileName = file.getName();
+                    if (!fileName.startsWith("_") && fileName.endsWith(".txt")) {
+                        fileList.add(file);
+                    }
                 }
             }
             Debug.logInfo("No file list found; using directory order : " + fileList.size() + " entities.", module);
@@ -219,35 +224,33 @@ public class EntityDataServices {
         String[] header = null;
         File headerFile = new File(FileUtil.getFile(filePath.substring(0, filePath.lastIndexOf('/'))), "_" + file.getName());
 
-        boolean uniqueHeaderFile = true;
-        BufferedReader reader = null;
         if (headerFile.exists()) {
-            reader = new BufferedReader(new FileReader(headerFile));
+            try (
+                    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(headerFile),
+                            UtilIO.getUtf8()));) {
+
+                String firstLine = reader.readLine();
+                if (firstLine != null) {
+                    header = firstLine.split(delimiter);
+                }
+            } catch (IOException | SecurityException e) {
+                Debug.logError(e, module);
+            }
         } else {
-            uniqueHeaderFile = false;
-            reader = dataReader;
+            BufferedReader reader = dataReader;
+            String firstLine = reader.readLine();
+            if (firstLine != null) {
+                header = firstLine.split(delimiter);
+            }
         }
-
         // read one line from either the header file or the data file if no header file exists
-        String firstLine = reader.readLine();
-        if (firstLine != null) {
-            header = firstLine.split(delimiter);
-        }
-
-        if (uniqueHeaderFile) {
-            reader.close();
-        }
-
         return header;
     }
 
     private static int readEntityFile(File file, String delimiter, Delegator delegator) throws IOException, GeneralException {
         String entityName = file.getName().substring(0, file.getName().lastIndexOf('.'));
-        if (entityName == null) {
-            throw new GeneralException("Entity name cannot be null : [" + file.getName() + "]");
-        }
 
-        BufferedReader reader = new BufferedReader(new FileReader(file));
+        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UtilIO.getUtf8()));
         String[] header = readEntityHeader(file, delimiter, reader);
 
         //Debug.logInfo("Opened data file [" + file.getName() + "] now running...", module);
@@ -422,7 +425,7 @@ public class EntityDataServices {
         String entityName = (String) context.get("entityName");
         String fieldName = (String) context.get("fieldName");
         Locale locale = (Locale) context.get("locale");
-        
+
         try (EntityListIterator eli = EntityQuery.use(delegator)
                 .from(entityName)
                 .queryIterator()) {