/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.execution.application;

import javax.annotation.Nonnull;

import com.intellij.execution.CommonJavaRunConfigurationParameters;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.JavaRunConfigurationExtensionManager;
import com.intellij.execution.RunConfigurationExtension;
import com.intellij.execution.configurations.JavaCommandLineState;
import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.execution.process.KillableColoredProcessHandler;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessTerminatedListener;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.util.JavaParametersUtil;
import com.intellij.openapi.util.SystemInfo;
import consulo.java.execution.configurations.OwnJavaParameters;

/**
 * @author nik
 */
public abstract class BaseJavaApplicationCommandLineState<T extends RunConfigurationBase & CommonJavaRunConfigurationParameters> extends JavaCommandLineState
{
	protected final T myConfiguration;

	public BaseJavaApplicationCommandLineState(ExecutionEnvironment environment, @Nonnull final T configuration)
	{
		super(environment);
		myConfiguration = configuration;
	}

	protected void setupJavaParameters(OwnJavaParameters params) throws ExecutionException
	{
		JavaParametersUtil.configureConfiguration(params, myConfiguration);

		for(RunConfigurationExtension ext : RunConfigurationExtension.EP_NAME.getExtensions())
		{
			ext.updateJavaParameters(getConfiguration(), params, getRunnerSettings());
		}
	}

	@Nonnull
	@Override
	protected OSProcessHandler startProcess() throws ExecutionException
	{
		OSProcessHandler handler;
		if(SystemInfo.isWindows)
		{
			handler = super.startProcess();
		}
		else
		{
			handler = KillableColoredProcessHandler.create(createCommandLine());
			ProcessTerminatedListener.attach(handler);
		}

		RunnerSettings runnerSettings = getRunnerSettings();
		JavaRunConfigurationExtensionManager.getInstance().attachExtensionsToProcess(getConfiguration(), handler, runnerSettings);
		return handler;
	}

	@Override
	protected boolean ansiColoringEnabled()
	{
		return true;
	}

	protected T getConfiguration()
	{
		return myConfiguration;
	}
}

@@ -643,42 +643,8 @@
 n;%0A%0A
-import javax.annotation.Nonnull;%0A%0A
 impo
@@ -707,16 +707,16 @@
 meters;%0A
+
 import c
@@ -1020,69 +1020,8 @@
 se;%0A
-import com.intellij.execution.configurations.RunnerSettings;%0A
 impo
@@ -1325,53 +1325,8 @@
 il;%0A
-import com.intellij.openapi.util.SystemInfo;%0A
 impo
@@ -1386,16 +1386,50 @@
 eters;%0A%0A
+import javax.annotation.Nonnull;%0A%0A
 /**%0A * @
@@ -2060,17 +2060,20 @@
 xtension
-s
+List
 ())%0A%09%09%7B%0A
@@ -2281,103 +2281,14 @@
 dler
-;%0A%09%09if(SystemInfo.isWindows)%0A%09%09%7B%0A%09%09%09handler = super.startProcess();%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09handler =
+ = new
  Kil
@@ -2314,22 +2314,22 @@
 Handler.
-create
+Silent
 (createC
@@ -2343,17 +2343,16 @@
 ine());%0A
-%09
 %09%09Proces
@@ -2392,68 +2392,8 @@
 r);%0A
-%09%09%7D%0A%0A%09%09RunnerSettings runnerSettings = getRunnerSettings();%0A
 %09%09Ja
@@ -2496,17 +2496,20 @@
 andler, 
-r
+getR
 unnerSet
@@ -2513,16 +2513,18 @@
 Settings
+()
 );%0A%09%09ret

use silent version of process handler - don't throw error about output reader