Search in sources :

Example 26 with ClassLoaderHolder

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

the class ColocMessageObserver method onMessage.

public void onMessage(Message m) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("Processing Message at collocated endpoint.  Request message: " + m);
        }
        Exchange ex = new ExchangeImpl();
        setExchangeProperties(ex, m);
        Message inMsg = endpoint.getBinding().createMessage();
        MessageImpl.copyContent(m, inMsg);
        // Copy Request Context to Server inBound Message
        // TODO a Context Filter Strategy required.
        inMsg.putAll(m);
        inMsg.put(COLOCATED, Boolean.TRUE);
        inMsg.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
        inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
        BindingOperationInfo boi = ex.getBindingOperationInfo();
        OperationInfo oi = boi != null ? boi.getOperationInfo() : null;
        if (oi != null) {
            inMsg.put(MessageInfo.class, oi.getInput());
        }
        ex.setInMessage(inMsg);
        inMsg.setExchange(ex);
        if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Build inbound interceptor chain.");
        }
        // Add all interceptors between USER_LOGICAL and INVOKE.
        SortedSet<Phase> phases = new TreeSet<Phase>(bus.getExtension(PhaseManager.class).getInPhases());
        ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.INVOKE);
        InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, phases);
        chain.add(addColocInterceptors());
        inMsg.setInterceptorChain(chain);
        // Convert the coloc object type if necessary
        BindingOperationInfo bop = m.getExchange().getBindingOperationInfo();
        OperationInfo soi = bop != null ? bop.getOperationInfo() : null;
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class) && !ColocUtil.isAssignableOperationInfo(oi, Source.class)) {
                // converting source -> pojo
                ColocUtil.convertSourceToObject(inMsg);
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class) && !ColocUtil.isAssignableOperationInfo(soi, Source.class)) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(inMsg);
            }
        }
        chain.doIntercept(inMsg);
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class) && !ColocUtil.isAssignableOperationInfo(oi, Source.class) && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(ex.getOutMessage());
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class) && !ColocUtil.isAssignableOperationInfo(soi, Source.class) && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertSourceToObject(ex.getOutMessage());
            }
        }
        // Set Server OutBound Message onto InBound Exchange.
        setOutBoundMessage(ex, m.getExchange());
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) Bus(org.apache.cxf.Bus) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Phase(org.apache.cxf.phase.Phase) Message(org.apache.cxf.message.Message) TreeSet(java.util.TreeSet) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Source(javax.xml.transform.Source)

Example 27 with ClassLoaderHolder

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

the class EndpointImpl method doPublish.

/**
 * Performs the publication action by setting up a {@link Server}
 * instance based on this endpoint's configuration.
 *
 * @param addr the optional endpoint address.
 *
 * @throws IllegalStateException if the endpoint cannot be published/republished
 * @throws SecurityException if permission checking is enabled and policy forbids publishing
 * @throws WebServiceException if there is an error publishing the endpoint
 *
 * @see #checkPublishPermission()
 * @see #checkPublishable()
 * @see #getServer(String)
 */
protected void doPublish(String addr) {
    checkPublishPermission();
    checkPublishable();
    ServerImpl serv = null;
    ClassLoaderHolder loader = null;
    try {
        if (bus != null) {
            ClassLoader newLoader = bus.getExtension(ClassLoader.class);
            if (newLoader != null) {
                loader = ClassLoaderUtils.setThreadContextClassloader(newLoader);
            }
        }
        serv = getServer(addr);
        if (addr != null) {
            EndpointInfo endpointInfo = serv.getEndpoint().getEndpointInfo();
            if (endpointInfo.getAddress() == null || !endpointInfo.getAddress().contains(addr)) {
                endpointInfo.setAddress(addr);
            }
            if (publishedEndpointUrl != null) {
                endpointInfo.setProperty(WSDLGetUtils.PUBLISHED_ENDPOINT_URL, publishedEndpointUrl);
            }
            if (publishedEndpointUrl != null && wsdlLocation != null) {
                // early update the publishedEndpointUrl so that endpoints in the same app sharing the same wsdl
                // do not require all of them to be queried for wsdl before the wsdl is finally fully updated
                Definition def = endpointInfo.getService().getProperty(WSDLServiceBuilder.WSDL_DEFINITION, Definition.class);
                if (def == null) {
                    def = bus.getExtension(WSDLManager.class).getDefinition(wsdlLocation);
                }
                new WSDLGetUtils().updateWSDLPublishedEndpointAddress(def, endpointInfo);
            }
            if (null != properties) {
                for (Entry<String, Object> entry : properties.entrySet()) {
                    endpointInfo.setProperty(entry.getKey(), entry.getValue());
                }
            }
            this.address = endpointInfo.getAddress();
        }
        serv.start();
        publishable = false;
    } catch (Exception ex) {
        try {
            stop();
        } catch (Exception e) {
        // Nothing we can do.
        }
        throw new WebServiceException(ex);
    } finally {
        if (loader != null) {
            loader.reset();
        }
    }
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ServerImpl(org.apache.cxf.endpoint.ServerImpl) WebServiceException(javax.xml.ws.WebServiceException) Definition(javax.wsdl.Definition) WSDLGetUtils(org.apache.cxf.frontend.WSDLGetUtils) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) WebServiceException(javax.xml.ws.WebServiceException)

Example 28 with ClassLoaderHolder

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

the class JaxWsProxyFactoryBean method create.

/**
 * Creates a JAX-WS proxy that can be used to make remote invocations.
 *
 * @return the proxy. You must cast the returned object to the approriate class
 * before making remote calls
 */
@Override
public synchronized Object create() {
    ClassLoaderHolder orig = null;
    try {
        if (getBus() != null) {
            ClassLoader loader = getBus().getExtension(ClassLoader.class);
            if (loader != null) {
                orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
        }
        Object obj = super.create();
        Service service = getServiceFactory().getService();
        if (needWrapperClassInterceptor(service.getServiceInfos().get(0))) {
            List<Interceptor<? extends Message>> in = super.getInInterceptors();
            List<Interceptor<? extends Message>> out = super.getOutInterceptors();
            in.add(new WrapperClassInInterceptor());
            in.add(new HolderInInterceptor());
            out.add(new WrapperClassOutInterceptor());
            out.add(new HolderOutInterceptor());
        }
        return obj;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) HolderInInterceptor(org.apache.cxf.jaxws.interceptors.HolderInInterceptor) HolderOutInterceptor(org.apache.cxf.jaxws.interceptors.HolderOutInterceptor) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Service(org.apache.cxf.service.Service) WrapperClassOutInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor) WrapperClassOutInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassOutInterceptor) HolderInInterceptor(org.apache.cxf.jaxws.interceptors.HolderInInterceptor) HolderOutInterceptor(org.apache.cxf.jaxws.interceptors.HolderOutInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) WrapperClassInInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassInInterceptor) WrapperClassInInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassInInterceptor)

Example 29 with ClassLoaderHolder

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

the class CXFNonSpringServlet method invoke.

@Override
protected void invoke(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    ClassLoaderHolder origLoader = null;
    Bus origBus = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (bus != null) {
            origBus = BusFactory.getAndSetThreadDefaultBus(bus);
        }
        controller.invoke(request, response);
    } 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 30 with ClassLoaderHolder

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

the class ServletController method invoke.

public boolean invoke(HttpServletRequest request, HttpServletResponse res, boolean returnErrors) throws ServletException {
    try {
        String pathInfo = request.getPathInfo() == null ? "" : request.getPathInfo();
        AbstractHTTPDestination d = destinationRegistry.getDestinationForPath(pathInfo, true);
        if (d == null) {
            if (!isHideServiceList && (request.getRequestURI().endsWith(serviceListRelativePath) || request.getRequestURI().endsWith(serviceListRelativePath + "/") || StringUtils.isEmpty(pathInfo) || "/".equals(pathInfo))) {
                if (isAuthServiceListPage) {
                    setAuthServiceListPageAttribute(request);
                }
                setBaseURLAttribute(request);
                serviceListGenerator.service(request, res);
            } else {
                d = destinationRegistry.checkRestfulRequest(pathInfo);
                if (d == null || d.getMessageObserver() == null) {
                    if (returnErrors) {
                        LOG.warning("Can't find the request for " + request.getRequestURL() + "'s Observer ");
                        generateNotFound(request, res);
                    }
                    return false;
                }
            }
        }
        if (d != null && d.getMessageObserver() != null) {
            Bus bus = d.getBus();
            ClassLoaderHolder orig = null;
            try {
                if (bus != null) {
                    ClassLoader loader = bus.getExtension(ClassLoader.class);
                    if (loader == null) {
                        ResourceManager manager = bus.getExtension(ResourceManager.class);
                        if (manager != null) {
                            loader = manager.resolveResource("", ClassLoader.class);
                        }
                    }
                    if (loader != null) {
                        // need to set the context classloader to the loader of the bundle
                        orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                    }
                }
                updateDestination(request, d);
                invokeDestination(request, res, d);
            } finally {
                if (orig != null) {
                    orig.reset();
                }
            }
        }
    } catch (IOException e) {
        throw new ServletException(e);
    }
    return true;
}
Also used : ServletException(javax.servlet.ServletException) Bus(org.apache.cxf.Bus) AbstractHTTPDestination(org.apache.cxf.transport.http.AbstractHTTPDestination) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ResourceManager(org.apache.cxf.resource.ResourceManager) IOException(java.io.IOException)

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