From 6429cd9772ee4655fd1257d0312e49e9047de0c7 Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Fri, 07 Jun 2013 07:31:12 +0000
Subject: [PATCH] CR-1804 OPENDJ-931 OpenDJ QuickSetup should display the license The jnlp / webstart is now able to display the license if it is present and to validate it.

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/LicenseFile.java                          |  225 +++++++++++++++++++++++++++++---------------
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/ui/InstallLicensePanel.java     |    2 
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java |    4 
 opendj-sdk/opends/resource/webstart/create-webstart-standalone.sh                                |   15 +++
 4 files changed, 168 insertions(+), 78 deletions(-)

diff --git a/opendj-sdk/opends/resource/webstart/create-webstart-standalone.sh b/opendj-sdk/opends/resource/webstart/create-webstart-standalone.sh
index f9f5baf..c55070d 100755
--- a/opendj-sdk/opends/resource/webstart/create-webstart-standalone.sh
+++ b/opendj-sdk/opends/resource/webstart/create-webstart-standalone.sh
@@ -175,6 +175,20 @@
                  "${INSTALL_DIR}/lib/${LIBFILE}" "${CERT_ALIAS}"
 done
 
+# Create and sign the licence.jar file if exists.
+if [ -d ${BUILD_DIR}/package/${ZIP_FILENAME_BASE}/Legal ]
+then
+echo "Creating license.jar ..."
+cp "${BUILD_DIR}/package/${ZIP_FILENAME_BASE}/Legal" "${INSTALL_DIR}/lib"
+cd "${BUILD_DIR}/package"
+"${JAR}" -cf "${INSTALL_DIR}/lib/license.jar" -C "${BUILD_DIR}/package/${ZIP_FILENAME_BASE}/" "Legal"
+cd "${INSTALL_DIR}/lib"
+echo "Signing license.jar ..."
+"${JARSIGNER}" -keystore "${CERT_KEYSTORE}" -keypass "${CERT_KEYSTORE_PIN}" \
+               -storepass "${CERT_KEYSTORE_PIN}" license.jar "${CERT_ALIAS}"
+# Create the resource line to add to the jnlp script.
+LICENSEJAR="<jar href=\"lib/license.jar\" download=\"eager\"/>"               
+fi
 
 # Create and sign the zipped.jar file.
 echo "Creating zipped.jar ..."
@@ -211,6 +225,7 @@
   <resources>
     <j2se version="1.6+" java-vm-args="-client"/>
     <jar href="lib/quicksetup.jar" download="eager" main="true"/>
+    ${LICENSEJAR}
     <jar href="lib/${PRODUCT_NAME}.jar" download="lazy"/>
     <jar href="lib/je.jar" download="lazy"/>
     <jar href="lib/zipped.jar" download="lazy"/>
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/LicenseFile.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/LicenseFile.java
index 6243829..9efe009 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/LicenseFile.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/LicenseFile.java
@@ -28,42 +28,60 @@
 
 package org.opends.quicksetup;
 
+import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
 
 import org.opends.quicksetup.util.Utils;
+import org.opends.server.util.ServerConstants;
+import org.opends.server.util.StaticUtils;
 
 /**
  * Represents information about the license file.
  */
-public class LicenseFile {
+public class LicenseFile
+{
+
+  /**
+   * The license file name in Legal directory.
+   */
+  private final static String LICENSE_FILE_NAME = "license_to_accept.txt";
+
+  /**
+   * The Legal folder which contains license file.
+   */
+  private final static String LEGAL_FOLDER_NAME = "Legal";
+
+  /**
+   * The accepted license file name.
+   */
+  private final static String ACCEPTED_LICENSE_FILE_NAME = "licenseAccepted";
 
   /**
    * Get the directory in which legal files are stored.
    */
   private static String getLegalDirectory()
   {
-    if (Utils.isWebStart())
+
+    String installRootFromSystem = System.getProperty("INSTALL_ROOT");
+
+    if (installRootFromSystem == null)
     {
-      return File.separatorChar + "Legal";
+      installRootFromSystem = System.getenv("INSTALL_ROOT");
     }
-    else
+
+    if (installRootFromSystem == null)
     {
-      String installRootFromSystem = System.getProperty("INSTALL_ROOT");
-
-      if (installRootFromSystem == null)
-      {
-        installRootFromSystem = System.getenv("INSTALL_ROOT");
-      }
-
-      if (installRootFromSystem == null)
-      {
-        installRootFromSystem = "";
-      }
-
-      return installRootFromSystem + File.separatorChar + "Legal";
+      installRootFromSystem = "";
     }
+
+    return installRootFromSystem + File.separatorChar + LEGAL_FOLDER_NAME;
+
   }
 
   /**
@@ -72,34 +90,27 @@
   private static String getInstanceLegalDirectory()
   {
     String instanceLegalDirName;
-    if (Utils.isWebStart())
+    String installDirName = System.getProperty("INSTALL_ROOT");
+
+    if (installDirName == null)
     {
-      instanceLegalDirName = File.separatorChar + "Legal";
+      installDirName = System.getenv("INSTALL_ROOT");
     }
-    else
+
+    if (installDirName == null)
     {
-      String installDirName = System.getProperty("INSTALL_ROOT");
-
-      if (installDirName == null)
-      {
-        installDirName = System.getenv("INSTALL_ROOT");
-      }
-
-      if (installDirName == null)
-      {
-        installDirName = ".";
-      }
-
-      String instanceDirname = Utils
-          .getInstancePathFromInstallPath(installDirName);
-      instanceLegalDirName = instanceDirname + File.separator + "Legal";
-      File instanceLegalDir = new File(instanceLegalDirName);
-      if (!instanceLegalDir.exists())
-      {
-        instanceLegalDir.mkdir();
-      }
+      installDirName = ".";
     }
-    return instanceLegalDirName ;
+
+    String instanceDirname =
+        Utils.getInstancePathFromInstallPath(installDirName);
+    instanceLegalDirName = instanceDirname + File.separator + LEGAL_FOLDER_NAME;
+    File instanceLegalDir = new File(instanceLegalDirName);
+    if (!instanceLegalDir.exists())
+    {
+      instanceLegalDir.mkdir();
+    }
+    return instanceLegalDirName;
   }
 
   /**
@@ -117,7 +128,7 @@
    */
   static private String getName()
   {
-    return getLegalDirectory() + File.separatorChar + "license_to_accept.txt";
+    return getLegalDirectory() + File.separatorChar + LICENSE_FILE_NAME;
   }
 
   /**
@@ -125,69 +136,127 @@
    */
   static private File getFile()
   {
-    if (licFile == null) {
-       licFile = new File(getName());
+    if (licFile == null)
+    {
+      licFile = new File(getName());
     }
 
     return licFile;
   }
 
+  /**
+   * Returns the URL to the license file when using jnlp / java web start.
+   */
+  static private URL getWebStartLicenseFile()
+  {
+    final String licenseResource = LEGAL_FOLDER_NAME + File.separatorChar
+        + LICENSE_FILE_NAME;
+    return Thread.currentThread().getContextClassLoader().getResource(
+        licenseResource);
+  }
 
   /**
    * Checks if the license file exists.
-   * @return <CODE>true</CODE> a license file license_to_accept.txt
-   * exists in the Legal directory in the top level installation directory
-   * <CODE>false</CODE> otherwise.
+   *
+   * @return <CODE>true</CODE> a license file {@value #LICENSE_FILE_NAME} exists
+   *         in the Legal directory in the top level installation directory
+   *         <CODE>false</CODE> otherwise.
    */
   static public boolean exists()
   {
-    // TODO: Try to support License with webstart
-    return !Utils.isWebStart() && getFile().exists();
+    if (Utils.isWebStart())
+    {
+      return (getWebStartLicenseFile() != null);
+    }
+    else
+    {
+      return getFile().exists();
+    }
   }
 
-
   /**
    * Get the textual contents of the license file.
+   *
    * @return the textual contents of the license file.
    */
   static public String getText()
   {
-    FileReader reader;
-
-    try {
-       reader = new FileReader(getFile());
-    } catch(Exception e) {
-      return "";
+    InputStream input = null;
+    // Gets the inputstream of the license
+    // From a file as the usual way,
+    // from an URL if we use web start / jnlp.
+    if (!Utils.isWebStart())
+    {
+      try
+      {
+        input = new FileInputStream(getFile());
+      }
+      catch (FileNotFoundException e)
+      {
+        // Should not happen
+        return "";
+      }
     }
-
-    int fileLen = (int) getFile().length();
-
-    char[] charArray = new char[fileLen];
-
-    try {
-      reader.read(charArray);
-    } catch(IOException ioe) {
-      System.out.println("Could not read license file");
+    else
+    {
+      URL licenseURL = getWebStartLicenseFile();
+      if (licenseURL != null)
+      {
+        try
+        {
+          input = licenseURL.openStream();
+        }
+        catch (Exception e)
+        {
+          // Should not happen
+          return "";
+        }
+      }
     }
+    // Reads the inputstream content.
+    final StringBuilder sb = new StringBuilder();
+    if (input != null)
+    {
+      try
+      {
+        final BufferedReader br =
+            new BufferedReader(new InputStreamReader(input));
+        String read = br.readLine();
 
-    return new String(charArray);
+        while (read != null)
+        {
+          sb.append(read);
+          sb.append(ServerConstants.EOL);
+          read = br.readLine();
+        }
+      }
+      catch (IOException ioe)
+      {
+        // Should not happen
+        return "";
+      }
+    }
+    StaticUtils.close(input);
+
+    return sb.toString();
   }
 
-
   /**
    * Get the license approval status.
+   *
    * @return <CODE>true</CODE> if the license has been accepted by the user
-   * <CODE>false</CODE> otherwise.
+   *         <CODE>false</CODE> otherwise.
    */
   static public boolean getApproval()
   {
     return approved;
   }
 
-
   /**
    * Sets the license approval status.
-   * @param p_approved the license approval status
+   *
+   * @param p_approved
+   *          the license approval status
    */
   static public void setApproval(boolean p_approved)
   {
@@ -199,12 +268,12 @@
    */
   static public void createFileLicenseApproved()
   {
-    if ( getApproval() )
+    if (getApproval())
     {
       try
       {
         new File(getInstanceLegalDirectory() + File.separatorChar
-            + "licenseAccepted").createNewFile();
+            + ACCEPTED_LICENSE_FILE_NAME).createNewFile();
       }
       catch (IOException e)
       {
@@ -215,13 +284,15 @@
 
   /**
    * Indicate if the license had already been approved..
-   * @return <CODE>true</CODE> if the license had already been approved
-   * by the user <CODE>false</CODE> otherwise.
+   *
+   * @return <CODE>true</CODE> if the license had already been approved by the
+   *         user <CODE>false</CODE> otherwise.
    */
   static public boolean isAlreadyApproved()
   {
-    File f = new File(getInstanceLegalDirectory() + File.separatorChar
-        + "licenseAccepted");
+    final File f =
+        new File(getInstanceLegalDirectory() + File.separatorChar
+            + ACCEPTED_LICENSE_FILE_NAME);
     return f.exists();
   }
 
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/ui/InstallLicensePanel.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/ui/InstallLicensePanel.java
index 751bed2..b214d0f 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/ui/InstallLicensePanel.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/ui/InstallLicensePanel.java
@@ -108,7 +108,7 @@
     JTextArea detailsTextArea = new JTextArea(10, 50);
     detailsTextArea.setBackground(
         UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
-
+    detailsTextArea.setFont(UIFactory.TEXTFIELD_FONT);
     detailsTextArea.setText(LicenseFile.getText());
 
     gbc.insets = UIFactory.getEmptyInsets();
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
index 49ebad1..7223f4d 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/webstart/WebStartInstaller.java
@@ -38,6 +38,7 @@
 import java.util.logging.Logger;
 
 import org.opends.quicksetup.ApplicationException;
+import org.opends.quicksetup.LicenseFile;
 import org.opends.quicksetup.ReturnCode;
 import org.opends.quicksetup.ProgressStep;
 import org.opends.quicksetup.Installation;
@@ -156,6 +157,9 @@
 
       checkAbort();
 
+      // create license accepted file
+      LicenseFile.createFileLicenseApproved();
+
       createData();
 
       checkAbort();

--
Gitblit v1.10.0