svn commit: r1811413 - in /ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start: AdminClient.java AdminServer.java Classpath.java Config.java Start.java StartupCommandUtil.java StartupControlPanel.java

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

svn commit: r1811413 - in /ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start: AdminClient.java AdminServer.java Classpath.java Config.java Start.java StartupCommandUtil.java StartupControlPanel.java

mbrohl
Author: mbrohl
Date: Sat Oct  7 12:07:16 2017
New Revision: 1811413

URL: http://svn.apache.org/viewvc?rev=1811413&view=rev
Log:
Improved: Fixing defects reported by FindBugs, package
org.apache.ofbiz.base.start.
(OFBIZ-9573)

I slighly modified the patch because of a conflicting change.

Thanks Dennis Balkir for reporting and providing the patch.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminClient.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java
    ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminClient.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminClient.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminClient.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminClient.java Sat Oct  7 12:07:16 2017
@@ -21,9 +21,11 @@ package org.apache.ofbiz.base.start;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.ConnectException;
 import java.net.Socket;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.ofbiz.base.start.AdminServer.OfbizSocketCommand;
 
@@ -74,9 +76,8 @@ class AdminClient {
     private static String sendSocketCommand(OfbizSocketCommand socketCommand, Config config) throws IOException {
         String response = "OFBiz is Down";
         try (Socket socket = new Socket(config.adminAddress, config.adminPort);
-                PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
-                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
-
+                PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8), true);
+                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
             // send the command
             writer.println(config.adminKey + ":" + socketCommand);
             writer.flush();

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/AdminServer.java Sat Oct  7 12:07:16 2017
@@ -21,13 +21,16 @@ package org.apache.ofbiz.base.start;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.ofbiz.base.start.Start.ServerState;
+import org.apache.ofbiz.base.util.UtilValidate;
 
 /**
  * The AdminServer provides a way to communicate with a running
@@ -81,8 +84,8 @@ final class AdminServer extends Thread {
 
     private void processClientRequest(Socket client, List<StartupLoader> loaders, AtomicReference<ServerState> serverState) throws IOException {
 
-        try (BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
-                PrintWriter writer = new PrintWriter(client.getOutputStream(), true)) {
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8));
+                PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.getOutputStream(), StandardCharsets.UTF_8), true)) {
 
             // read client request and prepare response
             String clientRequest = reader.readLine();
@@ -101,17 +104,22 @@ final class AdminServer extends Thread {
     }
 
     private OfbizSocketCommand determineClientCommand(String request) {
-        OfbizSocketCommand clientCommand;
-        if(request == null
-                || request.isEmpty()
-                || !request.contains(":")
-                || !request.substring(0, request.indexOf(':')).equals(config.adminKey)
-                || request.substring(request.indexOf(':') + 1) == null) {
-            clientCommand = OfbizSocketCommand.FAIL;
-        } else {
-            clientCommand = OfbizSocketCommand.valueOf(request.substring(request.indexOf(':') + 1));
+        if(!isValidRequest(request)) {
+            return OfbizSocketCommand.FAIL;
         }
-        return clientCommand;
+        return OfbizSocketCommand.valueOf(request.substring(request.indexOf(':') + 1));
+    }
+
+    /**
+     * Validates if request is a suitable String
+     * @param request
+     * @return boolean which shows if request is suitable
+     */
+    private boolean isValidRequest(String request) {
+        return UtilValidate.isNotEmpty(request)
+                && request.contains(":")
+                && request.substring(0, request.indexOf(':')).equals(config.adminKey)
+                && !request.substring(request.indexOf(':') + 1).isEmpty();
     }
 
     private String prepareResponseToClient(OfbizSocketCommand control, AtomicReference<ServerState> serverState) {

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Classpath.java Sat Oct  7 12:07:16 2017
@@ -27,6 +27,9 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
+
+import org.apache.poi.util.LocaleUtil;
 
 /**
  * A class path accumulator.
@@ -35,7 +38,7 @@ import java.util.List;
  */
 public final class Classpath {
 
-    private List<File> elements = new ArrayList<File>();
+    private List<File> elements = new ArrayList<>();
 
     /**
      * Default constructor.
@@ -99,10 +102,16 @@ public final class Classpath {
         if (path == null) {
             throw new IllegalArgumentException("path cannot be null");
         }
+        File[] listedFiles;
         if (path.isDirectory() && path.exists()) {
             // load all .jar, .zip files and native libs in this directory
-            for (File file : path.listFiles()) {
-                String fileName = file.getName().toLowerCase();
+            listedFiles = path.listFiles();
+        } else {
+            listedFiles = null;
+        }
+        if (listedFiles != null) {
+            for (File file : listedFiles) {
+                String fileName = file.getName().toLowerCase(Locale.getDefault());
                 if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
                     File key = file.getCanonicalFile();
                     synchronized (elements) {

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java Sat Oct  7 12:07:16 2017
@@ -161,6 +161,8 @@ public final class Config {
         case 3:
             locale = new Locale(locales[0], locales[1], locales[2]);
             break;
+        default:
+            throw new IllegalArgumentException("The combination of properties, ofbiz.locale.default and defaultLocale is invalid. " + Arrays.toString(locales));
         }
         System.setProperty("user.language", localeString);
         return locale;

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Start.java Sat Oct  7 12:07:16 2017
@@ -19,6 +19,7 @@
 package org.apache.ofbiz.base.start;
 
 import java.util.List;
+import java.util.Locale;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -118,7 +119,7 @@ public final class Start {
 
         @Override
         public String toString() {
-            return name().charAt(0) + name().substring(1).toLowerCase();
+            return name().charAt(0) + name().substring(1).toLowerCase(Locale.getDefault());
         }
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupCommandUtil.java Sat Oct  7 12:07:16 2017
@@ -18,8 +18,10 @@
  *******************************************************************************/
 package org.apache.ofbiz.base.start;
 
+import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
@@ -175,7 +177,7 @@ public final class StartupCommandUtil {
     static final void printOfbizStartupHelp(final PrintStream printStream) {
         HelpFormatter formatter = new HelpFormatter();
         formatter.printHelp(
-                new PrintWriter(printStream, true),
+                new PrintWriter(new OutputStreamWriter(printStream, StandardCharsets.UTF_8), true),
                 HelpFormatter.DEFAULT_WIDTH + 6,
                 "ofbiz|ofbizDebug|ofbizBackground",
                 System.lineSeparator() + "Executes OFBiz command e.g. start, shutdown, check status, etc",

Modified: ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java?rev=1811413&r1=1811412&r2=1811413&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/StartupControlPanel.java Sat Oct  7 12:07:16 2017
@@ -158,6 +158,14 @@ final class StartupControlPanel {
                 stream.close();
             } catch (IOException e) {
                 throw new StartupException("Couldn't load global system props", e);
+            } finally {
+                try {
+                    if (stream != null) {
+                    stream.close();
+                    }
+                } catch (IOException e) {
+                    System.out.println("There was a problem while closing the Stream " + e);
+                }
             }
         }
     }