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;
037import java.net.URL;
038
039import org.opends.quicksetup.util.Utils;
040import org.opends.server.util.ServerConstants;
041import org.opends.server.util.StaticUtils;
042
043/**
044 * Represents information about the license file. NOTE: the license file
045 * location must be kept in sync with build.xml and
046 * org.opends.server.tools.upgrade.LicenseFile.
047 */
048public class LicenseFile
049{
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 getInstanceLegalDirectory()
070  {
071    String instanceLegalDirName;
072    String installDirName = System.getProperty("INSTALL_ROOT");
073
074    if (installDirName == null)
075    {
076      installDirName = System.getenv("INSTALL_ROOT");
077    }
078
079    if (installDirName == null)
080    {
081      installDirName = ".";
082    }
083
084    String instanceDirname =
085        Utils.getInstancePathFromInstallPath(installDirName);
086    instanceLegalDirName = instanceDirname + File.separator + LEGAL_FOLDER_NAME;
087    File instanceLegalDir = new File(instanceLegalDirName);
088    if (!instanceLegalDir.exists())
089    {
090      instanceLegalDir.mkdir();
091    }
092    return instanceLegalDirName;
093  }
094
095  /**
096   * The File object related to the license file.
097   */
098  private static File licFile;
099
100  /**
101   * The license file approval state.
102   */
103  private static boolean approved;
104
105  /**
106   * Returns the license file name.
107   */
108  private static String getName()
109  {
110    return getInstanceLegalDirectory() + File.separatorChar
111        + LICENSE_FILE_NAME;
112  }
113
114  /**
115   * Returns the license file object.
116   */
117  private static File getFile()
118  {
119    if (licFile == null)
120    {
121      licFile = new File(getName());
122    }
123    return licFile;
124  }
125
126  /**
127   * Returns the URL to the license file when using jnlp / java web start.
128   */
129  private static URL getWebStartLicenseFile()
130  {
131    final String licenseResource =
132        LEGAL_FOLDER_NAME + File.separatorChar + LICENSE_FILE_NAME;
133    return Thread.currentThread().getContextClassLoader().getResource(
134        licenseResource);
135  }
136
137  /**
138   * Checks if the license file exists.
139   *
140   * @return <CODE>true</CODE> if the license file exists in the Legal directory
141   *         in the top level installation directory <CODE>false</CODE>
142   *         otherwise.
143   */
144  public static boolean exists()
145  {
146    if (Utils.isWebStart())
147    {
148      return getWebStartLicenseFile() != null;
149    }
150    else
151    {
152      return getFile().exists();
153    }
154  }
155
156  /**
157   * Get the textual contents of the license file.
158   *
159   * @return the textual contents of the license file.
160   */
161  public static String getText()
162  {
163    InputStream input = null;
164    // Gets the inputstream of the license
165    // From a file as the usual way,
166    // from an URL if we use web start / jnlp.
167    if (!Utils.isWebStart())
168    {
169      try
170      {
171        input = new FileInputStream(getFile());
172      }
173      catch (FileNotFoundException e)
174      {
175        // Should not happen
176        return "";
177      }
178    }
179    else
180    {
181      URL licenseURL = getWebStartLicenseFile();
182      if (licenseURL != null)
183      {
184        try
185        {
186          input = licenseURL.openStream();
187        }
188        catch (Exception e)
189        {
190          // Should not happen
191          return "";
192        }
193      }
194    }
195    // Reads the inputstream content.
196    final StringBuilder sb = new StringBuilder();
197    if (input != null)
198    {
199      try
200      {
201        final BufferedReader br =
202            new BufferedReader(new InputStreamReader(input));
203        String read = br.readLine();
204
205        while (read != null)
206        {
207          sb.append(read);
208          sb.append(ServerConstants.EOL);
209          read = br.readLine();
210        }
211      }
212      catch (IOException ioe)
213      {
214        // Should not happen
215        return "";
216      }
217    }
218    StaticUtils.close(input);
219
220    return sb.toString();
221  }
222
223  /**
224   * Get the license approval status.
225   *
226   * @return <CODE>true</CODE> if the license has been accepted by the user
227   *         <CODE>false</CODE> otherwise.
228   */
229  public static boolean getApproval()
230  {
231    return approved;
232  }
233
234  /**
235   * Sets the license approval status.
236   *
237   * @param p_approved
238   *          the license approval status
239   */
240  public static void setApproval(boolean p_approved)
241  {
242    approved = p_approved;
243  }
244
245  /**
246   * Creates a file - in the legal folder from the specified directory; which
247   * indicates that the license has been approved.
248   *
249   * @param installationPath
250   *          The server installation's path.
251   */
252  public static void createFileLicenseApproved(final String installationPath)
253  {
254    if (getApproval() && installationPath != null)
255    {
256      try
257      {
258        new File(installationPath + File.separatorChar + LEGAL_FOLDER_NAME
259            + File.separatorChar + ACCEPTED_LICENSE_FILE_NAME).createNewFile();
260      }
261      catch (IOException e)
262      {
263        // do nothing
264      }
265    }
266  }
267
268  /**
269   * Indicate if the license had already been approved..
270   *
271   * @return <CODE>true</CODE> if the license had already been approved by the
272   *         user <CODE>false</CODE> otherwise.
273   */
274  public static boolean isAlreadyApproved()
275  {
276    final File f =
277        new File(getInstanceLegalDirectory() + File.separatorChar
278            + ACCEPTED_LICENSE_FILE_NAME);
279    return f.exists();
280  }
281
282}