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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.task;
029
030import static org.opends.messages.AdminToolMessages.*;
031
032import java.util.ArrayList;
033
034import javax.swing.SwingUtilities;
035
036import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
037import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
038import org.opends.guitools.controlpanel.ui.ProgressDialog;
039import org.opends.guitools.controlpanel.util.Utilities;
040import org.forgerock.i18n.LocalizableMessage;
041
042/**
043 * The task called when we want to restart the server.
044 *
045 */
046public class RestartServerTask extends StartStopTask
047{
048  private boolean starting;
049
050  private StartServerTask startTask;
051
052  /**
053   * Constructor of the task.
054   * @param info the control panel information.
055   * @param dlg the progress dialog where the task progress will be displayed.
056   */
057  public RestartServerTask(ControlPanelInfo info, ProgressDialog dlg)
058  {
059    super(info, dlg);
060    startTask = new StartServerTask(info, dlg);
061  }
062
063  /** {@inheritDoc} */
064  public Type getType()
065  {
066    if (starting)
067    {
068      return Type.START_SERVER;
069    }
070    else
071    {
072      return Type.STOP_SERVER;
073    }
074  }
075
076  /** {@inheritDoc} */
077  public LocalizableMessage getTaskDescription()
078  {
079    return INFO_CTRL_PANEL_RESTART_SERVER_TASK_DESCRIPTION.get();
080  }
081
082  /** {@inheritDoc} */
083  protected String getCommandLinePath()
084  {
085    return null;
086  }
087
088  /**
089   * Returns the full path of the start command-line.
090   * @return the full path of the start command-line.
091   */
092  private String getStartCommandLineName()
093  {
094    return startTask.getCommandLinePath();
095  }
096
097  /**
098   * Returns the arguments of the start command-line.
099   * @return the arguments of the start command-line.
100   */
101  private ArrayList<String> getStartCommandLineArguments()
102  {
103    return startTask.getCommandLineArguments();
104  }
105
106  /**
107   * Returns the full path of the stop command-line.
108   * @return the full path of the stop command-line.
109   */
110  private String getStopCommandLineName()
111  {
112    return getCommandLinePath("stop-ds");
113  }
114
115  /** {@inheritDoc} */
116  public void runTask()
117  {
118    state = State.RUNNING;
119    starting = false;
120    lastException = null;
121    final ProgressDialog dlg = getProgressDialog();
122    SwingUtilities.invokeLater(new Runnable()
123    {
124      public void run()
125      {
126        String cmdLine = getStopCommandLineName();
127        printEquivalentCommandLine(cmdLine, getCommandLineArguments(),
128            INFO_CTRL_PANEL_EQUIVALENT_CMD_TO_STOP_SERVER.get());
129        dlg.setSummary(LocalizableMessage.raw(
130            Utilities.applyFont(
131            INFO_CTRL_PANEL_STOPPING_SERVER_SUMMARY.get(),
132            ColorAndFontConstants.defaultFont)));
133      }
134    });
135    // To display new status
136    getInfo().regenerateDescriptor();
137    getInfo().stopPooling();
138    try
139    {
140      ArrayList<String> arguments = getCommandLineArguments();
141
142      String[] args = new String[arguments.size()];
143
144      arguments.toArray(args);
145      returnCode = executeCommandLine(getStopCommandLineName(), args);
146
147      if (returnCode != 0)
148      {
149        state = State.FINISHED_WITH_ERROR;
150      }
151      else
152      {
153        SwingUtilities.invokeLater(new Runnable()
154        {
155          public void run()
156          {
157            getProgressDialog().getProgressBar().setIndeterminate(false);
158            dlg.getProgressBar().setValue(30);
159            dlg.appendProgressHtml(Utilities.applyFont(
160                "<b>"+INFO_CTRL_PANEL_SERVER_STOPPED.get()+"</b><br><br>",
161                ColorAndFontConstants.progressFont));
162            String cmdLine = getStartCommandLineName();
163            printEquivalentCommandLine(cmdLine, getStartCommandLineArguments(),
164                INFO_CTRL_PANEL_EQUIVALENT_CMD_TO_START_SERVER.get());
165
166            dlg.setSummary(LocalizableMessage.raw(
167                Utilities.applyFont(
168                INFO_CTRL_PANEL_STARTING_SERVER_SUMMARY.get(),
169                ColorAndFontConstants.defaultFont)));
170          }
171        });
172
173        starting = true;
174        // To display new status
175        getInfo().regenerateDescriptor();
176        arguments = getStartCommandLineArguments();
177        args = new String[arguments.size()];
178        arguments.toArray(args);
179
180        returnCode = executeCommandLine(getStartCommandLineName(), args);
181        if (returnCode != 0)
182        {
183          state = State.FINISHED_WITH_ERROR;
184        }
185        else
186        {
187          state = State.FINISHED_SUCCESSFULLY;
188        }
189      }
190    }
191    catch (Throwable t)
192    {
193      lastException = t;
194      state = State.FINISHED_WITH_ERROR;
195    }
196    getInfo().startPooling();
197  }
198}