/*
 * Copyright 2011 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.api.internal.tasks.compile.fork;

import com.google.common.collect.Lists;
import org.gradle.api.Action;
import org.gradle.api.AntBuilder;
import org.gradle.internal.Factory;
import org.gradle.api.internal.file.collections.SimpleFileCollection;
import org.gradle.api.internal.project.ant.BasicAntBuilder;
import org.gradle.api.internal.tasks.compile.AntJavaCompiler;
import org.gradle.api.internal.tasks.compile.JavaCompiler;
import org.gradle.api.internal.tasks.compile.JavaCompilerSupport;
import org.gradle.api.tasks.WorkResult;
import org.gradle.messaging.remote.ObjectConnection;
import org.gradle.process.internal.WorkerProcessContext;

import java.io.Serializable;

public class CompilationAction extends JavaCompilerSupport implements Action<WorkerProcessContext>, Serializable {
    public WorkResult execute() {
        throw new UnsupportedOperationException();
    }

    public void execute(WorkerProcessContext workerProcessContext) {
        ObjectConnection connection = workerProcessContext.getServerConnection();
        final CompilationListener listener = connection.addOutgoing(CompilationListener.class);
        
        Factory<AntBuilder> antBuilderFactory = new Factory<AntBuilder>() {
            public AntBuilder create() {
                return new BasicAntBuilder();
            }
        };
        
        try {
            JavaCompiler javaCompiler = new AntJavaCompiler(antBuilderFactory);
            javaCompiler.setSource(source);
            javaCompiler.setDestinationDir(destinationDir);
            javaCompiler.setClasspath(classpath);
            javaCompiler.setSourceCompatibility(sourceCompatibility);
            javaCompiler.setTargetCompatibility(targetCompatibility);
            javaCompiler.setCompileOptions(compileOptions);
            WorkResult result = javaCompiler.execute();
            listener.completed(new CompilationResult(result.getDidWork(), null));
        } catch (Throwable t) {
            listener.completed(new CompilationResult(true, t));
        } finally {
            connection.stop();
        }
    }

    public CompilationAction makeSerializable() {
        if (!(source instanceof Serializable)) {
            source = new SimpleFileCollection(source.getFiles());
        }
        if (!(classpath instanceof Serializable)) {
            classpath = new SimpleFileCollection(Lists.newArrayList(classpath));
        }
        return this;
    }
}

@@ -702,16 +702,17 @@
 .Lists;%0A
+%0A
 import o
@@ -982,148 +982,9 @@
 ile.
-AntJavaCompiler;%0Aimport org.gradle.api.internal.tasks.compile.JavaCompiler;%0Aimport org.gradle.api.internal.tasks.compile.JavaCompilerSupport
+*
 ;%0Aim
@@ -1532,14 +1532,8 @@
     
-final 
 Comp
@@ -1631,187 +1631,592 @@
     
-Factory%3CAntBuilder%3E antBuilderFactory = new Factory%3CAntBuilder%3E() %7B%0A            public AntBuilder create() %7B%0A                return new BasicAntBuilder();%0A            %7D%0A        %7D;
+try %7B%0A            JavaCompiler compiler = createCompiler();%0A            WorkResult result = compiler.execute();%0A            listener.completed(new CompilationResult(result.getDidWork(), null));%0A        %7D catch (Throwable t) %7B%0A            listener.completed(new CompilationResult(true, t));%0A        %7D finally %7B%0A            connection.stop();%0A        %7D%0A    %7D%0A%0A    public CompilationAction makeSerializable() %7B%0A        if (!(source instanceof Serializable)) %7B%0A            source = new SimpleFileCollection(source.getFiles());%0A        %7D%0A        if (!(classpath instanceof Serializable)) %7B
 %0A   
@@ -2224,34 +2224,130 @@
     
-%0A
     
-    try %7B%0A           
+classpath = new SimpleFileCollection(Lists.newArrayList(classpath));%0A        %7D%0A        return this;%0A    %7D%0A%0A    private
  Jav
@@ -2356,28 +2356,30 @@
 ompiler 
-java
+create
 Compiler
  = new A
@@ -2374,48 +2374,73 @@
 iler
- = new AntJavaCompiler(antBu
+() %7B%0A        JavaCompilerChooser chooser = createComp
 il
-d
 er
-Factory
+Chooser(
 );%0A 
@@ -2442,33 +2442,94 @@
 ();%0A        
-    javaC
+JavaCompiler compiler = new SwitchableJavaCompiler(chooser);%0A        c
 ompiler.setS
@@ -2547,33 +2547,25 @@
 e);%0A        
-    javaC
+c
 ompiler.setD
@@ -2599,33 +2599,25 @@
 r);%0A        
-    javaC
+c
 ompiler.setC
@@ -2641,33 +2641,25 @@
 h);%0A        
-    javaC
+c
 ompiler.setS
@@ -2703,33 +2703,25 @@
 y);%0A        
-    javaC
+c
 ompiler.setT
@@ -2773,17 +2773,9 @@
     
-    javaC
+c
 ompi
@@ -2825,302 +2825,174 @@
     
-    WorkResult result = javaCompiler.execute()
+return compiler
 ;%0A    
+%7D%0A%0A
     
-    listener.c
+private JavaC
 omp
+i
 le
-ted(new CompilationResult(result.getDidWork(), null));%0A        %7D catch (Throwable t) %7B%0A            listener.completed(new CompilationResult(true, t));%0A        %7D finally %7B%0A            connection.stop();%0A 
+rChooser createCompilerChooser() %7B%0A        Factory%3CAntBuilder%3E antBuilderFactory = new Factory%3CAntBuilder%3E() %7B%0A
        
-%7D%0A
  
-   %7D%0A%0A
     
@@ -3002,162 +3002,80 @@
 lic 
-CompilationAction makeSerializable() %7B%0A        if (!(source instanceof Serializable)) %7B%0A            source = new SimpleFileCollection(source.getFiles
+AntBuilder create() %7B%0A                return new BasicAntBuilder
 ()
-)
 ;%0A
+    
     
@@ -3092,162 +3092,77 @@
     
-if (!(classpath instanceof Serializable)) %7B%0A            classpath = new SimpleFileCollection(Lists.newArrayList(classpath));%0A        %7D%0A        return this
+%7D;%0A        return new InProcessJavaCompilerChooser(antBuilderFactory)
 ;%0A  

use InProcessJavaCompilerChooser in CompilationAction