Package org.apache.cxf.common.classloader.ClassLoaderUtils

Examples of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder


        } else if (request != null) {
            params = new MessageContentsList(request);
        }

        Object result = null;
        ClassLoaderHolder contextLoader = null;
        try {
            if (setServiceLoaderAsContextLoader(inMessage)) {
                contextLoader = ClassLoaderUtils
                    .setThreadContextClassloader(resourceObject.getClass().getClassLoader());
            }
            AsyncResponseImpl asyncResponse = null;
            if (!ori.isSubResourceLocator()) {
                asyncResponse = (AsyncResponseImpl)inMessage.get(AsyncResponse.class);
            }
            result = invoke(exchange, resourceObject, methodToInvoke, params);
            if (asyncResponse != null) {
                if (!asyncResponse.isSuspended() && !asyncResponse.isResumedByApplication()) {
                    asyncResponse.suspendContinuation();
                } else {
                    result = handleAsyncResponse(exchange, asyncResponse.getResponseObject());
                }
            }
        } catch (Fault ex) {
            return handleFault(ex, inMessage, cri, methodToInvoke);
        } finally {
            exchange.put(LAST_SERVICE_OBJECT, resourceObject);
            if (contextLoader != null) {
                contextLoader.reset();
            }
        }
        ClassResourceInfo subCri = null;
        if (ori.isSubResourceLocator()) {
            try {
View Full Code Here


    /**
     * 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();
                updateClassResourceProviders();
            }
           
            Endpoint ep = createEndpoint();
            server = new ServerImpl(getBus(),
                                    ep,
                                    getDestinationFactory(),
                                    getBindingFactory());

            Invoker invoker = serviceFactory.getInvoker();
            if (invoker == null) {
                ep.getService().setInvoker(createInvoker());
            } else {
                ep.getService().setInvoker(invoker);
            }
           
            ProviderFactory factory = setupFactory(ep);
            ep.put(Application.class.getName(), appProvider);
            factory.setApplicationProvider(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);
           
            getServiceFactory().sendEvent(FactoryBeanListener.Event.SERVER_CREATED,
                                          server,
                                          null,
                                          null);
           
            factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
            applyFeatures();
           
           
           
            if (start) {
                server.start();
            }
        } 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;
    }
View Full Code Here

        } else if (request != null) {
            params = new MessageContentsList(request);
        }

        Object result = null;
        ClassLoaderHolder contextLoader = null;
        try {
            if (setServiceLoaderAsContextLoader(inMessage)) {
                contextLoader = ClassLoaderUtils
                    .setThreadContextClassloader(resourceObject.getClass().getClassLoader());
            }
            AsyncResponseImpl asyncResponse = null;
            if (!ori.isSubResourceLocator()) {
                asyncResponse = (AsyncResponseImpl)inMessage.get(AsyncResponse.class);
            }
            result = invoke(exchange, resourceObject, methodToInvoke, params);
            if (asyncResponse != null) {
                if (!asyncResponse.isSuspended() && !asyncResponse.isResumedByApplication()) {
                    asyncResponse.suspendContinuation();
                    providerFactory.clearThreadLocalProxies();
                } else {
                    result = handleAsyncResponse(exchange, asyncResponse.getResponseObject());
                }
            }
        } catch (Fault ex) {
            return handleFault(ex, inMessage, cri, methodToInvoke);
        } finally {
            exchange.put(LAST_SERVICE_OBJECT, resourceObject);
            if (contextLoader != null) {
                contextLoader.reset();
            }
        }
        ClassResourceInfo subCri = null;
        if (ori.isSubResourceLocator()) {
            try {
View Full Code Here

    public void setBus(Bus bus) {
        this.bus = bus;
    }
    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);
                }
                if (controller.filter((HttpServletRequest)request, (HttpServletResponse)response)) {
                    return;
                }
            } finally {
                if (origBus != bus) {
                    BusFactory.setThreadDefaultBus(origBus);
                }
                if (origLoader != null) {
                    origLoader.reset();
                }
            }
        }
        chain.doFilter(request, response);
    }
View Full Code Here

        }
        chain.doFilter(request, response);
    }
    @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(null);
            }
            if (origLoader != null) {
                origLoader.reset();
            }
        }
    }
View Full Code Here

                              BindingOperationInfo oi,
                              Object[] params,
                              Map<String, Object> context,
                              Exchange exchange) throws Exception {
        Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
        ClassLoaderHolder origLoader = null;
        try {
            ClassLoader loader = bus.getExtension(ClassLoader.class);
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            if (exchange == null) {
                exchange = new ExchangeImpl();
            }
            exchange.setSynchronous(callback == null);
            Endpoint endpoint = getEndpoint();
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
            }
            Message message = endpoint.getBinding().createMessage();
           
            // Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
            // on message
            Map<String, Object> reqContext = null;
            Map<String, Object> resContext = null;
            if (context == null) {
                context = new HashMap<String, Object>();
            }
            reqContext = CastUtils.cast((Map<?, ?>)context.get(REQUEST_CONTEXT));
            resContext = CastUtils.cast((Map<?, ?>)context.get(RESPONSE_CONTEXT));
            if (reqContext == null) {
                reqContext = new HashMap<String, Object>(getRequestContext());
                context.put(REQUEST_CONTEXT, reqContext);
            }
            if (resContext == null) {
                resContext = new HashMap<String, Object>();
                context.put(RESPONSE_CONTEXT, resContext);
            }
           
            message.put(Message.INVOCATION_CONTEXT, context);
            setContext(reqContext, message);
            exchange.putAll(reqContext);
           
            setParameters(params, message);

            if (null != oi) {
                exchange.setOneWay(oi.getOutput() == null);
            }

            exchange.setOutMessage(message);
            exchange.put(ClientCallback.class, callback);
           
            setOutMessageProperties(message, oi);
            setExchangeProperties(exchange, endpoint, oi);

            PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
            message.setInterceptorChain(chain);
            chain.setFaultObserver(outFaultObserver);
            prepareConduitSelector(message);

            // add additional interceptors and such
            modifyChain(chain, message, false);
            try {
                chain.doIntercept(message);
            } catch (Fault fault) {
                enrichFault(fault);
                throw fault;
            }
           
            if (callback != null) {
                return null;
            } else {
                return processResult(message, exchange, oi, resContext);
            }
        } finally {
            if (origLoader != null) {
                origLoader.reset();
            }
            if (origBus != bus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
        }
View Full Code Here

        this();
        xpath.setNamespaceContext(ctx);
    }

    public Object getValue(String xpathExpression, Node node, QName type) {
        ClassLoaderHolder loader
            = ClassLoaderUtils.setThreadContextClassloader(xpath.getClass().getClassLoader());
        try {
            return xpath.evaluate(xpathExpression, node, type);
        } catch (Exception e) {
            return null;
        } finally {
            if (loader != null) {
                loader.reset();
            }
        }
    }
View Full Code Here

   
    public static void doHacks() {
        try {
            // Use the system classloader as the victim for all this
            // ClassLoader pinning we're about to do.
            ClassLoaderHolder orig = ClassLoaderUtils
                .setThreadContextClassloader(ClassLoader.getSystemClassLoader());
            try {
               
                try {
                    //Trigger a call to sun.awt.AppContext.getAppContext()
                    ImageIO.getCacheDirectory();
                } catch (Throwable t) {
                    //ignore
                }
                try {
                    // Several components end up opening JarURLConnections without first
                    // disabling caching. This effectively locks the file.
                    // JAXB does this and thus affects us pretty badly.
                    // Doesn't matter that this JAR doesn't exist - just as long as
                    // the URL is well-formed
                    boolean skip = skipHack("org.apache.cxf.JDKBugHacks.defaultUsesCaches");
                    if (!skip) {
                        URL url = new URL("jar:file://dummy.jar!/");
                        URLConnection uConn = new URLConnection(url) {
                            @Override
                            public void connect() throws IOException {
                                // NOOP
                            }
                        };
                        uConn.setDefaultUseCaches(false);
                    }
                } catch (Throwable e) {
                    //ignore
                }
                try {
                    //DocumentBuilderFactory seems to SOMETIMES pin the classloader
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    factory.newDocumentBuilder();
                } catch (Throwable e) {
                    //ignore
                }
                // Several components end up calling:
                // sun.misc.GC.requestLatency(long)
                //
                // Those libraries / components known to trigger memory leaks due to
                // eventual calls to requestLatency(long) are:
                // - javax.management.remote.rmi.RMIConnectorServer.start()
                try {
                    Class<?> clazz = Class.forName("sun.misc.GC");
                    Method method = clazz.getDeclaredMethod("requestLatency",
                            new Class[] {Long.TYPE});
                    method.invoke(null, Long.valueOf(3600000));
                } catch (Throwable e) {
                    //ignore
                }
               
                // Calling getPolicy retains a static reference to the context
                // class loader.
                try {
                    // Policy.getPolicy();
                    Class<?> policyClass = Class
                        .forName("javax.security.auth.Policy");
                    Method method = policyClass.getMethod("getPolicy");
                    method.invoke(null);
                } catch (Throwable e) {
                    // ignore
                }
                try {
                    // Initializing javax.security.auth.login.Configuration retains a static reference
                    // to the context class loader.
                    Class.forName("javax.security.auth.login.Configuration", true,
                                  ClassLoader.getSystemClassLoader());
                } catch (Throwable e) {
                    // Ignore
                }
                // Creating a MessageDigest during web application startup
                // initializes the Java Cryptography Architecture. Under certain
                // conditions this starts a Token poller thread with TCCL equal
                // to the web application class loader.
                java.security.Security.getProviders();
            } finally {
                if (orig != null) {
                    orig.reset();
                }
            }
        } catch (Throwable t) {
            //ignore
        }
View Full Code Here

     
        assert null != message;
       
        Bus origBus = BusFactory.getThreadDefaultBus(false);
        BusFactory.setThreadDefaultBus(bus);
        ClassLoaderHolder origLoader = null;
        try {
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
           
            Exchange exchange = message.getExchange();
   
            Message faultMessage = null;
   
            // now that we have switched over to the fault chain,
            // prevent any further operations on the in/out message
   
            if (isOutboundObserver()) {
                Exception ex = message.getContent(Exception.class);
                if (!(ex instanceof Fault)) {
                    ex = new Fault(ex);
                }
                FaultMode mode = (FaultMode)message.get(FaultMode.class);
               
                faultMessage = exchange.getOutMessage();
                if (null == faultMessage) {
                    faultMessage = new MessageImpl();
                    faultMessage.setExchange(exchange);
                    faultMessage = exchange.get(Endpoint.class).getBinding().createMessage(faultMessage);
                }
                faultMessage.setContent(Exception.class, ex);
                if (null != mode) {
                    faultMessage.put(FaultMode.class, mode);
                }
                //CXF-3981
                if (message.get("org.apache.cxf.ws.rm.inbound") == null
                    &&  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 (Exception exc) {
                LOG.log(Level.SEVERE, "Error occurred during error handling, give up!", exc);
                throw new RuntimeException(exc);
            }
        } finally {
            BusFactory.setThreadDefaultBus(origBus);
            if (origLoader != null) {
                origLoader.reset();
            }
        }
    }
View Full Code Here

                              BindingOperationInfo oi,
                              Object[] params,
                              Map<String, Object> context,
                              Exchange exchange) throws Exception {
        Bus origBus = BusFactory.getThreadDefaultBus(false);
        ClassLoaderHolder origLoader = null;
        try {
            BusFactory.setThreadDefaultBus(bus);
            ClassLoader loader = bus.getExtension(ClassLoader.class);
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            if (exchange == null) {
                exchange = new ExchangeImpl();
            }
            exchange.setSynchronous(callback == null);
            Endpoint endpoint = getEndpoint();
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
            }
            Message message = endpoint.getBinding().createMessage();
           
            // Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
            // on message
            Map<String, Object> reqContext = null;
            Map<String, Object> resContext = null;
            if (context == null) {
                context = new HashMap<String, Object>();
            }
            reqContext = CastUtils.cast((Map)context.get(REQUEST_CONTEXT));
            resContext = CastUtils.cast((Map)context.get(RESPONSE_CONTEXT));
            if (reqContext == null) {
                reqContext = new HashMap<String, Object>(getRequestContext());
                context.put(REQUEST_CONTEXT, reqContext);
            }
            if (resContext == null) {
                resContext = new HashMap<String, Object>();
                context.put(RESPONSE_CONTEXT, resContext);
            }
           
            message.put(Message.INVOCATION_CONTEXT, context);
            setContext(reqContext, message);
            if (null != reqContext) {
                exchange.putAll(reqContext);
            }
           
            setParameters(params, message);

            if (null != oi) {
                exchange.setOneWay(oi.getOutput() == null);
            }

            exchange.setOutMessage(message);
            exchange.put(ClientCallback.class, callback);
           
            setOutMessageProperties(message, oi);
            setExchangeProperties(exchange, endpoint, oi);

            PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
            message.setInterceptorChain(chain);
            chain.setFaultObserver(outFaultObserver);
            prepareConduitSelector(message);

            // add additional interceptors and such
            modifyChain(chain, message, false);
            try {
                chain.doIntercept(message);
            } catch (Fault fault) {
                enrichFault(fault);
                throw fault;
            }
           
            if (callback != null) {
                return null;
            } else {
                return processResult(message, exchange, oi, resContext);
            }
        } finally {
            if (origLoader != null) {
                origLoader.reset();
            }
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.