Index: lutincommandline/src/java/org/codelutin/option/AbstractContext.java diff -u lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.4 lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.5 --- lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.4 Mon Mar 17 22:39:33 2008 +++ lutincommandline/src/java/org/codelutin/option/AbstractContext.java Tue Mar 18 23:40:47 2008 @@ -17,6 +17,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.util.ReflectUtil; +import org.codelutin.util.StringUtil; import java.io.BufferedOutputStream; import java.io.File; @@ -46,6 +47,9 @@ /** flag pour indiquer si 'lon veut quitter l'appli après les options */ protected boolean quit; + /** flag to say use ui */ + protected boolean useUI = true; + /** flag pour indiquer si l'on affiche ou non l'ui principale */ protected boolean launchUI = true; @@ -66,7 +70,11 @@ /** les configs */ protected final Config[] configs; - protected AbstractContext(Class

parserClass) throws Exception { + /** le timestamp de démarrage de l'application */ + protected final long startingTime; + + protected AbstractContext(Class

parserClass, Class... configs) throws Exception { + startingTime = System.nanoTime(); String home = System.getProperty("user.home"); if (home == null) { // this is a serious fatal error @@ -82,6 +90,9 @@ keyList.add(configKey); } this.configKeys = Collections.unmodifiableList(keyList); + for (Class config : configs) { + registerConfig(config); + } this.configs = new Config[this.configKeys.size()]; int i = 0; for (ConfigKey configKey : getConfigKeys()) { @@ -89,14 +100,38 @@ } } + protected AbstractContext(Class

parserClass) throws Exception { + startingTime = System.nanoTime(); + String home = System.getProperty("user.home"); + if (home == null) { + // this is a serious fatal error + throw new RuntimeException("lutinutil.error.user.home"); + } + this.home = new File(home); + + this.parser = parserClass.newInstance(); - public void init(String[] args) throws Exception { + List> keyList = new ArrayList>(); + List c = ReflectUtil.getConstants(getClass(), ConfigKey.class); + for (ConfigKey configKey : c) { + keyList.add(configKey); + } + this.configKeys = Collections.unmodifiableList(keyList); + this.configs = new Config[this.configKeys.size()]; + int i = 0; + for (ConfigKey configKey : getConfigKeys()) { + this.configs[i++] = configKey.instanciateConfig(); + } + } - // parse arguments - parser.doParse(args); + public void init(String[] args) throws Exception { + if (args != null) { + // parse arguments + parser.doParse(args); - if (parser.getLastResult().getError() != null) { - throw parser.getLastResult().getError(); + if (parser.getLastResult().getError() != null) { + throw parser.getLastResult().getError(); + } } // set config file location initSource(); @@ -111,6 +146,18 @@ save(); } + public long getStartingTime() { + return startingTime; + } + + public long getElapsedTime() { + return System.nanoTime() - startingTime; + } + + public String getElapsedTimeAsString() { + return StringUtil.convertTime(System.nanoTime() - startingTime); + } + /** * Initialize all configs, simply by instanciate them. * @@ -187,6 +234,10 @@ return quit; } + public boolean isUseUI() { + return useUI; + } + /** * @return true si on doit lancer l'ui au démarrage de * l'application, après avoir traité toutes les options utilisateurs Index: lutincommandline/src/java/org/codelutin/option/OptionAction.java diff -u lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.5 lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.6 --- lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.5 Mon Mar 17 22:39:33 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionAction.java Tue Mar 18 23:40:47 2008 @@ -3,6 +3,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.lang.reflect.Method; + /** * A simple empty action for an option. *

@@ -25,6 +27,18 @@ protected final Log log = LogFactory.getLog(getClass()); + protected OptionAction() { + this(null); + } + + public OptionAction(String delegateClass) { + this.delegateClass = delegateClass; + if (delegateClass == null) { + this.delegateMethod = null; + } + + } + /** * Method to implement logic of the action for the current option. *

@@ -33,7 +47,7 @@ * * @throws Exception if any problem while action */ - protected abstract void run() throws Exception; + //protected abstract void run() throws Exception; /** the current context to be used for running. */ protected C context; @@ -41,10 +55,31 @@ /** the current option to be used for running. */ protected O option; + /** the fully qualified name of the delegate runner */ + protected final String delegateClass; + + /** the delegate run action */ + protected OptionActionRunnable delegate; + + /** the delgate method to call al delegate runner */ + protected Method delegateMethod; + + protected void run() throws Exception { + if (delegateClass == null) { + return; + } + + OptionActionRunnable runner = getDelegate(); + + Method m = getDelegateMethod(runner); + + m.invoke(runner, this); + } + public void doRun(C context, O... options) throws Exception { try { - this.context=context; - beforeAll( options); + this.context = context; + beforeAll(options); for (O option : options) { this.option = option; beforeRun(); @@ -52,10 +87,10 @@ afterRun(); this.option = null; } - afterAll( options); + afterAll(options); } finally { - this.context=null; - this.option=null; + this.context = null; + this.option = null; } } @@ -68,7 +103,7 @@ protected void afterRun() { } - protected void afterAll( O... options) throws Exception { + protected void afterAll(O... options) throws Exception { } public Option getOption() { @@ -78,4 +113,30 @@ public C getContext() { return context; } + + protected Method getDelegateMethod(OptionActionRunnable runner) { + if (delegateMethod == null) { + try { + delegateMethod = runner.getClass().getDeclaredMethod("run", OptionAction.class); + } catch (NoSuchMethodException e) { + throw new IllegalStateException("could not load delegate method run for optionAction " + delegateClass + " for reason " + e.getMessage(), e); + } + } + return delegateMethod; + } + + protected OptionActionRunnable getDelegate() { + if (delegate == null) { + try { + delegate = (OptionActionRunnable) Class.forName(delegateClass).newInstance(); + } catch (InstantiationException e) { + throw new IllegalStateException("could not instanciation optionAction " + delegateClass + " for reason " + e.getMessage(), e); + } catch (IllegalAccessException e) { + throw new IllegalStateException("could not instanciation optionAction " + delegateClass + " for reason " + e.getMessage(), e); + } catch (ClassNotFoundException e) { + throw new IllegalStateException("could not instanciation optionAction " + delegateClass + " for reason " + e.getMessage(), e); + } + } + return delegate; + } }