<commit_before>/*******************************************************************************
 * Copyright (c) 2015 Pivotal Software, Inc. 
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * 
 * The Eclipse Public License is available at 
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * and the Apache License v2.0 is available at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You may elect to redistribute this code under either of these licenses.
 *  
 *  Contributors:
 *     <NAME>, Inc. - initial API and implementation
 ********************************************************************************/
package org.eclipse.cft.server.core.internal.client;

import org.cloudfoundry.client.lib.CloudFoundryOperations;
import org.cloudfoundry.client.lib.StartingInfo;
import org.cloudfoundry.client.lib.domain.CloudApplication.AppState;
import org.cloudfoundry.client.lib.domain.InstanceState;
import org.eclipse.cft.server.core.AbstractAppStateTracker;
import org.eclipse.cft.server.core.internal.ApplicationAction;
import org.eclipse.cft.server.core.internal.CloudErrorUtil;
import org.eclipse.cft.server.core.internal.CloudFoundryPlugin;
import org.eclipse.cft.server.core.internal.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.internal.Server;

/**
 * 
 * Attempts to start an application. It does not create an application, or
 * incrementally or fully push the application's resources. It simply starts the
 * application in the server with the application's currently published
 * resources, regardless of local changes have occurred or not.
 * 
 */
@SuppressWarnings("restriction")
public class RestartOperation extends ApplicationOperation {

	/**
	 * 
	 */

	public RestartOperation(CloudFoundryServerBehaviour behaviour, IModule[] modules, boolean clearConsole) {
		super(behaviour, modules, clearConsole);
	}

	@Override
	public String getOperationName() {
		return Messages.RestartOperation_STARTING_APP;
	}

	@Override
	protected void performDeployment(CloudFoundryApplicationModule appModule, IProgressMonitor monitor)
			throws CoreException {
		final Server server = (Server) getBehaviour().getServer();

		try {
			appModule.setStatus(null);

			final String deploymentName = appModule.getDeploymentInfo().getDeploymentName();

			server.setModuleState(getModules(), IServer.STATE_STARTING);

			if (deploymentName == null) {
				server.setModuleState(getModules(), IServer.STATE_STOPPED);

				throw CloudErrorUtil.toCoreException(
						"Unable to start application. Missing application deployment name in application deployment information."); //$NON-NLS-1$
			}

			SubMonitor subMonitor = SubMonitor.convert(monitor, 100);

			// Update the module with the latest CloudApplication from the
			// client before starting the application
			appModule = getBehaviour().updateCloudModule(appModule.getDeployedApplicationName(),
					subMonitor.newChild(20));

			final CloudFoundryApplicationModule cloudModule = appModule;

			final ApplicationAction deploymentMode = getDeploymentConfiguration().getApplicationStartMode();
			if (deploymentMode != ApplicationAction.STOP) {

				// Start the application. Use a regular request rather than
				// a staging-aware request, as any staging errors should not
				// result in a reattempt, unlike other cases (e.g. get the
				// staging
				// logs or refreshing app instance stats after an app has
				// started).

				String startLabel = Messages.RestartOperation_STARTING_APP + " - " + deploymentName; //$NON-NLS-1$
				getBehaviour().printlnToConsole(cloudModule, startLabel);

				CloudFoundryPlugin.getCallback().startApplicationConsole(getBehaviour().getCloudFoundryServer(),
						cloudModule, 0, subMonitor.newChild(20));

				getBehaviour().getRequestFactory().stopApplication("Stopping application" + deploymentName, //$NON-NLS-1$
						cloudModule);

				new BehaviourRequest<Void>(startLabel, getBehaviour()) {
					@Override
					protected Void doRun(final CloudFoundryOperations client, SubMonitor progress)
							throws CoreException, OperationCanceledException {
						CloudFoundryPlugin.trace("Application " + deploymentName + " starting"); //$NON-NLS-1$ //$NON-NLS-2$

						if (progress.isCanceled()) {
							throw new OperationCanceledException(
									Messages.bind(Messages.OPERATION_CANCELED, getRequestLabel()));
						}

						StartingInfo info = client.startApplication(deploymentName);

						// Similarly, check for cancel at this point
						if (progress.isCanceled()) {
							throw new OperationCanceledException(
									Messages.bind(Messages.OPERATION_CANCELED, getRequestLabel()));<commit_msg>[482535] Add back call to stop(...) in RestartOperation.<commit_after>/*******************************************************************************
 * Copyright (c) 2015 Pivotal Software, Inc. 
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * 
 * The Eclipse Public License is available at 
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * and the Apache License v2.0 is available at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You may elect to redistribute this code under either of these licenses.
 *  
 *  Contributors:
 *     <NAME>, Inc. - initial API and implementation
 ********************************************************************************/
package org.eclipse.cft.server.core.internal.client;

import org.cloudfoundry.client.lib.CloudFoundryOperations;
import org.cloudfoundry.client.lib.StartingInfo;
import org.cloudfoundry.client.lib.domain.CloudApplication.AppState;
import org.cloudfoundry.client.lib.domain.InstanceState;
import org.eclipse.cft.server.core.AbstractAppStateTracker;
import org.eclipse.cft.server.core.internal.ApplicationAction;
import org.eclipse.cft.server.core.internal.CloudErrorUtil;
import org.eclipse.cft.server.core.internal.CloudFoundryPlugin;
import org.eclipse.cft.server.core.internal.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.internal.Server;

/**
 * 
 * Attempts to start an application. It does not create an application, or
 * incrementally or fully push the application's resources. It simply starts the
 * application in the server with the application's currently published
 * resources, regardless of local changes have occurred or not.
 * 
 */
@SuppressWarnings("restriction")
public class RestartOperation extends ApplicationOperation {

	/**
	 * 
	 */

	public RestartOperation(CloudFoundryServerBehaviour behaviour, IModule[] modules, boolean clearConsole) {
		super(behaviour, modules, clearConsole);
	}

	@Override
	public String getOperationName() {
		return Messages.RestartOperation_STARTING_APP;
	}

	@Override
	protected void performDeployment(CloudFoundryApplicationModule appModule, IProgressMonitor monitor)
			throws CoreException {
		final Server server = (Server) getBehaviour().getServer();

		try {
			appModule.setStatus(null);

			final String deploymentName = appModule.getDeploymentInfo().getDeploymentName();

			server.setModuleState(getModules(), IServer.STATE_STARTING);

			if (deploymentName == null) {
				server.setModuleState(getModules(), IServer.STATE_STOPPED);

				throw CloudErrorUtil.toCoreException(
						"Unable to start application. Missing application deployment name in application deployment information."); //$NON-NLS-1$
			}

			SubMonitor subMonitor = SubMonitor.convert(monitor, 100);

			// Update the module with the latest CloudApplication from the
			// client before starting the application
			appModule = getBehaviour().updateCloudModule(appModule.getDeployedApplicationName(),
					subMonitor.newChild(20));

			final CloudFoundryApplicationModule cloudModule = appModule;

			final ApplicationAction deploymentMode = getDeploymentConfiguration().getApplicationStartMode();
			if (deploymentMode != ApplicationAction.STOP) {

				// Start the application. Use a regular request rather than
				// a staging-aware request, as any staging errors should not
				// result in a reattempt, unlike other cases (e.g. get the
				// staging
				// logs or refreshing app instance stats after an app has
				// started).

				String startLabel = Messages.RestartOperation_STARTING_APP + " - " + deploymentName; //$NON-NLS-1$
				getBehaviour().printlnToConsole(cloudModule, startLabel);

				CloudFoundryPlugin.getCallback().startApplicationConsole(getBehaviour().getCloudFoundryServer(),
						cloudModule, 0, subMonitor.newChild(20));

				getBehaviour().getRequestFactory().stopApplication("Stopping application" + deploymentName, //$NON-NLS-1$
						cloudModule).run(subMonitor.newChild(20));

				new BehaviourRequest<Void>(startLabel, getBehaviour()) {
					@Override
					protected Void doRun(final CloudFoundryOperations client, SubMonitor progress)
							throws CoreException, OperationCanceledException {
						CloudFoundryPlugin.trace("Application " + deploymentName + " starting"); //$NON-NLS-1$ //$NON-NLS-2$

						if (progress.isCanceled()) {
							throw new OperationCanceledException(
									Messages.bind(Messages.OPERATION_CANCELED, getRequestLabel()));
						}

						StartingInfo info = client.startApplication(deploymentName);

						// Similarly, check for cancel at this point
						if (progress.isCanceled()) {
							throw new OperationCanceledException(
									Messages.bind(Messages.OPERATION_CANCELED, getRequestLabel()));
{'fraction_non_alphanumeric': 0.12421052631578948, 'fraction_numerical': 0.005933014354066985, 'mean_word_length': 4.637001078748652, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7326149', 'n_tokens_mistral': 3182, 'n_tokens_neox': 2732, 'n_words': 702}