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

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


                                           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);
            }
            URI uri = getCurrentURI();
            Message m = createMessage(body, httpMethod, headers, uri, exchange,
                    invContext, false);
           
            Map<String, Object> reqContext = getRequestContext(m);
            reqContext.put(Message.HTTP_REQUEST_METHOD, httpMethod);
            reqContext.put(REQUEST_CLASS, requestClass);
            reqContext.put(REQUEST_TYPE, inGenericType);
            reqContext.put(RESPONSE_CLASS, responseClass);
            reqContext.put(RESPONSE_TYPE, outGenericType);
           
            if (body != null) {
                m.getInterceptorChain().add(new BodyWriter());
            }
            setPlainOperationNameProperty(m, httpMethod + ":" + uri.toString());
           
            try {
                m.getInterceptorChain().doIntercept(m);
            } catch (Exception ex) {
                m.setContent(Exception.class, ex);
            }
            try {
                Object[] results = preProcessResult(m);
                if (results != null && results.length == 1) {
                    // this can happen if a connection exception has occurred and
                    // failover feature used this client to invoke on a different address 
                    return (Response)results[0];
                }
            } catch (Exception ex) {
                throw ex instanceof ServerWebApplicationException
                    ? (ServerWebApplicationException)ex
                    : ex instanceof ClientWebApplicationException
                    ? new ClientWebApplicationException(ex) : new RuntimeException(ex);
            }
           
            Response response = null;
            Object entity = null;
            try {
                response = handleResponse(m, responseClass, outGenericType);
                entity = response.getEntity();
                return response;
            } catch (RuntimeException ex) {
                entity = ex;
                throw ex;
            } finally {
                completeExchange(entity, m.getExchange(), false);
            }
        } finally {
            if (origLoader != null) {
                origLoader.reset();
            }
            if (origBus != configuredBus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
        }   
View Full Code Here


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

        Object result = null;
        ClassLoaderHolder contextLoader = null;
        try {
            if (setServiceLoaderAsContextLoader(exchange.getInMessage())) {
                contextLoader = ClassLoaderUtils
                    .setThreadContextClassloader(resourceObject.getClass().getClassLoader());
            }
            result = invoke(exchange, resourceObject, methodToInvoke, params);
        } catch (Fault ex) {
            String errorMessage = ex.getCause().getMessage();
            if (errorMessage != null
                && errorMessage.contains(PROXY_INVOCATION_ERROR_FRAGMENT)) {
                org.apache.cxf.common.i18n.Message errorM =
                    new org.apache.cxf.common.i18n.Message("PROXY_INVOCATION_FAILURE",
                                                           BUNDLE,
                                                           methodToInvoke,
                                                           cri.getServiceClass().getName());
                LOG.severe(errorM.toString());
            }
            Response excResponse = JAXRSUtils.convertFaultToResponse(ex.getCause(),
                                                                     exchange.getInMessage());
            if (excResponse == null) {
                ProviderFactory.getInstance(exchange.getInMessage()).clearThreadLocalProxies();
                ClassResourceInfo criRoot =
                    (ClassResourceInfo)exchange.get(JAXRSUtils.ROOT_RESOURCE_CLASS);
                if (criRoot != null) {
                    criRoot.clearThreadLocalProxies();
                }
                exchange.put(Message.PROPOGATE_EXCEPTION,
                             JAXRSUtils.propogateException(exchange.getInMessage()));
                throw ex;
            }
            return new MessageContentsList(excResponse);
        } finally {
            exchange.put(LAST_SERVICE_OBJECT, resourceObject);
            if (contextLoader != null) {
                contextLoader.reset();
            }
        }
        ClassResourceInfo subCri = null;
        if (ori.isSubResourceLocator()) {
            try {
View Full Code Here

    @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();
            }
        }
    }
View Full Code Here

                                       int bodyIndex,
                                       Exchange exchange,
                                       Map<String, Object> invocationContext) throws Throwable {
        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.getHttpMethod(), headers, uri,
                                               exchange, invocationContext, true);
           
            outMessage.getExchange().setOneWay(ori.isOneway());
            outMessage.setContent(OperationResourceInfo.class, ori);
            setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
            outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());
           
            if (body != null) {
                outMessage.put("BODY_INDEX", bodyIndex);
                outMessage.getInterceptorChain().add(new BodyWriter());
            }
   
            Map<String, Object> reqContext = getRequestContext(outMessage);
            reqContext.put(OperationResourceInfo.class.getName(), ori);
            reqContext.put("BODY_INDEX", bodyIndex);
           
            // execute chain   
            try {
                outMessage.getInterceptorChain().doIntercept(outMessage);
            } catch (Exception ex) {
                outMessage.setContent(Exception.class, ex);
            }
           
            Object[] results = preProcessResult(outMessage);
            if (results != null && results.length == 1) {
                // this can happen if a connection exception has occurred and
                // failover feature used this client to invoke on a different address 
                return results[0];
            }
           
            Object response = null;
            try {
                response = handleResponse(outMessage, ori.getClassResourceInfo().getServiceClass());
                return response;
            } catch (Exception ex) {
                response = ex;
                throw ex;
            } finally {
                completeExchange(response, outMessage.getExchange(), true);
            }
        } finally {
            if (origLoader != null) {
                origLoader.reset();
            }
            if (origBus != configuredBus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
        }
View Full Code Here

        crypto = cryptoCache != null ? cryptoCache.get(propResourceName) : null;
        if (crypto != null) {
            return crypto;
        }
       
        ClassLoaderHolder orig = null;
        try {
            URL url = ClassLoaderUtils.getResource(propResourceName, this.getClass());
            if (url == null) {
                ResourceManager manager = message.getExchange()
                        .getBus().getExtension(ResourceManager.class);
                ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                if (loader != null) {
                    orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                }
                url = manager.resolveResource(propResourceName, URL.class);
            }
            if (url == null) {
                try {
                    URI propResourceUri = URI.create(propResourceName);
                    if (propResourceUri.getScheme() != null) {
                        url = propResourceUri.toURL();
                    } else {
                        File f = new File(propResourceUri.toString());
                        if (f.exists()) {
                            url = f.toURI().toURL();
                        }
                    }
                } catch (IOException ex) {
                    // let CryptoFactory try to load it
                }  
            }
            if (url != null) {
                Properties props = new Properties();
                InputStream in = url.openStream();
                props.load(in);
                in.close();
                crypto = CryptoFactory.getInstance(props);
            } else {
                crypto = CryptoFactory.getInstance(propResourceName, Thread.currentThread().getContextClassLoader());
            }
            getCryptoCache(message).put(o, crypto);
            return crypto;
        } finally {
            if (orig != null) {
                orig.reset();
            }
        }
    }
View Full Code Here

        return null;
    }
   

    public Server create() {
        ClassLoaderHolder orig = null;
        try {
            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(serviceBean));
                }
                if (invoker != null) {
                    getServiceFactory().setInvoker(invoker);
                } else if (serviceBean != null) {
                    invoker = createInvoker();
                    getServiceFactory().setInvoker(invoker);
                }
   
                Endpoint ep = createEndpoint();
                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 e) {
                throw new ServiceConstructionException(e);
            } catch (BusException e) {
                throw new ServiceConstructionException(e);
            } catch (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();
  
            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;
                }
            }
            return server;
        } finally {
            if (orig != null) {
                orig.reset();
            }
        }           
    }
View Full Code Here

            }
        }           
    }
    public void init() {
        if (getServer() == null) {
            ClassLoaderHolder orig = null;
            try {
                if (bus != null) {
                    ClassLoader loader = bus.getExtension(ClassLoader.class);
                    if (loader != null) {
                        orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                    }
                }
                create();
            } finally {
                if (orig != null) {
                    orig.reset();
                }
            }
        }
    }
View Full Code Here

                    }
                }
            } else {
                EndpointInfo ei = d.getEndpointInfo();
                Bus bus = d.getBus();
                ClassLoaderHolder orig = null;
                try {
                    ResourceManager manager = bus.getExtension(ResourceManager.class);
                    if (manager != null) {
                        ClassLoader loader = manager.resolveResource("", ClassLoader.class);
                        if (loader != null) {
                            //need to set the context classloader to the loader of the bundle
                            orig = ClassLoaderUtils.setThreadContextClassloader(loader);
                        }
                    }
                    QueryHandlerRegistry queryHandlerRegistry = bus.getExtension(QueryHandlerRegistry.class);

                    if (!StringUtils.isEmpty(request.getQueryString()) && queryHandlerRegistry != null) {
                       
                        // update the EndPoint Address with request url
                        if ("GET".equals(request.getMethod())) {
                            updateDestination(request, d);
                        }
                       
                        String ctxUri = request.getPathInfo();
                        String baseUri = request.getRequestURL().toString()
                            + "?" + request.getQueryString();

                        QueryHandler selectedHandler =
                            findQueryHandler(queryHandlerRegistry, ei, ctxUri, baseUri);
                       
                        if (selectedHandler != null) {
                            respondUsingQueryHandler(selectedHandler, res, ei, ctxUri, baseUri);
                            return;
                        }
                    } else {
                        updateDestination(request, d);
                    }
                    invokeDestination(request, res, d);
                } finally {
                    if (orig != null) {
                        orig.reset();
                    }
                }
               
            }
        } catch (IOException e) {
View Full Code Here

                }
            }
        }

        // REVISIT: service on executor if associated with endpoint
        ClassLoaderHolder origLoader = null;
        Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
        try {
            if (loader != null) {
                origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
            }
            serviceRequest(context, req, resp);
        } finally {
            if (origBus != bus) {
                BusFactory.setThreadDefaultBus(origBus);
            }
            if (origLoader != null) {
                origLoader.reset();
            }
        }   
    }
View Full Code Here

        this.bus = bus;
    }

    @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

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.