org.eclipse.microprofile.context.spi.ThreadContextSnapshot Java Examples

The following examples show how to use org.eclipse.microprofile.context.spi.ThreadContextSnapshot. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArcContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public ThreadContextSnapshot currentContext(Map<String, String> map) {
    ArcContainer arc = Arc.container();
    if (arc == null || !arc.isRunning()) {
        //return null as per docs to state that propagation of this context is not supported
        return null;
    }

    // capture the state, null indicates no active context while capturing snapshot
    InjectableContext.ContextState state = null;
    ManagedContext requestContext = arc.requestContext();
    if (requestContext.isActive()) {
        state = requestContext.getState();
    }
    if (state == null) {
        return NULL_CONTEXT_SNAPSHOT;
    } else {
        return new ContextSnapshot(state);
    }
}
 
Example #2
Source File: LabelContextProvider.java    From microprofile-context-propagation with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a snapshot of context for the specified 'label'
 */
private ThreadContextSnapshot snapshot(String label) {
    return () -> {
        String labelToRestore = Label.get();
        AtomicBoolean restored = new AtomicBoolean();

        // Construct an instance that restores the previous context that was on the thread
        // prior to applying the specified 'label' as the new context.
        ThreadContextController contextRestorer = () -> {
            if (restored.compareAndSet(false, true)) {
                Label.set(labelToRestore);
            }
            else {
                throw new IllegalStateException();
            }
        };

        Label.set(label);
        return contextRestorer;
    };
}
 
Example #3
Source File: CustomContextProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private ThreadContextSnapshot snapshot(String label) {
    return () -> {
        String labelToRestore = CustomContext.get();
        AtomicBoolean restored = new AtomicBoolean();

        // Construct an instance that restores the previous context that was on the thread
        // prior to applying the specified 'label' as the new context.
        ThreadContextController contextRestorer = () -> {
            if (restored.compareAndSet(false, true)) {
                CustomContext.set(labelToRestore);
            } else {
                throw new IllegalStateException();
            }
        };

        CustomContext.set(label);
        return contextRestorer;
    };
}
 
Example #4
Source File: MyThreadContextProvider.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return () -> {
        MyContext movedContext = MyContext.get();
        MyContext.clear();
        return () -> {
            if (movedContext == null)
                MyContext.clear();
            else
                MyContext.set(movedContext);
        };
    };
}
 
Example #5
Source File: ResteasyContextProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    Map<Class<?>, Object> context = Collections.emptyMap();
    return () -> {
        ResteasyContext.pushContextDataMap(context);
        return () -> {
            ResteasyContext.removeContextDataLevel();
        };
    };
}
 
Example #6
Source File: ResteasyContextProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    Map<Class<?>, Object> context = ResteasyContext.getContextDataMap(false);
    if (context == null) {
        return null;
    }
    return () -> {
        ResteasyContext.pushContextDataMap(context);
        return () -> {
            ResteasyContext.removeContextDataLevel();
        };
    };
}
 
Example #7
Source File: ServletThreadContextProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return () -> {
        ServletRequestContext current = restore(null);
        return () -> restore(current);
    };
}
 
Example #8
Source File: ArcContextProvider.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> map) {
    // note that by cleared we mean that we still activate context if need be, just leave the contents blank
    ArcContainer arc = Arc.container();
    if (arc == null || !arc.isRunning()) {
        //return null as per docs to state that propagation of this context is not supported
        return null;
    }
    return CLEAR_CONTEXT_SNAPSHOT;
}
 
Example #9
Source File: TracingContextProvider.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return () -> {
        Tracer tracer = GlobalTracer.get();
        ScopeManager scopeManager = tracer.scopeManager();
        Scope activeScope = scopeManager.active();
        if (activeScope != null) {
            activeScope.close();
        }
        return () -> {
            // TODO: we should bring back the span here
        };
    };
}
 
Example #10
Source File: TracingContextProvider.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    Tracer tracer = GlobalTracer.get();
    ScopeManager scopeManager = tracer.scopeManager();
    Scope activeScope = scopeManager.active();

    if (activeScope != null) {
        Span span = activeScope.span();
        return () -> {
            Scope propagated = scopeManager.activate(span, false);
            return propagated::close;
        };
    }
    return () -> DO_NOTHING;
}
 
Example #11
Source File: MyThreadContextProvider.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    MyContext capturedContext = MyContext.get();
    return () -> {
        MyContext movedContext = MyContext.get();
        MyContext.set(capturedContext);
        return () -> {
            MyContext.set(movedContext);
        };
    };
}
 
Example #12
Source File: BufferContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Save the buffer instance that is associated with the current thread.
 */
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    return new BufferContextSnapshot(Buffer.get());
}
 
Example #13
Source File: ThreadPriorityContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Thread priority context is considered cleared by resetting to normal priority (Thread.NORM_PRIORITY).
 */
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return new ThreadPrioritySnapshot(Thread.NORM_PRIORITY);
}
 
Example #14
Source File: ThreadPriorityContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Save the current thread priority.
 */
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    return new ThreadPrioritySnapshot(Thread.currentThread().getPriority());
}
 
Example #15
Source File: CustomContextProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return snapshot("");
}
 
Example #16
Source File: CustomContextProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    return snapshot(CustomContext.get());
}
 
Example #17
Source File: BufferContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Buffer context is considered cleared by associating a new empty StringBuffer with the thread.
 */
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return new BufferContextSnapshot(null);
}
 
Example #18
Source File: LabelContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Save the current context.
 */
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
    return snapshot(Label.get());
}
 
Example #19
Source File: LabelContextProvider.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Cleared context is the empty string.
 */
@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
    return snapshot("");
}