Search in sources :

Example 6 with ServerImpl

use of org.apache.cxf.endpoint.ServerImpl in project cxf by apache.

the class ServerFactoryBean method create.

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 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(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) {
                // prevent resource leak
                server.destroy();
                throw re;
            }
        }
        getServiceFactory().reset();
        return server;
    } finally {
        if (orig != null) {
            orig.reset();
        }
    }
}
Also used : Server(org.apache.cxf.endpoint.Server) Endpoint(org.apache.cxf.endpoint.Endpoint) ServerImpl(org.apache.cxf.endpoint.ServerImpl) EndpointException(org.apache.cxf.endpoint.EndpointException) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) ServiceConstructionException(org.apache.cxf.service.factory.ServiceConstructionException) IOException(java.io.IOException) BusException(org.apache.cxf.BusException)

Example 7 with ServerImpl

use of org.apache.cxf.endpoint.ServerImpl in project cxf by apache.

the class EngineLifecycleTest method testUpDownWithServlets.

@Test
public void testUpDownWithServlets() throws Exception {
    setUpBus(true);
    Bus bus = (Bus) applicationContext.getBean("cxf");
    ServerRegistry sr = bus.getExtension(ServerRegistry.class);
    ServerImpl si = (ServerImpl) sr.getServers().get(0);
    JettyHTTPDestination jhd = (JettyHTTPDestination) si.getDestination();
    JettyHTTPServerEngine e = (JettyHTTPServerEngine) jhd.getEngine();
    org.eclipse.jetty.server.Server jettyServer = e.getServer();
    Handler[] contexts = jettyServer.getChildHandlersByClass(WebAppContext.class);
    WebAppContext servletContext = null;
    for (Handler h : contexts) {
        WebAppContext wac = (WebAppContext) h;
        if (wac.getContextPath().equals("/jsunit")) {
            servletContext = wac;
            break;
        }
    }
    servletContext.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/bloop");
    getTestHtml();
    invokeService();
    shutdownService();
    verifyNoServer(PORT2);
    verifyNoServer(PORT1);
}
Also used : Bus(org.apache.cxf.Bus) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ServerImpl(org.apache.cxf.endpoint.ServerImpl) JettyHTTPServerEngine(org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine) ServerRegistry(org.apache.cxf.endpoint.ServerRegistry) Handler(org.eclipse.jetty.server.Handler) JettyHTTPDestination(org.apache.cxf.transport.http_jetty.JettyHTTPDestination) Test(org.junit.Test)

Example 8 with ServerImpl

use of org.apache.cxf.endpoint.ServerImpl in project cxf by apache.

the class SoapBindingSelectionTest method testMultipleSoapBindings.

@Test
public void testMultipleSoapBindings() throws Exception {
    ServerFactoryBean svrBean1 = new ServerFactoryBean();
    svrBean1.setAddress("http://localhost/Hello");
    svrBean1.setServiceClass(HelloService.class);
    svrBean1.setServiceBean(new HelloServiceImpl());
    svrBean1.setBus(getBus());
    svrBean1.getInInterceptors().add(new AbstractPhaseInterceptor<Message>(Phase.USER_LOGICAL) {

        public void handleMessage(Message message) throws Fault {
            service1Invoked = true;
        }
    });
    svrBean1.create();
    ServerFactoryBean svrBean2 = new ServerFactoryBean();
    svrBean2.setAddress("http://localhost/Hello");
    svrBean2.setServiceClass(HelloService.class);
    svrBean2.setServiceBean(new HelloServiceImpl());
    svrBean2.setBus(getBus());
    svrBean2.getInInterceptors().add(new AbstractPhaseInterceptor<Message>(Phase.USER_LOGICAL) {

        public void handleMessage(Message message) throws Fault {
            service2Invoked = true;
        }
    });
    SoapBindingConfiguration config = new SoapBindingConfiguration();
    config.setVersion(Soap12.getInstance());
    svrBean2.setBindingConfig(config);
    ServerImpl server2 = (ServerImpl) svrBean2.create();
    Destination d = server2.getDestination();
    MessageObserver mo = d.getMessageObserver();
    assertTrue(mo instanceof MultipleEndpointObserver);
    MultipleEndpointObserver meo = (MultipleEndpointObserver) mo;
    assertEquals(2, meo.getEndpoints().size());
    Node nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap11.xml");
    assertEquals("http://schemas.xmlsoap.org/soap/envelope/", getNs(nd));
    assertTrue(service1Invoked);
    assertFalse(service2Invoked);
    service1Invoked = false;
    nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap12.xml");
    assertEquals("http://www.w3.org/2003/05/soap-envelope", getNs(nd));
    assertFalse(service1Invoked);
    assertTrue(service2Invoked);
    server2.stop();
    server2.start();
    nd = invoke("http://localhost/Hello", LocalTransportFactory.TRANSPORT_ID, "soap12.xml");
    assertEquals("http://www.w3.org/2003/05/soap-envelope", getNs(nd));
    assertFalse(service1Invoked);
    assertTrue(service2Invoked);
}
Also used : Destination(org.apache.cxf.transport.Destination) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) ServerImpl(org.apache.cxf.endpoint.ServerImpl) SoapBindingConfiguration(org.apache.cxf.binding.soap.SoapBindingConfiguration) MultipleEndpointObserver(org.apache.cxf.transport.MultipleEndpointObserver) Node(org.w3c.dom.Node) ServerFactoryBean(org.apache.cxf.frontend.ServerFactoryBean) HelloServiceImpl(org.apache.cxf.service.factory.HelloServiceImpl) Fault(org.apache.cxf.interceptor.Fault) Test(org.junit.Test) AbstractSimpleFrontendTest(org.apache.cxf.service.factory.AbstractSimpleFrontendTest)

Example 9 with ServerImpl

use of org.apache.cxf.endpoint.ServerImpl in project cxf by apache.

the class EndpointImpl method doPublish.

/**
 * Performs the publication action by setting up a {@link Server}
 * instance based on this endpoint's configuration.
 *
 * @param addr the optional endpoint address.
 *
 * @throws IllegalStateException if the endpoint cannot be published/republished
 * @throws SecurityException if permission checking is enabled and policy forbids publishing
 * @throws WebServiceException if there is an error publishing the endpoint
 *
 * @see #checkPublishPermission()
 * @see #checkPublishable()
 * @see #getServer(String)
 */
protected void doPublish(String addr) {
    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() == null || !endpointInfo.getAddress().contains(addr)) {
                endpointInfo.setAddress(addr);
            }
            if (publishedEndpointUrl != null) {
                endpointInfo.setProperty(WSDLGetUtils.PUBLISHED_ENDPOINT_URL, publishedEndpointUrl);
            }
            if (publishedEndpointUrl != null && wsdlLocation != null) {
                // early update the publishedEndpointUrl so that endpoints in the same app sharing the same wsdl
                // do not require all of them to be queried for wsdl before the wsdl is finally fully updated
                Definition def = endpointInfo.getService().getProperty(WSDLServiceBuilder.WSDL_DEFINITION, Definition.class);
                if (def == null) {
                    def = bus.getExtension(WSDLManager.class).getDefinition(wsdlLocation);
                }
                new WSDLGetUtils().updateWSDLPublishedEndpointAddress(def, endpointInfo);
            }
            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();
        }
    }
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) ServerImpl(org.apache.cxf.endpoint.ServerImpl) WebServiceException(javax.xml.ws.WebServiceException) Definition(javax.wsdl.Definition) WSDLGetUtils(org.apache.cxf.frontend.WSDLGetUtils) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) WebServiceException(javax.xml.ws.WebServiceException)

Example 10 with ServerImpl

use of org.apache.cxf.endpoint.ServerImpl in project cxf by apache.

the class ProviderServiceFactoryTest method testXMLBindingFromCode.

@Test
public void testXMLBindingFromCode() throws Exception {
    JaxWsServiceFactoryBean bean = new JaxWsServiceFactoryBean();
    bean.setServiceClass(DOMSourcePayloadProvider.class);
    bean.setBus(getBus());
    bean.setInvoker(new JAXWSMethodInvoker(new DOMSourcePayloadProvider()));
    Service service = bean.create();
    assertEquals("DOMSourcePayloadProviderService", service.getName().getLocalPart());
    InterfaceInfo intf = service.getServiceInfos().get(0).getInterface();
    assertNotNull(intf);
    JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
    svrFactory.setBus(getBus());
    svrFactory.setServiceFactory(bean);
    String address = "http://localhost:9000/test";
    svrFactory.setAddress(address);
    svrFactory.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    ServerImpl server = (ServerImpl) svrFactory.create();
    assertEquals(1, service.getServiceInfos().get(0).getEndpoints().size());
    Endpoint endpoint = server.getEndpoint();
    Binding binding = endpoint.getBinding();
    assertTrue(binding instanceof XMLBinding);
    Node res = invoke(address, LocalTransportFactory.TRANSPORT_ID, "/org/apache/cxf/jaxws/provider/sayHi.xml");
    addNamespace("j", "http://service.jaxws.cxf.apache.org/");
    assertValid("/j:sayHi", res);
}
Also used : Binding(org.apache.cxf.binding.Binding) SoapBinding(org.apache.cxf.binding.soap.SoapBinding) XMLBinding(org.apache.cxf.binding.xml.XMLBinding) JaxWsServiceFactoryBean(org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean) ServerImpl(org.apache.cxf.endpoint.ServerImpl) Endpoint(org.apache.cxf.endpoint.Endpoint) Node(org.w3c.dom.Node) Service(org.apache.cxf.service.Service) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) XMLBinding(org.apache.cxf.binding.xml.XMLBinding) JAXWSMethodInvoker(org.apache.cxf.jaxws.JAXWSMethodInvoker) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean) Test(org.junit.Test) AbstractJaxWsTest(org.apache.cxf.jaxws.AbstractJaxWsTest)

Aggregations

ServerImpl (org.apache.cxf.endpoint.ServerImpl)12 Test (org.junit.Test)8 Endpoint (org.apache.cxf.endpoint.Endpoint)6 Binding (org.apache.cxf.binding.Binding)4 SoapBinding (org.apache.cxf.binding.soap.SoapBinding)4 XMLBinding (org.apache.cxf.binding.xml.XMLBinding)4 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)4 AbstractJaxWsTest (org.apache.cxf.jaxws.AbstractJaxWsTest)4 JAXWSMethodInvoker (org.apache.cxf.jaxws.JAXWSMethodInvoker)4 JaxWsServerFactoryBean (org.apache.cxf.jaxws.JaxWsServerFactoryBean)4 JaxWsServiceFactoryBean (org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean)4 Service (org.apache.cxf.service.Service)4 InterfaceInfo (org.apache.cxf.service.model.InterfaceInfo)4 Node (org.w3c.dom.Node)4 Bus (org.apache.cxf.Bus)3 ServerFactoryBean (org.apache.cxf.frontend.ServerFactoryBean)3 IOException (java.io.IOException)2 BusException (org.apache.cxf.BusException)2 SoapBindingInfo (org.apache.cxf.binding.soap.model.SoapBindingInfo)2 EndpointException (org.apache.cxf.endpoint.EndpointException)2