import java.lang.reflect.*; class Launcher { public static void main(String[] args) throws Throwable { /* Этап 1: Сохраняем список параметров */ Args.args = args; /* Этап 2: Получаем имена связки Модуль.Команда */ if(args == null || args.length < 1) { System.err.println("uses: Launcher Module[.Command] {Argumets}"); System.exit(1); } String moduleName = args[0]; String commandName = ""; int i = args[0].indexOf('.'); if(i >= 0) { moduleName = args[0].substring(0, i); commandName = args[0].substring(i + 1); if(commandName == "BEGIN") { commandName = ""; } } /* Этап 3: Выполнение тела модуля и команды */ Class module = null; Method begin = null; Method command = null; try { module = Class.forName(moduleName); } catch(ClassNotFoundException e) { System.err.println("Module " + moduleName + " not found"); System.exit(1); } try { begin = module.getMethod("BEGIN"); } catch(Exception e) { System.err.println("Invalid module " + moduleName); System.exit(1); } try { begin.invoke(null); } catch(IllegalAccessException e) { System.err.println("Invalid module " + moduleName); System.exit(1); } catch(IllegalArgumentException e) { System.err.println("Invalid module " + moduleName); System.exit(1); } catch(InvocationTargetException e) { throw e.getTargetException(); } if(commandName == "") { return; } try { command = module.getMethod(commandName); } catch(Exception e) { System.err.println("Invalid command " + moduleName + "." + commandName); System.exit(1); } try { command.invoke(null); } catch(IllegalAccessException e) { System.err.println("Invalid command " + moduleName + "." + commandName); System.exit(1); } catch(IllegalArgumentException e) { System.err.println("Invalid command " + moduleName + "." + commandName); System.exit(1); } catch(InvocationTargetException e) { throw e.getTargetException(); } } }