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 2008-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.quicksetup.installer;
028
029import static org.opends.messages.QuickSetupMessages.*;
030import static org.opends.messages.ToolMessages.*;
031import static com.forgerock.opendj.util.OperatingSystem.isWindows;
032
033import java.io.File;
034
035import org.opends.quicksetup.Constants;
036import org.opends.quicksetup.ReturnCode;
037import org.opends.quicksetup.CliApplication;
038import org.opends.quicksetup.Installation;
039import org.opends.quicksetup.Launcher;
040import org.opends.quicksetup.QuickSetupLog;
041import org.opends.quicksetup.installer.offline.OfflineInstaller;
042import org.opends.quicksetup.util.IncompatibleVersionException;
043import org.opends.quicksetup.util.Utils;
044import org.forgerock.i18n.LocalizableMessage;
045import org.opends.server.tools.InstallDS;
046import org.opends.server.tools.InstallDSArgumentParser;
047import org.opends.server.util.DynamicConstants;
048import org.opends.server.util.ServerConstants;
049import com.forgerock.opendj.cli.ArgumentException;
050import com.forgerock.opendj.cli.ArgumentParser;
051
052/**
053 * This class is called by the setup command line to launch the setup
054 * of the Directory Server. It just checks the command line arguments and the
055 * environment and determines whether the graphical or the command line
056 * based setup much be launched.
057 */
058public class SetupLauncher extends Launcher {
059  /**
060   * The main method which is called by the setup command lines.
061   *
062   * @param args the arguments passed by the command lines.  In the case
063   * we want to launch the cli setup they are basically the arguments that we
064   * will pass to the org.opends.server.tools.InstallDS class.
065   */
066  public static void main(String[] args) {
067    try {
068      QuickSetupLog.initLogFileHandler(
069              File.createTempFile(Constants.LOG_FILE_PREFIX,
070                  Constants.LOG_FILE_SUFFIX));
071    } catch (Throwable t) {
072      System.err.println("Unable to initialize log");
073      t.printStackTrace();
074    }
075    new SetupLauncher(args).launch();
076  }
077
078  private InstallDSArgumentParser argParser;
079
080  /**
081   * Creates a launcher.
082   *
083   * @param args the arguments passed by the command lines.
084   */
085  public SetupLauncher(String[] args) {
086    super(args);
087    String scriptName;
088    if (isWindows()) {
089      scriptName = Installation.WINDOWS_SETUP_FILE_NAME;
090    } else {
091      scriptName = Installation.UNIX_SETUP_FILE_NAME;
092    }
093    if (System.getProperty(ServerConstants.PROPERTY_SCRIPT_NAME) == null)
094    {
095      System.setProperty(ServerConstants.PROPERTY_SCRIPT_NAME, scriptName);
096    }
097    initializeParser();
098  }
099
100  /**
101   * Initialize the contents of the argument parser.
102   */
103  protected void initializeParser()
104  {
105    argParser = new InstallDSArgumentParser(InstallDS.class.getName());
106    try
107    {
108      argParser.initializeArguments();
109    }
110    catch (ArgumentException ae)
111    {
112      LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
113      System.out.println(message);
114    }
115  }
116
117  /** {@inheritDoc} */
118  public void launch() {
119    try
120    {
121      argParser.parseArguments(args);
122
123      if (argParser.isVersionArgumentPresent())
124      {
125        System.exit(ReturnCode.PRINT_VERSION.getReturnCode());
126      }
127      // The second condition is required when the user specifies '?'
128      else if (argParser.isUsageArgumentPresent() ||
129          argParser.usageOrVersionDisplayed())
130      {
131        System.exit(ReturnCode.SUCCESSFUL.getReturnCode());
132      }
133      else if (isCli())
134      {
135        Utils.checkJavaVersion();
136        System.exit(InstallDS.mainCLI(args));
137      }
138      else
139      {
140        willLaunchGui();
141        // The java version is checked in the launchGui code to be sure
142        // that if there is a problem with the java version the message
143        // (if possible) is displayed graphically.
144        int exitCode = launchGui(args);
145        if (exitCode != 0) {
146          File logFile = QuickSetupLog.getLogFile();
147          if (logFile != null)
148          {
149            guiLaunchFailed(logFile.toString());
150          }
151          else
152          {
153            guiLaunchFailed(null);
154          }
155          Utils.checkJavaVersion();
156          System.exit(InstallDS.mainCLI(args));
157        }
158      }
159    }
160    catch (ArgumentException ae)
161    {
162      argParser.displayMessageAndUsageReference(System.err, ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
163      System.exit(ReturnCode.USER_DATA_ERROR.getReturnCode());
164    }
165    catch (IncompatibleVersionException ive)
166    {
167      System.err.println(ive.getMessageObject());
168      System.exit(ReturnCode.JAVA_VERSION_INCOMPATIBLE.getReturnCode());
169    }
170  }
171
172  /** {@inheritDoc} */
173  public ArgumentParser getArgumentParser() {
174    return this.argParser;
175  }
176
177  /** {@inheritDoc} */
178  protected void guiLaunchFailed(String logFileName) {
179    if (logFileName != null)
180    {
181      System.err.println(INFO_SETUP_LAUNCHER_GUI_LAUNCHED_FAILED_DETAILS.get(
182              logFileName));
183    }
184    else
185    {
186      System.err.println(INFO_SETUP_LAUNCHER_GUI_LAUNCHED_FAILED.get());
187    }
188  }
189
190  /** {@inheritDoc} */
191  protected void willLaunchGui() {
192    System.out.println(INFO_SETUP_LAUNCHER_LAUNCHING_GUI.get());
193    System.setProperty("org.opends.quicksetup.Application.class",
194            OfflineInstaller.class.getName());
195  }
196
197  /** {@inheritDoc} */
198  protected LocalizableMessage getFrameTitle() {
199    return Utils.getCustomizedObject("INFO_FRAME_INSTALL_TITLE",
200        INFO_FRAME_INSTALL_TITLE.get(DynamicConstants.PRODUCT_NAME),
201        LocalizableMessage.class);
202  }
203
204  /** {@inheritDoc} */
205  protected CliApplication createCliApplication() {
206    return null;
207  }
208
209  /** {@inheritDoc} */
210  protected boolean isCli() {
211    return argParser.isCli();
212  }
213}