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