Java Code Examples for org.eclipse.microprofile.context.spi.ContextManagerProvider#instance()

The following examples show how to use org.eclipse.microprofile.context.spi.ContextManagerProvider#instance() . 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: SmallRyeContextPropagationRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public void configureRuntime(BeanContainer container, ExecutorService executorService) {
    // associate the static init manager to the runtime CL
    ContextManagerProvider contextManagerProvider = ContextManagerProvider.instance();
    // finish building our manager
    builder.withDefaultExecutorService(executorService);

    SmallRyeContextManager contextManager = builder.build();

    contextManagerProvider.registerContextManager(contextManager, Thread.currentThread().getContextClassLoader());

    // initialise injection
    SmallRyeContextPropagationProvider cpProvider = container.instance(SmallRyeContextPropagationProvider.class);
    cpProvider.initialize(executorService);
}
 
Example 2
Source File: ContextManagerTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
/**
 * Verify obtaining a ContextManager builder from the ContextManagerProvider. Then validate building,
 * registering, and releasing a custom ContextManager. If a ContextManager builder is not supported
 * then this test will no-op.
 */
@Test
public void builderForContextManagerIsProvided() {
    ContextManagerProvider provider = ContextManagerProvider.instance();
    ClassLoader classLoader = ContextManagerTest.class.getClassLoader();
    Builder contextManagerBuilder = null;

    try {
        //obtain the ContextManagerBuilder
        contextManagerBuilder = provider.getContextManagerBuilder();
        Assert.assertNotNull(contextManagerBuilder,
                "MicroProfile Context Propagation implementation does not provide a ContextManager builder.");
    } 
    catch (UnsupportedOperationException ex1) {
        //ContextManagerProvider.getContextManagerBuilder() support is optional.
        System.out.println("ContextManagerProvider.getContextManagerBuilder is not supported.");

        //Verify ContextManagerProvider.registerContextManager() is also unsupported.
        try {
            provider.registerContextManager(provider.getContextManager(), classLoader);
            Assert.fail("ContextManagerProvider.registerContextManager should not be supported, "
                    + "if ContextManagerProvider.getContextManagerBuilder is not supported.");
        }
        catch (UnsupportedOperationException ex2) {
            System.out.println("ContextManagerProvider.registerContextManager is not supported.");
        }

        //Verify ContextManagerProvider.releaseContextManager() is also unsupported.
        try {
            provider.releaseContextManager(provider.getContextManager());
            Assert.fail("ContextManagerProvider.releaseContextManager should not be supported, "
                    + "if ContextManagerProvider.getContextManagerBuilder is not supported.");
        }
        catch (UnsupportedOperationException ex3) {
            System.out.println("ContextManagerProvider.releaseContextManager is not supported.");
        }

        //Unsupported path of test has passed.
        return;
    }   

    //build and register a ContextManager
    ContextManager builtManager = contextManagerBuilder.build();
    provider.registerContextManager(builtManager, classLoader);
    ContextManager registeredManager = provider.getContextManager(classLoader);
    Assert.assertEquals(builtManager, registeredManager,
            "ContextManagerProvider.getContextManager(classLoader) did not return the same manager that was registered.");

    //release the ContextManager
    provider.releaseContextManager(registeredManager);
    Assert.assertNotEquals(builtManager, provider.getContextManager(classLoader),
            "ContextManager was not released from the ContextManagerProvider.");
}
 
Example 3
Source File: TckTest.java    From microprofile-context-propagation with Apache License 2.0 4 votes vote down vote up
@Test
public void providerSet() {
    ContextManagerProvider provider = ContextManagerProvider.instance();
    Assert.assertNotNull(provider, "ContextManagerProvider is not set");
}