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