Commit 5e4c7bab389896ec99a697eb68966f0cea7dcc5b
1 parent
437a8ecb
Exists in
master
Refatorado levantamento de contextos temporários durante execução de
métodos @Startup e @Shutdown
Showing
1 changed file
with
54 additions
and
51 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java
| ... | ... | @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; |
| 39 | 39 | import java.lang.annotation.Annotation; |
| 40 | 40 | import java.util.ArrayList; |
| 41 | 41 | import java.util.Collections; |
| 42 | +import java.util.HashMap; | |
| 42 | 43 | import java.util.Iterator; |
| 43 | 44 | import java.util.List; |
| 44 | 45 | |
| ... | ... | @@ -69,9 +70,11 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 69 | 70 | private List<AnnotatedMethodProcessor> processors = Collections |
| 70 | 71 | .synchronizedList(new ArrayList<AnnotatedMethodProcessor>()); |
| 71 | 72 | |
| 72 | - private boolean registered = false; | |
| 73 | - | |
| 74 | 73 | private transient static ResourceBundle bundle; |
| 74 | + | |
| 75 | + private boolean registered = false; | |
| 76 | + | |
| 77 | + private HashMap<String, Boolean> startedContextHere = new HashMap<String, Boolean>(); | |
| 75 | 78 | |
| 76 | 79 | protected abstract Logger getLogger(); |
| 77 | 80 | |
| ... | ... | @@ -106,18 +109,6 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 106 | 109 | } |
| 107 | 110 | } |
| 108 | 111 | |
| 109 | - /*public void loadTempContexts(@Observes final AfterBeanDiscovery event) { | |
| 110 | - // Caso este bootstrap rode antes do CoreBootstrap. Não há problemas em chamar este método várias vezes, ele | |
| 111 | - // ignora chamadas adicionais. | |
| 112 | - ContextManager.initialize(event); | |
| 113 | - | |
| 114 | - // Não registrar o contexto de aplicação pq ele já é registrado pela implementação do CDI | |
| 115 | - ContextManager.add(new ThreadLocalContext(ViewScoped.class), event); | |
| 116 | - ContextManager.add(new ThreadLocalContext(SessionScoped.class), event); | |
| 117 | - ContextManager.add(new ThreadLocalContext(ConversationScoped.class), event); | |
| 118 | - ContextManager.add(new ThreadLocalContext(RequestScoped.class), event); | |
| 119 | - }*/ | |
| 120 | - | |
| 121 | 112 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 122 | 113 | protected synchronized void proccessEvent() { |
| 123 | 114 | getLogger().debug(getBundle().getString("executing-all", getAnnotationClass().getSimpleName())); |
| ... | ... | @@ -125,32 +116,8 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 125 | 116 | Collections.sort(processors); |
| 126 | 117 | Exception failure = null; |
| 127 | 118 | |
| 128 | - RequestContext tempRequestContext = Beans.getReference(RequestContext.class); | |
| 129 | - SessionContext tempSessionContext = Beans.getReference(SessionContext.class); | |
| 130 | - ViewContext tempViewContext = Beans.getReference(ViewContext.class); | |
| 131 | - ConversationContext tempConversationContext = Beans.getReference(ConversationContext.class); | |
| 132 | - | |
| 133 | - boolean requestActivatedHere = tempRequestContext!=null && !tempRequestContext.isActive(); | |
| 134 | - boolean sessionActivatedHere = tempSessionContext!=null && !tempSessionContext.isActive(); | |
| 135 | - boolean viewActivatedHere = tempViewContext!=null && !tempViewContext.isActive(); | |
| 136 | - boolean conversationActivatedHere = tempConversationContext!=null && !tempConversationContext.isActive(); | |
| 119 | + startContexts(); | |
| 137 | 120 | |
| 138 | - if (!registered) { | |
| 139 | - if (tempRequestContext!=null && !tempRequestContext.isActive()) | |
| 140 | - tempRequestContext.activate(); | |
| 141 | - | |
| 142 | - if (tempSessionContext!=null && !tempSessionContext.isActive()) | |
| 143 | - tempSessionContext.activate(); | |
| 144 | - | |
| 145 | - if (tempViewContext!=null && !tempViewContext.isActive()) | |
| 146 | - tempViewContext.activate(); | |
| 147 | - | |
| 148 | - if (tempConversationContext!=null && !tempConversationContext.isActive()) | |
| 149 | - tempConversationContext.activate(); | |
| 150 | - | |
| 151 | - registered = true; | |
| 152 | - } | |
| 153 | - | |
| 154 | 121 | for (Iterator<AnnotatedMethodProcessor> iter = processors.iterator(); iter.hasNext();) { |
| 155 | 122 | AnnotatedMethodProcessor<?> processor = iter.next(); |
| 156 | 123 | |
| ... | ... | @@ -167,27 +134,63 @@ public abstract class AbstractLifecycleBootstrap<A extends Annotation> implement |
| 167 | 134 | failure = cause; |
| 168 | 135 | } |
| 169 | 136 | } |
| 137 | + | |
| 138 | + stopContexts(); | |
| 170 | 139 | |
| 171 | - if (processors.isEmpty()) { | |
| 172 | - if (requestActivatedHere){ | |
| 173 | - tempRequestContext.deactivate(); | |
| 140 | + if (failure != null) { | |
| 141 | + throw new DemoiselleException(failure); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + private void startContexts(){ | |
| 146 | + if (!registered){ | |
| 147 | + RequestContext requestContext = Beans.getReference(RequestContext.class); | |
| 148 | + SessionContext sessionContext = Beans.getReference(SessionContext.class); | |
| 149 | + ViewContext viewContext = Beans.getReference(ViewContext.class); | |
| 150 | + ConversationContext conversationContext = Beans.getReference(ConversationContext.class); | |
| 151 | + | |
| 152 | + if (requestContext!=null){ | |
| 153 | + startedContextHere.put("request", requestContext.activate()); | |
| 174 | 154 | } |
| 175 | 155 | |
| 176 | - if (sessionActivatedHere){ | |
| 177 | - tempSessionContext.deactivate(); | |
| 156 | + if (sessionContext!=null){ | |
| 157 | + startedContextHere.put("session", sessionContext.activate()); | |
| 178 | 158 | } |
| 179 | 159 | |
| 180 | - if (viewActivatedHere){ | |
| 181 | - tempViewContext.deactivate(); | |
| 160 | + if (viewContext!=null){ | |
| 161 | + startedContextHere.put("view", viewContext.activate()); | |
| 182 | 162 | } |
| 183 | 163 | |
| 184 | - if (conversationActivatedHere){ | |
| 185 | - tempConversationContext.deactivate(); | |
| 164 | + if (conversationContext!=null){ | |
| 165 | + startedContextHere.put("conversation", conversationContext.activate()); | |
| 186 | 166 | } |
| 167 | + | |
| 168 | + registered = true; | |
| 187 | 169 | } |
| 188 | - | |
| 189 | - if (failure != null) { | |
| 190 | - throw new DemoiselleException(failure); | |
| 170 | + } | |
| 171 | + | |
| 172 | + private void stopContexts(){ | |
| 173 | + if (registered){ | |
| 174 | + RequestContext requestContext = Beans.getReference(RequestContext.class); | |
| 175 | + SessionContext sessionContext = Beans.getReference(SessionContext.class); | |
| 176 | + ViewContext viewContext = Beans.getReference(ViewContext.class); | |
| 177 | + ConversationContext conversationContext = Beans.getReference(ConversationContext.class); | |
| 178 | + | |
| 179 | + if (requestContext!=null && Boolean.TRUE.equals(startedContextHere.get("request"))){ | |
| 180 | + requestContext.deactivate(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + if (sessionContext!=null && Boolean.TRUE.equals(startedContextHere.get("session"))){ | |
| 184 | + sessionContext.deactivate(); | |
| 185 | + } | |
| 186 | + | |
| 187 | + if (viewContext!=null && Boolean.TRUE.equals(startedContextHere.get("view"))){ | |
| 188 | + viewContext.deactivate(); | |
| 189 | + } | |
| 190 | + | |
| 191 | + if (conversationContext!=null && Boolean.TRUE.equals(startedContextHere.get("conversation"))){ | |
| 192 | + conversationContext.deactivate(); | |
| 193 | + } | |
| 191 | 194 | } |
| 192 | 195 | } |
| 193 | 196 | } | ... | ... |