Java Code Examples for org.apache.cxf.BusFactory#getAndSetThreadDefaultBus()

The following examples show how to use org.apache.cxf.BusFactory#getAndSetThreadDefaultBus() . 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: CXFNonSpringServlet.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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();
        }
    }
}
 
Example 2
Source File: JMSContinuation.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void doResume() {
    suspendendContinuations.decrementAndGet();
    ClassLoaderHolder origLoader = null;
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        incomingObserver.onMessage(inMessage);
    } finally {
        isPending = false;
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}
 
Example 3
Source File: UndertowHTTPDestination.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Override
public void service(ServletContext context, HttpServletRequest req, HttpServletResponse resp) throws IOException {

    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();
        }
    }
}
 
Example 4
Source File: WSDiscoveryClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
private synchronized ServiceImpl getService() {
    if (service == null) {
        Bus b = BusFactory.getAndSetThreadDefaultBus(bus);
        try {
            service = new ServiceImpl(bus, null,
                                      version.getServiceName(),
                                      Service.class);
            service.addPort(version.getServiceName(),
                            soapVersion, address);
        } finally {
            BusFactory.setThreadDefaultBus(b);
        }
    }
    return service;
}
 
Example 5
Source File: WSDiscoveryServiceImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public synchronized boolean startup(boolean optional) {
    String preferIPv4StackValue = System.getProperty("java.net.preferIPv4Stack");
    String preferIPv6AddressesValue = System.getProperty("java.net.preferIPv6Addresses");
    if (!started && client.isAdHoc()) {
        Bus b = BusFactory.getAndSetThreadDefaultBus(bus);
        try {
            udpEndpoint = new EndpointImpl(bus, new WSDiscoveryProvider());
            Map<String, Object> props = new HashMap<>();
            props.put("jaxws.provider.interpretNullAsOneway", "true");
            udpEndpoint.setProperties(props);
            if ("true".equals(preferIPv6AddressesValue) && "false".equals(preferIPv4StackValue)) {
                try {
                    udpEndpoint.publish("soap.udp://[FF02::C]:3702");
                    started = true;
                } catch (Exception e) {
                    LOG.log(Level.WARNING, "Could not start WS-Discovery Service with ipv6 address", e);
                }
            }
            if (!started) {
                udpEndpoint.publish("soap.udp://239.255.255.250:3702");
                started = true;
            }
        } catch (RuntimeException ex) {
            if (!optional) {
                throw ex;
            }
            LOG.log(Level.WARNING, "Could not start WS-Discovery Service.", ex);
        } finally {
            if (b != bus) {
                BusFactory.setThreadDefaultBus(b);
            }
        }
    }
    return true;
}
 
Example 6
Source File: JAXWSHttpSpiDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * This is called by handlers for servicing requests
 *
 * @param req
 * @param resp
 * @throws IOException
 */
protected void doService(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {

    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    try {
        serviceRequest(req, resp);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
 
Example 7
Source File: WebClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Response doChainedInvocation(String httpMethod, //NOPMD
                                       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);
        }
    }
}
 
Example 8
Source File: ClientMessageObserver.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void onMessage(Message m) {

        Message message = cfg.getConduitSelector().getEndpoint().getBinding().createMessage(m);
        message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
        message.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
        PhaseInterceptorChain chain = AbstractClient.setupInInterceptorChain(cfg);
        message.setInterceptorChain(chain);
        message.getExchange().setInMessage(message);
        Bus bus = cfg.getBus();
        Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
        ClassLoaderHolder origLoader = null;
        try {
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            // execute chain
            chain.doIntercept(message);
        } finally {
            if (origBus != bus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
            if (origLoader != null) {
                origLoader.reset();
            }
            synchronized (message.getExchange()) {
                message.getExchange().notifyAll();
            }
        }
    }
 
Example 9
Source File: CXFNonSpringServlet.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
    ClassLoaderHolder origLoader = null;
    Bus origBus = null;
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
        try {
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            if (bus != null) {
                origBus = BusFactory.getAndSetThreadDefaultBus(bus);
            }
            HttpServletRequest httpRequest = (HttpServletRequest)request;
            if (controller.filter(new HttpServletRequestFilter(httpRequest,
                                                               super.getServletName()),
                                  (HttpServletResponse)response)) {
                return;
            }
        } finally {
            if (origBus != bus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
            if (origLoader != null) {
                origLoader.reset();
            }
        }
    }
    chain.doFilter(request, response);
}
 
Example 10
Source File: JettyHTTPDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void doService(ServletContext context,
                         HttpServletRequest req,
                         HttpServletResponse resp) throws IOException {
    if (context == null) {
        context = servletContext;
    }
    Request baseRequest = (req instanceof Request)
        ? (Request)req : getCurrentRequest();

    HTTPServerPolicy sp = getServer();
    if (sp.isSetRedirectURL()) {
        resp.sendRedirect(sp.getRedirectURL());
        resp.flushBuffer();
        baseRequest.setHandled(true);
        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();
        }
    }
}
 
Example 11
Source File: UndertowHTTPDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example 12
Source File: NettyHttpDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example 13
Source File: OSGiJaxwsEndpointManager.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Object setCXFBusInternal() {
    return BusFactory.getAndSetThreadDefaultBus((Bus)cxfBus);
}
 
Example 14
Source File: ColocMessageObserver.java    From cxf with Apache License 2.0 4 votes vote down vote up
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<>(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();
        }
    }
}
 
Example 15
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected Object doChainedInvocation(URI uri,
                                   MultivaluedMap<String, String> headers,
                                   OperationResourceInfo ori,
                                   Object[] methodParams,
                                   Object body,
                                   int bodyIndex,
                                   Exchange exchange,
                                   Map<String, Object> invocationContext) throws Throwable {
//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 outMessage = createMessage(body, ori, headers, uri, exchange, invocationContext, true);
        if (bodyIndex != -1) {
            outMessage.put(Type.class, ori.getMethodToInvoke().getGenericParameterTypes()[bodyIndex]);
        }
        outMessage.getExchange().setOneWay(ori.isOneway());
        setSupportOnewayResponseProperty(outMessage);
        outMessage.setContent(OperationResourceInfo.class, ori);
        setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
        outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());

        outMessage.put(Annotation.class.getName(),
                       getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));

        outMessage.getExchange().put(Message.SERVICE_OBJECT, proxy);
        if (methodParams != null) {
            outMessage.put(List.class, Arrays.asList(methodParams));
        }
        if (body != null) {
            outMessage.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);
        }
        outMessage.getInterceptorChain().add(bodyWriter);

        Map<String, Object> reqContext = getRequestContext(outMessage);
        reqContext.put(OperationResourceInfo.class.getName(), ori);
        reqContext.put(PROXY_METHOD_PARAM_BODY_INDEX, bodyIndex);

        // execute chain
        InvocationCallback<Object> asyncCallback = checkAsyncCallback(ori, reqContext, outMessage);
        if (asyncCallback != null) {
            return doInvokeAsync(ori, outMessage, asyncCallback);
        }
        doRunInterceptorChain(outMessage);

        Object[] results = preProcessResult(outMessage);
        if (results != null && results.length == 1) {
            return results[0];
        }

        try {
            return handleResponse(outMessage, ori.getClassResourceInfo().getServiceClass());
        } finally {
            completeExchange(outMessage.getExchange(), true);
        }

    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != configuredBus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }

}