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

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


        checkPublishPermission();
        checkPublishable();
       
        ServerImpl serv = null;
       
        ClassLoaderHolder loader = null;
        try {
            if (bus != null) {
                ClassLoader newLoader = bus.getExtension(ClassLoader.class);
                if (newLoader != null) {
                    loader = ClassLoaderUtils.setThreadContextClassloader(newLoader);
                }
            }
            serv = getServer(addr);
            if (addr != null) {           
                EndpointInfo endpointInfo = serv.getEndpoint().getEndpointInfo();
                if (!endpointInfo.getAddress().contains(addr)) {
                    endpointInfo.setAddress(addr);
                }
                if (publishedEndpointUrl != null) {
                    // TODO is there a good place to put this key-string as a constant?
                    endpointInfo.setProperty("publishedEndpointUrl", publishedEndpointUrl);
                }

                if (null != properties) {
                    for (Entry<String, Object> entry : properties.entrySet()) {
                        endpointInfo.setProperty(entry.getKey(), entry.getValue());
                    }
                }

                this.address = endpointInfo.getAddress();
            }
            serv.start();
            publishable = false;
        } catch (Exception ex) {
            try {
                stop();
            } catch (Exception e) {
                // Nothing we can do.
            }
           
            throw new WebServiceException(ex);
        } finally {
            if (loader != null) {
                loader.reset();
            }
        }
    }
View Full Code Here


   
    public synchronized ServerImpl getServer(String addr) {
        if (server == null) {
            checkProperties();

            ClassLoaderHolder loader = null;
            try {
                if (bus != null) {
                    ClassLoader newLoader = bus.getExtension(ClassLoader.class);
                    if (newLoader != null) {
                        loader = ClassLoaderUtils.setThreadContextClassloader(newLoader);
                    }
                }
   
                // Initialize the endpointName so we can do configureObject
                QName origEpn = endpointName;
                if (endpointName == null) {
                    JaxWsImplementorInfo implInfo = new JaxWsImplementorInfo(getImplementorClass());
                    endpointName = implInfo.getEndpointName();
                }
               
                if (serviceFactory != null) {
                    serverFactory.setServiceFactory(serviceFactory);
                }
   
                /*if (serviceName != null) {
                    serverFactory.getServiceFactory().setServiceName(serviceName);
                }*/
   
                configureObject(this);
                endpointName = origEpn;
               
                // Set up the server factory
                serverFactory.setAddress(addr);
                serverFactory.setStart(false);
                serverFactory.setEndpointName(endpointName);
                serverFactory.setServiceBean(implementor);
                serverFactory.setBus(bus);
                serverFactory.setFeatures(getFeatures());
                serverFactory.setInvoker(invoker);
                serverFactory.setSchemaLocations(schemaLocations);
                if (serverFactory.getProperties() != null) {
                    serverFactory.getProperties().putAll(properties);
                } else {
                    serverFactory.setProperties(properties);
                }
               
                // Be careful not to override any serverfactory settings as a user might
                // have supplied their own.
                if (getWsdlLocation() != null) {
                    serverFactory.setWsdlURL(getWsdlLocation());
                }
               
                if (bindingUri != null) {
                    serverFactory.setBindingId(bindingUri);
                }
   
                if (serviceName != null) {
                    serverFactory.getServiceFactory().setServiceName(serviceName);
                }
               
                if (implementorClass != null) {
                    serverFactory.setServiceClass(implementorClass);
                }
               
                if (executor != null) {
                    serverFactory.getServiceFactory().setExecutor(executor);
                }
                if (handlers.size() > 0) {
                    serverFactory.addHandlers(handlers);
                }
   
                configureObject(serverFactory);
               
                server = serverFactory.create();
               
                org.apache.cxf.endpoint.Endpoint endpoint = getEndpoint();
                if (in != null) {
                    endpoint.getInInterceptors().addAll(in);
                }
                if (out != null) {
                    endpoint.getOutInterceptors().addAll(out);
                }
                if (inFault != null) {
                    endpoint.getInFaultInterceptors().addAll(inFault);
                }
                if (outFault != null) {
                    endpoint.getOutFaultInterceptors().addAll(outFault);
                }
               
                if (properties != null) {
                    endpoint.putAll(properties);
                }
               
                configureObject(endpoint.getService());
                configureObject(endpoint);
                this.service = endpoint.getService();
               
                if (getWsdlLocation() == null) {
                    //hold onto the wsdl location so cache won't clear till we go away
                    setWsdlLocation(serverFactory.getWsdlURL());
                }
               
                if (serviceName == null) {
                    setServiceName(serverFactory.getServiceFactory().getServiceQName());
                }
                if (endpointName == null) {
                    endpointName = endpoint.getEndpointInfo().getName();
                }
            } finally {
                if (loader != null) {
                    loader.reset();
                }
            }
        }
        return (ServerImpl) server;
    }
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);
            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

   
    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

        } 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();

            getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE,
                                          server,
                                          null,
                                          null);
           
            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);
           
            factory.applyDynamicFeatures(getServiceFactory().getClassResourceInfo());
            applyFeatures();

            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 (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

        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();

                getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_SERVER_CREATE, server, serviceBean,
                                              serviceBean == null
                                              ? getServiceClass() == null
                                                  ? getServiceFactory().getServiceClass()
                                                  : getServiceClass()
                                              : getServiceClass() == null
                                                  ? ClassHelper.getRealClass(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 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

                                       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());
           
            outMessage.put(Annotation.class.getName(),
                           getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));
           
            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   
            doRunInterceptorChain(outMessage);
           
            Object[] results = preProcessResult(outMessage);
            if (results != null && results.length == 1) {
                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

                                       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());
           
            outMessage.put(Annotation.class.getName(),
                           getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));
           
            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   
            doRunInterceptorChain(outMessage);
           
            Object[] results = preProcessResult(outMessage);
            if (results != null && results.length == 1) {
                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

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.