/*
 * The MIT License
 *
 * Copyright (c) 2014 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.github.olivergondza.dumpling.cli;

import groovy.lang.Binding;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;

import javax.annotation.Nonnull;

import com.github.olivergondza.dumpling.factory.JmxRuntimeFactory;
import com.github.olivergondza.dumpling.factory.JvmRuntimeFactory;
import com.github.olivergondza.dumpling.factory.PidRuntimeFactory;
import com.github.olivergondza.dumpling.factory.ThreadDumpFactory;
import com.github.olivergondza.dumpling.model.ProcessRuntime;

/*package*/ class GroovyInterpretterConfig {

    /*package*/ Collection<String> getStarImports() {
        return Arrays.asList(
                "com.github.olivergondza.dumpling.cli",
                "com.github.olivergondza.dumpling.factory",
                "com.github.olivergondza.dumpling.model",
                "com.github.olivergondza.dumpling.query"
        );
    }

    /*package*/ Collection<String> getStaticStars() {
        return Arrays.asList("com.github.olivergondza.dumpling.model.ProcessThread");
    }

    /**
     * Default binding to be used in groovy interpreters.
     *
     * Dumpling exposed API is available via <tt>D</tt> property.
     */
    /*package*/ Binding getDefaultBinding(@Nonnull ProcessStream stream) {
        Binding binding = new Binding();
        binding.setProperty("out", stream.out());
        binding.setProperty("err", stream.err());

        binding.setProperty("D", new CliApiEntryPoint(ProcessStream.system()));

        binding.setProperty("load", new Load(stream)); // Compatibility
        binding.setProperty("$load", new LoadCommand(stream)); // Compatibility

        return binding;
    }

    /**
     * @deprecated Use <tt>D.load.threaddump(String)</tt> instead.
     */
    @Deprecated
    @SuppressWarnings("unused")
    private static final class Load {
        private ProcessStream stream;

        public Load(ProcessStream stream) {
            this.stream = stream;
        }

        public ProcessRuntime call(String filename) throws Exception {
            stream.err().println("load(String) command is deprecated. Use 'D.load.threaddump(String)' instead.");
            return new ThreadDumpFactory().fromFile(new File(filename));
        }
    }

    @SuppressWarnings("unused")
    private static final class CliApiEntryPoint {

        private final @Nonnull ProcessStream streams;

        public CliApiEntryPoint(@Nonnull ProcessStream streams) {
            this.streams = streams;
        }

        public @Nonnull LoadCommand getLoad() {
            return new LoadCommand(streams);
        }
    }

    @SuppressWarnings("unused")
    private static final class LoadCommand {

        private final @Nonnull ProcessStream streams;

        public LoadCommand(@Nonnull ProcessStream streams) {
            this.streams = streams;
        }

        public ProcessRuntime threaddump(@Nonnull String filename) throws Exception {
            return new ThreadDumpFactory().fromFile(new File(filename));
        }

        public ProcessRuntime process(int pid ) throws Exception {
            return new PidRuntimeFactory().fromProcess(pid);
        }

        public ProcessRuntime jmx(int pid) throws Exception {
            return new JmxRuntimeFactory().forLocalProcess(pid);
        }

        public ProcessRuntime jmx(@Nonnull String connection) throws Exception {
            return new JmxRuntimeFactory().createRuntime(connection, streams);
        }

        public ProcessRuntime getJvm() {
            return new JvmRuntimeFactory().currentRuntime();
        }
    }
}

@@ -1230,16 +1230,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -3437,39 +3437,43 @@
 nused%22)%0A    
-private
+/*package*/
  static 
 final class 
@@ -3456,30 +3456,24 @@
 ge*/ static 
-final 
 class CliApi
@@ -4071,32 +4071,34 @@
 ilename) throws 
+IO
 Exception %7B%0A    
@@ -4085,32 +4085,32 @@
 s IOException %7B%0A
-
             retu
@@ -4222,17 +4222,16 @@
 (int pid
- 
 ) throws
@@ -4223,32 +4223,56 @@
 int pid) throws 
+IOException, Interrupted
 Exception %7B%0A    
@@ -4381,33 +4381,16 @@
 int pid)
- throws Exception
  %7B%0A     
@@ -4452,32 +4452,32 @@
 id);%0A        %7D%0A%0A
+
         public P
@@ -4521,33 +4521,16 @@
 nection)
- throws Exception
  %7B%0A     

Fix declared exceptions for groovy load API