8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna * Copyright (c) 2014 ForgeRock AS All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.tools.bundles;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.setup.Bootstrap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.File;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.IOException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.MessageFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Properties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.ResourceBundle;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Lunaimport org.forgerock.guice.core.InjectorHolder;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Lunaimport org.forgerock.openam.license.LicensePresenter;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Lunaimport org.forgerock.openam.license.LicenseRejectedException;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Lunapublic class Main implements SetupConstants {
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna private static final LicensePresenter LICENSE_PRESENTER = InjectorHolder.getInstance(LicensePresenter.class);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static void main(String[] args) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle bundle = ResourceBundle.getBundle(System.getProperty(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SETUP_PROPERTIES_FILE, DEFAULT_PROPERTIES_FILE));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((System.getProperty(PRINT_HELP) != null) &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.getProperty(PRINT_HELP).equals(YES)){
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupUtils.printUsage(bundle);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (System.getProperty(CHECK_VERSION) != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (System.getProperty(CHECK_VERSION).equals(YES)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(VersionCheck.isValid());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna boolean acceptLicense = false;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna for (String arg : args) {
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna if (arg.equals(ACCEPT_LICENSE)) {
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna acceptLicense = true;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna break;
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna }
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna }
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna try {
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna LICENSE_PRESENTER.presentLicenses(acceptLicense);
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna } catch (LicenseRejectedException e) {
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna System.out.println(bundle.getString("message.error.license"));
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna System.exit(0);
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna }
c375ffd945a63cb2de20fda03e2ef0cdf96e64d9David Luna
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean loadConfig = (System.getProperty(CONFIG_LOAD) != null) &&
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.getProperty(CONFIG_LOAD).equals(YES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String currentOS = SetupUtils.determineOS();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Properties configProp = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String debugPath = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String logPath = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (loadConfig) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String configPath = System.getProperty(AMCONFIG_PATH);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debugPath = System.getProperty(DEBUG_PATH);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster logPath = System.getProperty(LOG_PATH);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String currentDir = System.getProperty("user.dir");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((configPath == null) || (configPath.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configPath = SetupUtils.getUserInput(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster currentOS + QUESTION), System.getProperty("user.home")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + File.separator + "openam");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(new File(configPath).isAbsolute())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.dir.absolute"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((debugPath == null) || (debugPath.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debugPath = SetupUtils.getUserInput(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster currentOS + ".debug.dir"), currentDir
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + File.separator + "debug");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(new File(debugPath).isAbsolute())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.dir.absolute"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!isWriteable(debugPath)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.debug.dir.not.writable"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((logPath == null) || (logPath.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster logPath = SetupUtils.getUserInput(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster currentOS + ".log.dir"), currentDir
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + File.separator + "log");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!(new File(logPath).isAbsolute())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.dir.absolute"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!isWriteable(logPath)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.log.dir.not.writable"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String toolsHome = new File(".").getCanonicalPath();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster toolsHome = toolsHome.replaceAll("\\\\", "/");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((debugPath == null) || (debugPath.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debugPath = toolsHome + "/debug";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((logPath == null) || (logPath.length() == 0)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster logPath = toolsHome + "/log";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp = Bootstrap.load(configPath, false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (configProp == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.error.dir"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File path = new File(debugPath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean created = path.exists() || path.mkdirs();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!created) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.debug.dir.not.writable"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster path = new File(logPath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster created = path.exists() || path.mkdirs();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!created) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.error.log.dir.not.writable"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!configPath.endsWith(FILE_SEPARATOR)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configPath = configPath + FILE_SEPARATOR;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp.setProperty(USER_INPUT,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configPath.substring(0, configPath.length() - 1));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp.setProperty("LogDir", logPath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp.setProperty("DebugDir",debugPath);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp.setProperty(CURRENT_PLATFORM, currentOS);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(ex.getMessage());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster configProp = new Properties();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupUtils.evaluateBundleValues(bundle, configProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SetupUtils.copyAndFilterScripts(bundle, configProp);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (loadConfig) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object[] p = {debugPath};
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(MessageFormat.format(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle.getString("message.info.debug.dir"), p));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster p[0] = logPath;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(MessageFormat.format(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle.getString("message.info.log.dir"), p));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "message.info.version.tools") + " " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle.getString(TOOLS_VERSION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle.getString("message.info.version.am") +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster " " + SystemProperties.get("com.iplanet.am.version"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (IOException ex) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.out.println(bundle.getString("message.error.copy"));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster System.exit(0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static boolean isWriteable(String file) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean exist = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean writable = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ((file != null) && !exist) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File f = new File(file);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster exist = f.exists();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!exist) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster file = f.getParent();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (file != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster File f = new File(file);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writable = f.canWrite();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return writable;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster