[izpack-changes] r1768 - in izpack-src/trunk/src/lib/com: coi/tools/os/izpack izforge/izpack/panels

noreply at berlios.de noreply at berlios.de
Wed Mar 7 12:39:59 CET 2007


Author: bartzkau
Date: 2007-03-07 12:39:54 +0100 (Wed, 07 Mar 2007)
New Revision: 1768

Modified:
   izpack-src/trunk/src/lib/com/coi/tools/os/izpack/COIOSHelper.java
   izpack-src/trunk/src/lib/com/izforge/izpack/panels/JDKPathPanel.java
Log:
Version determination via Registry if possible.


Modified: izpack-src/trunk/src/lib/com/coi/tools/os/izpack/COIOSHelper.java
===================================================================
--- izpack-src/trunk/src/lib/com/coi/tools/os/izpack/COIOSHelper.java	2007-03-06 17:41:09 UTC (rev 1767)
+++ izpack-src/trunk/src/lib/com/coi/tools/os/izpack/COIOSHelper.java	2007-03-07 11:39:54 UTC (rev 1768)
@@ -1,4 +1,5 @@
 /*
+ * $Id:$
  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
  * 
  * http://www.izforge.com/izpack/
@@ -40,6 +41,8 @@
     private static int used = 0;
 
     private static boolean destroyed = false;
+    
+    private boolean failed = false;
 
     /**
      * This method is used to free the library at the end of progam execution. After this call, any
@@ -110,15 +113,23 @@
     public void addDependant(NativeLibraryClient dependant) throws Exception
     {
         used++;
+        if( failed )
+            throw (new Exception("load native library failed"));
         try
         {
             Librarian.getInstance().loadLibrary("COIOSHelper", dependant);
         }
         catch (UnsatisfiedLinkError exception)
         {
+            failed = true;
             throw (new Exception("could not locate native library"));
         }
-
+        catch( Throwable t)
+        {
+            failed = true;
+            throw (new Exception(t));
+        }
+        
     }
 
 }

Modified: izpack-src/trunk/src/lib/com/izforge/izpack/panels/JDKPathPanel.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/panels/JDKPathPanel.java	2007-03-06 17:41:09 UTC (rev 1767)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/panels/JDKPathPanel.java	2007-03-07 11:39:54 UTC (rev 1768)
@@ -1,4 +1,5 @@
 /*
+ * $Id:$
  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
  * 
  * http://www.izforge.com/izpack/
@@ -22,13 +23,21 @@
 package com.izforge.izpack.panels;
 
 import java.io.File;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.StringTokenizer;
 
+import com.coi.tools.os.win.MSWinConstants;
+import com.coi.tools.os.win.NativeLibException;
+import com.coi.tools.os.win.RegDataContainer;
 import com.izforge.izpack.installer.InstallData;
 import com.izforge.izpack.installer.InstallerFrame;
 import com.izforge.izpack.util.AbstractUIHandler;
 import com.izforge.izpack.util.FileExecutor;
 import com.izforge.izpack.util.OsVersion;
+import com.izforge.izpack.util.os.RegistryDefaultHandler;
+import com.izforge.izpack.util.os.RegistryHandler;
 
 /**
  * Panel which asks for the JDK path.
@@ -42,7 +51,16 @@
     private static final long serialVersionUID = 3257006553327810104L;
 
     private static final String[] testFiles = new String[] { "lib" + File.separator + "tools.jar"};
+    
+    private static final String JDK_ROOT_KEY = "Software\\JavaSoft\\Java Development Kit";
+    
+    private static final String JDK_VALUE_NAME = "JavaHome";
 
+    private static final int OK = 0;
+    private static final int BAD_VERSION = 1;
+    private static final int BAD_REAL_PATH = 2;
+    private static final int BAD_REG_PATH = 3;
+
     private String detectedVersion;
 
     private String minVersion = null;
@@ -50,7 +68,13 @@
     private String maxVersion = null;
 
     private String variableName;
+    
+    private Set badRegEntries = null; 
+    
 
+    
+    
+
     /**
      * The constructor.
      * 
@@ -79,30 +103,43 @@
             return(true);
         if (super.isValidated())
         {
-            if (verifyVersion())
+            switch( verifyVersionEx())
             {
+            case OK:
                 idata.setVariable(getVariableName(), pathSelectionPanel.getPath());
                 return (true);
-            }
-            // Bad version detected.
-            String min = getMinVersion();
-            String max = getMaxVersion();
-            StringBuffer message = new StringBuffer();
-            message.append(parent.langpack.getString("JDKPathPanel.badVersion1")).append(
-                    getDetectedVersion()).append(
-                    parent.langpack.getString("JDKPathPanel.badVersion2"));
-            if (min != null && max != null)
-                message.append(min).append(" - ").append(max);
-            else if (min != null)
-                message.append(" >= ").append(min);
-            else if (max != null) message.append(" <= ").append(max);
+            case BAD_REG_PATH:
+                if (askQuestion(parent.langpack.getString("installer.warning"), parent.langpack.getString("JDKPathPanel.nonValidPathInReg"),
+                        AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_NO) == AbstractUIHandler.ANSWER_YES)
+                {
+                    idata.setVariable(getVariableName(), pathSelectionPanel.getPath());
+                    return (true);
+                }
+                return(false);
+            case BAD_REAL_PATH:
+                return(false);
+            case BAD_VERSION:
+                String min = getMinVersion();
+                String max = getMaxVersion();
+                StringBuffer message = new StringBuffer();
+                message.append(parent.langpack.getString("JDKPathPanel.badVersion1")).append(
+                        getDetectedVersion()).append(
+                        parent.langpack.getString("JDKPathPanel.badVersion2"));
+                if (min != null && max != null)
+                    message.append(min).append(" - ").append(max);
+                else if (min != null)
+                    message.append(" >= ").append(min);
+                else if (max != null) message.append(" <= ").append(max);
 
-            message.append(parent.langpack.getString("JDKPathPanel.badVersion3"));
-            if (askQuestion(parent.langpack.getString("installer.warning"), message.toString(),
-                    AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_NO) == AbstractUIHandler.ANSWER_YES)
-            {
-                idata.setVariable(getVariableName(), pathSelectionPanel.getPath());
-                return (true);
+                message.append(parent.langpack.getString("JDKPathPanel.badVersion3"));
+                if (askQuestion(parent.langpack.getString("installer.warning"), message.toString(),
+                        AbstractUIHandler.CHOICES_YES_NO, AbstractUIHandler.ANSWER_NO) == AbstractUIHandler.ANSWER_YES)
+                {
+                    idata.setVariable(getVariableName(), pathSelectionPanel.getPath());
+                    return (true);
+                }
+                default: throw new RuntimeException("Internal error: unknown result of version verification.");
+                
             }
         }
         return (false);
@@ -125,7 +162,12 @@
         // Set the path for method pathIsValid ...
         pathSelectionPanel.setPath(chosenPath);
 
-        if (!pathIsValid() || !verifyVersion()) chosenPath = "";
+        if (!pathIsValid() || !verifyVersion())
+        {
+            chosenPath = resolveInRegistry();
+            if (!pathIsValid() || !verifyVersion())
+                chosenPath = "";
+        }
         // Set the default to the path selection panel.
         pathSelectionPanel.setPath(chosenPath);
         String var = idata.getVariable("JDKPathPanel.skipIfValid");
@@ -138,14 +180,92 @@
 
     }
 
-    private boolean verifyVersion()
+    /**
+     * Returns the path to the needed JDK if found in the registry. If there are more than one JDKs
+     * registered, that one with the highest allowd version will be returned. Works only on windows.
+     * On Unix an empty string returns.
+     * 
+     * @return the path to the needed JDK if found in the windows registry
+     */
+    private String resolveInRegistry()
     {
+        String retval = "";
+        int oldVal = 0;
+        RegistryHandler rh = null;
+        badRegEntries = new HashSet();
+        try
+        {
+            // Get the default registry handler.
+            rh = RegistryDefaultHandler.getInstance();
+            if (rh == null)
+            // We are on a os which has no registry or the
+                // needed dll was not bound to this installation. In
+                // both cases we forget the try to get the JDK path from registry.
+                return (retval);
+            rh.verify(idata);
+            oldVal = rh.getRoot(); // Only for security...
+            rh.setRoot(MSWinConstants.HKEY_LOCAL_MACHINE);
+            String[] keys = rh.getSubkeys(JDK_ROOT_KEY);
+            if (keys == null || keys.length == 0) return (retval);
+            Arrays.sort(keys);
+            int i = keys.length - 1;
+            String min = getMinVersion();
+            String max = getMaxVersion();
+            // We search for the highest allowd version, therefore retrograde
+            while (i > 0)
+            {
+                if (compareVersions(keys[i], max, false, 4, 4, "__NO_NOT_IDENTIFIER_"))
+                { // First allowd version found, now we have to test that the min value
+                    // also allows this version.
+                    if (compareVersions(keys[i], min, true, 4, 4, "__NO_NOT_IDENTIFIER_"))
+                    {
+                        String cv = JDK_ROOT_KEY + "\\" + keys[i];
+                        String path  = rh.getValue(cv, JDK_VALUE_NAME).getStringData();
+                        // Use it only if the path is valid.
+                        // Set the path for method pathIsValid ...
+                        pathSelectionPanel.setPath(path);
+                        if (!pathIsValid())
+                        {
+                            badRegEntries.add(keys[i]);
+                        }
+                        else if( "".equals(retval))
+                        {
+                            retval = path;
+                        }
+                        pathSelectionPanel.setPath(retval);
+                    }
+                }
+                i--;
+            }
+        }
+        catch (Exception e)
+        { // Will only be happen if registry handler is good, but an
+            // exception at performing was thrown. This is an error...
+            e.printStackTrace();
+        }
+        finally
+        {
+            if(rh != null && oldVal != 0) try
+            {
+                rh.setRoot(MSWinConstants.HKEY_LOCAL_MACHINE);
+            }
+            catch (NativeLibException e)
+            {
+                e.printStackTrace();
+            }
+        }
+        return (retval);
+    }
+
+    private int verifyVersionEx()
+    {
         String min = getMinVersion();
         String max = getMaxVersion();
+        int retval = OK;
         // No min and max, version always ok.
-        if (min == null && max == null) return (true);
+        if (min == null && max == null) return (OK);
 
-        if (!pathIsValid()) return (false);
+        if (!pathIsValid()) return (BAD_REAL_PATH);
         // No get the version ...
         // We cannot look to the version of this vm because we should
         // test the given JDK VM.
@@ -159,12 +279,22 @@
         String vs = (output[0].length() > 0) ? output[0] : output[1];
         if (min != null)
         {
-            if (!compareVersions(vs, min, true, 4, 4, "__NO_NOT_IDENTIFIER_")) return (false);
+            if (!compareVersions(vs, min, true, 4, 4, "__NO_NOT_IDENTIFIER_")) retval = BAD_VERSION;
         }
         if (max != null)
-            if (!compareVersions(vs, max, false, 4, 4, "__NO_NOT_IDENTIFIER_")) return (false);
-        return (true);
+            if (!compareVersions(vs, max, false, 4, 4, "__NO_NOT_IDENTIFIER_")) retval = BAD_VERSION;
+        if( retval == OK && badRegEntries.size() > 0 )
+        {   // Test for bad registry entry.
+            if( badRegEntries.contains(getDetectedVersion()))
+                retval = BAD_REG_PATH;
+        }
+        return (retval);
+        
     }
+    private boolean verifyVersion()
+    {
+        return( verifyVersionEx() > 0 ? false : true );
+    }
 
     private boolean compareVersions(String in, String template, boolean isMin,
             int assumedPlace, int halfRange, String useNotIdentifier)
@@ -229,14 +359,16 @@
                 // the minimal needed version will be 1.5.0.2.
                 return (false);
             }
-            if (curVal < neededVal) if (isMin)
-                return (false);
-            else
+            if (curVal < neededVal)
+            {
+                if (isMin) return (false);
                 return (true);
-            if (Integer.parseInt(cur) > Integer.parseInt(nee)) if (isMin)
-                return (true);
-            else
+            }
+            if (Integer.parseInt(cur) > Integer.parseInt(nee))
+            {
+                if (isMin) return (true);
                 return (false);
+            }
         }
         return (true);
     }
@@ -288,7 +420,10 @@
      */
     protected void setMaxVersion(String string)
     {
-        maxVersion = string;
+        if( string != null && string.length() > 0)
+            maxVersion = string;
+        else
+            maxVersion = "99.0.0";
     }
 
     /**
@@ -298,7 +433,10 @@
      */
     protected void setMinVersion(String string)
     {
-        minVersion = string;
+        if( string != null && string.length() > 0)
+            minVersion = string;
+        else
+            minVersion = "1.0.0";
     }
 
     /**




More information about the izpack-changes mailing list