|
||||||||||
| 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
useOnlyAliases
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(String[]) ou
saveForUser(String[]) seul les options lu dans un fichier ou modifier par
programmation (setOption(java.lang.String, java.lang.String) seront sauvegardees. Par exemple les
options passees sur la ligne de commande ne seront pas sauvees.
setOption(java.lang.String, java.lang.String)(key, value)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 utiilser la methode 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".
| Nested Class Summary | |
|---|---|
static class |
ApplicationConfig.Action
|
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 Map<Integer,List<ApplicationConfig.Action>> |
actions
|
protected Map<String,List<String>> |
aliases
|
protected Map<Class<?>,Object> |
cacheAction
|
protected Map<String,ApplicationConfig.CacheItem<?>> |
cacheOption
|
protected Properties |
classpath
|
static String |
CONFIG_FILE_NAME
|
protected Properties |
curfile
|
protected Properties |
defaults
|
protected Properties |
env
|
protected Properties |
etcfile
|
protected Properties |
homefile
|
protected boolean |
inParseOptionPhase
vrai si on est en train de parser les options de la ligne de commande |
protected Properties |
jvm
|
protected Properties |
line
|
protected Properties |
options
|
protected PropertyChangeSupport |
pcs
suport of config modification |
protected List<String> |
unparsed
contient apres l'appel de parse, la liste des arguments non utilises |
protected boolean |
useOnlyAliases
|
| Constructor Summary | |
|---|---|
ApplicationConfig()
|
|
| Method Summary | ||
|---|---|---|
void |
addAction(ApplicationConfig.Action action)
Add action to list of action to do |
|
void |
addActionAlias(String alias,
String actionMethod)
Add alias for action. |
|
void |
addAlias(String alias,
String... target)
All argument in aliases as key is substitued by target |
|
void |
addPropertyChangeListener(PropertyChangeListener listener)
|
|
void |
addPropertyChangeListener(String propertyName,
PropertyChangeListener listener)
|
|
protected ApplicationConfig.Action |
createAction(String name,
ListIterator<String> args)
Create action from string, string must be [package.] |
|
void |
doAction(int step)
|
|
protected void |
firePropertyChange(String propertyName,
Object oldValue,
Object newValue)
|
|
String |
getConfigFileName()
|
|
protected Map<String,Method> |
getMethods()
Get all set method on this object or super object |
|
Object |
getOption(ApplicationConfig.OptionDef key)
get option value from a option definition |
|
|
getOption(Class<T> clazz,
String key)
get option value as typed value |
|
String |
getOption(String key)
get option value as string |
|
boolean |
getOptionAsBoolean(String key)
get option value as typed value |
|
Class<?> |
getOptionAsClass(String key)
get option value as typed value |
|
Date |
getOptionAsDate(String key)
get option value as typed value |
|
double |
getOptionAsDouble(String key)
get option value as typed value |
|
File |
getOptionAsFile(String key)
get option value as typed value |
|
int |
getOptionAsInt(String key)
get option value as typed value |
|
Time |
getOptionAsTime(String key)
get option value as typed value |
|
Timestamp |
getOptionAsTimestamp(String key)
get option value as typed value |
|
URL |
getOptionAsURL(String key)
get option value as typed value |
|
Properties |
getOptions()
Get all options from configuration. |
|
Properties |
getOptionStartsWith(String prefix)
Permet de recuperer l'ensemble des options commencant par une certaine chaine |
|
protected String[] |
getParams(Method m,
ListIterator<String> args)
Take required argument for method in args. |
|
PropertyChangeListener[] |
getPropertyChangeListeners()
|
|
PropertyChangeListener[] |
getPropertyChangeListeners(String propertyName)
|
|
List<String> |
getUnparsed()
Return list of unparsed command line argument |
|
static String |
getUserHome()
|
|
String |
getUsername()
|
|
boolean |
hasListeners(String propertyName)
|
|
boolean |
isUseOnlyAliases()
|
|
void |
parse(String[] args)
Parse option and call set necessary method, read jvm, env variable, Load configuration file and prepare Action. |
|
void |
printConfig()
For debugging |
|
void |
removePropertyChangeListener(PropertyChangeListener listener)
|
|
void |
removePropertyChangeListener(String propertyName,
PropertyChangeListener listener)
|
|
protected String |
replaceRecursiveOptions(String option)
Replace inlclude ${xxx} suboption by their values. |
|
void |
save(File file,
boolean forceAll,
String... excludeKeys)
Save configuration, in specified file |
|
void |
saveForSystem(String... excludeKeys)
Save configuration, in system directory (/etc/) using the getConfigFileName(). |
|
void |
saveForUser(String... excludeKeys)
Save configuration, in user home directory using the getConfigFileName(). |
|
void |
setConfigFileName(String name)
Set name of file where options are read (in /etc, $HOME, $CURDIR) This set used setDefaultOption(String, String) |
|
protected void |
setDefaultOption(String key,
String value)
Used to put default configuration option in config option. |
|
void |
setOption(String key,
String value)
Set option value |
|
void |
setOptions(Properties options)
Set manually options when you don't want to use parse method to check properties file configured by setConfigFileName. |
|
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 String CONFIG_FILE_NAME
protected boolean useOnlyAliases
protected Map<String,List<String>> aliases
protected boolean inParseOptionPhase
protected Properties defaults
protected Properties classpath
protected Properties etcfile
protected Properties homefile
protected Properties curfile
protected Properties env
protected Properties jvm
protected Properties line
protected Properties options
protected Map<String,ApplicationConfig.CacheItem<?>> cacheOption
protected Map<Class<?>,Object> cacheAction
protected List<String> unparsed
protected Map<Integer,List<ApplicationConfig.Action>> actions
protected PropertyChangeSupport pcs
| Constructor Detail |
|---|
public ApplicationConfig()
| Method Detail |
|---|
public static String getUserHome()
public String getUsername()
protected void setDefaultOption(String key, String value)
key - default property keyvalue - default property valuepublic void save(File file, boolean forceAll, String... excludeKeys) throws 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
IOException - if IO pbpublic void saveForSystem(String... excludeKeys)
getConfigFileName(). Default, env and commande line note saved.
excludeKeys - optional list of keys to exclude frompublic void saveForUser(String... excludeKeys)
getConfigFileName(). Default, env and commande line note saved
excludeKeys - optional list of keys to exclude frompublic List<String> getUnparsed()
public void addAction(ApplicationConfig.Action action)
action - action to add, can be null.
public void doAction(int step)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException,
InstantiationException
public void setUseOnlyAliases(boolean useOnlyAliases)
public boolean isUseOnlyAliases()
public void addAlias(String alias, String... target)
alias - alias string as '-v'target - substitution as '--option verbose true'public void addActionAlias(String alias, 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(String name)
setDefaultOption(String, String)
name - file namepublic String getConfigFileName()
public void setOption(String key, String value)
key - property keyvalue - property valuepublic String getOption(String key)
key - the option's key
protected String replaceRecursiveOptions(String option)
option - option to replace into
public Properties getOptionStartsWith(String prefix)
prefix - debut de cle a recuperer
public Object getOption(ApplicationConfig.OptionDef key)
key - the definition of the option
public <T> T getOption(Class<T> clazz, String key)
T - type of the object wanted as return typeclazz - type of object wanted as return typekey - the option's key
public File getOptionAsFile(String key)
key - the option's key
public URL getOptionAsURL(String key)
key - the option's key
public Class<?> getOptionAsClass(String key)
key - the option's key
public Date getOptionAsDate(String key)
key - the option's key
public Time getOptionAsTime(String key)
key - the option's key
public Timestamp getOptionAsTimestamp(String key)
key - the option's key
public int getOptionAsInt(String key)
key - the option's key
public double getOptionAsDouble(String key)
key - the option's key
public boolean getOptionAsBoolean(String key)
key - the option's key
public Properties getOptions()
public void setOptions(Properties options)
options - Properties which contains all options to setprotected Map<String,Method> getMethods()
protected String[] getParams(Method m, ListIterator<String> args)
m - the method to callargs - iterator with many argument (equals or more than necessary
protected ApplicationConfig.Action createAction(String name, ListIterator<String> args) throws ArgumentsParserException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
name - name of the actionargs -
ArgumentsParserException
IllegalAccessException
IllegalArgumentException
InstantiationException
InvocationTargetExceptionpublic void parse(String[] args) throws ArgumentsParserException
args - argument as main(String[] args)
ArgumentsParserExceptionpublic void printConfig()
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)
public void addPropertyChangeListener(PropertyChangeListener listener)
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
public void removePropertyChangeListener(PropertyChangeListener listener)
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
public boolean hasListeners(String propertyName)
public PropertyChangeListener[] getPropertyChangeListeners(String propertyName)
public PropertyChangeListener[] getPropertyChangeListeners()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||