/*
 * 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.client.program;

import org.apache.flink.api.common.InvalidProgramException;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.JobSubmissionResult;
import org.apache.flink.api.common.Plan;
import org.apache.flink.optimizer.plan.FlinkPlan;
import org.apache.flink.optimizer.plan.OptimizedPlan;
import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.List;

/**
 * Execution Environment for remote execution with the Client in detached mode.
 */
public class DetachedEnvironment extends ContextEnvironment {

	/** Keeps track of the program plan for the Client to access. */
	private FlinkPlan detachedPlan;

	private static final Logger LOG = LoggerFactory.getLogger(DetachedEnvironment.class);

	public DetachedEnvironment(
			ClusterClient<?> remoteConnection,
			List<URL> jarFiles,
			List<URL> classpaths,
			ClassLoader userCodeClassLoader,
			SavepointRestoreSettings savepointSettings) {
		super(remoteConnection, jarFiles, classpaths, userCodeClassLoader, savepointSettings);
	}

	@Override
	public JobExecutionResult execute(String jobName) throws Exception {
		Plan p = createProgramPlan(jobName);
		OptimizedPlan optPlan = ClusterClient.getOptimizedPlan(client.compiler, p, getParallelism());

		setDetachedPlan(optPlan);
		
		final JobSubmissionResult result = client.run(optPlan, jarFilesToAttach, classpathsToAttach, userCodeClassLoader, savepointSettings);
		this.lastJobExecutionResult = result.getJobExecutionResult();
		return this.lastJobExecutionResult;
	}

	public void setDetachedPlan(FlinkPlan plan) {
		if (detachedPlan == null) {
			detachedPlan = plan;
		} else {
			throw new InvalidProgramException(DetachedJobExecutionResult.DETACHED_MESSAGE +
					DetachedJobExecutionResult.EXECUTE_TWICE_MESSAGE);
		}
	}
}

@@ -1058,58 +1058,8 @@
 an;%0A
-import org.apache.flink.optimizer.plan.FlinkPlan;%0A
 impo
@@ -1104,24 +1104,24 @@
 imizedPlan;%0A
+
 import org.a
@@ -1180,66 +1180,8 @@
 s;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -1451,118 +1451,29 @@
 ate 
-FlinkPlan detachedPlan;%0A%0A%09private static final Logger LOG = LoggerFactory.getLogger(DetachedEnvironment.class)
+boolean alreadyCalled
 ;%0A%0A%09
@@ -1760,16 +1760,41 @@
 tings);%0A
+%09%09alreadyCalled = false;%0A
 %09%7D%0A%0A%09@Ov
@@ -1870,16 +1870,48 @@
 ption %7B%0A
+%09%09verifyExecuteIsCalledOnce();%0A%0A
 %09%09Plan p
@@ -2042,39 +2042,8 @@
 );%0A%0A
-%09%09setDetachedPlan(optPlan);%0A%09%09%0A
 %09%09fi
@@ -2286,115 +2286,70 @@
 %0A%0A%09p
-ublic void setDetachedPlan(FlinkPlan plan) %7B%0A%09%09if (detachedPlan == null) %7B%0A%09%09%09detachedPlan = plan;%0A%09%09%7D else
+rivate void verifyExecuteIsCalledOnce() %7B%0A%09%09if (alreadyCalled)
  %7B%0A%09
@@ -2433,14 +2433,9 @@
 GE +
-%0A%09%09%09%09%09
+ 
 Deta
@@ -2485,13 +2485,37 @@
 E);%0A%09%09%7D%0A
+%09%09alreadyCalled = true;%0A
 %09%7D%0A%7D%0A

Remove unnecessary method in DetachedEnvironment