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