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

import org.gradle.api.JavaVersion;
import org.gradle.internal.Cast;
import org.gradle.internal.concurrent.ExecutorFactory;
import org.gradle.internal.concurrent.Stoppable;
import org.gradle.internal.reflect.DirectInstantiator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;

public class DefaultFileWatcherService implements FileWatcherService, Stoppable {
    private final ExecutorService executor;
    private final JavaVersion javaVersion;
    private final ClassLoader classLoader;
    private final FileWatcherService fileWatcherService;

    public DefaultFileWatcherService(ExecutorFactory executorFactory) {
        this(JavaVersion.current(), DefaultFileWatcherService.class.getClassLoader(), executorFactory);
    }

    DefaultFileWatcherService(JavaVersion javaVersion, ClassLoader classLoader, ExecutorFactory executorFactory) {
        this.javaVersion = javaVersion;
        this.classLoader = classLoader;
        this.executor = executorFactory.create("filewatcher");
        this.fileWatcherService = createFileWatcherService();
    }

    protected FileWatcherService createFileWatcherService() {
        if(javaVersion.isJava7Compatible()) {
            Class clazz;
            try {
                clazz = classLoader.loadClass("org.gradle.internal.filewatch.jdk7.DefaultFileWatcher");
            } catch (ClassNotFoundException e) {
                throw new RuntimeException("Cannot find FileWatcherService implementation class", e);
            }
            return Cast.uncheckedCast(DirectInstantiator.instantiate(clazz, executor));
        } else {
            throw new RuntimeException("FileWatcherService is only available on Java 7 compatible JVMs");
        }
    }

    @Override
    public void stop() {
        executor.shutdown();
    }

    @Override
    public Stoppable watch(FileWatchInputs inputs, Runnable callback) throws IOException {
        return fileWatcherService.watch(inputs, callback);
    }
}

@@ -718,16 +718,74 @@
 l.Cast;%0A
+import org.gradle.internal.concurrent.CompositeStoppable;%0A
 import o
@@ -1280,16 +1280,281 @@
 rvice;%0A%0A
+    private static class NoOpFileWatcherService implements FileWatcherService %7B%0A        @Override%0A        public Stoppable watch(FileWatchInputs inputs, Runnable callback) throws IOException %7B%0A            return CompositeStoppable.NO_OP_STOPPABLE;%0A        %7D%0A    %7D%0A%0A
     publ
@@ -2596,99 +2596,101 @@
     
-throw new RuntimeException(%22FileWatcherService is only available on Java 7 compatible JVMs%22
+// TODO: Maybe we'll eventually support Java 6%0A            return new NoOpFileWatcherService(
 );%0A 

Add no-op file watcher for jdk6