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