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