Skip to content

Commit

Permalink
ArC - add debug logging for the request context manipulation
Browse files Browse the repository at this point in the history
- activation, deactivation and destruction
  • Loading branch information
mkouba committed Aug 11, 2022
1 parent ba3587b commit 0abc74c
Showing 1 changed file with 32 additions and 3 deletions.
Expand Up @@ -31,19 +31,23 @@
*/
class RequestContext implements ManagedContext {

private static final Logger LOGGER = Logger.getLogger(RequestContext.class.getPackage().getName());
private static final Logger LOG = Logger.getLogger(RequestContext.class.getPackage().getName());

private final CurrentContext<RequestContextState> currentContext;

private final LazyValue<Notifier<Object>> initializedNotifier;
private final LazyValue<Notifier<Object>> beforeDestroyedNotifier;
private final LazyValue<Notifier<Object>> destroyedNotifier;

private final boolean debugEnabled;

public RequestContext(CurrentContext<RequestContextState> currentContext) {
this.currentContext = currentContext;
this.initializedNotifier = new LazyValue<>(RequestContext::createInitializedNotifier);
this.beforeDestroyedNotifier = new LazyValue<>(RequestContext::createBeforeDestroyedNotifier);
this.destroyedNotifier = new LazyValue<>(RequestContext::createDestroyedNotifier);
// we do not need to check the effective log level
this.debugEnabled = LOG.isDebugEnabled();
}

@Override
Expand Down Expand Up @@ -122,6 +126,15 @@ public void destroy(Contextual<?> contextual) {

@Override
public void activate(ContextState initialState) {
if (debugEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.debugf("Activate %s %s\n\t...",
initialState != null ? Integer.toHexString(initialState.hashCode()) : "new", stack);
}
if (initialState == null) {
currentContext.set(new RequestContextState(new ConcurrentHashMap<>()));
// Fire an event with qualifier @Initialized(RequestScoped.class) if there are any observers for it
Expand Down Expand Up @@ -151,6 +164,14 @@ public ContextState getStateIfActive() {

@Override
public void deactivate() {
if (debugEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.debugf("Deactivate%s\n\t...", stack);
}
currentContext.remove();
}

Expand All @@ -161,6 +182,14 @@ public void destroy() {

@Override
public void destroy(ContextState state) {
if (debugEnabled) {
String stack = Arrays.stream(Thread.currentThread().getStackTrace())
.skip(2)
.limit(7)
.map(se -> "\n\t" + se.toString())
.collect(Collectors.joining());
LOG.debugf("Destroy %s%s\n\t...", state != null ? Integer.toHexString(state.hashCode()) : "", stack);
}
if (state == null) {
// nothing to destroy
return;
Expand All @@ -174,15 +203,15 @@ public void destroy(ContextState state) {
try {
fireIfNotEmpty(beforeDestroyedNotifier);
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @BeforeDestroyed(RequestScoped.class) event", e);
LOG.warn("An error occurred during delivery of the @BeforeDestroyed(RequestScoped.class) event", e);
}
//Performance: avoid an iterator on the map elements
map.forEach(this::destroyContextElement);
// Fire an event with qualifier @Destroyed(RequestScoped.class) if there are any observers for it
try {
fireIfNotEmpty(destroyedNotifier);
} catch (Exception e) {
LOGGER.warn("An error occurred during delivery of the @Destroyed(RequestScoped.class) event", e);
LOG.warn("An error occurred during delivery of the @Destroyed(RequestScoped.class) event", e);
}
map.clear();
}
Expand Down

0 comments on commit 0abc74c

Please sign in to comment.