From: Web H. <web...@us...> - 2004-08-10 10:49:16
|
Update of /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14094/modules/core/src/com/babeldoc/core Modified Files: BabeldocCommand.java Main.java Log Message: Modified entry points and supporting files to allow programmatically starting and stopping Babeldoc from within the same JVM. Some problems still exist when restarting, the PipelineStage method getOptions(...) return null, under investigation. Regular method of invocation via babeldoc script files is unchanged. Added some optimisations to method of invocation of "main" methodsvia reflection, now istead of looping thru all available methods, directly retrieve the method of interest. Index: BabeldocCommand.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/BabeldocCommand.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** BabeldocCommand.java 30 Jul 2004 01:32:59 -0000 1.20 --- BabeldocCommand.java 10 Aug 2004 10:49:06 -0000 1.21 *************** *** 195,199 **** * Stopping - execute all necessary shutdown processing. */ ! public void finishUp() { } --- 195,199 ---- * Stopping - execute all necessary shutdown processing. */ ! public void stop() { } *************** *** 295,299 **** public void run() { LogService.getInstance().logDebug(I18n.get("001001")); ! command.finishUp(); } } --- 295,299 ---- public void run() { LogService.getInstance().logDebug(I18n.get("001001")); ! command.stop(); } } Index: Main.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/core/src/com/babeldoc/core/Main.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Main.java 30 Jul 2004 01:32:59 -0000 1.11 --- Main.java 10 Aug 2004 10:49:06 -0000 1.12 *************** *** 75,78 **** --- 75,80 ---- import java.util.Set; import java.util.Map; + import java.util.List; + import java.util.ArrayList; *************** *** 89,194 **** * @version 1.0 */ ! public class Main { ! /** Service prefix for commands.*/ ! public static final String SERVICE_PREFIX = "Command."; ! /** ! * <p>This method initializes the environment by loading the ! * <code>System.properties</code> from the env/configuration. ! * It then takes the first argument on the command line and ! * finds the service that corresponds to it. It then calls ! * that service. ! * ! * @param args command line arguments ! */ ! public static void handleCommand(String[] args) { ! //load env file. It is necessary for logger configuration ! EnvironmentLoader.loadEnvironment(); - if (args.length > 0) { - String serviceName = args[0]; ! try { ! Class cls = ServiceFactory.getServiceClass(SERVICE_PREFIX + ! serviceName); ! Method[] methods = cls.getDeclaredMethods(); ! boolean hasMain = false; ! for (int i = 0; i < methods.length; ++i) { ! if ("main".equals(methods[i].getName())) { ! Method method = methods[i]; ! Object[] methodArgs = new Object[] { args }; ! try { ! hasMain = true; ! method.invoke(cls, methodArgs); ! return; ! } catch (IllegalAccessException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } catch (IllegalArgumentException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } catch (InvocationTargetException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } ! break; ! } ! } ! if (!hasMain) { ! System.out.println(I18n.get("core.Main.noMainMethod", cls.getName())); ! } ! //throw new ServiceException(); ! } catch (ServiceException e) { ! System.out.println(I18n.get("001011", serviceName)); ! System.out.println(I18n.get("001012")); ! listCommandServices(); ! System.out.println(""); ! } ! } else { ! System.out.println(I18n.get("001012")); ! listCommandServices(); ! String version = ConfigService.getString("env/build", "babeldoc_version"); ! System.out.println("\n\n" + I18n.get("001006", version)); ! System.out.println(I18n.get("001007")); ! System.out.println(I18n.get("001008")); ! System.out.println(I18n.get("001009")); } ! } ! /** ! * List all the command services. ! */ ! public static void listCommandServices() { ! Map services = ServiceFactory.getAllServices(SERVICE_PREFIX); ! if (services != null) { ! Set keys = services.keySet(); ! Object[] objectArray = keys.toArray(); - for (int i = 0; i < objectArray.length; i++) { - if (i != (objectArray.length - 1)) { - System.out.print(objectArray[i] + ", "); - } else { - System.out.print("or "+objectArray[i] + "."); - } - } } ! } ! ! /** ! * The main method of babeldoc command is a little difference. If there is a ! * first argument on the commandline, it will try and load this from the ! * service catalog. Using reflection it will attempt to forward execution to ! * the main method of that class. By this means you can get babeldoc ! * command to run any other command in babeldoc. ! * ! * @param args command line arguments ! */ ! public static void main(String[] args) { ! handleCommand(args); ! } } --- 91,218 ---- * @version 1.0 */ ! public class Main implements Runnable { ! /** Service prefix for commands.*/ ! public static final String SERVICE_PREFIX = "Command."; ! private String[] args; ! private Thread thread; ! private List commandList; ! /** ! * Creates a new <code>Main</code> instance. ! * This is usefull for classes which need to programatically launch and stop ! * Babeldoc. ! * ! */ ! public Main(String[] args) { ! this.args = args; ! commandList = new ArrayList(); ! thread = new Thread(this); ! thread.start(); ! } ! public void run() { ! commandList.add(handleCommand(args)); ! } ! public void interrupt() { ! thread.interrupt(); ! // interrupt any commands that were started as well ! BabeldocCommand command; ! for (int i = 0; i < commandList.size(); i++) { ! command = (BabeldocCommand)commandList.get(i); ! command.stop(); ! } ! } ! /** ! * <p>This method initializes the environment by loading the ! * <code>System.properties</code> from the env/configuration. ! * It then takes the first argument on the command line and ! * finds the service that corresponds to it. It then calls ! * that service. ! * ! * @param args command line arguments ! */ ! public static BabeldocCommand handleCommand(String[] args) { ! BabeldocCommand command = null; ! //load env file. It is necessary for logger configuration ! EnvironmentLoader.loadEnvironment(); ! if (args.length > 0) { ! String serviceName = args[0]; ! try { ! Class cls = ServiceFactory.getServiceClass(SERVICE_PREFIX + ! serviceName); ! try { ! Method method = cls.getDeclaredMethod("main",new Class[]{String[].class}); ! Object[] methodArgs = new Object[] { args }; ! ! command = (BabeldocCommand)method.invoke(cls, methodArgs); ! ! return command; ! } catch (IllegalAccessException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } catch (NoSuchMethodException e) { ! System.out.println(I18n.get("core.Main.noMainMethod", cls.getName())); ! com.babeldoc.core.LogService.getInstance().logError(e); ! } catch (IllegalArgumentException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } catch (InvocationTargetException e) { ! com.babeldoc.core.LogService.getInstance().logError(e); ! } ! //throw new ServiceException(); ! } catch (ServiceException e) { ! System.out.println(I18n.get("001011", serviceName)); ! System.out.println(I18n.get("001012")); ! listCommandServices(); ! System.out.println(""); ! } ! } else { ! System.out.println(I18n.get("001012")); ! listCommandServices(); ! String version = ConfigService.getString("env/build", "babeldoc_version"); ! System.out.println("\n\n" + I18n.get("001006", version)); ! System.out.println(I18n.get("001007")); ! System.out.println(I18n.get("001008")); ! System.out.println(I18n.get("001009")); } ! return command; ! } ! /** ! * List all the command services. ! */ ! public static void listCommandServices() { ! Map services = ServiceFactory.getAllServices(SERVICE_PREFIX); ! if (services != null) { ! Set keys = services.keySet(); ! Object[] objectArray = keys.toArray(); ! for (int i = 0; i < objectArray.length; i++) { ! if (i != (objectArray.length - 1)) { ! System.out.print(objectArray[i] + ", "); ! } else { ! System.out.print("or "+objectArray[i] + "."); ! } ! } ! } } ! /** ! * The main method of babeldoc command is a little difference. If there is a ! * first argument on the commandline, it will try and load this from the ! * service catalog. Using reflection it will attempt to forward execution to ! * the main method of that class. By this means you can get babeldoc ! * command to run any other command in babeldoc. ! * ! * @param args command line arguments ! */ ! public static void main(String[] args) { ! handleCommand(args); ! } } |