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