Search in sources :

Example 1 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project wildfly-camel by wildfly-extras.

the class UndertowHTTPDestination method doService.

public void doService(ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    if (context == null) {
        context = servletContext;
    }
    ClassLoaderHolder origLoader = null;
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        invoke(null, context, req, resp);
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

Example 2 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project tomee by apache.

the class AutomaticWorkQueueImpl method execute.

public void execute(final Runnable command) {
    // Grab the context classloader of this thread.   We'll make sure we use that
    // on the thread the runnable actually runs on.
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Runnable r = new Runnable() {

        public void run() {
            ClassLoaderHolder orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            try {
                command.run();
            } finally {
                if (orig != null) {
                    orig.reset();
                }
            }
        }
    };
    // The ThreadPoolExecutor in the JDK doesn't expand the number
    // of threads until the queue is full.   However, we would
    // prefer the number of threads to expand immediately and
    // only uses the queue if we've reached the maximum number
    // of threads.
    ThreadPoolExecutor ex = getExecutor();
    ex.execute(r);
    if (addWorkerMethod != null && !ex.getQueue().isEmpty() && this.approxThreadCount.get() < highWaterMark && addThreadLock.tryLock()) {
        try {
            mainLock.lock();
            try {
                int ps = this.getPoolSize();
                int sz = executor.getQueue().size();
                int sz2 = this.getActiveCount();
                if ((sz + sz2) > ps) {
                    // Needs --add-opens java.base/java.util.concurrent=ALL-UNNAMED for JDK16+
                    ReflectionUtil.setAccessible(addWorkerMethod).invoke(executor, addWorkerArgs);
                }
            } catch (Exception exc) {
            // ignore
            } finally {
                mainLock.unlock();
            }
        } finally {
            addThreadLock.unlock();
        }
    }
}
Also used : ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project tomee by apache.

the class WebClient method doChainedInvocation.

// CHECKSTYLE:OFF
protected // NOPMD
Response doChainedInvocation(// NOPMD
String httpMethod, MultivaluedMap<String, String> headers, Object body, Class<?> requestClass, Type inType, Annotation[] inAnns, Class<?> respClass, Type outType, Exchange exchange, Map<String, Object> invContext) {
    // CHECKSTYLE:ON
    Bus configuredBus = getConfiguration().getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(configuredBus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = configuredBus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Message m = finalizeMessage(httpMethod, headers, body, requestClass, inType, inAnns, respClass, outType, exchange, invContext);
        doRunInterceptorChain(m);
        return doResponse(m, respClass, outType);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != configuredBus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

Example 4 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project tomee by apache.

the class AbstractFaultChainInitiatorObserver method onMessage.

public void onMessage(Message message) {
    assert null != message;
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Exchange exchange = message.getExchange();
        Message faultMessage;
        if (isOutboundObserver()) {
            Exception ex = message.getContent(Exception.class);
            if (!(ex instanceof Fault)) {
                ex = new Fault(ex);
            }
            FaultMode mode = message.get(FaultMode.class);
            faultMessage = exchange.getOutMessage();
            if (null == faultMessage) {
                faultMessage = new MessageImpl();
                faultMessage.setExchange(exchange);
                faultMessage = exchange.getEndpoint().getBinding().createMessage(faultMessage);
            }
            faultMessage.setContent(Exception.class, ex);
            if (null != mode) {
                faultMessage.put(FaultMode.class, mode);
            }
            // CXF-3981
            if (message.get("javax.xml.ws.addressing.context.inbound") != null) {
                faultMessage.put("javax.xml.ws.addressing.context.inbound", message.get("javax.xml.ws.addressing.context.inbound"));
            }
            exchange.setOutMessage(null);
            exchange.setOutFaultMessage(faultMessage);
            if (message.get(BindingFaultInfo.class) != null) {
                faultMessage.put(BindingFaultInfo.class, message.get(BindingFaultInfo.class));
            }
        } else {
            faultMessage = message;
            exchange.setInMessage(null);
            exchange.setInFaultMessage(faultMessage);
        }
        // setup chain
        PhaseInterceptorChain chain = new PhaseInterceptorChain(getPhases());
        initializeInterceptors(faultMessage.getExchange(), chain);
        faultMessage.setInterceptorChain(chain);
        try {
            chain.doIntercept(faultMessage);
        } catch (RuntimeException exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw exc;
        } catch (Exception exc) {
            LOG.log(Level.SEVERE, "ERROR_DURING_ERROR_PROCESSING", exc);
            throw new RuntimeException(exc);
        }
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) FaultMode(org.apache.cxf.message.FaultMode) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) MessageImpl(org.apache.cxf.message.MessageImpl) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo)

Example 5 with ClassLoaderHolder

use of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder in project cxf by apache.

the class AutomaticWorkQueueImpl method execute.

public void execute(final Runnable command) {
    // Grab the context classloader of this thread.   We'll make sure we use that
    // on the thread the runnable actually runs on.
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Runnable r = new Runnable() {

        public void run() {
            ClassLoaderHolder orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            try {
                command.run();
            } finally {
                if (orig != null) {
                    orig.reset();
                }
            }
        }
    };
    // The ThreadPoolExecutor in the JDK doesn't expand the number
    // of threads until the queue is full.   However, we would
    // prefer the number of threads to expand immediately and
    // only uses the queue if we've reached the maximum number
    // of threads.
    ThreadPoolExecutor ex = getExecutor();
    ex.execute(r);
    if (addWorkerMethod != null && !ex.getQueue().isEmpty() && this.approxThreadCount.get() < highWaterMark && addThreadLock.tryLock()) {
        try {
            mainLock.lock();
            try {
                int ps = this.getPoolSize();
                int sz = executor.getQueue().size();
                int sz2 = this.getActiveCount();
                if ((sz + sz2) > ps) {
                    // Needs --add-opens java.base/java.util.concurrent=ALL-UNNAMED for JDK16+
                    ReflectionUtil.setAccessible(addWorkerMethod).invoke(executor, addWorkerArgs);
                }
            } catch (Exception exc) {
            // ignore
            } finally {
                mainLock.unlock();
            }
        } finally {
            addThreadLock.unlock();
        }
    }
}
Also used : ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)36 Bus (org.apache.cxf.Bus)24 Message (org.apache.cxf.message.Message)15 IOException (java.io.IOException)6 Exchange (org.apache.cxf.message.Exchange)6 PhaseInterceptorChain (org.apache.cxf.phase.PhaseInterceptorChain)6 ServerImpl (org.apache.cxf.endpoint.ServerImpl)5 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)5 Endpoint (org.apache.cxf.endpoint.Endpoint)4 URISyntaxException (java.net.URISyntaxException)3 List (java.util.List)3 Application (javax.ws.rs.core.Application)3 Fault (org.apache.cxf.interceptor.Fault)3 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 URI (java.net.URI)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2