branch develop updated (c189a71 -> 516171f)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git from c189a71 clean code new 20d0b87 Introduce ApplicationConfigScope and ApplicationConfigInit (See #3491) new cd33ea7 Generate application config with both constructors (default and with ApplicationConfigInit) new 516171f Be able to configure where configuration can get options (Fixes #3491) Merge branch 'feature/3491' into develop The 3 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 516171fbf7b3d4e84de5211b4fae3a4e35a97a38 Merge: c189a71 cd33ea7 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:28:22 2016 +0200 Be able to configure where configuration can get options (Fixes #3491) Merge branch 'feature/3491' into develop commit cd33ea714f8aae9bdb62a82088b3d61df9fb4813 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:28:05 2016 +0200 Generate application config with both constructors (default and with ApplicationConfigInit) commit 20d0b87da86a1d681dfb689119cbf3e68208da06 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:27:33 2016 +0200 Introduce ApplicationConfigScope and ApplicationConfigInit (See #3491) Summary of changes: .../templates/ApplicationConfigTransformer.java | 26 +- .../java/org/nuiton/config/ApplicationConfig.java | 469 ++++++++++++--------- .../org/nuiton/config/ApplicationConfigInit.java | 54 +++ .../org/nuiton/config/ApplicationConfigScope.java | 22 + .../nuiton/config/OverwriteApplicationConfig.java | 2 +- .../org/nuiton/config/SubApplicationConfig.java | 18 +- .../org/nuiton/config/ApplicationConfigTest.java | 4 +- 7 files changed, 385 insertions(+), 210 deletions(-) create mode 100644 nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigInit.java create mode 100644 nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigScope.java -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git commit 20d0b87da86a1d681dfb689119cbf3e68208da06 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:27:33 2016 +0200 Introduce ApplicationConfigScope and ApplicationConfigInit (See #3491) --- .../java/org/nuiton/config/ApplicationConfig.java | 469 ++++++++++++--------- .../org/nuiton/config/ApplicationConfigInit.java | 54 +++ .../org/nuiton/config/ApplicationConfigScope.java | 22 + .../nuiton/config/OverwriteApplicationConfig.java | 2 +- .../org/nuiton/config/SubApplicationConfig.java | 18 +- .../org/nuiton/config/ApplicationConfigTest.java | 4 +- 6 files changed, 360 insertions(+), 209 deletions(-) diff --git a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java index e375e5e..c5fa43f 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfig.java @@ -64,6 +64,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; +import java.util.EnumMap; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; @@ -94,6 +95,7 @@ import java.util.Set; * </ul> * <h3>Bonnes pratiques</h3> * <p> + * TODO A revoir en introduisant le nouveau constructeur avec ApplicationConfigInit * Vous devez créer une factory pour créer les instances d'{@link ApplicationConfig} qui contiendra par exemple une méthode : * </p> * <pre> @@ -117,8 +119,8 @@ import java.util.Set; * * </pre> * <ul> - * <li>MyAppConfigOption doit étendre {@link ConfigOptionDef} et décrire la configuration de l'application. - * <li>MyAppConfigAction doit étendre {@link ConfigActionDef} et décrire la liste des options + * <li>MyAppConfigOption doit étendre {@link ConfigOptionDef} et décrire les options de la configuration de l'application. + * <li>MyAppConfigAction doit étendre {@link ConfigActionDef} et décrire la liste des actions * et de leur alias disponible pour l'application. * </ul> * <h3>Lecture des fichiers de configuration</h3> @@ -157,7 +159,7 @@ import java.util.Set; * <li>fichier de configuration du repertoire home de l'utilisateur: $user.home/.filename</li> * <li>fichier de configuration du repertoire /etc: /etc/filename</li> * <li>fichier de configuration trouve dans le classpath: $CLASSPATH/filename</li> - * <li>options ajoutees par programmation: {@link #defaults}.put(key, value)</li> + * <li>options ajoutees par programmation: {@link #setDefaultOption(String, String)}</li> * </ul> * <p> * Les options sur la ligne de commande sont de la forme: @@ -357,81 +359,9 @@ public class ApplicationConfig { protected boolean inParseOptionPhase; /** - * TODO - */ - protected Properties defaults = new Properties(); - - /** - * TODO - */ - protected Properties classpath = new Properties(defaults); - - /** - * TODO - */ - protected Properties etcfile = new Properties(classpath); - - /** - * TODO - */ - protected Properties homefile = new Properties(etcfile); - - /** - * TODO - */ - protected Properties curfile = new Properties(homefile); - - /** - * TODO - */ - protected Properties env = new Properties(curfile) { - - private static final long serialVersionUID = 1L; - - /** - * Environnement variables can't contains dot (bash, csh, ...). Dots are - * replaced by underscore (_) to find property if property is not find - * with dot - */ - @Override - public synchronized Object get(Object key) { - Object result = super.get(key); - if (result == null && key instanceof String) { - String skey = (String) key; - skey = skey.replace(".", "_"); - result = super.get(skey); - } - return result; - } - - /** - * override to use get(key) and not super.get(key) as in initial implementation :( - */ - @Override - public String getProperty(String key) { - Object oval = get(key); - String sval = (oval instanceof String) ? (String) oval : null; - //TODO TC-2013-02-26 Make sure what we want :using Applicationconfig#defaults or super.defaults ? - return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; - - } - - }; - - /** - * TODO - */ - protected Properties jvm = new Properties(env); - - /** - * TODO - */ - protected Properties line = new Properties(jvm); - - /** - * TODO + * Contient les fichiers de propriétés par scope. */ - protected Properties options = new Properties(line); + protected EnumMap<ApplicationConfigScope, Properties> propertiesByScope = new EnumMap<>(ApplicationConfigScope.class); /** * TODO @@ -526,18 +456,84 @@ public class ApplicationConfig { * @since 2.4.8 */ public ApplicationConfig(Properties defaults, String configFilename) { - init(defaults, configFilename); + this(ApplicationConfigInit.forAllScopes() + .setConfigFileName(configFilename) + .setDefaults(defaults)); } /** - * On separt l'init du corps du constructeur, car les sous classes ne doivent - * pas l'executer. + * All in one, this constructor allow to pass all necessary argument to + * initialise ApplicationConfig and parse command line * - * @param defaults properties that override default value of optionClass, can be null - * @param configFilename override default config filename, can be null - * @since 2.4.9 + * @param init configuration builder + * @since 3.0 */ - protected void init(Properties defaults, String configFilename) { + public ApplicationConfig(ApplicationConfigInit init) { + init(init); + } + + /** + * On sépare l'initialisation du constructeur pour pouvoir ne pas exécuter ce code sur des classes surchargeant ApplicationConfig + * + * @param init l'objet d'initialisation de l'applicationConfig + */ + protected void init(ApplicationConfigInit init) { + + Set<ApplicationConfigScope> scopes = init.getScopes(); + + Properties lastProperties = null; + + // defaults + if (scopes.contains(ApplicationConfigScope.DEFAULTS)) { + lastProperties = new Properties(); + propertiesByScope.put(ApplicationConfigScope.DEFAULTS, lastProperties); + } + // class-path + if (scopes.contains(ApplicationConfigScope.CLASS_PATH)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.CLASS_PATH, lastProperties); + } + + // system + if (scopes.contains(ApplicationConfigScope.SYSTEM)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.SYSTEM, lastProperties); + } + // home + if (scopes.contains(ApplicationConfigScope.HOME)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.HOME, lastProperties); + } + // current + if (scopes.contains(ApplicationConfigScope.CURRENT)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.CURRENT, lastProperties); + } + // env + if (scopes.contains(ApplicationConfigScope.ENV)) { + lastProperties = (lastProperties == null ? new EnvProperties() : new EnvProperties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.ENV, lastProperties); + } + // jvm + if (scopes.contains(ApplicationConfigScope.JVM)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.JVM, lastProperties); + } + // line + if (scopes.contains(ApplicationConfigScope.LINE)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.LINE, lastProperties); + } + // options + if (scopes.contains(ApplicationConfigScope.OPTIONS)) { + lastProperties = (lastProperties == null ? new Properties() : new Properties(lastProperties)); + propertiesByScope.put(ApplicationConfigScope.OPTIONS, lastProperties); + } + + Properties defaults = init.getDefaults(); + + String configFilename = init.getConfigFilename(); + if (defaults != null) { // iterate with Properties method and not with Hashtable method to // prevent missed value with chained Properties object @@ -564,11 +560,25 @@ public class ApplicationConfig { } /** + * On separt l'init du corps du constructeur, car les sous classes ne doivent + * pas l'executer. + * + * @param defaults properties that override default value of optionClass, can be null + * @param configFilename override default config filename, can be null + * @since 2.4.9 + * @deprecated since 3.0, no more used, use now {@link #init(ApplicationConfigInit)} + */ + @Deprecated + protected void init(Properties defaults, String configFilename) { + // no more used + } + + /** * All in one, this constructor allow to pass all necessary argument to * initialise ApplicationConfig and parse command line * - * @param <O> option type - * @param <A> action type + * @param <O> option type + * @param <A> action type * @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 @@ -698,7 +708,18 @@ public class ApplicationConfig { * @param value default property value */ public void setDefaultOption(String key, String value) { - defaults.setProperty(key, value); + getProperties(ApplicationConfigScope.DEFAULTS).setProperty(key, value); + } + + protected Properties getProperties(ApplicationConfigScope scope) { + return propertiesByScope.get(scope); + } + + protected void putAll(Properties prop, ApplicationConfigScope scope) { + Properties properties = getProperties(scope); + if (properties != null) { + prop.putAll(properties); + } } /** @@ -718,18 +739,18 @@ public class ApplicationConfig { Properties prop = new SortedProperties(); if (forceAll) { - prop.putAll(defaults); - prop.putAll(classpath); + putAll(prop, ApplicationConfigScope.DEFAULTS); + putAll(prop, ApplicationConfigScope.CLASS_PATH); } - prop.putAll(etcfile); - prop.putAll(homefile); - prop.putAll(curfile); + putAll(prop, ApplicationConfigScope.SYSTEM); + putAll(prop, ApplicationConfigScope.HOME); + putAll(prop, ApplicationConfigScope.CURRENT); if (forceAll) { - prop.putAll(jvm); - prop.putAll(env); - prop.putAll(line); + putAll(prop, ApplicationConfigScope.JVM); + putAll(prop, ApplicationConfigScope.ENV); + putAll(prop, ApplicationConfigScope.LINE); } - prop.putAll(options); + putAll(prop, ApplicationConfigScope.OPTIONS); for (String excludeKey : excludeKeys) { prop.remove(excludeKey); @@ -803,24 +824,27 @@ public class ApplicationConfig { */ public void cleanUserConfig(String... excludeKeys) throws ApplicationConfigSaveException { - Set<String> keys = new HashSet<>(homefile.stringPropertyNames()); + Properties homefile = getProperties(ApplicationConfigScope.HOME); + if (homefile != null) { + Set<String> keys = new HashSet<>(homefile.stringPropertyNames()); - List<String> toExclude = Arrays.asList(excludeKeys); + List<String> toExclude = Arrays.asList(excludeKeys); - for (String key : keys) { - if (!toExclude.contains(key)) { - String property = homefile.getProperty(key); - if (StringUtils.isBlank(property)) { - if (log.isInfoEnabled()) { - log.info("Remove blank property: " + key); + for (String key : keys) { + if (!toExclude.contains(key)) { + String property = homefile.getProperty(key); + if (StringUtils.isBlank(property)) { + if (log.isInfoEnabled()) { + log.info("Remove blank property: " + key); + } + homefile.remove(key); } - homefile.remove(key); } } - } - // can now save cleaned user config - saveForUser(excludeKeys); + // can now save cleaned user config + saveForUser(excludeKeys); + } } /** @@ -1219,7 +1243,7 @@ public class ApplicationConfig { public boolean hasOption(String key) { // on est oblige de faire un get, car le containsKey n'est pas recursif // sur tous les properties - boolean result = options.getProperty(key) != null; + boolean result = getProperties(ApplicationConfigScope.OPTIONS).getProperty(key) != null; return result; } @@ -1379,9 +1403,9 @@ public class ApplicationConfig { public void setOption(String key, String value) { Properties props; if (inParseOptionPhase) { - props = line; + props = getProperties(ApplicationConfigScope.LINE); } else { - props = options; + props = getProperties(ApplicationConfigScope.OPTIONS); } if (value == null) { props.remove(key); @@ -1399,7 +1423,8 @@ public class ApplicationConfig { * @return String representation value */ public String getOption(String key) { - String value = options.getProperty(key); + Properties options = getProperties(ApplicationConfigScope.OPTIONS); + String value = options == null ? null : options.getProperty(key); // replace ${xxx} value = replaceRecursiveOptions(value); return value; @@ -1482,9 +1507,12 @@ public class ApplicationConfig { public Properties getOptionStartsWith(String prefix) { Properties result = new Properties(); - for (String key : options.stringPropertyNames()) { - if (key.startsWith(prefix)) { - result.setProperty(key, options.getProperty(key)); + Properties options = getProperties(ApplicationConfigScope.OPTIONS); + if (options != null) { + for (String key : options.stringPropertyNames()) { + if (key.startsWith(prefix)) { + result.setProperty(key, options.getProperty(key)); + } } } @@ -1810,7 +1838,7 @@ public class ApplicationConfig { * @return Properties which contains all options */ public Properties getOptions() { - return options; + return getProperties(ApplicationConfigScope.OPTIONS); } /** @@ -1820,7 +1848,7 @@ public class ApplicationConfig { * @param options Properties which contains all options to set */ public void setOptions(Properties options) { - this.options = options; + propertiesByScope.put(ApplicationConfigScope.OPTIONS, options); } /** @@ -1842,16 +1870,19 @@ public class ApplicationConfig { */ public Properties getFlatOptions(boolean replaceInner) { Properties props = new Properties(); - for (String propertyKey : options.stringPropertyNames()) { - String propertyValue; - if (replaceInner) { - // replace ${xxx} option - propertyValue = getOption(propertyKey); - } else { - // do not replace ${xxx} option - propertyValue = options.getProperty(propertyKey); + Properties options = getProperties(ApplicationConfigScope.OPTIONS); + if (options != null) { + for (String propertyKey : options.stringPropertyNames()) { + String propertyValue; + if (replaceInner) { + // replace ${xxx} option + propertyValue = getOption(propertyKey); + } else { + // do not replace ${xxx} option + propertyValue = options.getProperty(propertyKey); + } + props.setProperty(propertyKey, propertyValue); } - props.setProperty(propertyKey, propertyValue); } return props; } @@ -2060,74 +2091,95 @@ public class ApplicationConfig { // second load options from all sources // // JVM - jvm.putAll(System.getProperties()); + Properties jvm = getProperties(ApplicationConfigScope.JVM); + if (jvm != null) { + jvm.putAll(System.getProperties()); + } // ENV - env.putAll(System.getenv()); + Properties env = getProperties(ApplicationConfigScope.ENV); + if (env != null) { + env.putAll(System.getenv()); + } - // classpath String filename = getConfigFileName(); - Enumeration<URL> enumInClasspath = ClassLoader.getSystemClassLoader().getResources(filename); - Set<URL> urlsInClasspath = new HashSet<>(EnumerationUtils.toList(enumInClasspath)); - enumInClasspath = ApplicationConfig.class.getClassLoader().getResources(filename); - urlsInClasspath.addAll(EnumerationUtils.toList(enumInClasspath)); + // classpath + Properties classpath = getProperties(ApplicationConfigScope.CLASS_PATH); + if (classpath != null) { + Enumeration<URL> enumInClasspath = ClassLoader.getSystemClassLoader().getResources(filename); + Set<URL> urlsInClasspath = new HashSet<>(EnumerationUtils.toList(enumInClasspath)); - if (log.isDebugEnabled() && urlsInClasspath.isEmpty()) { - log.debug("No configuration file found in classpath : /" + filename); - } + enumInClasspath = ApplicationConfig.class.getClassLoader().getResources(filename); + urlsInClasspath.addAll(EnumerationUtils.toList(enumInClasspath)); + + if (log.isDebugEnabled() && urlsInClasspath.isEmpty()) { + log.debug("No configuration file found in classpath : /" + filename); + } - for (URL inClasspath : urlsInClasspath) { - if (log.isInfoEnabled()) { - log.info("Loading configuration file (classpath) : " + - inClasspath); + for (URL inClasspath : urlsInClasspath) { + if (log.isInfoEnabled()) { + log.info("Loading configuration file (classpath) : " + + inClasspath); + } + loadResource(inClasspath.toURI(), classpath); } - loadResource(inClasspath.toURI(), classpath); } // system directory - File etcConfig = getSystemConfigFile(); - if (etcConfig.exists()) { - if (log.isInfoEnabled()) { - log.info("Loading configuration file (etc) : " + etcConfig); - } - loadResource(etcConfig.toURI(), etcfile); - } else { - if (log.isDebugEnabled()) { - log.debug("No configuration file found in system : " + - etcConfig.getAbsolutePath()); + Properties etcfile = getProperties(ApplicationConfigScope.SYSTEM); + if (etcfile != null) { + + File etcConfig = getSystemConfigFile(); + if (etcConfig.exists()) { + if (log.isInfoEnabled()) { + log.info("Loading configuration file (etc) : " + etcConfig); + } + loadResource(etcConfig.toURI(), etcfile); + } else { + if (log.isDebugEnabled()) { + log.debug("No configuration file found in system : " + + etcConfig.getAbsolutePath()); + } } - } - // user home directory - File homeConfig = getUserConfigFile(); - if (log.isDebugEnabled()) { - log.debug("User configuration file : " + homeConfig); } - if (homeConfig.exists()) { - if (log.isInfoEnabled()) { - log.info("Loading configuration file (home) : " + - homeConfig); - } - loadResource(homeConfig.toURI(), homefile); - } else { + // user home directory + Properties homefile = getProperties(ApplicationConfigScope.HOME); + if (homefile != null) { + File homeConfig = getUserConfigFile(); if (log.isDebugEnabled()) { - log.debug("No configuration file found in user home : " + - homeConfig.getAbsolutePath()); + log.debug("User configuration file : " + homeConfig); + } + + if (homeConfig.exists()) { + if (log.isInfoEnabled()) { + log.info("Loading configuration file (home) : " + + homeConfig); + } + loadResource(homeConfig.toURI(), homefile); + } else { + if (log.isDebugEnabled()) { + log.debug("No configuration file found in user home : " + + homeConfig.getAbsolutePath()); + } } } // file $CURDIR/filename - File config = new File(filename); - if (config.exists()) { - if (log.isInfoEnabled()) { - log.info("Loading configuration file (curr) : " + config); - } - loadResource(config.toURI(), curfile); - } else { - if (log.isDebugEnabled()) { - log.debug("No configuration file found in current" + - " directory : " + config.getAbsolutePath()); + Properties curfile = getProperties(ApplicationConfigScope.CURRENT); + if (curfile != null) { + File config = new File(filename); + if (config.exists()) { + if (log.isInfoEnabled()) { + log.info("Loading configuration file (curr) : " + config); + } + loadResource(config.toURI(), curfile); + } else { + if (log.isDebugEnabled()) { + log.debug("No configuration file found in current" + + " directory : " + config.getAbsolutePath()); + } } } @@ -2171,22 +2223,16 @@ public class ApplicationConfig { * @param homeConfig new configuration file path * @throws IOException if could not move configuration file */ - protected void migrateUserConfigurationFile(File oldHomeConfig, - File homeConfig) + protected void migrateUserConfigurationFile(File oldHomeConfig, File homeConfig) throws IOException { if (log.isInfoEnabled()) { - log.info(String.format("Moving old configuration file from %s to %s", - oldHomeConfig.getPath(), homeConfig.getPath())); + log.info(String.format("Moving old configuration file from %s to %s", oldHomeConfig.getPath(), homeConfig.getPath())); } boolean b = oldHomeConfig.renameTo(homeConfig); if (!b) { // could not move... - String message = String.format( - "could not move old configuration file %s to %s", - oldHomeConfig, - homeConfig - ); + String message = String.format("could not move old configuration file %s to %s", oldHomeConfig, homeConfig); throw new IOException(message); } } @@ -2218,9 +2264,7 @@ public class ApplicationConfig { * @see Properties#store(Writer, String) * @since 2.3 */ - protected void saveResource(File file, - Properties properties, - String comment) throws IOException { + protected void saveResource(File file, Properties properties, String comment) throws IOException { try (Writer reader = new OutputStreamWriter(new FileOutputStream(file), getEncoding())) { properties.store(reader, comment); } @@ -2242,15 +2286,9 @@ public class ApplicationConfig { * @since 1.1.4 */ public void printConfig(PrintStream output) { - output.println("defaults " + defaults); - output.println("classpath " + classpath); - output.println("etcfile " + etcfile); - output.println("homefile " + homefile); - output.println("curfile " + curfile); - output.println("env " + env); - output.println("jvm " + jvm); - output.println("line " + line); - output.println("options " + options); + for (Map.Entry<ApplicationConfigScope, Properties> entry : propertiesByScope.entrySet()) { + output.println(entry.getKey().name() + " " + entry.getValue()); + } } /** @@ -2534,4 +2572,43 @@ public class ApplicationConfig { } } + private static class EnvProperties extends Properties { + + private static final long serialVersionUID = 1L; + + public EnvProperties() { + } + + public EnvProperties(Properties defaults) { + super(defaults); + } + + /** + * Environnement variables can't contains dot (bash, csh, ...). Dots are + * replaced by underscore (_) to find property if property is not find + * with dot + */ + @Override + public synchronized Object get(Object key) { + Object result = super.get(key); + if (result == null && key instanceof String) { + String skey = (String) key; + skey = skey.replace(".", "_"); + result = super.get(skey); + } + return result; + } + + /** + * override to use get(key) and not super.get(key) as in initial implementation :( + */ + @Override + public String getProperty(String key) { + Object oval = get(key); + String sval = (oval instanceof String) ? (String) oval : null; + return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; + + } + + } } diff --git a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigInit.java b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigInit.java new file mode 100644 index 0000000..5abe140 --- /dev/null +++ b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigInit.java @@ -0,0 +1,54 @@ +package org.nuiton.config; + +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.Properties; +import java.util.Set; + +/** + * Created by tchemit on 30/09/16. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ApplicationConfigInit { + + public static ApplicationConfigInit forAllScopes() { + return new ApplicationConfigInit(ApplicationConfigScope.values()); + } + + public static ApplicationConfigInit forScopes(ApplicationConfigScope... scopes) { + return new ApplicationConfigInit(scopes); + } + + protected String configFilename; + protected Properties defaults; + protected final Set<ApplicationConfigScope> scopes ; + + protected ApplicationConfigInit(ApplicationConfigScope...scopes) { + this.scopes = Collections.unmodifiableSet(EnumSet.copyOf(Arrays.asList(scopes))); + } + + public ApplicationConfigInit setConfigFileName(String configFilename) { + this.configFilename = configFilename; + return this; + } + + public ApplicationConfigInit setDefaults(Properties defaults) { + this.defaults = defaults; + return this; + } + + public String getConfigFilename() { + return configFilename; + } + + public Properties getDefaults() { + return defaults; + } + + public Set<ApplicationConfigScope> getScopes() { + return scopes; + } + +} diff --git a/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigScope.java b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigScope.java new file mode 100644 index 0000000..d5f7a16 --- /dev/null +++ b/nuiton-config/src/main/java/org/nuiton/config/ApplicationConfigScope.java @@ -0,0 +1,22 @@ +package org.nuiton.config; + +/** + * Définit le scope d'un lecture/écriture d'une option dans application config. + * + * Created by tchemit on 30/09/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public enum ApplicationConfigScope { + + DEFAULTS, // les valeurs par défaut des options + CLASS_PATH, // options contenu dans un fichier lu dans le class-path + SYSTEM, // options contenues dans un fichier lu sur le système (/etc/) + HOME, // options contenues dans un fichier lu dans la home utilisateur (~/.config/) + CURRENT, // options contenues dans un fichier lu dans le répertoire courant + ENV, // options contenues dans les variables d'environnements + JVM, // options de la jvm + LINE, // ??? + OPTIONS // options obtenues après lecture de la ligne de commande +} diff --git a/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java index 3754b27..ddb0372 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/OverwriteApplicationConfig.java @@ -50,7 +50,7 @@ public class OverwriteApplicationConfig extends ApplicationConfig { } @Override - protected void init(Properties defaults, String configFilename) { + protected void init(ApplicationConfigInit init) { // do nothing } diff --git a/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java b/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java index 9f3134b..2d9590e 100644 --- a/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java +++ b/nuiton-config/src/main/java/org/nuiton/config/SubApplicationConfig.java @@ -57,7 +57,7 @@ public class SubApplicationConfig extends ApplicationConfig { } @Override - protected void init(Properties defaults, String configFilename) { + protected void init(ApplicationConfigInit init) { // do nothing } @@ -91,12 +91,11 @@ public class SubApplicationConfig extends ApplicationConfig { /** * Surcharge pour recherche la cle avec le prefix. Si on ne la retrouve - * pas, on recherche sans le prefix pour permettre d'avoir des valeurs - * par defaut. + * pas, on recherche sans le prefixe pour permettre d'avoir des valeurs + * par défaut. * * @param key La cle de l'option - * @return l'option trouvé avec le prefix ou sinon celle sans le prefix - * si pas trouvé. + * @return l'option trouvée avec le prefixe ou sinon celle sans le prefixe si pas trouvée. */ @Override public String getOption(String key) { @@ -115,9 +114,9 @@ public class SubApplicationConfig extends ApplicationConfig { * * @param replaceInner le prefix à remplacer * @return les options commencant par le prefix - * soit modifiee pour qu'elle est la meme cle sans le prefix. Le but - * est de garder les autres options et si une option avait le meme nom - * qu'elle soit effacee par celle dont on a supprime le prefix + * soit modifiee pour qu'elle est la meme cle sans le prefix. Le but + * est de garder les autres options et si une option avait le meme nom + * qu'elle soit effacee par celle dont on a supprime le prefix */ @Override public Properties getFlatOptions(boolean replaceInner) { @@ -142,8 +141,7 @@ public class SubApplicationConfig extends ApplicationConfig { * permettre aussi les valeurs par defaut * * @param prefix prefix to use - * @return les valeurs commençant par le prefix demandé en plus du - * prefix 'sub'. + * @return les valeurs commençant par le prefix demandé en plus du prefix 'sub'. */ @Override public Properties getOptionStartsWith(String prefix) { diff --git a/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java b/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java index 3e90c98..506b299 100644 --- a/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java +++ b/nuiton-config/src/test/java/org/nuiton/config/ApplicationConfigTest.java @@ -430,7 +430,7 @@ public class ApplicationConfigTest { instance.setOption("hellomessage", "Hello ${user.name} !"); Assert.assertEquals("Hello " + System.getProperty("user.name") + " !", instance.getOption("hellomessage")); - Assert.assertEquals("Hello ${user.name} !", instance.options.getProperty("hellomessage")); + Assert.assertEquals("Hello ${user.name} !", instance.getProperties(ApplicationConfigScope.OPTIONS).getProperty("hellomessage")); instance.setOption("tempdir", "${java.io.tmpdir}" + File.separator + "blah"); File tempDir = instance.getOptionAsFile("tempdir"); @@ -456,7 +456,7 @@ public class ApplicationConfigTest { ApplicationConfig instance = new ApplicationConfig(); // simulate env variable with _ to replace dot - instance.env.put("test_env", "value"); + instance.getProperties(ApplicationConfigScope.ENV).put("test_env", "value"); String value = instance.getOption("test.env"); Assert.assertEquals("value", value); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git commit cd33ea714f8aae9bdb62a82088b3d61df9fb4813 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:28:05 2016 +0200 Generate application config with both constructors (default and with ApplicationConfigInit) --- .../templates/ApplicationConfigTransformer.java | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/templates/ApplicationConfigTransformer.java b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/templates/ApplicationConfigTransformer.java index 9315ccf..76964dd 100644 --- a/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/templates/ApplicationConfigTransformer.java +++ b/nuiton-config-maven-plugin/src/main/java/org/nuiton/config/plugin/templates/ApplicationConfigTransformer.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codehaus.plexus.util.StringUtils; import org.nuiton.config.ApplicationConfig; +import org.nuiton.config.ApplicationConfigInit; import org.nuiton.config.ApplicationConfigProvider; import org.nuiton.config.ConfigActionDef; import org.nuiton.eugene.GeneratorUtil; @@ -191,11 +192,19 @@ public class ApplicationConfigTransformer extends ObjectModelTransformerToJava { } addAttribute(output, "applicationConfig", ApplicationConfig.class, "", ObjectModelJavaModifier.PRIVATE); + ObjectModelOperation publicConstructor = addConstructor(output, ObjectModelJavaModifier.PROTECTED); + setOperationBody(publicConstructor, "" + /*{ + this(ApplicationConfigInit.forAllScopes()); + }*/ + ); + ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PROTECTED); + addParameter(constructor, ApplicationConfigInit.class, "init"); StringBuilder builder = new StringBuilder(); builder.append("" /*{ - this.applicationConfig = new ApplicationConfig(); + this.applicationConfig = new ApplicationConfig(init); this.applicationConfig.loadDefaultOptions(<%=optionClassSimpleName%>.values()); }*/ ); @@ -270,6 +279,21 @@ public class ApplicationConfigTransformer extends ObjectModelTransformerToJava { } setSuperClass(output, abstractClassName); + ObjectModelOperation publicConstructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC); + setOperationBody(publicConstructor, "" + /*{ + super(); + }*/ + ); + + ObjectModelOperation constructor = addConstructor(output, ObjectModelJavaModifier.PUBLIC); + addParameter(constructor, ApplicationConfigInit.class, "init"); + setOperationBody(constructor, "" + /*{ + super(init); + }*/ + ); + } private boolean canGenerate(String input) { -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-config. See https://gitlab.nuiton.org/nuiton/nuiton-config.git commit 516171fbf7b3d4e84de5211b4fae3a4e35a97a38 Merge: c189a71 cd33ea7 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Sep 30 19:28:22 2016 +0200 Be able to configure where configuration can get options (Fixes #3491) Merge branch 'feature/3491' into develop .../templates/ApplicationConfigTransformer.java | 26 +- .../java/org/nuiton/config/ApplicationConfig.java | 469 ++++++++++++--------- .../org/nuiton/config/ApplicationConfigInit.java | 54 +++ .../org/nuiton/config/ApplicationConfigScope.java | 22 + .../nuiton/config/OverwriteApplicationConfig.java | 2 +- .../org/nuiton/config/SubApplicationConfig.java | 18 +- .../org/nuiton/config/ApplicationConfigTest.java | 4 +- 7 files changed, 385 insertions(+), 210 deletions(-) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm