/*
 * Copyright 2016 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.tooling.internal.composite;

import org.gradle.api.Transformer;
import org.gradle.tooling.ResultHandler;
import org.gradle.tooling.internal.consumer.ConnectionParameters;
import org.gradle.tooling.internal.consumer.DefaultBuildLauncher;
import org.gradle.tooling.internal.consumer.ExceptionTransformer;
import org.gradle.tooling.internal.consumer.async.AsyncConsumerActionExecutor;
import org.gradle.tooling.internal.consumer.connection.ConsumerAction;
import org.gradle.tooling.internal.consumer.connection.ConsumerConnection;
import org.gradle.tooling.internal.consumer.parameters.ConsumerOperationParameters;

import java.io.File;

public class DefaultCompositeBuildLauncher extends DefaultBuildLauncher {
    public DefaultCompositeBuildLauncher(File targetBuildDir, AsyncConsumerActionExecutor connection, ConnectionParameters parameters) {
        super(connection, parameters);
        operationParamsBuilder.setCompositeTargetBuildRootDir(targetBuildDir);
    }

    public void run(final ResultHandler<? super Void> handler) {
        final ConsumerOperationParameters operationParameters = getConsumerOperationParameters();

        connection.run(new ConsumerAction<Void>() {
            public ConsumerOperationParameters getParameters() {
                return operationParameters;
            }

            public Void run(ConsumerConnection connection) {
                Void sink = connection.run(Void.class, operationParameters);
                return sink;
            }
        }, new ResultHandlerAdapter(handler));
    }

    private class ResultHandlerAdapter extends org.gradle.tooling.internal.consumer.ResultHandlerAdapter<Void> {
        public ResultHandlerAdapter(ResultHandler<? super Void> handler) {
            super(handler, new ExceptionTransformer(new Transformer<String, Throwable>() {
                @Override
                public String transform(Throwable throwable) {
                    return String.format("Could not execute build using %s.", connection.getDisplayName());
                }
            }));
        }
    }

}

@@ -669,32 +669,113 @@
 ort 
-org.gradle.api.Transform
+com.google.common.collect.Lists;%0Aimport org.gradle.api.Transformer;%0Aimport org.gradle.tooling.BuildLaunch
 er;%0A
@@ -1326,28 +1326,145 @@
 rs;%0A
-%0Aimport java.io.File
+import org.gradle.tooling.model.UnsupportedMethodException;%0A%0Aimport java.io.File;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream
 ;%0A%0Ap
@@ -1534,16 +1534,41 @@
 auncher 
+implements BuildLauncher 
 %7B%0A    pu
@@ -2394,24 +2394,1550 @@
 r));%0A    %7D%0A%0A
+%0A    // TODO: Make all configuration methods configure underlying model builders%0A    private DefaultCompositeBuildLauncher unsupportedMethod() %7B%0A        throw new UnsupportedMethodException(%22Not supported for composite connections.%22);%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher withArguments(String... arguments) %7B%0A        return withArguments(Lists.newArrayList(arguments));%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher withArguments(Iterable%3CString%3E arguments) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setStandardOutput(OutputStream outputStream) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setStandardError(OutputStream outputStream) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setColorOutput(boolean colorOutput) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setStandardInput(InputStream inputStream) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setJavaHome(File javaHome) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setJvmArguments(String... jvmArguments) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A    @Override%0A    public DefaultCompositeBuildLauncher setJvmArguments(Iterable%3CString%3E jvmArguments) %7B%0A        return unsupportedMethod();%0A    %7D%0A%0A%0A
     private 

Throw unsupported errors for unsupported BuildLauncher methods in composite