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 2008-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.quicksetup.installer;
018
019import static org.opends.messages.QuickSetupMessages.*;
020import static org.opends.messages.ToolMessages.*;
021import static org.opends.server.util.ServerConstants.*;
022
023import org.forgerock.i18n.LocalizableMessage;
024import org.opends.quicksetup.CliApplication;
025import org.opends.quicksetup.Installation;
026import org.opends.quicksetup.Launcher;
027import org.opends.quicksetup.ReturnCode;
028import org.opends.quicksetup.util.IncompatibleVersionException;
029import org.opends.quicksetup.util.Utils;
030import org.opends.server.tools.InstallDS;
031import org.opends.server.tools.InstallDSArgumentParser;
032import org.opends.server.util.DynamicConstants;
033
034import com.forgerock.opendj.cli.ArgumentException;
035import com.forgerock.opendj.cli.ArgumentParser;
036
037/**
038 * This class is called by the setup command line to launch the setup
039 * of the Directory Server. It just checks the command line arguments and the
040 * environment and determines whether the graphical or the command line
041 * based setup much be launched.
042 */
043public class SetupLauncher extends Launcher {
044
045  private static final String LOG_FILE_PREFIX = "opendj-setup-";
046
047  /**
048   * The main method which is called by the setup command lines.
049   *
050   * @param args the arguments passed by the command lines.  In the case
051   * we want to launch the cli setup they are basically the arguments that we
052   * will pass to the org.opends.server.tools.InstallDS class.
053   */
054  public static void main(String[] args) {
055    new SetupLauncher(args).launch();
056  }
057
058  private InstallDSArgumentParser argParser;
059
060  /**
061   * Creates a launcher.
062   *
063   * @param args the arguments passed by the command lines.
064   */
065  public SetupLauncher(String[] args) {
066    super(args, LOG_FILE_PREFIX);
067    if (System.getProperty(PROPERTY_SCRIPT_NAME) == null)
068    {
069      System.setProperty(PROPERTY_SCRIPT_NAME, Installation.getSetupFileName());
070    }
071    initializeParser();
072  }
073
074  /** Initialize the contents of the argument parser. */
075  protected void initializeParser()
076  {
077    argParser = new InstallDSArgumentParser(InstallDS.class.getName());
078    try
079    {
080      argParser.initializeArguments();
081    }
082    catch (ArgumentException ae)
083    {
084      LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
085      System.out.println(message);
086    }
087  }
088
089  @Override
090  public void launch() {
091    try
092    {
093      argParser.parseArguments(args);
094
095      if (argParser.isVersionArgumentPresent())
096      {
097        System.exit(ReturnCode.PRINT_VERSION.getReturnCode());
098      }
099      // The second condition is required when the user specifies '?'
100      else if (argParser.isUsageArgumentPresent() ||
101          argParser.usageOrVersionDisplayed())
102      {
103        System.exit(ReturnCode.SUCCESSFUL.getReturnCode());
104      }
105      else if (isCli())
106      {
107        Utils.checkJavaVersion();
108        System.exit(InstallDS.mainCLI(args, tempLogFile));
109      }
110      else
111      {
112        willLaunchGui();
113        // The java version is checked in the launchGui code to be sure
114        // that if there is a problem with the java version the message
115        // (if possible) is displayed graphically.
116        int exitCode = launchGui(args);
117        if (exitCode != 0) {
118          guiLaunchFailed();
119          Utils.checkJavaVersion();
120          System.exit(InstallDS.mainCLI(args, tempLogFile));
121        }
122      }
123    }
124    catch (ArgumentException ae)
125    {
126      argParser.displayMessageAndUsageReference(System.err, ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
127      System.exit(ReturnCode.USER_DATA_ERROR.getReturnCode());
128    }
129    catch (IncompatibleVersionException ive)
130    {
131      System.err.println(ive.getMessageObject());
132      System.exit(ReturnCode.JAVA_VERSION_INCOMPATIBLE.getReturnCode());
133    }
134  }
135
136  @Override
137  public ArgumentParser getArgumentParser() {
138    return this.argParser;
139  }
140
141  @Override
142  protected void guiLaunchFailed() {
143      System.err.println(
144          tempLogFile.isEnabled() ? INFO_SETUP_LAUNCHER_GUI_LAUNCHED_FAILED_DETAILS.get(tempLogFile.getPath())
145                                  : INFO_SETUP_LAUNCHER_GUI_LAUNCHED_FAILED.get());
146  }
147
148  @Override
149  protected void willLaunchGui() {
150    System.out.println(INFO_SETUP_LAUNCHER_LAUNCHING_GUI.get());
151    System.setProperty("org.opends.quicksetup.Application.class", Installer.class.getName());
152  }
153
154  @Override
155  protected LocalizableMessage getFrameTitle() {
156    return Utils.getCustomizedObject("INFO_FRAME_INSTALL_TITLE",
157        INFO_FRAME_INSTALL_TITLE.get(DynamicConstants.PRODUCT_NAME),
158        LocalizableMessage.class);
159  }
160
161  @Override
162  protected CliApplication createCliApplication() {
163    return null;
164  }
165
166  @Override
167  protected boolean isCli() {
168    return argParser.isCli();
169  }
170}