From 42599406dd5b0b15e679e30eeeeb73c123eaf48f Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 5 Feb 2013 08:51:42 +0000
Subject: [PATCH] SP-80, BIS-55: Installer upgrade check of database acces by
 owner and admin user.

SVN: 28291
---
 installation/resource/installer/install.xml   |  4 +
 .../resource/installer/userInputSpec.xml      |  6 ++
 .../izpack/DBConnectionValidator.java         | 82 +++++++++++++++++--
 3 files changed, 86 insertions(+), 6 deletions(-)

diff --git a/installation/resource/installer/install.xml b/installation/resource/installer/install.xml
index 2435d9661e7..d2799fef5b4 100644
--- a/installation/resource/installer/install.xml
+++ b/installation/resource/installer/install.xml
@@ -99,6 +99,10 @@
         <validator classname="ch.systemsx.cisd.openbis.installer.izpack.DBConnectionValidator"/>
     </panel>
     
+    <panel classname="com.izforge.izpack.panels.userinput.UserInputPanel" id="UserInputPanel.DB_CHECK" condition="isUpdateInstallation">
+        <validator classname="ch.systemsx.cisd.openbis.installer.izpack.DBConnectionValidator"/>
+    </panel>
+    
     <panel classname="com.izforge.izpack.panels.userinput.UserInputPanel" id="UserInputPanel.PSQL_PATH" condition="noPsqlToolsOnPath">
         <validator classname="ch.systemsx.cisd.openbis.installer.izpack.PostgresToolsPathValidator"/>
     </panel>
diff --git a/installation/resource/installer/userInputSpec.xml b/installation/resource/installer/userInputSpec.xml
index 93d0ee4d6d3..716e09525f1 100644
--- a/installation/resource/installer/userInputSpec.xml
+++ b/installation/resource/installer/userInputSpec.xml
@@ -1,4 +1,10 @@
 <userInput>
+  <panel id="UserInputPanel.DB_CHECK">
+    <field type="staticText" align="left"
+      txt="Click on 'Next' to perform the check for the owner and the admin user specified in service.properties." />
+    <field type="title" txt="Database access check" bold="true" size="2" />
+  </panel>
+
   <panel id="UserInputPanel.PSQL_PATH">
     <field type="staticText" align="left"
       txt="The openBIS installation process requires access to the PostreSQL command line tools 'psql' and 'pg_dump'. Please specify their location:" />
diff --git a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/DBConnectionValidator.java b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/DBConnectionValidator.java
index f934d534766..06cfd6b6bd4 100644
--- a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/DBConnectionValidator.java
+++ b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/DBConnectionValidator.java
@@ -71,14 +71,78 @@ public class DBConnectionValidator implements DataValidator
     @Override
     public Status validateData(AutomatedInstallData data)
     {
-        if (testConnectionOK(POSTGRES_USER, NO_PASSWORD))
+        String admin = getAdmin();
+        String adminPassword = getAdminPassword();
+        String owner = getOwner();
+        String ownerPassword = getOwnerPassword();
+        if (testConnectionOK(admin, adminPassword, "admin user") == false)
         {
-                return Status.OK;
+            return Status.ERROR;
         }
-        return Status.ERROR;
+        if (testConnectionOK(owner, ownerPassword, "owner") == false)
+        {
+            return Status.ERROR;
+        }
+        return Status.OK;
     }
 
-    private boolean testConnectionOK(String username, String password)
+    private String getOwner()
+    {
+        String user = System.getProperty("user.name").toLowerCase();
+        if (GlobalInstallationContext.isFirstTimeInstallation)
+        {
+            return user;
+        }
+        String owner =
+                Utils.tryToGetServicePropertyOfAS(GlobalInstallationContext.installDir,
+                        "database.owner");
+        if (owner != null && owner.length() > 0)
+        {
+            return owner;
+        }
+        return user;
+    }
+
+    private String getOwnerPassword()
+    {
+        if (GlobalInstallationContext.isFirstTimeInstallation)
+        {
+            return NO_PASSWORD;
+        }
+        String password =
+                Utils.tryToGetServicePropertyOfAS(GlobalInstallationContext.installDir,
+                        "database.owner-password");
+        return password == null ? "" : password;
+    }
+
+    private String getAdmin()
+    {
+        String defaultAdmin = POSTGRES_USER;
+        if (GlobalInstallationContext.isFirstTimeInstallation)
+        {
+            return defaultAdmin;
+        }
+        String admin =
+                Utils.tryToGetServicePropertyOfAS(GlobalInstallationContext.installDir,
+                        "database.admin-user");
+        if (admin != null && admin.length() > 0)
+        {
+            return admin;
+        }
+        return defaultAdmin;
+    }
+
+    private String getAdminPassword()
+    {
+        if (GlobalInstallationContext.isFirstTimeInstallation)
+        {
+            return NO_PASSWORD;
+        }
+        return Utils.tryToGetServicePropertyOfAS(GlobalInstallationContext.installDir,
+                "database.admin-password");
+    }
+
+    private boolean testConnectionOK(String username, String password, String messagePostfix)
     {
         boolean connected = false;
         try
@@ -93,12 +157,18 @@ public class DBConnectionValidator implements DataValidator
             }
         } catch (ClassNotFoundException cnfe)
         {
-            errorMessage = cnfe.getMessage();
+            errorMessage = createMessage(cnfe, messagePostfix);
         } catch (SQLException e)
         {
-            errorMessage = e.getMessage();
+            errorMessage = createMessage(e, messagePostfix);
         }
 
         return connected;
     }
+
+    private String createMessage(Exception exception, String messagePostfix)
+    {
+        return exception.getMessage() + ".\nThe error is probably caused by an illconfigured "
+                + messagePostfix + ".";
+    }
 }
-- 
GitLab