/*
 * Copyright 2015 the original author or authors.
 *
 * 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 org.gradle.internal.concurrent;

import org.gradle.internal.UncheckedException;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class ManagedExecutorImpl extends AbstractDelegatingExecutorService implements ManagedExecutor {
    private final ExecutorService executor;
    private final ThreadLocal<Object> executing = new ThreadLocal<Object>();
    private final ExecutorPolicy executorPolicy;

    ManagedExecutorImpl(ExecutorService executor, ExecutorPolicy executorPolicy) {
        super(executor);
        this.executor = executor;
        this.executorPolicy = executorPolicy;
    }

    public void execute(final Runnable command) {
        executor.execute(trackedCommand(command));
    }

    protected Runnable trackedCommand(final Runnable command) {
        return new Runnable() {
            public void run() {
                executing.set(command);
                try {
                    executorPolicy.onExecute(command);
                } finally {
                    executing.set(null);
                }
            }
        };
    }

    protected <V> Callable<V> trackedCommand(final Callable<V> command) {
        return new Callable<V>() {
            public V call() throws Exception {
                executing.set(command);
                try {
                    return executorPolicy.onExecute(command);
                } finally {
                    executing.set(null);
                }
            }
        };
    }

    public void requestStop() {
        executor.shutdown();
    }

    public void stop() {
        stop(Integer.MAX_VALUE, TimeUnit.SECONDS);
    }

    public void stop(int timeoutValue, TimeUnit timeoutUnits) throws IllegalStateException {
        requestStop();
        if (executing.get() != null) {
            throw new IllegalStateException("Cannot stop this executor from an executor thread.");
        }
        try {
            if (!executor.awaitTermination(timeoutValue, timeoutUnits)) {
                executor.shutdownNow();
                throw new IllegalStateException("Timeout waiting for concurrent jobs to complete.");
            }
        } catch (InterruptedException e) {
            throw new UncheckedException(e);
        }
        executorPolicy.onStop();
    }

    @Override
    public void setFixedPoolSize(int numThreads) {
        if (executor instanceof ThreadPoolExecutor) {
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
            threadPoolExecutor.setCorePoolSize(numThreads);
            threadPoolExecutor.setMaximumPoolSize(numThreads);
        } else {
            throw new UnsupportedOperationException();
        }
    }
}

@@ -3193,32 +3193,258 @@
 utor) executor;%0A
+            // we do this because in jdk9 these methods throw an exception if you%0A            // try to set one such that maximumPoolSize %3C corePoolSize%0A            if (numThreads %3C threadPoolExecutor.getCorePoolSize()) %7B%0A    
             thre
@@ -3479,32 +3479,36 @@
 ze(numThreads);%0A
+    
             thre
@@ -3546,32 +3546,198 @@
 ze(numThreads);%0A
+            %7D else %7B%0A                threadPoolExecutor.setMaximumPoolSize(numThreads);%0A                threadPoolExecutor.setCorePoolSize(numThreads);%0A            %7D%0A
         %7D else %7B

Change logic for setting fixed pool size to work with java 9