Search in sources :

Example 1 with ThreadContext

use of org.jboss.resteasy.spi.concurrent.ThreadContext in project resteasy by resteasy.

the class ContextualExecutors method getContexts.

@SuppressWarnings("unchecked")
private static Map<ThreadContext<Object>, Object> getContexts() {
    final Map<ThreadContext<Object>, Object> contexts = new LinkedHashMap<>();
    if (System.getSecurityManager() == null) {
        ServiceLoader.load(ThreadContext.class).forEach(context -> contexts.put(context, context.capture()));
    } else {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            ServiceLoader.load(ThreadContext.class).forEach(context -> contexts.put(context, context.capture()));
            return null;
        });
    }
    // Load any registered providers
    final ThreadContexts threadContexts = ResteasyProviderFactory.getInstance().getContextData(ThreadContexts.class);
    if (threadContexts != null) {
        for (ThreadContext<Object> context : threadContexts.getThreadContexts()) {
            contexts.put(context, context.capture());
        }
    }
    return contexts;
}
Also used : InitialContext(javax.naming.InitialContext) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServiceLoader(java.util.ServiceLoader) Callable(java.util.concurrent.Callable) NamingException(javax.naming.NamingException) ConfigurationFactory(org.jboss.resteasy.spi.config.ConfigurationFactory) PrivilegedAction(java.security.PrivilegedAction) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ThreadContexts(org.jboss.resteasy.spi.concurrent.ThreadContexts) Executors(java.util.concurrent.Executors) LinkedHashMap(java.util.LinkedHashMap) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogMessages(org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages) Map(java.util.Map) Configuration(org.jboss.resteasy.spi.config.Configuration) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AccessController(java.security.AccessController) ThreadFactory(java.util.concurrent.ThreadFactory) ThreadContext(org.jboss.resteasy.spi.concurrent.ThreadContext) ExecutorService(java.util.concurrent.ExecutorService) ThreadContext(org.jboss.resteasy.spi.concurrent.ThreadContext) ThreadContexts(org.jboss.resteasy.spi.concurrent.ThreadContexts) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ThreadContext

use of org.jboss.resteasy.spi.concurrent.ThreadContext in project resteasy by resteasy.

the class ResteasyProviderFactoryImpl method processProviderContracts.

private void processProviderContracts(Class provider, Integer priorityOverride, boolean isBuiltin, Map<Class<?>, Integer> contracts, Map<Class<?>, Integer> newContracts) {
    clientHelper.processProviderContracts(provider, priorityOverride, isBuiltin, contracts, newContracts);
    serverHelper.processProviderContracts(provider, priorityOverride, isBuiltin, contracts, newContracts);
    if (Utils.isA(provider, ParamConverterProvider.class, contracts)) {
        int priority = Utils.getPriority(priorityOverride, contracts, ParamConverterProvider.class, provider);
        addParameterConverterProvider(provider, isBuiltin, priority);
        newContracts.put(ParamConverterProvider.class, priority);
    }
    if (Utils.isA(provider, ContextResolver.class, contracts)) {
        try {
            int priority = Utils.getPriority(priorityOverride, contracts, ContextResolver.class, provider);
            addContextResolver(provider, isBuiltin, priority);
            newContracts.put(ContextResolver.class, priority);
        } catch (Exception e) {
            throw new RuntimeException(Messages.MESSAGES.unableToInstantiateContextResolver(), e);
        }
    }
    if (Utils.isA(provider, ContextInjector.class, contracts)) {
        try {
            addContextInjector(provider);
            int priority = Utils.getPriority(priorityOverride, contracts, ContextInjector.class, provider);
            newContracts.put(ContextInjector.class, priority);
        } catch (Exception e) {
            throw new RuntimeException(Messages.MESSAGES.unableToInstantiateContextInjector(), e);
        }
    }
    if (Utils.isA(provider, StringParameterUnmarshaller.class, contracts)) {
        addStringParameterUnmarshaller(provider);
        int priority = Utils.getPriority(priorityOverride, contracts, StringParameterUnmarshaller.class, provider);
        newContracts.put(StringParameterUnmarshaller.class, priority);
    }
    if (Utils.isA(provider, InjectorFactory.class, contracts)) {
        try {
            addInjectorFactory(provider);
            newContracts.put(InjectorFactory.class, 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    if (Utils.isA(provider, Feature.class, contracts)) {
        ConstrainedTo constrainedTo = (ConstrainedTo) provider.getAnnotation(ConstrainedTo.class);
        int priority = Utils.getPriority(priorityOverride, contracts, Feature.class, provider);
        if (constrainedTo == null || constrainedTo.value() == getRuntimeType()) {
            addFeature(provider);
        }
        newContracts.put(Feature.class, priority);
    }
    if (Utils.isA(provider, ResourceClassProcessor.class, contracts)) {
        int priority = Utils.getPriority(priorityOverride, contracts, ResourceClassProcessor.class, provider);
        addResourceClassProcessor(provider, priority);
        newContracts.put(ResourceClassProcessor.class, priority);
    }
    if (Utils.isA(provider, HeaderDelegate.class, contracts)) {
        addHeaderDelegate(provider);
    }
    if (Utils.isA(provider, ThreadContext.class, contracts)) {
        ResteasyContext.computeIfAbsent(ThreadContexts.class, ThreadContexts::new).add(createProviderInstance((Class<? extends ThreadContext>) provider));
    }
}
Also used : ConstrainedTo(jakarta.ws.rs.ConstrainedTo) ThreadContext(org.jboss.resteasy.spi.concurrent.ThreadContext) ThreadContexts(org.jboss.resteasy.spi.concurrent.ThreadContexts)

Aggregations

ThreadContext (org.jboss.resteasy.spi.concurrent.ThreadContext)2 ThreadContexts (org.jboss.resteasy.spi.concurrent.ThreadContexts)2 ConstrainedTo (jakarta.ws.rs.ConstrainedTo)1 AccessController (java.security.AccessController)1 PrivilegedAction (java.security.PrivilegedAction)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ServiceLoader (java.util.ServiceLoader)1 Callable (java.util.concurrent.Callable)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Executor (java.util.concurrent.Executor)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 InitialContext (javax.naming.InitialContext)1