Search in sources :

Example 21 with ClassLoaderHolder

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

the class ChainInitiationObserver method onMessage.

public void onMessage(Message m) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        InterceptorChain phaseChain = null;
        if (m.getInterceptorChain() != null) {
            phaseChain = m.getInterceptorChain();
            // To make sure the phase chain is run by one thread once
            synchronized (phaseChain) {
                if (phaseChain.getState() == InterceptorChain.State.PAUSED || phaseChain.getState() == InterceptorChain.State.SUSPENDED) {
                    phaseChain.resume();
                    return;
                }
            }
        }
        Message message = getBinding().createMessage(m);
        Exchange exchange = message.getExchange();
        if (exchange == null) {
            exchange = new ExchangeImpl();
            m.setExchange(exchange);
        }
        exchange.setInMessage(message);
        setExchangeProperties(exchange, message);
        InterceptorProvider dbp = null;
        if (endpoint.getService().getDataBinding() instanceof InterceptorProvider) {
            dbp = (InterceptorProvider) endpoint.getService().getDataBinding();
        }
        // setup chain
        if (dbp == null) {
            phaseChain = chainCache.get(bus.getExtension(PhaseManager.class).getInPhases(), bus.getInInterceptors(), endpoint.getService().getInInterceptors(), endpoint.getInInterceptors(), getBinding().getInInterceptors());
        } else {
            phaseChain = chainCache.get(bus.getExtension(PhaseManager.class).getInPhases(), bus.getInInterceptors(), endpoint.getService().getInInterceptors(), endpoint.getInInterceptors(), getBinding().getInInterceptors(), dbp.getInInterceptors());
        }
        message.setInterceptorChain(phaseChain);
        phaseChain.setFaultObserver(endpoint.getOutFaultObserver());
        addToChain(phaseChain, message);
        phaseChain.doIntercept(message);
    } 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) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Message(org.apache.cxf.message.Message) PhaseManager(org.apache.cxf.phase.PhaseManager) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) InterceptorProvider(org.apache.cxf.interceptor.InterceptorProvider) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 22 with ClassLoaderHolder

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

the class MultipleEndpointObserver method onMessage.

public void onMessage(Message message) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        Exchange exchange = message.getExchange();
        if (exchange == null) {
            exchange = new ExchangeImpl();
            exchange.setInMessage(message);
            message.setExchange(exchange);
        }
        message = createMessage(message);
        message.setExchange(exchange);
        exchange.setInMessage(message);
        setExchangeProperties(exchange, message);
        // setup chain
        PhaseInterceptorChain chain = createChain();
        message.setInterceptorChain(chain);
        chain.add(bus.getInInterceptors());
        if (bindingInterceptors != null) {
            chain.add(bindingInterceptors);
        }
        if (routingInterceptors != null) {
            chain.add(routingInterceptors);
        }
        if (endpoints != null) {
            exchange.put(ENDPOINTS, endpoints);
        }
        chain.doIntercept(message);
    } 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) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 23 with ClassLoaderHolder

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

the class JAXRSServerFactoryBean method create.

/**
 * Creates the JAX-RS Server instance
 * @return the server
 */
public Server create() {
    ClassLoaderHolder origLoader = null;
    try {
        Bus bus = getBus();
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        serviceFactory.setBus(bus);
        checkResources(true);
        if (serviceFactory.getService() == null) {
            serviceFactory.create();
        }
        Endpoint ep = createEndpoint();
        getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server);
        server = new ServerImpl(getBus(), ep, getDestinationFactory(), getBindingFactory());
        Invoker invoker = serviceFactory.getInvoker();
        if (invoker == null) {
            ep.getService().setInvoker(createInvoker());
        } else {
            ep.getService().setInvoker(invoker);
        }
        ServerProviderFactory factory = setupFactory(ep);
        ep.put(Application.class.getName(), appProvider);
        factory.setRequestPreprocessor(new RequestPreprocessor(languageMappings, extensionMappings));
        ep.put(Bus.class.getName(), getBus());
        if (documentLocation != null) {
            ep.put(JAXRSUtils.DOC_LOCATION, documentLocation);
        }
        if (rc != null) {
            ep.put("org.apache.cxf.jaxrs.comparator", rc);
        }
        checkPrivateEndpoint(ep);
        applyBusFeatures(getBus());
        applyFeatures();
        updateClassResourceProviders(ep);
        injectContexts(factory);
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, null, null);
        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                // prevent resource leak
                server.destroy();
                throw re;
            }
        }
    } catch (EndpointException e) {
        throw new ServiceConstructionException(e);
    } catch (BusException e) {
        throw new ServiceConstructionException(e);
    } catch (IOException e) {
        throw new ServiceConstructionException(e);
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }
    return server;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Bus(org.apache.cxf.Bus) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) IOException(java.io.IOException) BusException(org.apache.cxf.BusException) IOException(java.io.IOException) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) EndpointException(org.apache.cxf.endpoint.EndpointException) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Invoker(org.apache.cxf.service.invoker.Invoker) EndpointException(org.apache.cxf.endpoint.EndpointException) Application(javax.ws.rs.core.Application) BusException(org.apache.cxf.BusException)

Example 24 with ClassLoaderHolder

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

the class NettyHttpDestination method doService.

protected void doService(ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    if (context == null) {
        context = servletContext;
    }
    if (getServer().isSetRedirectURL()) {
        resp.sendRedirect(getServer().getRedirectURL());
        resp.flushBuffer();
        return;
    }
    // REVISIT: service on executor if associated with endpoint
    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 25 with ClassLoaderHolder

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

the class UndertowHTTPDestination method doService.

protected void doService(ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    if (context == null) {
        context = servletContext;
    }
    HTTPServerPolicy sp = getServer();
    if (sp.isSetRedirectURL()) {
        resp.sendRedirect(sp.getRedirectURL());
        resp.flushBuffer();
        return;
    }
    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) HTTPServerPolicy(org.apache.cxf.transports.http.configuration.HTTPServerPolicy) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)

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)7 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