/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.flink.streaming.api.environment;

import org.apache.flink.annotation.Public;
import org.apache.flink.api.common.InvalidProgramException;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.PlanExecutor;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.graph.StreamGraph;

import javax.annotation.Nonnull;

import java.util.Collections;

/**
 * The LocalStreamEnvironment is a StreamExecutionEnvironment that runs the program locally,
 * multi-threaded, in the JVM where the environment is instantiated. It spawns an embedded
 * Flink cluster in the background and executes the program on that cluster.
 *
 * <p>When this environment is instantiated, it uses a default parallelism of {@code 1}. The default
 * parallelism can be set via {@link #setParallelism(int)}.
 */
@Public
public class LocalStreamEnvironment extends StreamExecutionEnvironment {

	private final Configuration configuration;

	/**
	 * Creates a new mini cluster stream environment that uses the default configuration.
	 */
	public LocalStreamEnvironment() {
		this(new Configuration());
	}

	/**
	 * Creates a new mini cluster stream environment that configures its local executor with the given configuration.
	 *
	 * @param configuration The configuration used to configure the local executor.
	 */
	public LocalStreamEnvironment(@Nonnull Configuration configuration) {
		if (!ExecutionEnvironment.areExplicitEnvironmentsAllowed()) {
			throw new InvalidProgramException(
				"The LocalStreamEnvironment cannot be used when submitting a program through a client, " +
					"or running in a TestEnvironment context.");
		}
		this.configuration = configuration;
		setParallelism(1);
	}

	/**
	 * Executes the JobGraph of the on a mini cluster of ClusterUtil with a user
	 * specified name.
	 *
	 * @return The result of the job execution, containing elapsed time and accumulators.
	 */
	@Override
	public JobExecutionResult execute(StreamGraph streamGraph) throws Exception {
		try {
			final PlanExecutor executor = PlanExecutor.createLocalExecutor(configuration);
			return executor.executePlan(streamGraph, Collections.emptyList(), Collections.emptyList());
		} finally {
			transformations.clear();
		}
	}
}

@@ -1010,57 +1010,8 @@
 lt;%0A
-import org.apache.flink.api.common.PlanExecutor;%0A
 impo
@@ -1110,24 +1110,81 @@
 figuration;%0A
+import org.apache.flink.configuration.DeploymentOptions;%0A
 import org.a
@@ -1274,29 +1274,63 @@
 ort 
-java.util.Collections
+static org.apache.flink.util.Preconditions.checkNotNull
 ;%0A%0A/
@@ -1847,53 +1847,8 @@
  %7B%0A%0A
-%09private final Configuration configuration;%0A%0A
 %09/**
@@ -2289,16 +2289,189 @@
 tion) %7B%0A
+%09%09super(validateAndGetConfiguration(configuration));%0A%09%09setParallelism(1);%0A%09%7D%0A%0A%09private static Configuration validateAndGetConfiguration(final Configuration configuration) %7B%0A
 %09%09if (!E
@@ -2568,16 +2568,17 @@
 on(%0A%09%09%09%09
+%09
 %22The Loc
@@ -2660,16 +2660,18 @@
 nt, %22 +%0A
+%09%09
 %09%09%09%09%09%22or
@@ -2722,29 +2722,84 @@
 %7D%0A%09%09
-this.configuration = 
+final Configuration effectiveConfiguration = new Configuration(checkNotNull(
 conf
@@ -2807,37 +2807,189 @@
 guration
+))
 ;%0A%09%09
-setParallelism(1)
+effectiveConfiguration.set(DeploymentOptions.TARGET, %22local-executor%22);%0A%09%09effectiveConfiguration.set(DeploymentOptions.ATTACHED, true);%0A%09%09return effectiveConfiguration
 ;%0A%09%7D%0A%0A%09/
@@ -3290,104 +3290,19 @@
 %0A%09%09%09
-final PlanExecutor executor = PlanExecutor.createLocalExecutor(configuration);%0A%09%09%09return executo
+return supe
 r.ex
@@ -3310,12 +3310,8 @@
 cute
-Plan
 (str
@@ -3322,58 +3322,8 @@
 raph
-, Collections.emptyList(), Collections.emptyList()
 );%0A%09

Make LocalStreamEnvironment use LocalExecutor