|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.nuiton.util.ApplicationConfig
public class ApplicationConfig
Application configuration.
useOnlyAliases
ApplicationConfig, where in constructor you call
setConfigFileName(String), setDefaultOption(String, String),
addAlias(String, String...), addActionAlias(String, String),
to have properly value.
conf = new MonAppConfig();
conf.parse(args);
here you can used conf.getOption(key);
conf.doAction(0);
...
conf.doAction(n);
parse(java.lang.String[]) en utilisant la valeur de getConfigFileName() pour
trouver les fichiers (voir Les options de configuration pour l'ordre de
chargement des fichiers)
save(File, boolean,String...) sauve les données dans le fichier demandé
saveForSystem(java.lang.String...) sauvegarde les donnees dans /etc
saveForUser(java.lang.String...) sauvegarde les donnees dans $HOME
saveForSystem(java.lang.String...) ou
saveForUser(java.lang.String...) seules les options lues dans un fichier ou modifiées par
programmation (setOption(java.lang.String, java.lang.String) seront sauvegardées. Par exemple les
options passees sur la ligne de commande ne seront pas sauvees.
setOption(String, String)defaults.put(key, value)--option key value --monOption key value1 value2
ApplicationConfig. Dans ce cas vous pouvez mettre les
arguments que vous souhaitez du moment qu'ils soient convertibles de la
representation String vers le type que vous avez mis.
--le.package.LaClass#laMethode arg1 arg2 arg3 ... argNUne action est donc defini par le chemin complet vers la methode qui traitera l'action. Cette methode peut-etre une methode static ou non. Si la methode n'est pas static lors de l'instanciation de l'objet on essaie de passer en parametre du constructeur la classe de configuration utilisee pour permettre a l'action d'avoir a sa disposition les options de configuration. Si aucun constructeur avec comme seul parametre une classe heritant de
ApplicationConfig n'existe alors le constructeur par defaut est
utilise (il doit etre accessible). Toutes methodes d'actions faisant
parties d'un meme objet utiliseront la meme instance de cette objet lors
de leur execution.
Si la methode utilise les arguments variants alors tous les arguments
jusqu'au prochain -- ou la fin de la ligne de commande sont utilises. Sinon
Le nombre exact d'argument necessaire a la methode sont utilises.
Les arguments sont automatiquement converti dans le bon type reclame par la
methode.
Si l'on veut des arguments optionnels le seul moyen actuellement est
d'utiliser une methode avec des arguments variants
Les actions ne sont pas execute mais seulement parsees. Pour les executer
il faut utiliser la méthode doAction(int) qui prend en argument un numero
de 'step'. Par defaut toutes les actions sont de niveau 0 et sont executee
dans l'ordre d'apparition sur la ligne de commande. Si l'on souhaite
distinguer les actions il est possible d'utiliser l'annotation
ApplicationConfig.Action.Step sur la methode qui fera l'action en
precisant une autre valeur que 0.
doAction(0); ... do something ... doAction(1);dans cette exemple on fait un traitement entre l'execution des actions de niveau 0 et les actions de niveau 1.
getUnparsed(). Si l'on souhaite forcer
la fin du parsing de la ligne de commande il est possible de mettre --.
Par exemple:
monProg "mon arg" --option k1 v1 -- --option k2 v2 -- autreDans cet exemple seule la premiere option sera considere comme une option. On retrouvera dans
unparsed: "mon arg", "--option", "k2", "v2", "--",
"autre"
addAlias(java.lang.String, java.lang.String...).
addAlias("-v", "--option", "verbose", "true");
addAlias("-o", "--option", "outputfile");
addAlias("-i", "--mon.package.MaClass#MaMethode", "import");
En faite avant le parsing de la ligne de commande tous les alias trouves sont
automatiquement remplacer par leur correspondance. Il est donc possible
d'utiliser ce mecanisme pour autre chose par exemple:
addAlias("cl", "Code Lutin");
addAlias("bp", "Benjamin POUSSIN);
Dans le premier exemple on simplifie une option de flags l'option -v n'attend
donc plus d'argument. Dans le second exemple on simplifie une option qui
attend encore un argment de type File. Enfin dans le troisieme exemple
on simplifie la syntaxe d'une action et on force le premier argument de
l'action a etre "import".
String
File
URL
Class
Date
Time
Timestamp
String. Chaque element doit
etre separe par une virgule.
ApplicationConfig supporte les substition de variables de la forme
${xxx} où xxx est une autre variable de la configuration.
Exemple (dans un fichier de configuration):
firstname = John
lastname = Doe
fullname = ${firstname} ${lastname}
getOption("fullname") retournera "John Doe".
| Nested Class Summary | |
|---|---|
static class |
ApplicationConfig.Action
Defines a runtime action to be launched via the ApplicationConfig.Action.doAction()
method. |
protected static class |
ApplicationConfig.CacheItem<T>
Item used for cacheOption |
static interface |
ApplicationConfig.OptionDef
Le contrat de marquage des options, on utilise cette interface pour caracteriser une option de configuration. |
| Field Summary | |
|---|---|
protected java.util.Map<java.lang.Integer,java.util.List<ApplicationConfig.Action>> |
actions
TODO |
static java.lang.String |
ADJUSTING_PROPERTY
Property name of adjusting internal state. |
protected java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
aliases
TODO |
static java.lang.String |
APP_NAME
TODO |
protected java.util.Map<java.lang.Class<?>,java.lang.Object> |
cacheAction
TODO |
protected java.util.Map<java.lang.String,ApplicationConfig.CacheItem<?>> |
cacheOption
TODO |
protected java.util.Properties |
classpath
TODO |
static java.lang.String |
CONFIG_FILE_NAME
Configuration file key option. |
static java.lang.String |
CONFIG_PATH
Configuration directory where config path in located. |
protected java.util.Properties |
curfile
TODO |
protected java.util.Properties |
defaults
TODO |
protected java.util.Properties |
env
TODO |
protected java.util.Properties |
etcfile
TODO |
protected java.util.Properties |
homefile
TODO |
protected boolean |
inParseOptionPhase
vrai si on est en train de parser les options de la ligne de commande. |
protected java.util.Properties |
jvm
TODO |
protected java.util.Properties |
line
TODO |
protected java.util.Properties |
options
TODO |
protected java.lang.String |
osName
System os name. |
protected java.beans.PropertyChangeSupport |
pcs
suport of config modification. |
protected java.util.List<java.lang.String> |
unparsed
contient apres l'appel de parse, la liste des arguments non utilises |
protected boolean |
useOnlyAliases
TODO |
protected java.lang.String |
userPath
Deprecated. since 1.2.1, and can't be removed (for migration purpose) |
| Constructor Summary | |
|---|---|
ApplicationConfig()
Init ApplicationConfig with current simple class name as config file. |
|
| Method Summary | ||
|---|---|---|
void |
addAction(ApplicationConfig.Action action)
Add action to list of action to do |
|
void |
addActionAlias(java.lang.String alias,
java.lang.String actionMethod)
Add alias for action. |
|
void |
addAlias(java.lang.String alias,
java.lang.String... target)
All argument in aliases as key is substitued by target. |
|
void |
addPropertyChangeListener(java.beans.PropertyChangeListener listener)
|
|
void |
addPropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
|
|
protected ApplicationConfig.Action |
createAction(java.lang.String name,
java.util.ListIterator<java.lang.String> args)
Create action from string, string must be [package.] |
|
void |
doAction(int step)
Do action in specified step. |
|
protected void |
firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue)
|
|
java.lang.String |
getConfigFileName()
Get name of file where options are read (in /etc, $HOME, $CURDIR). |
|
protected java.lang.String |
getConfigFileNameOption()
|
|
java.lang.String |
getConfigPath()
Get configuration file path to use. |
|
java.util.Properties |
getFlatOptions()
Get all options as flat Properties object (replace inner options). |
|
java.util.Properties |
getFlatOptions(boolean replaceInner)
Get all options as flat Properties object. |
|
protected java.util.Map<java.lang.String,java.lang.reflect.Method> |
getMethods()
Get all set method on this object or super object. |
|
java.lang.Object |
getOption(ApplicationConfig.OptionDef key)
Get option value from a option definition. |
|
|
getOption(java.lang.Class<T> clazz,
java.lang.String key)
Get option value as typed value. |
|
java.lang.String |
getOption(java.lang.String key)
get option value as string. |
|
boolean |
getOptionAsBoolean(java.lang.String key)
Get option value as boolean. |
|
java.lang.Class<?> |
getOptionAsClass(java.lang.String key)
Get option value as Class. |
|
java.sql.Date |
getOptionAsDate(java.lang.String key)
Get option value as Date. |
|
double |
getOptionAsDouble(java.lang.String key)
Get option value as double. |
|
java.io.File |
getOptionAsFile(java.lang.String key)
Get option value as File. |
|
int |
getOptionAsInt(java.lang.String key)
Get option value as int. |
|
java.sql.Time |
getOptionAsTime(java.lang.String key)
Get option value as Time. |
|
java.sql.Timestamp |
getOptionAsTimestamp(java.lang.String key)
Get option value as Timestamp. |
|
java.net.URL |
getOptionAsURL(java.lang.String key)
Get option value as URL. |
|
java.util.Properties |
getOptions()
Get all options from configuration. |
|
java.util.Properties |
getOptionStartsWith(java.lang.String prefix)
Permet de recuperer l'ensemble des options commencant par une certaine chaine. |
|
protected java.lang.String[] |
getParams(java.lang.reflect.Method m,
java.util.ListIterator<java.lang.String> args)
Take required argument for method in args. |
|
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners()
|
|
java.beans.PropertyChangeListener[] |
getPropertyChangeListeners(java.lang.String propertyName)
|
|
java.io.File |
getSystemConfigFile()
Obtain the system config file location. |
|
protected java.lang.String |
getSystemConfigurationPath()
Get system configuration path. |
|
java.util.List<java.lang.String> |
getUnparsed()
Return list of unparsed command line argument |
|
java.io.File |
getUserConfigFile()
Obtain the user config file location. |
|
static java.lang.String |
getUserHome()
Get user home directory (system property user.home). |
|
java.lang.String |
getUsername()
Get user name (system property user.name). |
|
protected java.lang.String |
getUserPath()
Get user configuration path. |
|
boolean |
hasListeners(java.lang.String propertyName)
|
|
protected void |
installSaveUserAction(java.lang.String... properties)
Install the saveUserAction on givne properties. |
|
boolean |
isAdjusting()
|
|
boolean |
isUseOnlyAliases()
|
|
protected void |
migrateUserConfigurationFile(java.io.File oldHomeConfig,
java.io.File homeConfig)
Move old user configuration file oldHomeConfig to homeConfig. |
|
void |
parse(java.lang.String[] args)
Parse option and call set necessary method, read jvm, env variable, Load configuration file and prepare Action. |
|
void |
printConfig()
For debugging. |
|
void |
printConfig(java.io.PrintStream output)
Print out current configuration in specified output. |
|
void |
removePropertyChangeListener(java.beans.PropertyChangeListener listener)
|
|
void |
removePropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
|
|
protected java.lang.String |
replaceRecursiveOptions(java.lang.String option)
Replace included ${xxx} suboptions by their values. |
|
void |
save(java.io.File file,
boolean forceAll,
java.lang.String... excludeKeys)
Save configuration, in specified file. |
|
void |
saveForSystem(java.lang.String... excludeKeys)
Save configuration, in system directory (/etc/) using the getConfigFileName(). |
|
void |
saveForUser(java.lang.String... excludeKeys)
Save configuration, in user home directory using the getConfigFileName(). |
|
void |
setAdjusting(boolean adjusting)
|
|
void |
setAppName(java.lang.String appName)
Use appName to add a context in config.file and config.path options. |
|
void |
setConfigFileName(java.lang.String name)
Set name of file where options are read (in /etc, $HOME, $CURDIR) This set used setDefaultOption(String, String) |
|
protected void |
setDefaultOption(java.lang.String key,
java.lang.String value)
Used to put default configuration option in config option. |
|
void |
setOption(java.lang.String key,
java.lang.String value)
Set option value. |
|
void |
setOptions(java.util.Properties options)
Set manually options when you don't want to use parse method to check properties file configured by setConfigFileName(java.lang.String). |
|
void |
setUseOnlyAliases(boolean useOnlyAliases)
|
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final java.lang.String CONFIG_FILE_NAME
public static final java.lang.String APP_NAME
public static final java.lang.String ADJUSTING_PROPERTY
adjusting internal state.
public static final java.lang.String CONFIG_PATH
protected java.lang.String osName
protected boolean useOnlyAliases
@Deprecated protected java.lang.String userPath
protected boolean inParseOptionPhase
protected java.util.Properties defaults
protected java.util.Properties classpath
protected java.util.Properties etcfile
protected java.util.Properties homefile
protected java.util.Properties curfile
protected java.util.Properties env
protected java.util.Properties jvm
protected java.util.Properties line
protected java.util.Properties options
protected java.util.Map<java.lang.String,ApplicationConfig.CacheItem<?>> cacheOption
protected java.util.Map<java.lang.Class<?>,java.lang.Object> cacheAction
protected java.util.List<java.lang.String> unparsed
protected java.util.Map<java.lang.String,java.util.List<java.lang.String>> aliases
protected java.util.Map<java.lang.Integer,java.util.List<ApplicationConfig.Action>> actions
protected java.beans.PropertyChangeSupport pcs
| Constructor Detail |
|---|
public ApplicationConfig()
ConverterUtil.initConverters()| Method Detail |
|---|
public static java.lang.String getUserHome()
user.home).
public java.lang.String getUsername()
user.name).
protected void setDefaultOption(java.lang.String key,
java.lang.String value)
key - default property keyvalue - default property value
public void save(java.io.File file,
boolean forceAll,
java.lang.String... excludeKeys)
throws java.io.IOException
file - file where config will be writenforceAll - if true save all config option
(with defaults, classpath, env, command line)excludeKeys - optional list of keys to exclude from
java.io.IOException - if IO pbpublic void saveForSystem(java.lang.String... excludeKeys)
getConfigFileName(). Default, env and commande line note saved.
excludeKeys - optional list of keys to exclude frompublic void saveForUser(java.lang.String... excludeKeys)
getConfigFileName(). Default, env and commande line note saved
excludeKeys - optional list of keys to exclude frompublic java.io.File getSystemConfigFile()
public java.io.File getUserConfigFile()
public java.util.List<java.lang.String> getUnparsed()
public void addAction(ApplicationConfig.Action action)
action - action to add, can be null.
public void doAction(int step)
throws java.lang.IllegalAccessException,
java.lang.IllegalArgumentException,
java.lang.reflect.InvocationTargetException,
java.lang.InstantiationException
step - do action only defined in this step
java.lang.IllegalAccessException - if action invocation failed
java.lang.IllegalArgumentException - if action invocation failed
java.lang.reflect.InvocationTargetException - if action invocation failed
java.lang.InstantiationException - if action invocation failedApplicationConfig.Action.Steppublic void setUseOnlyAliases(boolean useOnlyAliases)
public boolean isUseOnlyAliases()
public void addAlias(java.lang.String alias,
java.lang.String... target)
alias - alias string as '-v'target - substitution as '--option verbose true'
public void addActionAlias(java.lang.String alias,
java.lang.String actionMethod)
addAlias(String, String...)
alias - the alias to add for the given method actionactionMethod - must be fully qualified method path:
package.Class.methodpublic void setConfigFileName(java.lang.String name)
setDefaultOption(String, String)
name - file namepublic java.lang.String getConfigFileName()
public boolean isAdjusting()
public void setAdjusting(boolean adjusting)
protected java.lang.String getConfigFileNameOption()
public void setAppName(java.lang.String appName)
appName - to use as application contextpublic java.lang.String getConfigPath()
CONFIG_PATH option
File.separatorprotected java.lang.String getSystemConfigurationPath()
protected java.lang.String getUserPath()
public void setOption(java.lang.String key,
java.lang.String value)
key - property keyvalue - property valuepublic java.lang.String getOption(java.lang.String key)
key - the option's key
protected java.lang.String replaceRecursiveOptions(java.lang.String option)
option - option to replace into
public java.util.Properties getOptionStartsWith(java.lang.String prefix)
prefix - debut de cle a recuperer
public java.lang.Object getOption(ApplicationConfig.OptionDef key)
key - the definition of the option
public <T> T getOption(java.lang.Class<T> clazz,
java.lang.String key)
T - type of the object wanted as return typeclazz - type of object wanted as return typekey - the option's key
public java.io.File getOptionAsFile(java.lang.String key)
File.
key - the option's key
public java.net.URL getOptionAsURL(java.lang.String key)
URL.
key - the option's key
public java.lang.Class<?> getOptionAsClass(java.lang.String key)
Class.
key - the option's key
public java.sql.Date getOptionAsDate(java.lang.String key)
Date.
key - the option's key
public java.sql.Time getOptionAsTime(java.lang.String key)
Time.
key - the option's key
public java.sql.Timestamp getOptionAsTimestamp(java.lang.String key)
Timestamp.
key - the option's key
public int getOptionAsInt(java.lang.String key)
int.
key - the option's key
intpublic double getOptionAsDouble(java.lang.String key)
double.
key - the option's key
doublepublic boolean getOptionAsBoolean(java.lang.String key)
boolean.
key - the option's key
boolean.public java.util.Properties getOptions()
public void setOptions(java.util.Properties options)
setConfigFileName(java.lang.String).
options - Properties which contains all options to setpublic java.util.Properties getFlatOptions()
Properties object (replace inner options).
public java.util.Properties getFlatOptions(boolean replaceInner)
Properties object.
replaceInner - if true replace imbricated options by theirs values
protected void installSaveUserAction(java.lang.String... properties)
saveUserAction on givne properties.
properties - properties on which insalls the saveUserActionprotected java.util.Map<java.lang.String,java.lang.reflect.Method> getMethods()
protected java.lang.String[] getParams(java.lang.reflect.Method m,
java.util.ListIterator<java.lang.String> args)
m - the method to callargs - iterator with many argument (equals or more than necessary
protected ApplicationConfig.Action createAction(java.lang.String name,
java.util.ListIterator<java.lang.String> args)
throws ArgumentsParserException,
java.lang.InstantiationException,
java.lang.IllegalAccessException,
java.lang.IllegalArgumentException,
java.lang.reflect.InvocationTargetException
name - name of the actionargs - arguments for action invocation
ArgumentsParserException - if parsing failed
java.lang.IllegalAccessException - if could not create action
java.lang.IllegalArgumentException - if could not create action
java.lang.InstantiationException - if could not create action
java.lang.reflect.InvocationTargetException - if could not create action
public void parse(java.lang.String[] args)
throws ArgumentsParserException
args - argument as main(String[] args)
ArgumentsParserException - if parsing failed
protected void migrateUserConfigurationFile(java.io.File oldHomeConfig,
java.io.File homeConfig)
throws java.io.IOException
oldHomeConfig to homeConfig.
oldHomeConfig - old configuration file pathhomeConfig - new configuration file path
java.io.IOException - if could not move configuration filepublic void printConfig()
public void printConfig(java.io.PrintStream output)
output - output to write config to
protected void firePropertyChange(java.lang.String propertyName,
java.lang.Object oldValue,
java.lang.Object newValue)
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
public void addPropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
public void removePropertyChangeListener(java.lang.String propertyName,
java.beans.PropertyChangeListener listener)
public boolean hasListeners(java.lang.String propertyName)
public java.beans.PropertyChangeListener[] getPropertyChangeListeners(java.lang.String propertyName)
public java.beans.PropertyChangeListener[] getPropertyChangeListeners()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||