Author: bpoussin Date: 2011-04-13 16:42:23 +0200 (Wed, 13 Apr 2011) New Revision: 2108 Url: http://nuiton.org/repositories/revision/nuiton-utils/2108 Log: - refactoring du code (deplacement des declarations de classe a la fin, pour tomber des le debut sur ApplicationConfig et pas autre chose) - suppression des generics qui ne servent a rien sur loadDefaultOptions et loadActions - parse retourne this pour permettre ApplicationConfig conf = new ApplicationConfig().parse(args); - quelques methodes protected rendu public (installSaveUserAction, loadDefaultOptions, loadActions, setDefaultOption) - ajout d'un contructeur qui prend tout un tas arguments pour permettre la configuration de la config a la construction Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2011-04-11 12:49:16 UTC (rev 2107) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/ApplicationConfig.java 2011-04-13 14:42:23 UTC (rev 2108) @@ -384,323 +384,6 @@ protected PropertyChangeSupport pcs = new PropertyChangeSupport(this); /** - * Load default options of enum pass in param (enum must extend {@link OptionDef}) - * - * @param optionClass to load - * @param <O> type of enum extend {@link OptionDef} - */ - protected <O extends OptionDef> void loadDefaultOptions(Class<O> optionClass) { - - // load default option (included configuration file name : important) - for (O o : optionClass.getEnumConstants()) { - if (o.getDefaultValue() != null) { - setDefaultOption(o.getKey(), o.getDefaultValue()); - } - } - } - - /** - * Load actions of enum pass in param (enum must extend {@link ActionDef}) - * - * @param actionClass to load - * @param <A> type of enum extend {@link ActionDef} - */ - protected <A extends ActionDef> void loadActions(Class<A> actionClass) { - - // load actions - for (A a : actionClass.getEnumConstants()) { - for (String alias : a.getAliases()) { - addActionAlias(alias, a.getAction()); - } - } - } - - /** - * Action to save user configuration. - * <p/> - * Add it as a listener of the configuration for a given property. - * - * <b>Note:</b> Will not save if {@link #isAdjusting()} is {@code true}. - * - * @since 1.3 - */ - private final PropertyChangeListener saveUserAction = - new PropertyChangeListener() { - - @Override - public void propertyChange(PropertyChangeEvent evt) { - if (isAdjusting()) { - if (log.isDebugEnabled()) { - log.debug("Skip save while adjusting"); - } - return; - } - if (log.isDebugEnabled()) { - log.debug("Saving configuration fired by property [" + - evt.getPropertyName() + "] at " + - new java.util.Date()); - } - saveForUser(); - } - }; - - /** - * Le contrat de marquage des options, on utilise cette interface pour - * caracteriser une option de configuration. - * - * @since 1.0.0-rc-9 - */ - public interface OptionDef extends Serializable { - - /** - * @return la clef identifiant l'option - */ - String getKey(); - - /** - * @return le type de l'option - */ - Class<?> getType(); - - /** - * @return la clef i18n de description de l'option - */ - String getDescription(); - - /** - * @return la valeur par defaut de l'option sous forme de chaine de - * caracteres - */ - String getDefaultValue(); - - /** - * @return <code>true</code> si l'option ne peut etre sauvegardee sur - * disque (utile par exemple pour les mots de passe, ...) - */ - boolean isTransient(); - - /** - * @return <code>true</code> si l'option n'est pas modifiable (utilise - * par exemple pour la version de l'application, ...) - */ - boolean isFinal(); - - /** - * Changes the default value of the option. - * - * @param defaultValue the new default value of the option - */ - void setDefaultValue(String defaultValue); - - /** - * Changes the transient state of the option. - * - * @param isTransient the new value of the transient state - */ - void setTransient(boolean isTransient); - - /** - * Changes the final state of the option. - * - * @param isFinal the new transient state value - */ - void setFinal(boolean isFinal); - } - - /** - * Le contrat de marquage des action, on utilise cette interface pour - * caracteriser une action. - * - * @author sletellier - * @since 1.5.2 - */ - public interface ActionDef extends Serializable { - - /** - * @return action to run - */ - String getAction(); - - /** - * @return aliases used to execute action - */ - String[] getAliases(); - } - - /** - * Defines a runtime action to be launched via the {@link #doAction()} - * method. - * - * @author poussin - */ - static public class Action { - - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.METHOD) - public @interface Step { - - int value() default 0; - } - protected int step; - protected Object o; - protected Method m; - protected String[] params; - - public Action(int step, Object o, Method m, String... params) { - this.step = step; - this.o = o; - this.m = m; - this.params = params; - } - - public void doAction() throws IllegalAccessException, - IllegalArgumentException, - InvocationTargetException, - InstantiationException { - ObjectUtil.call(o, m, params); - } - } - - /** - * Item used for cacheOption - * - * @param <T> - */ - static protected class CacheItem<T> { - - /** typed option value */ - public T item; - /** hash of string representation */ - public int hash; - - public CacheItem(T item, int hash) { - this.item = item; - this.hash = hash; - } - } - - static public class OptionList { - - protected ApplicationConfig config; - protected String key; - protected String value; - - public OptionList(ApplicationConfig config, String key, String value) { - this.config = config; - this.key = key; - this.value = value; - } - - protected <T> List<T> convertListOption(Class<T> type) { - List<T> result= (List<T>) config.convertOption(type, key, value, true); - return result; - } - /** - * Get option value as {@link String}. - * - * @return value as String - */ - public List<String> getOption() { - List<String> result = convertListOption(String.class); - return result; - } - - /** - * Get option value as {@link File}. - * - * @return value as file - */ - public List<File> getOptionAsFile() { - List<File> tmp = convertListOption(File.class); - List<File> result = new ArrayList<File>(tmp.size()); - for (File file : tmp) { - result.add(file.getAbsoluteFile()); - } - return result; - } - - /** - * Get option value as {@link URL}. - * - * @return value as URL - */ - public List<URL> getOptionAsURL() { - List<URL> result = convertListOption(URL.class); - return result; - } - - /** - * Get option value as {@link Class}. - * - * @return value as Class - */ - public List<Class> getOptionAsClass() { - List<Class> result = convertListOption(Class.class); - return result; - } - - /** - * Get option value as {@link Date}. - * - * @return value as Date - */ - public List<Date> getOptionAsDate() { - List<Date> result = convertListOption(Date.class); - return result; - } - - /** - * Get option value as {@link Time}. - * - * @return value as Time - */ - public List<Time> getOptionAsTime() { - List<Time> result = convertListOption(Time.class); - return result; - } - - /** - * Get option value as {@link Timestamp}. - * - * @return value as Timestamp - */ - public List<Timestamp> getOptionAsTimestamp() { - List<Timestamp> result = convertListOption(Timestamp.class); - return result; - } - - /** - * Get option value as {@code int}. - * - * @return value as {@code int} - */ - public List<Integer> getOptionAsInt() { - List<Integer> result = convertListOption(Integer.class); - return result; - } - - /** - * Get option value as {@code double}. - * - * @return value as {@code double} - */ - public List<Double> getOptionAsDouble() { - List<Double> result = convertListOption(Double.class); - return result; - } - - /** - * Get option value as {@code boolean}. - * - * @return value as {@code boolean}. - */ - public List<Boolean> getOptionAsBoolean() { - List<Boolean> result = convertListOption(Boolean.class); - return result; - } - } - - /** * Init ApplicationConfig with current simple class name as config file. * * Also init converters. @@ -717,15 +400,12 @@ } /** - * Create WikittyConfig and load particular configuration filename, parse - * method is automaticaly called + * Create WikittyConfig and load particular configuration filename * - * @param configFilename name of wikitty config file - * @throws ArgumentsParserException + * @param configFilename name of config to use */ public ApplicationConfig(String configFilename) throws ArgumentsParserException { - this(); - parse("--option", CONFIG_FILE_NAME, configFilename); + this(null, null, null, configFilename); } /** @@ -737,12 +417,42 @@ * @see ConverterUtil#initConverters() */ public ApplicationConfig(Properties defaults) { + this(null, null, defaults, null); + } + + /** + * All in one, this constructor allow to pass all necessary argument to + * initialise ApplicationConfig and parse command line + * + * @param optionClass class that describe option, can be null + * @param actionClass class that describe action, can be null + * @param defaults properties that override default value of optionClass, can be null + * @param configFilename override default config filename, can be null + * @param args command line argument can be empty + */ + public ApplicationConfig( + Class<OptionDef> optionClass, Class<ActionDef> actionClass, + Properties defaults, String configFilename) { this(); - // iterate with Properties method and not with Hashtable method to - // prevent missed value with chained Properties object - for (String key : defaults.stringPropertyNames()) { - setDefaultOption(key, defaults.getProperty(key)); + if (optionClass != null) { + loadDefaultOptions(optionClass); } + if (actionClass != null) { + loadActions(actionClass); + } + + if (defaults != null) { + // iterate with Properties method and not with Hashtable method to + // prevent missed value with chained Properties object + for (String key : defaults.stringPropertyNames()) { + setDefaultOption(key, defaults.getProperty(key)); + } + } + + if (configFilename != null) { + setDefaultOption(CONFIG_FILE_NAME, configFilename); + } + } /** @@ -764,13 +474,45 @@ } /** + * Load default options of enum pass in param (enum must extend {@link OptionDef}) + * + * @param optionClass to load + * @param <O> type of enum extend {@link OptionDef} + */ + public void loadDefaultOptions(Class<OptionDef> optionClass) { + + // load default option (included configuration file name : important) + for (OptionDef o : optionClass.getEnumConstants()) { + if (o.getDefaultValue() != null) { + setDefaultOption(o.getKey(), o.getDefaultValue()); + } + } + } + + /** + * Load actions of enum pass in param (enum must extend {@link ActionDef}) + * + * @param actionClass to load + * @param <A> type of enum extend {@link ActionDef} + */ + public void loadActions(Class<ActionDef> actionClass) { + + // load actions + for (ActionDef a : actionClass.getEnumConstants()) { + for (String alias : a.getAliases()) { + addActionAlias(alias, a.getAction()); + } + } + } + + /** * Used to put default configuration option in config option. Those options * are used as fallback value. * * @param key default property key * @param value default property value */ - protected void setDefaultOption(String key, String value) { + public void setDefaultOption(String key, String value) { defaults.setProperty(key, value); } @@ -1754,7 +1496,7 @@ * @param args argument as main(String[] args) * @throws ArgumentsParserException if parsing failed */ - public void parse(String... args) throws ArgumentsParserException { + public ApplicationConfig parse(String... args) throws ArgumentsParserException { if (args == null) { args = ArrayUtil.EMPTY_STRING_ARRAY; } @@ -1941,6 +1683,7 @@ } throw new ArgumentsParserException("Can't parse argument", eee); } + return this; } /** @@ -2053,4 +1796,298 @@ public PropertyChangeListener[] getPropertyChangeListeners() { return pcs.getPropertyChangeListeners(); } + + + /////////////////////////////////////////////////////////////////////////// + // + // C L A S S E S D E C L A R A T I O N + // + /////////////////////////////////////////////////////////////////////////// + + + /** + * Action to save user configuration. + * <p/> + * Add it as a listener of the configuration for a given property. + * + * <b>Note:</b> Will not save if {@link #isAdjusting()} is {@code true}. + * + * @since 1.3 + */ + private final PropertyChangeListener saveUserAction = + new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (isAdjusting()) { + if (log.isDebugEnabled()) { + log.debug("Skip save while adjusting"); + } + return; + } + if (log.isDebugEnabled()) { + log.debug("Saving configuration fired by property [" + + evt.getPropertyName() + "] at " + + new java.util.Date()); + } + saveForUser(); + } + }; + + /** + * Le contrat de marquage des options, on utilise cette interface pour + * caracteriser une option de configuration. + * + * @since 1.0.0-rc-9 + */ + public interface OptionDef extends Serializable { + + /** + * @return la clef identifiant l'option + */ + String getKey(); + + /** + * @return le type de l'option + */ + Class<?> getType(); + + /** + * @return la clef i18n de description de l'option + */ + String getDescription(); + + /** + * @return la valeur par defaut de l'option sous forme de chaine de + * caracteres + */ + String getDefaultValue(); + + /** + * @return <code>true</code> si l'option ne peut etre sauvegardee sur + * disque (utile par exemple pour les mots de passe, ...) + */ + boolean isTransient(); + + /** + * @return <code>true</code> si l'option n'est pas modifiable (utilise + * par exemple pour la version de l'application, ...) + */ + boolean isFinal(); + + /** + * Changes the default value of the option. + * + * @param defaultValue the new default value of the option + */ + void setDefaultValue(String defaultValue); + + /** + * Changes the transient state of the option. + * + * @param isTransient the new value of the transient state + */ + void setTransient(boolean isTransient); + + /** + * Changes the final state of the option. + * + * @param isFinal the new transient state value + */ + void setFinal(boolean isFinal); + } + + /** + * Le contrat de marquage des action, on utilise cette interface pour + * caracteriser une action. + * + * @author sletellier + * @since 1.5.2 + */ + public interface ActionDef extends Serializable { + + /** + * @return action to run + */ + String getAction(); + + /** + * @return aliases used to execute action + */ + String[] getAliases(); + } + + /** + * Defines a runtime action to be launched via the {@link #doAction()} + * method. + * + * @author poussin + */ + static public class Action { + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public @interface Step { + + int value() default 0; + } + protected int step; + protected Object o; + protected Method m; + protected String[] params; + + public Action(int step, Object o, Method m, String... params) { + this.step = step; + this.o = o; + this.m = m; + this.params = params; + } + + public void doAction() throws IllegalAccessException, + IllegalArgumentException, + InvocationTargetException, + InstantiationException { + ObjectUtil.call(o, m, params); + } + } + + /** + * Item used for cacheOption + * + * @param <T> + */ + static protected class CacheItem<T> { + + /** typed option value */ + public T item; + /** hash of string representation */ + public int hash; + + public CacheItem(T item, int hash) { + this.item = item; + this.hash = hash; + } + } + + static public class OptionList { + + protected ApplicationConfig config; + protected String key; + protected String value; + + public OptionList(ApplicationConfig config, String key, String value) { + this.config = config; + this.key = key; + this.value = value; + } + + protected <T> List<T> convertListOption(Class<T> type) { + List<T> result= (List<T>) config.convertOption(type, key, value, true); + return result; + } + /** + * Get option value as {@link String}. + * + * @return value as String + */ + public List<String> getOption() { + List<String> result = convertListOption(String.class); + return result; + } + + /** + * Get option value as {@link File}. + * + * @return value as file + */ + public List<File> getOptionAsFile() { + List<File> tmp = convertListOption(File.class); + List<File> result = new ArrayList<File>(tmp.size()); + for (File file : tmp) { + result.add(file.getAbsoluteFile()); + } + return result; + } + + /** + * Get option value as {@link URL}. + * + * @return value as URL + */ + public List<URL> getOptionAsURL() { + List<URL> result = convertListOption(URL.class); + return result; + } + + /** + * Get option value as {@link Class}. + * + * @return value as Class + */ + public List<Class> getOptionAsClass() { + List<Class> result = convertListOption(Class.class); + return result; + } + + /** + * Get option value as {@link Date}. + * + * @return value as Date + */ + public List<Date> getOptionAsDate() { + List<Date> result = convertListOption(Date.class); + return result; + } + + /** + * Get option value as {@link Time}. + * + * @return value as Time + */ + public List<Time> getOptionAsTime() { + List<Time> result = convertListOption(Time.class); + return result; + } + + /** + * Get option value as {@link Timestamp}. + * + * @return value as Timestamp + */ + public List<Timestamp> getOptionAsTimestamp() { + List<Timestamp> result = convertListOption(Timestamp.class); + return result; + } + + /** + * Get option value as {@code int}. + * + * @return value as {@code int} + */ + public List<Integer> getOptionAsInt() { + List<Integer> result = convertListOption(Integer.class); + return result; + } + + /** + * Get option value as {@code double}. + * + * @return value as {@code double} + */ + public List<Double> getOptionAsDouble() { + List<Double> result = convertListOption(Double.class); + return result; + } + + /** + * Get option value as {@code boolean}. + * + * @return value as {@code boolean}. + */ + public List<Boolean> getOptionAsBoolean() { + List<Boolean> result = convertListOption(Boolean.class); + return result; + } + } + }
participants (1)
-
bpoussin@users.nuiton.org