1047N/A/*
1047N/A * CDDL HEADER START
1047N/A *
1047N/A * The contents of this file are subject to the terms of the
1047N/A * Common Development and Distribution License, Version 1.0 only
1047N/A * (the "License"). You may not use this file except in compliance
1047N/A * with the License.
1047N/A *
1047N/A * You can obtain a copy of the license at
1047N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
1047N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
1047N/A * See the License for the specific language governing permissions
1047N/A * and limitations under the License.
1047N/A *
1047N/A * When distributing Covered Code, include this CDDL HEADER in each
1047N/A * file and include the License file at
1047N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
1047N/A * add the following below this CDDL HEADER, with the fields enclosed
1047N/A * by brackets "[]" replaced with your own identifying information:
1047N/A * Portions Copyright [yyyy] [name of copyright owner]
1047N/A *
1047N/A * CDDL HEADER END
1047N/A *
1047N/A *
4268N/A * Copyright 2008-2009 Sun Microsystems, Inc.
1047N/A */
1047N/A
1047N/Apackage org.opends.quicksetup;
1047N/A
4268N/Aimport java.util.logging.Level;
4268N/Aimport java.util.logging.Logger;
4268N/A
1047N/Aimport org.opends.quicksetup.util.ProgressMessageFormatter;
1047N/Aimport org.opends.quicksetup.util.PlainTextProgressMessageFormatter;
1047N/Aimport org.opends.quicksetup.util.Utils;
1047N/Aimport org.opends.quicksetup.event.ProgressUpdateListener;
1047N/Aimport org.opends.quicksetup.event.ProgressUpdateEvent;
1907N/Aimport org.opends.server.util.StaticUtils;
3503N/Aimport org.opends.server.util.cli.CLIException;
2324N/Aimport org.opends.messages.Message;
1047N/A
1047N/A/**
3397N/A * Class used by Launcher to start a CLI application.
1935N/A *
1047N/A */
1047N/Apublic class QuickSetupCli {
1047N/A
1047N/A /** Arguments passed in the command line. */
1907N/A protected Launcher launcher;
1047N/A
1047N/A private CliApplication cliApp;
1047N/A
1907N/A private UserData userData;
1907N/A
4268N/A static private final Logger LOG =
4268N/A Logger.getLogger(QuickSetupCli.class.getName());
4268N/A
1047N/A /**
1047N/A * Creates a QuickSetupCli instance.
1047N/A * @param cliApp the application to be run
1907N/A * @param launcher that launched the app
1047N/A */
1907N/A public QuickSetupCli(CliApplication cliApp, Launcher launcher) {
1047N/A this.cliApp = cliApp;
1907N/A this.launcher = launcher;
1907N/A }
1907N/A
1907N/A /**
1907N/A * Gets the user data this application will use when running.
1907N/A * @return UserData to use when running
1907N/A */
1907N/A public UserData getUserData() {
1907N/A return this.userData;
1047N/A }
1047N/A
1047N/A /**
1047N/A * Parses the user data and prompts the user for data if required. If the
4268N/A * user provides all the required data it launches the application.
1047N/A *
1047N/A * @return the return code (SUCCESSFUL, CANCELLED, USER_DATA_ERROR,
1047N/A * ERROR_ACCESSING_FILE_SYSTEM, ERROR_STOPPING_SERVER or BUG.
1047N/A */
1935N/A public ReturnCode run()
1047N/A {
1935N/A ReturnCode returnValue;
1047N/A // Parse the arguments
1047N/A try
1047N/A {
3042N/A ProgressMessageFormatter formatter =
3042N/A new PlainTextProgressMessageFormatter();
3042N/A cliApp.setProgressMessageFormatter(formatter);
1907N/A userData = cliApp.createUserData(launcher);
1047N/A if (userData != null)
1047N/A {
1047N/A cliApp.setUserData(userData);
2210N/A if (!userData.isQuiet()) {
1516N/A cliApp.addProgressUpdateListener(
1907N/A new ProgressUpdateListener() {
1907N/A public void progressUpdate(ProgressUpdateEvent ev) {
2324N/A Message newLogs = ev.getNewLogs();
2324N/A if (newLogs != null) {
2324N/A System.out.print(
2324N/A StaticUtils.wrapText(
2324N/A newLogs,
2324N/A Utils.getCommandLineMaxLineWidth()));
2324N/A }
1907N/A }
1907N/A });
1516N/A }
1907N/A Thread appThread = new Thread(cliApp, "CLI Application");
4268N/A LOG.log(Level.INFO, "Launching application");
1907N/A appThread.start();
1907N/A while (!Thread.State.TERMINATED.equals(appThread.getState())) {
1907N/A try {
1047N/A Thread.sleep(100);
1907N/A } catch (Exception ex) {
1502N/A // do nothing;
1047N/A }
1047N/A }
2324N/A returnValue = cliApp.getReturnCode();
4268N/A LOG.log(Level.INFO, "Application returnValue: "+returnValue);
2324N/A if (returnValue == null) {
2324N/A ApplicationException ue = cliApp.getRunError();
2324N/A if (ue != null)
2324N/A {
4268N/A LOG.log(Level.INFO, "Application run error: "+ue, ue);
2324N/A returnValue = ue.getType();
2324N/A }
2324N/A else
2324N/A {
2324N/A returnValue = ReturnCode.SUCCESSFUL;
2324N/A }
1047N/A }
1047N/A }
3042N/A else
3042N/A {
2831N/A // User cancelled operation.
4576N/A returnValue = ReturnCode.CANCELED;
1047N/A }
1047N/A }
1047N/A catch (UserDataException uude)
1047N/A {
4268N/A LOG.log(Level.SEVERE, "UserDataException: "+uude, uude);
1907N/A System.err.println();
1907N/A System.err.println(StaticUtils.wrapText(uude.getLocalizedMessage(),
1907N/A Utils.getCommandLineMaxLineWidth()));
1907N/A System.err.println();
3503N/A if (uude.getCause() instanceof CLIException)
3503N/A {
3503N/A returnValue = ReturnCode.USER_INPUT_ERROR;
3503N/A }
3503N/A else
3503N/A {
3503N/A returnValue = ReturnCode.USER_DATA_ERROR;
3503N/A }
1047N/A }
3571N/A catch (ApplicationException ae)
3571N/A {
4268N/A LOG.log(Level.SEVERE, "ApplicationException: "+ae, ae);
3571N/A System.err.println();
3571N/A System.err.println(ae.getLocalizedMessage());
3571N/A System.err.println();
3571N/A returnValue = ae.getType();
3571N/A }
4268N/A catch (Throwable t)
4268N/A {
4268N/A LOG.log(Level.SEVERE, "Unexpected error: "+t, t);
4268N/A returnValue = ReturnCode.UNKNOWN;
4268N/A }
4268N/A LOG.log(Level.INFO, "returnValue: "+returnValue.getReturnCode());
1047N/A return returnValue;
1047N/A }
1047N/A
1047N/A}