Index: lutinutil/src/java/org/codelutin/util/OptionActionManager.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/OptionActionManager.java:1.1 --- /dev/null Fri Dec 14 18:47:57 2007 +++ lutinutil/src/java/org/codelutin/util/OptionActionManager.java Fri Dec 14 18:47:50 2007 @@ -0,0 +1,193 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Benjamin Poussin, Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.util; + + +import org.apache.commons.logging.LogFactory; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A simple manager of actions for Option. + *

+ * To use this type of manager extends it in a new class, add to the class some + *

+ * public static class implementation of OptionAction named extacly as + * OPTIONKEY_Action. + *

+ * Example : (Option_XXX extends Option) + *

+ * public class MyOptionActionManager extends OptionActionManager {
+ *     public static class XXX_Action implements OptionAction<Option_XXX> {
+ *         public void run(OptionParser parser, Option_XXX option) throws Exception {
+ *             System.out.println("Help!");
+ *             System.exit(0);
+ *         }
+ *     }
+ * } 
+ * When using method {@link #registerActions(OptionParser,Class,boolean)}, the {@link #cache} + * dictionnary will to be filled with all action class deinfed in the target + * class corresponding with found option's in parser. + *

+ * To execute actions, use method {@link #fireActions(OptionParser, String[])} + * with a {@link OptionParser} filled after a parse and a list of option you + * want associated action to be fired. + * + * @author tony + */ +public class OptionActionManager { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + protected static org.apache.commons.logging.Log log = LogFactory.getLog(OptionActionManager.class); + + /** the shared instance of manager. */ + private static OptionActionManager instance; + + public static OptionActionManager getInstance() { + if (instance == null) { + instance = new OptionActionManager(); + } + return instance; + } + + /** the map of known class of action associated to a option key. */ + protected Map>> cache; + + /** + * Register the action's implementation class found in + * targetClass if the option associated to the action was also + * found in parser. + * + * @param parser the parse used for parsing with some options parsed + * @param targetClass the class that contains the actions implementation + * @param safe flag to safe throw exception if action already registred. + */ + public void registerActions(OptionParser parser, Class targetClass, boolean safe) { + Map> options = parser.getAcceptedOptions(); + Map>> map = getActionClasses(targetClass); + for (Map.Entry> entry : options.entrySet()) { + String optionName = entry.getKey(); + if (!parser.isOptionEnabled(optionName)) { + continue; + } + String className = StringUtil.convertToConstantName(optionName) + "_Action"; + Class> optionClass = map.get(className); + Class> oldAction = checkNonRegistredAction(optionName, safe); + if (oldAction == null) { + log.info(optionName + " --> " + optionClass); + getCache().put(optionName, optionClass); + } + } + } + + /** + * Launch each option's command passed via options parameter, + * with no arguments only if the option was found in command line while + * importOptions process. + * + * @param parser parser to use + * @param options the list of options we want to process + * @throws Exception if any + */ + public void fireActions(OptionParser parser, String... options) throws Exception { + log.debug(options); + for (String optionName : options) { + if (!parser.isOptionEnabled(optionName)) { + continue; + } + Class> actionClass = checkRegistredAction(optionName); + try { + OptionAction action; + action = actionClass.newInstance(); + Option[] list = parser.getOptions(optionName); + fireAction(parser, action, list); + } catch (InstantiationException e) { + // should never appears + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + // should never appears + throw new RuntimeException(e); + } + } + } + + public void fireAction(OptionParser parser, OptionAction action, Option... list) throws Exception { + + // see if we have a multi-action + if (action instanceof MultiOptionAction) { + MultiOptionAction