Author: tchemit Date: 2008-07-27 09:05:50 +0000 (Sun, 27 Jul 2008) New Revision: 925 Modified: trunk/commandline/commandline-core/src/main/java/org/codelutin/option/ContextProvider.java trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-en_GB.properties trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-fr_FR.properties Log: improve context loading add javadoc i18n Modified: trunk/commandline/commandline-core/src/main/java/org/codelutin/option/ContextProvider.java =================================================================== --- trunk/commandline/commandline-core/src/main/java/org/codelutin/option/ContextProvider.java 2008-07-25 23:20:28 UTC (rev 924) +++ trunk/commandline/commandline-core/src/main/java/org/codelutin/option/ContextProvider.java 2008-07-27 09:05:50 UTC (rev 925) @@ -14,15 +14,20 @@ */ package org.codelutin.option; +import static org.codelutin.i18n.I18n._; + import java.util.Iterator; import java.util.ServiceLoader; /** * Cette classe permet de trouver dynamiquement le contexte Commandline en utilisant le - * m�canisme java de {@link java.util.ServiceLoader}. + * m�canisme java de {@link ServiceLoader}. * <p/> - * Le contexte trouv� lors du premier appel � la m�thode {@link #getContext()} est cach�� dans le champ {@link #context}. + * Avant tout utilisation du context, il faut lancer la m�thode {@link #init()} qui charge le context dans la + * variable statique {@link #context}. * <p/> + * Ensuite on peut r�cupere le context, via l'accesseur {@link #get()}. + * <p/> * Il est possible de supprimer un context charg� via la m�thode {@link #reset()}. * <p/> * <b>Note : Attention, un seul contexte commandline peut �tre d�fini.</b> @@ -30,29 +35,76 @@ * @author chemit */ public class ContextProvider { + + /** share instance of the context */ + protected static Context context; + /** - * share instance of the context + * Init the shared instance of the context. + * <p/> + * An {@link IllegalStateException} will be be thrown in theses cases : + * <ul> + * <li>context was previously init (says the shared instance is not null)</li> + * <li>no context was found by the {@link ServiceLoader}</li> + * <li>more than one context was found by the {@link ServiceLoader}</li> + * </ul> + * + * @return the shared instanciated context + * @throws IllegalStateException if the context waspreviously init, or not found or if more than one context was found. */ - private static Context context; + public static synchronized Context init() throws IllegalStateException { + if (context != null) { + throw new IllegalStateException(_("commandline.context.error.alredyinit")); + } + ServiceLoader<Context> loader = ServiceLoader.load(Context.class); + Iterator<Context> iterator = loader.iterator(); + if (!iterator.hasNext()) { + throw new IllegalStateException(_("commandline.context.error.unfound")); + } + context = iterator.next(); + if (iterator.hasNext()) { + // not authorized! + reset(); + throw new IllegalStateException(_("commandline.context.error.multicontextfound")); + } - public static synchronized Context getContext() { + return context; + } + + /** + * Obtain the shared instance of the context. + * <p/> + * An {@link IllegalStateException} will be thrown if the shared instances was not previously init via method {@link #init()} + * + * @return the shared instance of the context + * @throws IllegalStateException if shared instance was not init + */ + public static synchronized Context get() throws IllegalStateException { + checkContextInit(); + return context; + } + + /** + * Check if shared instance was previously init via method {@link #init()} + * + * @throws IllegalStateException if shared instance is null. + */ + public static void checkContextInit() throws IllegalStateException { if (context == null) { - ServiceLoader<Context> loader = ServiceLoader.load(Context.class); - Iterator<Context> iterator = loader.iterator(); - if (!iterator.hasNext()) { - throw new IllegalStateException("could not find a " + Context.class + " service"); - } - context = iterator.next(); - if (iterator.hasNext()) { - // not authorized! - reset(); - throw new IllegalStateException("only one " + Context.class + " service can be defined in classpath"); - } + throw new IllegalStateException(_("commandline.context.error.noinit")); } - return context; } + /** + * Remove the reference of the shared instance of context. + * <p/> + * This method must be used before to be able to reload a context via method {@link #init()} + */ public static void reset() { context = null; } + + protected ContextProvider() { + // no instance of this class + } } Modified: trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-en_GB.properties =================================================================== --- trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-en_GB.properties 2008-07-25 23:20:28 UTC (rev 924) +++ trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-en_GB.properties 2008-07-27 09:05:50 UTC (rev 925) @@ -1,3 +1,7 @@ +commandline.context.error.alredyinit=Context was previously initialized, use the method ContextProvider\#reset() to delete it +commandline.context.error.multicontextfound=More than one Context was found in classpath, this is forbidden +commandline.context.error.noinit=The context was not init (use before all ContextProvider\#init() method +commandline.context.error.unfound=Could not find a Context in the classpath lutinutil.change.config.property=changed for configuration {0} property {1} <old\: {2}, new\: {3}> lutinutil.error.config.unauthorized.key=The given key {0} is not autohrized, list of authorized keys {1} lutinutil.error.final.property=In configuration [{0}], can not modify the final property {1} Modified: trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-fr_FR.properties =================================================================== --- trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-fr_FR.properties 2008-07-25 23:20:28 UTC (rev 924) +++ trunk/commandline/commandline-core/src/main/resources/i18n/commandline-core-fr_FR.properties 2008-07-27 09:05:50 UTC (rev 925) @@ -1,3 +1,7 @@ +commandline.context.error.alredyinit=Le context a d\u00E9j\u00E0 \u00E9t\u00E9 initialis\u00E9, utiliser la m\u00E9thode ContextProvider\#reset() pour le supprimer +commandline.context.error.multicontextfound=Plusieurs context ont \u00E9t\u00E9 trouv\u00E9s dans le classpath, ce qui est interdit +commandline.context.error.noinit=Le context n'a pas \u00E9t\u00E9 initialis\u00E9, il faut utiliser la m\u00E9thode ContextProvier\#init() avant +commandline.context.error.unfound=Aucun Context n'a \u00E9t\u00E9 trouv\u00E9 dans le classpath lutinutil.change.config.property=modification pour la configuration {0} propri\u00E9t\u00E9 {1} <old\: {2}, new\: {3}> lutinutil.error.config.unauthorized.key=La clef {0} n''est pas autoris\u00E9, liste des clefs possibles {1} lutinutil.error.final.property=Dans la configuration [{0}], impossible de modifier la propri\u00E9t\u00E9 finale {1}