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 2011-2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.uninstaller;
019
020import static org.opends.messages.AdminToolMessages.*;
021import static org.opends.messages.ToolMessages.ERR_ERROR_PARSING_ARGS;
022import static com.forgerock.opendj.util.OperatingSystem.isWindows;
023import static com.forgerock.opendj.cli.Utils.wrapText;
024
025import org.forgerock.i18n.LocalizableMessage;
026import org.opends.messages.ToolMessages;
027
028import java.io.File;
029import org.opends.quicksetup.CliApplication;
030import org.opends.quicksetup.Launcher;
031import org.opends.quicksetup.Installation;
032import org.opends.quicksetup.QuickSetupLog;
033import org.opends.quicksetup.ReturnCode;
034import org.opends.quicksetup.util.Utils;
035import org.opends.server.util.DynamicConstants;
036import org.opends.server.util.ServerConstants;
037import com.forgerock.opendj.cli.ArgumentException;
038import com.forgerock.opendj.cli.ArgumentParser;
039
040/**
041 * This class is called by the uninstall command lines to launch the uninstall
042 * of the Directory Server. It just checks the command line arguments and the
043 * environment and determines whether the graphical or the command line
044 * based uninstall much be launched.
045 */
046public class UninstallLauncher extends Launcher {
047
048  /** Prefix for log files. */
049  public static final String LOG_FILE_PREFIX = "opendj-uninstall-";
050
051  /** Suffix for log files. */
052  public static final String LOG_FILE_SUFFIX = ".log";
053
054  /**
055   * The main method which is called by the uninstall command lines.
056   *
057   * @param args the arguments passed by the command lines.  In the case
058   * we want to launch the cli setup they are basically the arguments that we
059   * will pass to the org.opends.server.tools.InstallDS class.
060   */
061  public static void main(String[] args) {
062    try {
063      QuickSetupLog.initLogFileHandler(
064              File.createTempFile(LOG_FILE_PREFIX, LOG_FILE_SUFFIX));
065
066    } catch (Throwable t) {
067      System.err.println("Unable to initialize log");
068      t.printStackTrace();
069    }
070    new UninstallLauncher(args).launch();
071  }
072
073  private UninstallerArgumentParser argParser;
074
075  /**
076   * Creates a launcher.
077   *
078   * @param args the arguments passed by the command lines.
079   */
080  public UninstallLauncher(String[] args) {
081    super(args);
082
083    String scriptName;
084    if (isWindows()) {
085      scriptName = Installation.WINDOWS_UNINSTALL_FILE_NAME;
086    } else {
087      scriptName = Installation.UNIX_UNINSTALL_FILE_NAME;
088    }
089    if (System.getProperty(ServerConstants.PROPERTY_SCRIPT_NAME) == null)
090    {
091      System.setProperty(ServerConstants.PROPERTY_SCRIPT_NAME, scriptName);
092    }
093
094    initializeParser();
095  }
096
097  /** {@inheritDoc} */
098  public void launch() {
099    //  Validate user provided data
100    try
101    {
102      argParser.parseArguments(args);
103      if (argParser.isVersionArgumentPresent())
104      {
105        System.exit(ReturnCode.PRINT_VERSION.getReturnCode());
106      }
107      else if (argParser.usageOrVersionDisplayed())
108      {
109        // If there was no problem parsing arguments, this means that the user
110        // asked to display the usage.
111        System.exit(ReturnCode.SUCCESSFUL.getReturnCode());
112      }
113      else
114      {
115        super.launch();
116      }
117    }
118    catch (ArgumentException ae)
119    {
120      argParser.displayMessageAndUsageReference(System.err, ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
121      System.exit(ReturnCode.USER_DATA_ERROR.getReturnCode());
122    }
123  }
124
125  /**
126   * Initialize the contents of the argument parser.
127   */
128  protected void initializeParser()
129  {
130    argParser = new UninstallerArgumentParser(getClass().getName(),
131        INFO_UNINSTALL_LAUNCHER_USAGE_DESCRIPTION.get(), false);
132    try
133    {
134      argParser.initializeGlobalArguments(System.out);
135    }
136    catch (ArgumentException ae)
137    {
138      LocalizableMessage message =
139        ToolMessages.ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
140      System.err.println(wrapText(message,
141          Utils.getCommandLineMaxLineWidth()));
142    }
143  }
144
145  /** {@inheritDoc} */
146  protected void guiLaunchFailed(String logFilePath) {
147    if (logFilePath != null)
148    {
149      System.err.println(ERR_UNINSTALL_LAUNCHER_GUI_LAUNCHED_FAILED_DETAILS
150              .get(logFilePath));
151    }
152    else
153    {
154      System.err.println(ERR_UNINSTALL_LAUNCHER_GUI_LAUNCHED_FAILED.get());
155    }
156  }
157
158  /** {@inheritDoc} */
159  public ArgumentParser getArgumentParser() {
160    return this.argParser;
161  }
162
163  /** {@inheritDoc} */
164  protected void willLaunchGui() {
165    System.out.println(INFO_UNINSTALL_LAUNCHER_LAUNCHING_GUI.get());
166    System.setProperty("org.opends.quicksetup.Application.class",
167            org.opends.guitools.uninstaller.Uninstaller.class.getName());
168  }
169
170  /** {@inheritDoc} */
171  protected CliApplication createCliApplication() {
172    return new Uninstaller();
173  }
174
175  /** {@inheritDoc} */
176  protected LocalizableMessage getFrameTitle() {
177    return Utils.getCustomizedObject("INFO_FRAME_UNINSTALL_TITLE",
178        INFO_FRAME_UNINSTALL_TITLE.get(DynamicConstants.PRODUCT_NAME),
179        LocalizableMessage.class);
180  }
181
182  /**
183   * Indicates whether or not the launcher should print a usage
184   * statement based on the content of the arguments passed into
185   * the constructor.
186   * @return boolean where true indicates usage should be printed
187   */
188  protected boolean shouldPrintUsage() {
189    return argParser.isUsageArgumentPresent() &&
190    !argParser.usageOrVersionDisplayed();
191  }
192
193  /**
194   * Indicates whether or not the launcher should print a usage
195   * statement based on the content of the arguments passed into
196   * the constructor.
197   * @return boolean where true indicates usage should be printed
198   */
199  protected boolean isQuiet() {
200    return argParser.isQuiet();
201  }
202
203  /**
204   * Indicates whether or not the launcher should print a usage
205   * statement based on the content of the arguments passed into
206   * the constructor.
207   * @return boolean where true indicates usage should be printed
208   */
209  protected boolean isNoPrompt() {
210    return !argParser.isInteractive();
211  }
212
213  /**
214   * Indicates whether or not the launcher should print a version
215   * statement based on the content of the arguments passed into
216   * the constructor.
217   * @return boolean where true indicates version should be printed
218   */
219  protected boolean shouldPrintVersion() {
220    return argParser.isVersionArgumentPresent() &&
221    !argParser.usageOrVersionDisplayed();
222  }
223
224  /**
225   * Indicates whether the launcher will launch a command line versus
226   * a graphical application based on the contents of the arguments
227   * passed into the constructor.
228   *
229   * @return boolean where true indicates that a CLI application
230   *         should be launched
231   */
232  protected boolean isCli() {
233    return argParser.isCli();
234  }
235
236}