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 */
017package org.opends.guitools.controlpanel.task;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.HashSet;
022import java.util.Set;
023
024import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
025import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
026import org.opends.guitools.controlpanel.ui.ProgressDialog;
027import org.forgerock.i18n.LocalizableMessage;
028
029/**
030 * An abstract class used to re-factor some code between the start, stop and
031 * restart tasks.
032 */
033public abstract class StartStopTask extends Task
034{
035  Set<String> backendSet;
036
037  /**
038   * Constructor of the task.
039   * @param info the control panel information.
040   * @param progressDialog the progress dialog where the task progress will be
041   * displayed.
042   */
043  protected StartStopTask(ControlPanelInfo info, ProgressDialog progressDialog)
044  {
045    super(info, progressDialog);
046    backendSet = new HashSet<>();
047    for (BackendDescriptor backend :
048      info.getServerDescriptor().getBackends())
049    {
050      backendSet.add(backend.getBackendID());
051    }
052
053  }
054
055  /** {@inheritDoc} */
056  public Set<String> getBackends()
057  {
058    return backendSet;
059  }
060
061  /** {@inheritDoc} */
062  public boolean canLaunch(Task taskToBeLaunched,
063      Collection<LocalizableMessage> incompatibilityReasons)
064  {
065    boolean canLaunch = true;
066    if (state == State.RUNNING && runningOnSameServer(taskToBeLaunched))
067    {
068      incompatibilityReasons.add(getIncompatibilityMessage(this,
069          taskToBeLaunched));
070      canLaunch = false;
071    }
072    return canLaunch;
073  }
074
075  /** {@inheritDoc} */
076  public void runTask()
077  {
078    state = State.RUNNING;
079    lastException = null;
080    // To display new status
081    try
082    {
083      getInfo().stopPooling();
084      getInfo().regenerateDescriptor();
085
086      ArrayList<String> arguments = getCommandLineArguments();
087
088      String[] args = new String[arguments.size()];
089
090      arguments.toArray(args);
091      returnCode = executeCommandLine(getCommandLinePath(), args);
092
093      postCommandLine();
094    }
095    catch (Throwable t)
096    {
097      lastException = t;
098      state = State.FINISHED_WITH_ERROR;
099    }
100    getInfo().startPooling();
101  }
102
103  /** {@inheritDoc} */
104  protected ArrayList<String> getCommandLineArguments()
105  {
106    return new ArrayList<>();
107  }
108
109  /**
110   * Method called just after calling the command-line.  To be overwritten
111   * by the inheriting classes.
112   */
113  protected void postCommandLine()
114  {
115    if (returnCode != 0)
116    {
117      state = State.FINISHED_WITH_ERROR;
118    }
119    else
120    {
121      state = State.FINISHED_SUCCESSFULLY;
122    }
123  }
124}