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