From 618516d9fd1d194bec64fd5c2ec97a8860773742 Mon Sep 17 00:00:00 2001
From: Ednara Oliveira Manage custom contexts relevant to Demoiselle operations. When starting, the ContextManager must be initialized by calling {@link #initialize(AfterBeanDiscovery event)}
- * inside any methods observing the {@link AfterBeanDiscovery} event. Upon initialization a {@link StaticContext} will be
- * created to handle {@link StaticScoped} beans (but not activated, you must call {@link #activate(Class customContextClass, Class scope)}
- * to activate this context). If an extension wants to manage another custom context, it must first call {@link #add(CustomContext context, AfterBeanDiscovery event)}
- * to add it's context to the list of managed contexts and then call {@link #activate(Class customContextClass, Class scope)} whenever
- * it wants to activate this added context (contexts added through the {@link #add(CustomContext context, AfterBeanDiscovery event)} method are also
- * not activated upon adding).
+ * Manage custom contexts relevant to Demoiselle operations.
+ *
+ * When starting, the ContextManager must be initialized by calling {@link #initialize(AfterBeanDiscovery event)} inside
+ * any methods observing the {@link AfterBeanDiscovery} event. Upon initialization a {@link StaticContext} will be
+ * created to handle {@link StaticScoped} beans (but not activated, you must call
+ * {@link #activate(Class customContextClass, Class scope)} to activate this context).
+ *
+ * If an extension wants to manage another custom context, it must first call
+ * {@link #add(CustomContext context, AfterBeanDiscovery event)} to add it's context to the list of managed contexts and
+ * then call {@link #activate(Class customContextClass, Class scope)} whenever it wants to activate this added context
+ * (contexts added through the {@link #add(CustomContext context, AfterBeanDiscovery event)} method are also not
+ * activated upon adding).
+ * Initializes this manager and adds the {@link StaticContext} context to the list of managed contexts. Other
- * contexts must be added before they can be activated. It's OK to call this method multiple times, it will be initialized only once.
+ * Initializes this manager and adds the {@link StaticContext} context to the list of managed contexts. Other
+ * contexts must be added before they can be activated.
+ *
+ * It's OK to call this method multiple times, it will be initialized only once.
+ * Adds a context to the list of managed contexts. A context added through this method will be deactivated before management can start. Only after calling
- * {@link #activate(Class customContextClass, Class scope)} the context will be activated. Trying to add a context already managed will result in this method call being ignored.
+ * Adds a context to the list of managed contexts.
+ *
+ * A context added through this method will be deactivated before management can start. Only after calling
+ * {@link #activate(Class customContextClass, Class scope)} the context will be activated.
+ *
+ * Trying to add a context already managed will result in this method call being ignored.
+ * Activates a managed context. To be activated, a context must fulfill the following requisites:
+ *
+ * Activates a managed context.
+ *
+ * To be activated, a context must fulfill the following requisites:
*
- *
- *
*
true
if there is a managed context of the provided type and scope and no other context is active for the provided scope,
- * false
if there is a managed context of the provided type and scope but another context is active for the provided scope.
- *
- * @throws DemoiselleException if there is no managed context of the provided type and scope.
+ * @param customContextClass
+ * Type of context to activate
+ * @param scope
+ * The scope to activate this context for
+ * @return true
if there is a managed context of the provided type and scope and no other context is
+ * active for the provided scope, false
if there is a managed context of the provided type and
+ * scope but another context is active for the provided scope.
+ * @throws DemoiselleException
+ * if there is no managed context of the provided type and scope.
*/
- public static synchronized void activate(Class extends CustomContext> customContextClass , Class extends Annotation> scope){
- if (!initialized){
+ public static synchronized void activate(Class extends CustomContext> customContextClass,
+ Class extends Annotation> scope) {
+ if (!initialized) {
throw new DemoiselleException(getBundle().getString("custom-context-manager-not-initialized"));
}
-
- for (CustomContextCounter context : contexts){
- if ( context.isSame(customContextClass, scope) ){
+
+ for (CustomContextCounter context : contexts) {
+ if (context.isSame(customContextClass, scope)) {
context.activate();
return;
}
}
- throw new DemoiselleException(getBundle().getString("custom-context-not-found",customContextClass.getCanonicalName(),scope.getSimpleName()));
+ throw new DemoiselleException(getBundle().getString("custom-context-not-found",
+ customContextClass.getCanonicalName(), scope.getSimpleName()));
}
-
+
/**
- * Deactivates a managed context.
- * - *To be deactivated, a context must fulfill the following requisites: + *
+ * Deactivates a managed context. + *
+ *+ * To be deactivated, a context must fulfill the following requisites: *
true
.
- *
* true
if there was an active context of this type and scope and it was activated by a previous
- * call to {@link #activate(Class customContextClass, Class scope)}
- *
- * @throws DemoiselleException if there is no managed context of the provided type and scope.
+ * @param customContextClass
+ * Type of context to deactivate
+ * @param scope
+ * The scope the context controled when it was active
+ * @return true
if there was an active context of this type and scope and it was activated by a
+ * previous call to {@link #activate(Class customContextClass, Class scope)}
+ * @throws DemoiselleException
+ * if there is no managed context of the provided type and scope.
*/
- public static synchronized void deactivate(Class extends CustomContext> customContextClass,Class extends Annotation> scope){
- if (!initialized){
+ public static synchronized void deactivate(Class extends CustomContext> customContextClass,
+ Class extends Annotation> scope) {
+ if (!initialized) {
throw new DemoiselleException(getBundle().getString("custom-context-manager-not-initialized"));
}
-
- for (CustomContextCounter context : contexts){
- if (context.isSame(customContextClass, scope)){
+
+ for (CustomContextCounter context : contexts) {
+ if (context.isSame(customContextClass, scope)) {
context.deactivate();
return;
}
}
- throw new DemoiselleException(getBundle().getString("custom-context-not-found",customContextClass.getCanonicalName(),scope.getSimpleName()));
+ throw new DemoiselleException(getBundle().getString("custom-context-not-found",
+ customContextClass.getCanonicalName(), scope.getSimpleName()));
}
- static Logger getLogger(){
- if (logger==null){
+ public static synchronized void shutdown() {
+ for (CustomContextCounter context : contexts) {
+ context.deactivate();
+ }
+
+ contexts.clear();
+ initialized = false;
+ }
+
+ static Logger getLogger() {
+ if (logger == null) {
logger = LoggerProducer.create(ContextManager.class);
}
-
+
return logger;
}
-
- static ResourceBundle getBundle(){
- if (bundle==null){
- bundle = ResourceBundleProducer.create("demoiselle-core-bundle",Locale.getDefault());
+
+ static ResourceBundle getBundle() {
+ if (bundle == null) {
+ bundle = ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault());
}
-
+
return bundle;
}
}
+
/**
- * Class that counts how many attemps to activate and deactivate this context received, avoiding cases
- * where one client activates given context and another one deactivates it, leaving the first client
- * with no active context before completion.
+ * Class that counts how many attemps to activate and deactivate this context received, avoiding cases where one client
+ * activates given context and another one deactivates it, leaving the first client with no active context before
+ * completion.
*
* @author serpro
- *
*/
-class CustomContextCounter{
+class CustomContextCounter {
+
private CustomContext context;
+
private int activationCounter = 0;
-
+
public CustomContextCounter(CustomContext customContext) {
this.context = customContext;
}
-
- public boolean isSame(Class extends CustomContext> customContextClass,Class extends Annotation> scope){
- if (context.getClass().getCanonicalName().equals( customContextClass.getCanonicalName() )
- && context.getScope().equals(scope)){
+
+ public boolean isSame(Class extends CustomContext> customContextClass, Class extends Annotation> scope) {
+ if (context.getClass().getCanonicalName().equals(customContextClass.getCanonicalName())
+ && context.getScope().equals(scope)) {
return true;
}
-
+
return false;
}
-
- public CustomContext getInternalContext(){
+
+ public CustomContext getInternalContext() {
return this.context;
}
-
- public synchronized void activate(){
- try{
- Context c = Beans.getBeanManager().getContext(context.getScope());
- if (c==context){
+
+ public synchronized void activate() {
+ try {
+ BeanManager beanManager = Beans.getReference(BeanManager.class);
+ Context c = beanManager.getContext(context.getScope());
+ if (c == context) {
activationCounter++;
}
- }
- catch(ContextNotActiveException ce){
+ } catch (ContextNotActiveException ce) {
context.setActive(true);
activationCounter++;
ContextManager.getLogger().trace(
- ContextManager.getBundle().getString("custom-context-was-activated"
- , context.getClass().getCanonicalName()
- ,context.getScope().getCanonicalName()
- ));
+ ContextManager.getBundle().getString("custom-context-was-activated",
+ context.getClass().getCanonicalName(), context.getScope().getCanonicalName()));
}
}
-
- public synchronized void deactivate(){
- try{
+
+ public synchronized void deactivate() {
+ try {
Context c = Beans.getBeanManager().getContext(context.getScope());
- if (c==context){
+ if (c == context) {
activationCounter--;
- if (activationCounter==0){
+ if (activationCounter == 0) {
context.setActive(false);
ContextManager.getLogger().trace(
- ContextManager.getBundle().getString("custom-context-was-deactivated"
- , context.getClass().getCanonicalName()
- ,context.getScope().getCanonicalName()
- ));
+ ContextManager.getBundle().getString("custom-context-was-deactivated",
+ context.getClass().getCanonicalName(), context.getScope().getCanonicalName()));
}
}
+ } catch (ContextNotActiveException ce) {
}
- catch(ContextNotActiveException ce){}
}
}
diff --git a/impl/core/src/main/resources/demoiselle-core-bundle.properties b/impl/core/src/main/resources/demoiselle-core-bundle.properties
index dbb2440..8e604b9 100644
--- a/impl/core/src/main/resources/demoiselle-core-bundle.properties
+++ b/impl/core/src/main/resources/demoiselle-core-bundle.properties
@@ -53,6 +53,7 @@ transaction-commited=Transa\u00E7\u00E3o finalizada com sucesso
transaction-rolledback=Transa\u00E7\u00E3o finalizada com rollback
bootstrap.configuration.processing=Processando {0}
+bootstrap-context-already-managed=O contexto {0} para o escopo {1} j\u00E1 \u00E9 gerenciado
loading-configuration-class=Carregando a classe de configura\u00E7\u00E3o {0}
configuration-field-loaded=Configura\u00E7\u00E3o {0} atribu\u00EDda a {1} com o valor {2}
--
libgit2 0.21.2