Skip to content

Commit 8e41320

Browse files
mkoubajharting
authored andcommitted
WELD-1802 An exception during context deactivation/dissociation should
not abort further procesing
1 parent 6808b11 commit 8e41320

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

Diff for: impl/src/main/java/org/jboss/weld/logging/ServletLogger.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
import static org.jboss.weld.logging.WeldLogger.WELD_PROJECT_CODE;
2020

21-
import javax.enterprise.context.spi.Context;
22-
import javax.servlet.http.HttpServletRequest;
23-
2421
import org.jboss.logging.Logger;
2522
import org.jboss.logging.Logger.Level;
2623
import org.jboss.logging.annotations.Cause;
@@ -60,8 +57,8 @@ public interface ServletLogger extends WeldLogger {
6057
void webXmlMappingPatternIgnored(String pattern);
6158

6259
@LogMessage(level = Level.WARN)
63-
@Message(id = 712, value = "Unable to dissociate context {0} when destroying request {1}", format = Format.MESSAGE_FORMAT)
64-
void unableToDissociateContext(Context context, HttpServletRequest request);
60+
@Message(id = 712, value = "Unable to dissociate context {0} from the storage {1}", format = Format.MESSAGE_FORMAT)
61+
void unableToDissociateContext(Object context, Object storage);
6562

6663
@Message(id = 713, value = "Unable to inject ServletContext. None is associated with {0}, {1}", format = Format.MESSAGE_FORMAT)
6764
IllegalStateException cannotInjectServletContext(ClassLoader classLoader, ServletContextService service);
@@ -78,4 +75,8 @@ public interface ServletLogger extends WeldLogger {
7875
@Message(id = 716, value = "Running in Servlet 2.x environment. Asynchronous request support is disabled.")
7976
void servlet2Environment();
8077

78+
@LogMessage(level = Level.WARN)
79+
@Message(id = 717, value = "Unable to deactivate context {0} when destroying request {1}", format = Format.MESSAGE_FORMAT)
80+
void unableToDeactivateContext(Object context, Object request);
81+
8182
}

Diff for: impl/src/main/java/org/jboss/weld/servlet/ConversationContextActivator.java

+25-20
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,35 @@ private boolean isContextActivatedInRequest(HttpServletRequest request) {
138138
}
139139

140140
protected void deactivateConversationContext(HttpServletRequest request) {
141-
ConversationContext conversationContext = httpConversationContext();
142-
if (conversationContext.isActive()) {
143-
// Only deactivate the context if one is already active, otherwise we get Exceptions
144-
if (conversationContext instanceof LazyHttpConversationContextImpl) {
145-
LazyHttpConversationContextImpl lazyConversationContext = (LazyHttpConversationContextImpl) conversationContext;
146-
if (!lazyConversationContext.isInitialized()) {
147-
// if this lazy conversation has not been touched yet, just deactivate it
148-
lazyConversationContext.deactivate();
149-
return;
141+
try {
142+
ConversationContext conversationContext = httpConversationContext();
143+
if (conversationContext.isActive()) {
144+
// Only deactivate the context if one is already active, otherwise we get Exceptions
145+
if (conversationContext instanceof LazyHttpConversationContextImpl) {
146+
LazyHttpConversationContextImpl lazyConversationContext = (LazyHttpConversationContextImpl) conversationContext;
147+
if (!lazyConversationContext.isInitialized()) {
148+
// if this lazy conversation has not been touched yet, just deactivate it
149+
lazyConversationContext.deactivate();
150+
return;
151+
}
150152
}
151-
}
152-
boolean isTransient = conversationContext.getCurrentConversation().isTransient();
153-
if (ConversationLogger.LOG.isTraceEnabled()) {
153+
boolean isTransient = conversationContext.getCurrentConversation().isTransient();
154+
if (ConversationLogger.LOG.isTraceEnabled()) {
155+
if (isTransient) {
156+
ConversationLogger.LOG.cleaningUpTransientConversation();
157+
} else {
158+
ConversationLogger.LOG.cleaningUpConversation(conversationContext.getCurrentConversation().getId());
159+
}
160+
}
161+
conversationContext.invalidate();
162+
conversationContext.deactivate();
154163
if (isTransient) {
155-
ConversationLogger.LOG.cleaningUpTransientConversation();
156-
} else {
157-
ConversationLogger.LOG.cleaningUpConversation(conversationContext.getCurrentConversation().getId());
164+
conversationDestroyedEvent.fire(request);
158165
}
159166
}
160-
conversationContext.invalidate();
161-
conversationContext.deactivate();
162-
if (isTransient) {
163-
conversationDestroyedEvent.fire(request);
164-
}
167+
} catch (Exception e) {
168+
ServletLogger.LOG.unableToDeactivateContext(httpConversationContext(), request);
169+
ServletLogger.LOG.catchingDebug(e);
165170
}
166171
}
167172

Diff for: impl/src/main/java/org/jboss/weld/servlet/HttpContextLifecycle.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import org.jboss.weld.Container;
2525
import org.jboss.weld.bootstrap.api.Service;
26+
import org.jboss.weld.context.BoundContext;
27+
import org.jboss.weld.context.ManagedContext;
2628
import org.jboss.weld.context.cache.RequestScopedCache;
2729
import org.jboss.weld.context.http.HttpRequestContext;
2830
import org.jboss.weld.context.http.HttpRequestContextImpl;
@@ -279,24 +281,21 @@ public void requestDestroyed(HttpServletRequest request) {
279281
if (!servletApi.isAsyncSupported() || !servletApi.isAsyncStarted(request)) {
280282
getRequestContext().invalidate();
281283
}
282-
getRequestContext().deactivate();
284+
285+
safelyDeactivate(getRequestContext(), request);
283286
// fire @Destroyed(RequestScoped.class)
284287
requestDestroyedEvent.fire(request);
285-
getSessionContext().deactivate();
288+
289+
safelyDeactivate(getSessionContext(), request);
286290
// fire @Destroyed(SessionScoped.class)
287291
if (!getSessionContext().isValid()) {
288292
sessionDestroyedEvent.fire((HttpSession) request.getAttribute(HTTP_SESSION));
289293
}
290294
} finally {
291-
getRequestContext().dissociate(request);
292-
295+
safelyDissociate(getRequestContext(), request);
293296
// WFLY-1533 Underlying HTTP session may be invalid
294-
try {
295-
getSessionContext().dissociate(request);
296-
} catch (Exception e) {
297-
ServletLogger.LOG.unableToDissociateContext(getSessionContext(), request);
298-
ServletLogger.LOG.catchingDebug(e);
299-
}
297+
safelyDissociate(getSessionContext(), request);
298+
300299
// Catch block is inside the activator method so that we're able to log the context
301300
conversationContextActivator.disassociateConversationContext(request);
302301

@@ -312,6 +311,10 @@ public void setConversationActivationEnabled(boolean conversationActivationEnabl
312311
this.conversationActivationEnabled = conversationActivationEnabled;
313312
}
314313

314+
@Override
315+
public void cleanup() {
316+
}
317+
315318
/**
316319
* Some Servlet containers fire HttpServletListeners for include requests (inner requests caused by calling the include method of RequestDispatcher). This
317320
* causes problems with context shut down as context manipulation is not reentrant. This method detects if this request is an included request or not.
@@ -338,7 +341,22 @@ private boolean isRequestDestroyed(HttpServletRequest request) {
338341
return request.getAttribute(REQUEST_DESTROYED) != null;
339342
}
340343

341-
@Override
342-
public void cleanup() {
344+
private <T> void safelyDissociate(BoundContext<T> context, T storage) {
345+
try {
346+
context.dissociate(storage);
347+
} catch(Exception e) {
348+
ServletLogger.LOG.unableToDissociateContext(context, storage);
349+
ServletLogger.LOG.catchingDebug(e);
350+
}
351+
}
352+
353+
private void safelyDeactivate(ManagedContext context, HttpServletRequest request) {
354+
try {
355+
context.deactivate();
356+
} catch(Exception e) {
357+
ServletLogger.LOG.unableToDeactivateContext(context, request);
358+
ServletLogger.LOG.catchingDebug(e);
359+
}
343360
}
361+
344362
}

0 commit comments

Comments
 (0)