All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.graphql.cdi.context.GraphQLThreadContextProvider Maven / Gradle / Ivy

There is a newer version: 2.11.0
Show newest version
package io.smallrye.graphql.cdi.context;

import java.util.Map;

import org.eclipse.microprofile.context.spi.ThreadContextProvider;
import org.eclipse.microprofile.context.spi.ThreadContextSnapshot;

import io.smallrye.graphql.execution.context.SmallRyeContext;
import io.smallrye.graphql.execution.context.SmallRyeContextManager;

public class GraphQLThreadContextProvider implements ThreadContextProvider {
    public static final String TYPE = "MICROPROFILE_GRAPHQL_CONTEXT";

    @Override
    public ThreadContextSnapshot currentContext(Map map) {
        SmallRyeContext currentSmallRyeContext = SmallRyeContextManager.getCurrentSmallRyeContext();
        return () -> {
            SmallRyeContext current = restore(currentSmallRyeContext);
            return () -> restore(current);
        };
    }

    @Override
    public ThreadContextSnapshot clearedContext(Map map) {
        return () -> {
            SmallRyeContext current = restore(null);
            return () -> restore(current);
        };
    }

    @Override
    public String getThreadContextType() {
        return TYPE;
    }

    private SmallRyeContext restore(SmallRyeContext context) {
        SmallRyeContext currentSmallRyeContext = SmallRyeContextManager.getCurrentSmallRyeContext();
        if (context == null) {
            SmallRyeContextManager.clearCurrentSmallRyeContext();
        } else {
            SmallRyeContextManager.restore(context);
        }
        return currentSmallRyeContext;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy