package com.tkb.pandora.exec;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * An entry executable classes dispatcher.
 *
 * Run as: mvn exec:java -Dexec.mainClass="com.tkb.pandora.exec.Dispatcher" -Dexec.args="entry path/to/config.properties"
 * Run as: java -jar pandora.jar 'entry' 'configs/file.properties'
 *
 * @author Akis Papadopoulos
 */
public class Dispatcher {

    // Eecutable entries map
    private static final Map<String, Class<?>> entries;

    static {
        // Setting up entries
        entries = new HashMap<String, Class<?>>();

        entries.put("extract", Extractor.class);
        entries.put("sample", Sampler.class);
        entries.put("cluster", Clusterer.class);
        entries.put("build", Builder.class);
        entries.put("project", Projector.class);
        entries.put("reduce", Reducer.class);
        entries.put("index", Indexer.class);
    }

    public static void main(String[] args) throws Exception {
        // Printing the license notice
        System.out.println("\nCopyright 2016 Akis Papadopoulos, https://github.com/tzeikob/pandora\n");
        System.out.println("Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this");
        System.out.println("file except in compliance with the License. You may obtain a copy of the License at \n");
        System.out.println("http://www.apache.org/licenses/LICENSE-2.0 \n");
        System.out.println("Unless required by applicable law or agreed to in writing, software distributed under the");
        System.out.println("License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,");
        System.out.println("either express or implied. See the License for the specific language governing permissions");
        System.out.println("and limitations under the License.\n");

        if (args.length == 1 && args[0].equalsIgnoreCase("help")) {
            System.out.println("Pandora's Help Guide");
            System.out.println("Please run one of the available tasks: ");

            for (String key : entries.keySet()) {
                System.out.println(" java -jar pandora.jar " + key + " config/properties");
            }

            System.exit(1);
        } else if (args.length == 2) {
            final Class<?> entry = entries.get(args[0]);

            if (entry != null) {
                // Run the choosen entry given the rest of the arguments
                final Object arguments = Arrays.copyOfRange(args, 1, args.length);

                // Making sure all entries implement a main static method
                entry.getMethod("main", String[].class).invoke(null, arguments);
            } else {
                System.out.println("Unable to run, entry '" + args[0] + "' not found");
                System.out.println("Please run help: ");
                System.out.println(" java -jar pandora.jar help");

                System.exit(1);
            }
        } else {
            System.out.println("Unable to run, none or too many arguments found");
            System.out.println("Please run help: ");
            System.out.println(" java -jar pandora.jar help");

            System.exit(1);
        }
    }
}

@@ -24,16 +24,102 @@
 .exec;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0A
 import j
@@ -309,16 +309,19 @@
 patcher%22
+%0A *
  -Dexec.
@@ -358,19 +358,16 @@
 perties%22
-%0A *
  Run as:
@@ -388,16 +388,19 @@
 dora.jar
+%0A *
  'entry'
@@ -1119,661 +1119,264 @@
     
-System.out.println(%22%5CnCopyright 2016 Akis Papadopoulos, https://github.com/tzeikob/pandora%5Cn%22);%0A        System.out.println(%22Licensed under the Apache License, Version 2.0 (the %5C%22License%5C%22); you may not use this%22);%0A        System.out.println(%22file except in compliance with the License. You may obtain a copy of the License at %5Cn%22);%0A        System.out.println(%22http://www.apache.org/licenses/LICENSE-2.0 %5Cn%22);%0A        System.out.println(%22Unless required by applicable law or agreed to in writing, software distributed under the%22);%0A        System.out.println(%22License is distributed on an %5C%22AS IS%5C%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,%22);%0A
+try (BufferedReader br = new BufferedReader(new FileReader(%22LICENSE%22))) %7B%0A            String line = null;%0A%0A            while ((line = br.readLine()) != null) %7B%0A                System.out.println(line);%0A            %7D%0A        %7D catch (IOException exc) %7B%0A    
     
@@ -1378,35 +1378,35 @@
          System.
-out
+err
 .println(%22either
@@ -1403,98 +1403,57 @@
 ln(%22
-either express or implied. See the License for the specific language governing permissions
+Error: Application aborted, missing LICENSE file.
 %22);%0A
@@ -1464,67 +1464,37 @@
     
+ 
+   
 System.
-out.println(%22and limitations under the License.%5Cn%22);
+exit(1);%0A        %7D
 %0A%0A  

Print license note file at start up.