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.io.File;
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 start the server. */
033public class StopServerTask extends StartStopTask
034{
035
036  /**
037   * Constructor of the task.
038   * @param info the control panel information.
039   * @param dlg the progress dialog where the task progress will be displayed.
040   */
041  public StopServerTask(ControlPanelInfo info, ProgressDialog dlg)
042  {
043    super(info, dlg);
044  }
045
046  @Override
047  public Type getType()
048  {
049    return Type.STOP_SERVER;
050  }
051
052
053  @Override
054  public LocalizableMessage getTaskDescription()
055  {
056    return INFO_CTRL_PANEL_STOP_SERVER_TASK_DESCRIPTION.get();
057  }
058
059  @Override
060  public void runTask()
061  {
062    super.runTask();
063    if (state == State.FINISHED_SUCCESSFULLY)
064    {
065      // Verify that the server is actually stopped
066      SwingUtilities.invokeLater(new Runnable()
067      {
068        @Override
069        public void run()
070        {
071          getProgressDialog().appendProgressHtml(Utilities.applyFont(
072              "<b>"+INFO_CTRL_PANEL_SERVER_STOPPED.get()+"</b><br><br>",
073              ColorAndFontConstants.progressFont));
074        }
075      });
076    }
077  }
078
079  @Override
080  protected String getCommandLinePath()
081  {
082    return getCommandLinePath("stop-ds");
083  }
084
085  /**
086   * Method called just after calling the command-line.  To be overwritten
087   * by the inheriting classes.
088   */
089  @Override
090  protected void postCommandLine()
091  {
092    if (returnCode != 0)
093    {
094      state = State.FINISHED_WITH_ERROR;
095    }
096    else
097    {
098      File f = new File(getInfo().getServerDescriptor().getInstancePath());
099      // Check that the server is actually stopped.
100      boolean stopped = !Utilities.isServerRunning(f);
101      int nTries = 20;
102      while (!stopped && nTries > 0)
103      {
104        try
105        {
106          Thread.sleep(700);
107        }
108        catch (Throwable t)
109        {
110        }
111        stopped = !Utilities.isServerRunning(f);
112        nTries --;
113      }
114      if (!stopped)
115      {
116        SwingUtilities.invokeLater(new Runnable()
117        {
118          @Override
119          public void run()
120          {
121            getProgressDialog().appendProgressHtml(
122                Utilities.applyFont(
123                    "<br>"+
124                    ERR_CTRL_PANEL_STOPPING_SERVER_POST_CMD_LINE.get(
125                        getCommandLinePath("stop-ds"))+"<br>",
126                        ColorAndFontConstants.progressFont));
127          }
128        });
129        returnCode = -1;
130        state = State.FINISHED_WITH_ERROR;
131      }
132      else
133      {
134        state = State.FINISHED_SUCCESSFULLY;
135      }
136    }
137  }
138}