details:   https://code.openbravo.com/erp/devel/pi/rev/9ef9e20d439e
changeset: 34199:9ef9e20d439e
user:      Javier Armendáriz <javier.armendariz <at> openbravo.com>
date:      Mon Jun 11 14:26:36 2018 +0200
summary:   Fixed issue 38099: Upgrade from <3.0PR15.4 preserves old pool 
configuration.

3.0PR15.4 includes the Apache JDBC connection pool module as part of Openbravo
distribution and is set as the default DB pool. This changeset enforces 
upgrading
to the new pool instead of preserving the old Apache commons pool.
This implementation has been tested in the following scenarios:
- From an 3.0PR15Q3 instance with the parameter db.externalPoolClassName not set
or empty: After the upgrade the parameter should refer to the bundled Apache 
JDBC
pool module.
- From an 3.0PR15Q3 instance with the parameter db.externalPoolClassName set:
Property db.externalPoolClassName should be left as-is.
- Upgrading from a newer version (e.g. 3.0PR16Q1): This validation should not be
executed.

diffstat:

 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.class
     |  Bin 
 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMergeData.class
 |  Bin 
 
src-util/buildvalidation/src/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.java
                |  245 +++------
 
src-util/buildvalidation/src/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge_data.xsql
           |   32 -
 4 files changed, 75 insertions(+), 202 deletions(-)

diffs (truncated from 377 to 300 lines):

diff -r 847c3a02e269 -r 9ef9e20d439e 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.class
Binary file 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.class
 has changed
diff -r 847c3a02e269 -r 9ef9e20d439e 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMergeData.class
Binary file 
src-util/buildvalidation/build/classes/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMergeData.class
 has changed
diff -r 847c3a02e269 -r 9ef9e20d439e 
src-util/buildvalidation/src/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.java
--- 
a/src-util/buildvalidation/src/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.java
    Thu Jun 14 13:16:33 2018 +0000
+++ 
b/src-util/buildvalidation/src/org/openbravo/buildvalidation/CheckUpdateConnectionPoolMerge.java
    Mon Jun 11 14:26:36 2018 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2015-2017 Openbravo SLU
+ * All portions are Copyright (C) 2015-2018 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -20,190 +20,96 @@
 package org.openbravo.buildvalidation;
 
 import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.Properties;
 
 import org.apache.log4j.Logger;
 import org.openbravo.base.ExecutionLimits;
 import org.openbravo.buildvalidation.BuildValidation;
-import org.openbravo.database.ConnectionProvider;
 import org.openbravo.modulescript.OpenbravoVersion;
 
 /**
- * This build validation prevents a bad behaviour updating to PR15Q4 by taking 
into account the
- * following scenarios: - Update from < 3.0PR15Q4 using defaults connection 
pools. - Update from <
- * 3.0PR15Q4 using Apache JDBC Connection Pool module or another external 
connection pool.
- * 
- * It must be ensured that if an enviroment did not use "Apache JDBC 
Connection Pool" module, it
- * must continue without using the connection pool. On the other hand, 
environments that used the
- * module continue to use the Apache JDBC Connection pool. It will try to 
retrieve the configuration
- * of the module. The new instances will start using the new connection pool.
- * 
- * @author inigo.sanchez
+ * This build validation will be executed when updating from a version lower 
or equal to PR15Q3.
+ *
+ * This validation sets the value of the property db.externalPoolClassName in 
Openbravo.property if
+ * it does not exist or its value is set to empty. The default value used will 
be the one defined
+ * in Openbravo.properties.template
  * 
  */
 public class CheckUpdateConnectionPoolMerge extends BuildValidation {
 
   private final static String PROPERTY_CONNECTION_POOL = 
"db.externalPoolClassName";
-  private final static String PATH_CONNECTIONPOOL_PROPERTIES = 
"/WebContent/WEB-INF/connectionPool.properties";
   private final static String PATH_OPENBRAVO_PROPERTIES = 
"/config/Openbravo.properties";
-  private final static String TARGET_VERSION = "1.0.27056";
-  private final static String PREFIX_POOL_PROPERTIES = "db.pool.";
+  private final static String PATH_OPENBRAVO_PROPERTIES_TEMPLATE = 
"/config/Openbravo.properties.template";
   private final static String SUFFIX_AUX = "_aux";
 
   private static Logger log = 
Logger.getLogger(CheckUpdateConnectionPoolMerge.class);
-  private Properties obProperties = null;
 
   @Override
   public List<String> execute() {
-    ConnectionProvider cp = getConnectionProvider();
     try {
-      String versionOfModule = 
CheckUpdateConnectionPoolMergeData.versionOfConnectionPoolModule(cp);
-
-      if (versionOfModule == null || isNecessaryMerge(versionOfModule, 
TARGET_VERSION)) {
-        String obDir = getSourcePathFromOBProperties();
-        String openbravoPropertiesPath = obDir + PATH_OPENBRAVO_PROPERTIES;
-
-        // It must be ensured that if an enviroment did not use "Apache JDBC 
Connection Pool"
-        // module, it must continue without using the connection pool. In that 
case remove value of
-        // the property.
-        if (versionOfModule == null) {
-          File fileW = new File(openbravoPropertiesPath);
-          // removes value of property that merge in Openbravo.properties
-          replaceProperty(fileW, openbravoPropertiesPath + SUFFIX_AUX, 
PROPERTY_CONNECTION_POOL,
-              "=");
-          try {
-            fileW.delete();
-            File fileAux = new File(openbravoPropertiesPath + SUFFIX_AUX);
-            fileAux.renameTo(new File(openbravoPropertiesPath));
-          } catch (Exception ex) {
-            log.error("Error renaming/deleting Openbravo.properties", ex);
-          }
-          log.info("Removed value of " + PROPERTY_CONNECTION_POOL + " 
property.");
-
-        } else {
-          // Environments that previously used the connection pool module. It 
will try to retrieve
-          // the configuration of the module. It is necessary to merge 
connectionPool.properties
-          String connectionPoolPropertiesPath = obDir + 
PATH_CONNECTIONPOOL_PROPERTIES;
-          mergeOpenbravoPropertiesConnectionPool(openbravoPropertiesPath,
-              connectionPoolPropertiesPath);
-        }
-      }
+      setDefaultConnectionPoolInOpenbravoPropertiesIfNotSetOrEmpty();
     } catch (Exception e) {
-      handleError(e);
+      return handleError(e);
     }
-    return new ArrayList<String>();
+    return new ArrayList<>();
   }
 
-  /**
-   * Checks version of the module.
-   * 
-   * @return true if it is necessary to merge.
-   */
-  private boolean isNecessaryMerge(String version, String targetVersion) {
-    String[] targetNumberVersion = targetVersion.split(".");
-    String[] numberVersion = version.split(".");
+  private void setDefaultConnectionPoolInOpenbravoPropertiesIfNotSetOrEmpty() 
throws Exception {
+    String obDir = getSourcePathFromOBProperties();
+    String openbravoPropertiesPath = obDir + PATH_OPENBRAVO_PROPERTIES;
 
-    // if version is equal or lower than TARGET_VERSION, it must be merged
-    for (int i = 0; i < numberVersion.length; i++) {
-      if (Integer.parseInt(numberVersion[i]) > 
Integer.parseInt(targetNumberVersion[i])) {
-        return false;
-      } else if (Integer.parseInt(numberVersion[i]) < 
Integer.parseInt(targetNumberVersion[i])) {
-        return true;
-      }
+    Properties openbravoProperties = 
openPropertiesFile(openbravoPropertiesPath);
+    String connectionPoolValue = 
openbravoProperties.getProperty(PROPERTY_CONNECTION_POOL);
+
+    if (connectionPoolValue == null || connectionPoolValue.isEmpty()) {
+      String externalPoolClassName = getExternalPoolClassNameFromTemplate();
+      replacePropertyValue(openbravoPropertiesPath, PROPERTY_CONNECTION_POOL, 
externalPoolClassName);
+      log.info("External DB Pool class name property not found. Set to " + 
externalPoolClassName);
     }
-    // here it is same version than target version or at least smaller
-    return true;
+    else {
+      log.info("External DB Pool class name already defined: " + 
connectionPoolValue);
+    }
   }
 
-  /**
-   * When updating core and it is include Apache JDBC Connection Pool into 
distribution in some
-   * cases is necessary to update Openbravo.properties taking into account
-   * connectionPool.properties.
-   * 
-   * This connectionPool.properties file exists in instances with Apache JDBC 
Connection Pool
-   * module.
-   * 
-   * @return false in case no changes were needed, true in case the merge 
includes some changes
-   */
-  private static boolean mergeOpenbravoPropertiesConnectionPool(String 
OpenbravoPropertiesPath,
-      String connectionPoolPath) {
-    Properties openbravoProperties = new Properties();
-    Properties connectionPoolProperties = new Properties();
-    try {
-      // load both files
-      openbravoProperties.load(new FileInputStream(OpenbravoPropertiesPath));
-      connectionPoolProperties.load(new FileInputStream(connectionPoolPath));
-
-      Enumeration<?> propertiesConnectionPool = 
connectionPoolProperties.propertyNames();
-      while (propertiesConnectionPool.hasMoreElements()) {
-        String propName = (String) propertiesConnectionPool.nextElement();
-        String origValue = 
openbravoProperties.getProperty(PREFIX_POOL_PROPERTIES + propName);
-        String connectionPoolValue = 
connectionPoolProperties.getProperty(propName);
-
-        // try to get original value for new property, if it does not exist 
add it to original
-        // properties with its default value
-        if (origValue == null) {
-          addNewProperty(OpenbravoPropertiesPath, PREFIX_POOL_PROPERTIES + 
propName,
-              connectionPoolValue);
-          openbravoProperties.setProperty(PREFIX_POOL_PROPERTIES + propName, 
connectionPoolValue);
-        } else {
-          // replace value in Openbravo.properties by value in 
connectionPool.properties
-          try {
-            File fileW = new File(OpenbravoPropertiesPath);
-
-            if (!(origValue.equals(connectionPoolValue))) {
-              replaceProperty(fileW, OpenbravoPropertiesPath + SUFFIX_AUX, 
PREFIX_POOL_PROPERTIES
-                  + propName, "=" + connectionPoolValue);
-              try {
-                fileW.delete();
-                File fileAux = new File(OpenbravoPropertiesPath + SUFFIX_AUX);
-                fileAux.renameTo(new File(OpenbravoPropertiesPath));
-              } catch (Exception ex) {
-                log.error("Error renaming/deleting Openbravo.properties", ex);
-              }
-            }
-          } catch (Exception e) {
-            log.error("Error read/write Openbravo.properties", e);
-          }
-        }
-      }
-    } catch (Exception notFoundConnectionPoolProperties) {
-      return false;
-    }
-    log.info("Merged connection pool properties with Openbravo.properties 
file.");
-    return true;
+  private Properties openPropertiesFile(String path) throws IOException {
+    Properties propertiesFile = new Properties();
+    propertiesFile.load(new FileInputStream(path));
+    return propertiesFile;
   }
 
-  /**
-   * Adds a new property in a merge of properties file.
-   * 
-   * Extract from original method in 
org.openbravo.erpCommon.utility.Utility.java. It is necessary
-   * because build validations can not work with external methods.
-   * 
-   * @param pathFile
-   *          properties file path
-   * @param propertyName
-   *          new property to add
-   * @param value
-   *          new value to add
-   */
-  private static void addNewProperty(String pathFile, String propertyName, 
String value) {
-    File fileW = new File(pathFile);
+  private String getExternalPoolClassNameFromTemplate() throws IOException {
+    String obDir = getSourcePathFromOBProperties();
+    String openbravoPropertiesTemplatePath = obDir + 
PATH_OPENBRAVO_PROPERTIES_TEMPLATE;
+
+    Properties openbravoPropertiesTemplate = 
openPropertiesFile(openbravoPropertiesTemplatePath);
+    return openbravoPropertiesTemplate.getProperty(PROPERTY_CONNECTION_POOL);
+  }
+
+  private void replacePropertyValue(String openbravoPropertiesPath, String 
propertyName,
+      String newValue) throws Exception {
     try {
-      BufferedWriter bw = new BufferedWriter(new FileWriter(fileW, true));
-      bw.write("\n" + propertyName + "=" + value + "\n");
-      bw.close();
-    } catch (Exception e1) {
-      log.error("Exception reading/writing file: ", e1);
+      File fileW = new File(openbravoPropertiesPath);
+
+      replaceProperty(fileW, openbravoPropertiesPath + SUFFIX_AUX, 
propertyName, "=" + newValue);
+      try {
+        fileW.delete();
+        File fileAux = new File(openbravoPropertiesPath + SUFFIX_AUX);
+        fileAux.renameTo(new File(openbravoPropertiesPath));
+      } catch (Exception ex) {
+        log.error("Error renaming/deleting Openbravo.properties", ex);
+        throw ex;
+      }
+    } catch (IOException e) {
+      log.error("Error read/write Openbravo.properties", e);
+      throw e;
     }
   }
 
@@ -223,30 +129,30 @@
    * @param changeOption
    *          Value to write in addressFilePath
    */
-  private static void replaceProperty(File fileR, String addressFilePath, 
String searchOption,
+  private void replaceProperty(File fileR, String addressFilePath, String 
searchOption,
       String changeOption) throws Exception {
     boolean isFound = false;
-    FileReader fr = new FileReader(fileR);
-    BufferedReader br = new BufferedReader(fr);
     // auxiliary file to rewrite
     File fileW = new File(addressFilePath);
-    FileWriter fw = new FileWriter(fileW);
-    // data for restore
-    String line;
-    while ((line = br.readLine()) != null) {
-      if (line.indexOf(searchOption) == 0) {
-        // Replace new option
-        line = line.replace(line, searchOption + changeOption);
-        isFound = true;
+    try (
+      FileReader fr = new FileReader(fileR);
+      BufferedReader br = new BufferedReader(fr);
+      FileWriter fw = new FileWriter(fileW)
+      ) {
+      // data for restore
+      String line;
+      while ((line = br.readLine()) != null) {
+        if (line.indexOf(searchOption) == 0) {
+          // Replace new option
+          line = line.replace(line, searchOption + changeOption);
+          isFound = true;
+        }
+        fw.write(line + "\n");
       }
-      fw.write(line + "\n");
+      if (!isFound) {
+        fw.write(searchOption + changeOption);
+      }
     }
-    if (!isFound) {
-      fw.write(searchOption + changeOption);

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to