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.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/**
033 * The task called when we want to start the server.
034 *
035 */
036public class StopServerTask extends StartStopTask
037{
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 StopServerTask(ControlPanelInfo info, ProgressDialog dlg)
045  {
046    super(info, dlg);
047  }
048
049  /** {@inheritDoc} */
050  public Type getType()
051  {
052    return Type.STOP_SERVER;
053  }
054
055
056  /** {@inheritDoc} */
057  public LocalizableMessage getTaskDescription()
058  {
059    return INFO_CTRL_PANEL_STOP_SERVER_TASK_DESCRIPTION.get();
060  }
061
062  /** {@inheritDoc} */
063  public void runTask()
064  {
065    super.runTask();
066    if (state == State.FINISHED_SUCCESSFULLY)
067    {
068      // Verify that the server is actually stopped
069      SwingUtilities.invokeLater(new Runnable()
070      {
071        public void run()
072        {
073          getProgressDialog().appendProgressHtml(Utilities.applyFont(
074              "<b>"+INFO_CTRL_PANEL_SERVER_STOPPED.get()+"</b><br><br>",
075              ColorAndFontConstants.progressFont));
076        }
077      });
078    }
079  }
080
081  /** {@inheritDoc} */
082  protected String getCommandLinePath()
083  {
084    return getCommandLinePath("stop-ds");
085  }
086
087  /**
088   * Method called just after calling the command-line.  To be overwritten
089   * by the inheriting classes.
090   */
091  protected void postCommandLine()
092  {
093    if (returnCode != 0)
094    {
095      state = State.FINISHED_WITH_ERROR;
096    }
097    else
098    {
099      File f = new File(getInfo().getServerDescriptor().getInstancePath());
100      // Check that the server is actually stopped.
101      boolean stopped = !Utilities.isServerRunning(f);
102      int nTries = 20;
103      while (!stopped && nTries > 0)
104      {
105        try
106        {
107          Thread.sleep(700);
108        }
109        catch (Throwable t)
110        {
111        }
112        stopped = !Utilities.isServerRunning(f);
113        nTries --;
114      }
115      if (!stopped)
116      {
117        SwingUtilities.invokeLater(new Runnable()
118        {
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}