001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.quicksetup;
018
019import java.io.BufferedReader;
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025
026import org.opends.quicksetup.util.Utils;
027import org.opends.server.util.ServerConstants;
028
029/**
030 * Represents information about the license file. NOTE: the license file
031 * location must be kept in sync with build.xml and
032 * org.opends.server.tools.upgrade.LicenseFile.
033 */
034public class LicenseFile
035{
036  private static final String INSTALL_ROOT_SYSTEM_PROPERTY = "INSTALL_ROOT";
037  /** The license file name in Legal directory. */
038  private static final String LICENSE_FILE_NAME = "Forgerock_License.txt";
039  /** The Legal folder which contains license file. */
040  private static final String LEGAL_FOLDER_NAME = "legal-notices";
041  /** The accepted license file name. */
042  private static final String ACCEPTED_LICENSE_FILE_NAME = "licenseAccepted";
043
044  /** Get the directory in which legal files are stored. */
045  private static String getInstallDirectory() {
046    String installDirName = System.getProperty(INSTALL_ROOT_SYSTEM_PROPERTY);
047    if (installDirName == null)
048    {
049      installDirName = System.getenv(INSTALL_ROOT_SYSTEM_PROPERTY);
050    }
051    if (installDirName == null)
052    {
053      installDirName = ".";
054    }
055    return installDirName;
056  }
057
058  /** Get the directory in which approved legal files are stored. */
059  private static String getInstanceLegalDirectory()
060  {
061    String instanceLegalDirName = Utils.getInstancePathFromInstallPath(getInstallDirectory())
062        + File.separator + LEGAL_FOLDER_NAME;
063    File instanceLegalDir = new File(instanceLegalDirName);
064    if (!instanceLegalDir.exists())
065    {
066      instanceLegalDir.mkdir();
067    }
068    return instanceLegalDirName;
069  }
070
071  /** The File object related to the license file. */
072  private static File licenceFile;
073  /** The license file approval state. */
074  private static boolean approved;
075  /** Returns the license file name. */
076  private static String getName()
077  {
078    return getInstallDirectory() + File.separator + LEGAL_FOLDER_NAME + File.separator + LICENSE_FILE_NAME;
079  }
080
081  /** Returns the license file object. */
082  private static File getFile()
083  {
084    if (licenceFile == null)
085    {
086      licenceFile = new File(getName());
087    }
088    return licenceFile;
089  }
090
091  /**
092   * Checks if the license file exists.
093   *
094   * @return <CODE>true</CODE> if the license file exists in the Legal directory
095   *         in the top level installation directory <CODE>false</CODE>
096   *         otherwise.
097   */
098  public static boolean exists()
099  {
100    return getFile().exists();
101  }
102
103  /**
104   * Get the textual contents of the license file.
105   *
106   * @return the textual contents of the license file.
107   */
108  public static String getText()
109  {
110    // Reads the inputstream content.
111    try (InputStream input = new FileInputStream(getFile());
112        BufferedReader br = new BufferedReader(new InputStreamReader(input)))
113    {
114      final StringBuilder sb = new StringBuilder();
115      String read;
116      while ((read = br.readLine()) != null)
117      {
118        sb.append(read);
119        sb.append(ServerConstants.EOL);
120      }
121      return sb.toString();
122    }
123    catch (IOException ioe)
124    {
125      // Should not happen
126      return "";
127    }
128  }
129
130  /**
131   * Get the license approval status.
132   *
133   * @return <CODE>true</CODE> if the license has been accepted by the user
134   *         <CODE>false</CODE> otherwise.
135   */
136  public static boolean getApproval()
137  {
138    return approved;
139  }
140
141  /**
142   * Sets the license approval status.
143   *
144   * @param approved
145   *          the license approval status
146   */
147  public static void setApproval(boolean approved)
148  {
149    LicenseFile.approved = approved;
150  }
151
152  /**
153   * Creates a file - in the legal folder from the specified directory; which
154   * indicates that the license has been approved.
155   *
156   * @param installationPath
157   *          The server installation's path.
158   */
159  public static void createFileLicenseApproved(final String installationPath)
160  {
161    if (getApproval() && installationPath != null)
162    {
163      String instanceDirname = Utils.getInstancePathFromInstallPath(installationPath);
164      String instanceLegalDirName = instanceDirname + File.separator + LEGAL_FOLDER_NAME;
165      File instanceLegalDir = new File(instanceLegalDirName);
166
167      try
168      {
169        if (!instanceLegalDir.exists())
170        {
171          instanceLegalDir.mkdir();
172        }
173        new File(instanceLegalDir, ACCEPTED_LICENSE_FILE_NAME).createNewFile();
174      }
175      catch (IOException e)
176      {
177        // do nothing
178      }
179    }
180  }
181
182  /**
183   * Indicate if the license had already been approved..
184   *
185   * @return <CODE>true</CODE> if the license had already been approved by the
186   *         user <CODE>false</CODE> otherwise.
187   */
188  public static boolean isAlreadyApproved()
189  {
190    return new File(getInstanceLegalDirectory(), ACCEPTED_LICENSE_FILE_NAME).exists();
191  }
192}