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