001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.plugin.jdbc.feeder;
017
018import java.io.InputStreamReader;
019import java.io.OutputStreamWriter;
020
021/**
022 * Stub for loading a Java class and execute it. Isolating the main method from other methods is of advantage
023 * for JVMs that thoroughly check if the class can be executed.
024 */
025public class Runner {
026
027    public static void main(String[] args) {
028        try {
029            // here we load the "concrete" class we want to execute. Add shutdown hook and pass stdin/stdio/stderr.
030            Class clazz = Class.forName(args[0]);
031            CommandLineInterpreter commandLineInterpreter = (CommandLineInterpreter) clazz.newInstance();
032            Runtime.getRuntime().addShutdownHook(commandLineInterpreter.shutdownHook());
033            commandLineInterpreter.readFrom(new InputStreamReader(System.in, "UTF-8"))
034                    .writeTo(new OutputStreamWriter(System.out, "UTF-8"))
035                    .errorsTo(System.err)
036                    .start();
037        } catch (Throwable e) {
038            // ensure fatal errors are printed to stderr
039            e.printStackTrace();
040            System.exit(1);
041        }
042        System.exit(0);
043    }
044}