Commit d40862c65e34e30c4a60d618100a4bd43667904b

Authored by Dancovich
1 parent 95da4575
Exists in master

Refatorando gerenciador de contextos

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/Contexts.java
... ... @@ -36,25 +36,26 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.context;
38 38  
39   -import java.lang.annotation.Annotation;
40 39 import java.util.ArrayList;
41 40 import java.util.Collections;
42 41 import java.util.Iterator;
43 42 import java.util.List;
  43 +import java.util.Locale;
44 44  
  45 +import javax.enterprise.context.ContextNotActiveException;
  46 +import javax.enterprise.context.spi.Context;
45 47 import javax.enterprise.inject.spi.AfterBeanDiscovery;
46 48  
47 49 import org.slf4j.Logger;
48 50  
49 51 import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;
50 52 import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;
  53 +import br.gov.frameworkdemoiselle.util.Beans;
51 54 import br.gov.frameworkdemoiselle.util.ResourceBundle;
52 55  
53 56 public final class Contexts {
54 57  
55   - private static List<CustomContext> activeContexts = Collections.synchronizedList(new ArrayList<CustomContext>());
56   -
57   - private static List<CustomContext> inactiveContexts = Collections.synchronizedList(new ArrayList<CustomContext>());
  58 + private static List<CustomContext> contexts = Collections.synchronizedList(new ArrayList<CustomContext>());
58 59  
59 60 private static Logger logger;
60 61  
... ... @@ -73,13 +74,140 @@ public final class Contexts {
73 74  
74 75 private static ResourceBundle getBundle() {
75 76 if (bundle == null) {
76   - bundle = ResourceBundleProducer.create("demoiselle-core-bundle");
  77 + bundle = ResourceBundleProducer.create("demoiselle-core-bundle",Locale.getDefault());
77 78 }
78 79  
79 80 return bundle;
80 81 }
  82 +
  83 + /**
  84 + * Adds a custom context to the list of managed contexts. If the {@link CustomContext#isActive()} returns
  85 + * <code>true</code> the moment this method is called, it will be activated by calling {@link #activate(Class contextClass)} immediately.
  86 + * Otherwise the context will remain inactive until activated.
  87 + *
  88 + * @param context Context to be addedd
  89 + * @param event Captured CDI event for adding the context
  90 + */
  91 + public static synchronized void add(CustomContext context , AfterBeanDiscovery event){
  92 + getLogger().trace(getBundle().getString("custom-context-was-registered", context.getClass().getCanonicalName(),context.getScope().getCanonicalName()));
  93 + contexts.add(context);
  94 +
  95 + boolean mustActivate = context.isActive();
  96 + context.setActive(false);
  97 + event.addContext(context);
  98 +
  99 + if(mustActivate){
  100 + activate(context.getClass());
  101 + }
  102 + }
  103 +
  104 + /**
  105 + * Activates a custom context. If there's already another context registered for this custom context's scope then it will not be activated
  106 + * and this method returns <code>false</code>. It will also fail and return <code>false</code> if the custom context was
  107 + * not registered with {@link #add(CustomContext context, AfterBeanDiscovery event)}.
  108 + *
  109 + * @param contextClass Class of the contexto to activate
  110 + * @return <code>true</code> if the context was activated, <code>false</code> if it was not registered prior to activation or if there's already
  111 + * another context active for this context's scope.
  112 + */
  113 + public static synchronized boolean activate(Class<? extends CustomContext> contextClass){
  114 + for(CustomContext ctx : contexts){
  115 + if (contextClass.getCanonicalName().equals(ctx.getClass().getCanonicalName()) ){
  116 + activate(ctx);
  117 + }
  118 + }
  119 +
  120 + return false;
  121 + }
  122 +
  123 + public static synchronized boolean activate(CustomContext context){
  124 + try{
  125 + Beans.getBeanManager().getContext(context.getScope());
  126 + return false;
  127 + }
  128 + catch(ContextNotActiveException ce){
  129 + context.setActive(true);
  130 + getLogger().trace(getBundle().getString("custom-context-was-activated", context.getClass().getCanonicalName(),context.getScope().getCanonicalName()));
  131 + return true;
  132 + }
  133 + }
  134 +
  135 + /**
  136 + * Deactivates a custom context previously activated by {@link #activate(Class)}.
  137 + *
  138 + * @param contextClass Class of context to be deactivated
  139 + *
  140 + * @return <code>true</code> if this context was active and is now deactivated. <code>false</code> if no context
  141 + * matching contextClass is active at the moment.
  142 + */
  143 + public static synchronized boolean deactivate(Class<? extends CustomContext> contextClass){
  144 + for(CustomContext ctx : contexts){
  145 + if (contextClass.getCanonicalName().equals(ctx.getClass().getCanonicalName()) && ctx.isActive()){
  146 + return deactivate(ctx);
  147 + }
  148 + }
  149 +
  150 + return false;
  151 + }
  152 +
  153 + public static boolean deactivate(CustomContext ctx){
  154 + try{
  155 + Context activeContext = Beans.getBeanManager().getContext(ctx.getScope());
  156 + ctx.setActive(false);
  157 + if (activeContext == ctx){
  158 + getLogger().trace(getBundle().getString("custom-context-was-deactivated", ctx.getClass().getCanonicalName(),ctx.getScope().getCanonicalName()));
  159 + return true;
  160 + }
  161 + }
  162 + catch(ContextNotActiveException ce){
  163 + }
81 164  
82   - public static synchronized void add(CustomContext context, AfterBeanDiscovery event) {
  165 + return false;
  166 + }
  167 +
  168 + /**
  169 + * Unregister all custom contexts of the provided class. If they are active the moment they're being removed, they will first be deactivated.
  170 + *
  171 + * @param contextClass Custom context's class to me removed
  172 + */
  173 + public static void remove(Class<? extends CustomContext> contextClass){
  174 + for (Iterator<CustomContext> it = contexts.iterator();it.hasNext();){
  175 + CustomContext ctx = it.next();
  176 + if (contextClass.getCanonicalName().equals(ctx.getClass().getCanonicalName()) ){
  177 + deactivate(ctx);
  178 + it.remove();
  179 + getLogger().trace(getBundle().getString("custom-context-was-unregistered", ctx.getClass().getCanonicalName(),ctx.getScope().getCanonicalName()));
  180 + }
  181 + }
  182 + }
  183 +
  184 + /**
  185 + * Unregister a custom context. If it is active the moment it's being removed, it will first be deactivated.
  186 + *
  187 + * @param ctx Custom context to remove
  188 + */
  189 + public static void remove(CustomContext ctx){
  190 + if (contexts.indexOf(ctx)>-1){
  191 + deactivate(ctx);
  192 + contexts.remove(ctx);
  193 + getLogger().trace(getBundle().getString("custom-context-was-unregistered", ctx.getClass().getCanonicalName(),ctx.getScope().getCanonicalName()));
  194 + }
  195 + }
  196 +
  197 + /**
  198 + * Remove all registered custom contexts. All removed contexts are deactivated.
  199 + */
  200 + public static synchronized void clear(){
  201 + for (Iterator<CustomContext> it = contexts.iterator(); it.hasNext();){
  202 + CustomContext ctx = it.next();
  203 + deactivate(ctx);
  204 + it.remove();
  205 +
  206 + getLogger().trace(getBundle().getString("custom-context-was-unregistered", ctx.getClass().getCanonicalName(),ctx.getScope().getCanonicalName()));
  207 + }
  208 + }
  209 +
  210 + /*public static synchronized void add(CustomContext context, AfterBeanDiscovery event) {
83 211 Class<? extends Annotation> scope = context.getScope();
84 212  
85 213 getLogger()
... ... @@ -150,5 +278,5 @@ public final class Contexts {
150 278  
151 279 public static synchronized List<CustomContext> getInactiveContexts() {
152 280 return inactiveContexts;
153   - }
  281 + }*/
154 282 }
... ...