Java Code Examples for org.apache.cxf.common.classloader.ClassLoaderUtils#setThreadContextClassloader()

The following examples show how to use org.apache.cxf.common.classloader.ClassLoaderUtils#setThreadContextClassloader() . 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: 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 2
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 3
Source File: JaxWsServerFactoryBean.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Server create() {
    ClassLoaderHolder orig = null;
    try {
        if (bus != null) {
            ClassLoader loader = bus.getExtension(ClassLoader.class);
            if (loader != null) {
                orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
        }

        Server server = super.create();
        initializeResourcesAndHandlerChain(server);
        checkPrivateEndpoint(server.getEndpoint());

        return server;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 4
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 5
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 6
Source File: AbstractWSS4JInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
@Override
protected Crypto loadCryptoFromPropertiesFile(
    String propFilename, 
    RequestData reqData
) throws WSSecurityException {
    ClassLoaderHolder orig = null;
    try {
        try {
            URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
            if (url == null) {
                ResourceManager manager = ((Message)reqData.getMsgContext()).getExchange()
                        .getBus().getExtension(ResourceManager.class);
                ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
                url = manager.resolveResource(propFilename, URL.class);
            }
            if (url != null) {
                Properties props = new Properties();
                InputStream in = url.openStream(); 
                props.load(in);
                in.close();
                return CryptoFactory.getInstance(props,
                                                 this.getClassLoader(reqData.getMsgContext()));
            }
        } catch (Exception e) {
            //ignore
        } 
        return CryptoFactory.getInstance(propFilename, this.getClassLoader(reqData.getMsgContext()));
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 7
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 8
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 9
Source File: XPathUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Object getValue(String xpathExpression, Node node, QName type) {
    ClassLoaderHolder loader
        = ClassLoaderUtils.setThreadContextClassloader(getClassLoader(xpath.getClass()));
    try {
        return xpath.evaluate(xpathExpression, node, type);
    } catch (Exception e) {
        return null;
    } finally {
        if (loader != null) {
            loader.reset();
        }
    }
}
 
Example 10
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 11
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 12
Source File: JaxWsProxyFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
        }
    }
}
 
Example 13
Source File: AbstractWSS4JInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
@Override
protected Crypto loadCryptoFromPropertiesFile(
    String propFilename, 
    RequestData reqData
) throws WSSecurityException {
    ClassLoaderHolder orig = null;
    try {
        try {
            URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
            if (url == null) {
                ResourceManager manager = ((Message)reqData.getMsgContext()).getExchange()
                        .getBus().getExtension(ResourceManager.class);
                ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
                url = manager.resolveResource(propFilename, URL.class);
            }
            if (url != null) {
                Properties props = new Properties();
                InputStream in = url.openStream(); 
                props.load(in);
                in.close();
                return CryptoFactory.getInstance(props,
                                                 this.getClassLoader(reqData.getMsgContext()));
            }
        } catch (Exception e) {
            //ignore
        } 
        return CryptoFactory.getInstance(propFilename, this.getClassLoader(reqData.getMsgContext()));
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 14
Source File: AbstractWSS4JInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
@Override
protected Crypto loadCryptoFromPropertiesFile(
    String propFilename, 
    RequestData reqData
) throws WSSecurityException {
    ClassLoaderHolder orig = null;
    try {
        try {
            URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
            if (url == null) {
                ResourceManager manager = ((Message)reqData.getMsgContext()).getExchange()
                        .getBus().getExtension(ResourceManager.class);
                ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
                url = manager.resolveResource(propFilename, URL.class);
            }
            if (url != null) {
                Properties props = new Properties();
                InputStream in = url.openStream(); 
                props.load(in);
                in.close();
                return CryptoFactory.getInstance(props,
                                                 this.getClassLoader(reqData.getMsgContext()));
            }
        } catch (Exception e) {
            //ignore
        } 
        return CryptoFactory.getInstance(propFilename, this.getClassLoader(reqData.getMsgContext()));
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 15
Source File: AbstractWSS4JInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
@Override
protected Crypto loadCryptoFromPropertiesFile(
    String propFilename, 
    RequestData reqData
) throws WSSecurityException {
    ClassLoaderHolder orig = null;
    try {
        try {
            URL url = ClassLoaderUtils.getResource(propFilename, this.getClass());
            if (url == null) {
                ResourceManager manager = ((Message)reqData.getMsgContext()).getExchange()
                        .getBus().getExtension(ResourceManager.class);
                ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
                url = manager.resolveResource(propFilename, URL.class);
            }
            if (url != null) {
                Properties props = new Properties();
                InputStream in = url.openStream(); 
                props.load(in);
                in.close();
                return CryptoFactory.getInstance(props,
                                                 this.getClassLoader(reqData.getMsgContext()));
            }
        } catch (Exception e) {
            //ignore
        } 
        return CryptoFactory.getInstance(propFilename, this.getClassLoader(reqData.getMsgContext()));
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 16
Source File: JAXRSServerFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * 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, (ApplicationInfo)ep.get(Application.class.getName()));
        factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
        
        
        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED,
                                      server,
                                      null,
                                      null);


        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                server.destroy(); // prevent resource leak
                throw re;
            }
        }
    } catch (Exception e) {
        throw new ServiceConstructionException(e);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
    }

    return server;
}
 
Example 17
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);
        }
    }

}
 
Example 18
Source File: ClientProxyFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a proxy object that can be used to make remote invocations.
 *
 * @return the proxy. You must cast the returned object to the appropriate class before using it.
 */
public synchronized Object create() {
    ClassLoaderHolder orig = null;
    ClassLoader loader = null;
    try {
        if (getBus() != null) {
            loader = getBus().getExtension(ClassLoader.class);
            if (loader != null) {
                orig = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
        }
        configureObject();

        if (properties == null) {
            properties = new HashMap<>();
        }

        if (username != null) {
            AuthorizationPolicy authPolicy = new AuthorizationPolicy();
            authPolicy.setUserName(username);
            authPolicy.setPassword(password);
            properties.put(AuthorizationPolicy.class.getName(), authPolicy);
        }

        initFeatures();
        clientFactoryBean.setProperties(properties);

        if (bus != null) {
            clientFactoryBean.setBus(bus);
        }

        if (dataBinding != null) {
            clientFactoryBean.setDataBinding(dataBinding);
        }

        Client c = clientFactoryBean.create();
        if (getInInterceptors() != null) {
            c.getInInterceptors().addAll(getInInterceptors());
        }
        if (getOutInterceptors() != null) {
            c.getOutInterceptors().addAll(getOutInterceptors());
        }
        if (getInFaultInterceptors() != null) {
            c.getInFaultInterceptors().addAll(getInFaultInterceptors());
        }
        if (getOutFaultInterceptors() != null) {
            c.getOutFaultInterceptors().addAll(getOutFaultInterceptors());
        }

        ClientProxy handler = clientClientProxy(c);

        Class<?>[] classes = getImplementingClasses();
        Object obj = ProxyHelper.getProxy(getClassLoader(clientFactoryBean.getServiceClass()),
                                          classes,
                                          handler);

        this.getServiceFactory().sendEvent(FactoryBeanListener.Event.PROXY_CREATED,
                                           classes, handler, obj);
        return obj;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 19
Source File: ServerFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Server create() {
    ClassLoaderHolder orig = null;
    try {
        Server server = null;
        try {
            if (bus != null) {
                ClassLoader loader = bus.getExtension(ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
            }

            if (getServiceFactory().getProperties() == null) {
                getServiceFactory().setProperties(getProperties());
            } else if (getProperties() != null) {
                getServiceFactory().getProperties().putAll(getProperties());
            }
            if (serviceBean != null && getServiceClass() == null) {
                setServiceClass(ClassHelper.getRealClass(bus, serviceBean));
            }
            if (invoker != null) {
                getServiceFactory().setInvoker(invoker);
            } else if (serviceBean != null) {
                invoker = createInvoker();
                getServiceFactory().setInvoker(invoker);
            }

            Endpoint ep = createEndpoint();

            getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server, serviceBean,
                                          serviceBean == null
                                          ? getServiceClass() == null
                                              ? getServiceFactory().getServiceClass()
                                              : getServiceClass()
                                          : getServiceClass() == null
                                              ? ClassHelper.getRealClass(getBus(), getServiceBean())
                                              : getServiceClass());

            server = new ServerImpl(getBus(),
                                    ep,
                                    getDestinationFactory(),
                                    getBindingFactory());

            if (ep.getService().getInvoker() == null) {
                if (invoker == null) {
                    ep.getService().setInvoker(createInvoker());
                } else {
                    ep.getService().setInvoker(invoker);
                }
            }

        } catch (EndpointException | BusException | IOException e) {
            throw new ServiceConstructionException(e);
        }

        if (serviceBean != null) {
            Class<?> cls = ClassHelper.getRealClass(getServiceBean());
            if (getServiceClass() == null || cls.equals(getServiceClass())) {
                initializeAnnotationInterceptors(server.getEndpoint(), cls);
            } else {
                initializeAnnotationInterceptors(server.getEndpoint(), cls, getServiceClass());
            }
        } else if (getServiceClass() != null) {
            initializeAnnotationInterceptors(server.getEndpoint(), getServiceClass());
        }

        applyFeatures(server);

        getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED, server, serviceBean,
                                      serviceBean == null
                                      ? getServiceClass() == null
                                          ? getServiceFactory().getServiceClass()
                                          : getServiceClass()
                                      : getServiceClass() == null
                                          ? ClassHelper.getRealClass(getServiceBean())
                                          : getServiceClass());

        if (start) {
            try {
                server.start();
            } catch (RuntimeException re) {
                server.destroy(); // prevent resource leak
                throw re;
            }
        }
        getServiceFactory().reset();

        return server;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
 
Example 20
Source File: AutomaticWorkQueueImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
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 < highWaterMark
        && addThreadLock.tryLock()) {
        try {
            mainLock.lock();
            try {
                int ps = this.getPoolSize();
                int sz = executor.getQueue().size();
                int sz2 = this.getActiveCount();

                if ((sz + sz2) > ps) {
                    ReflectionUtil.setAccessible(addWorkerMethod).invoke(executor, addWorkerArgs);
                }
            } catch (Exception exc) {
                //ignore
            } finally {
                mainLock.unlock();
            }
        } finally {
            addThreadLock.unlock();
        }
    }
}