Java Code Examples for org.apache.cxf.endpoint.Server#start()

The following examples show how to use org.apache.cxf.endpoint.Server#start() . 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: RESTLoggingTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEvents() throws MalformedURLException {
    LoggingFeature loggingFeature = new LoggingFeature();
    loggingFeature.setLogBinary(true);
    TestEventSender sender = new TestEventSender();
    loggingFeature.setSender(sender);
    Server server = createService(SERVICE_URI, new TestServiceRest(), loggingFeature);
    server.start();
    WebClient client = createClient(SERVICE_URI, loggingFeature);
    String result = client.get(String.class);
    Assert.assertEquals("test1", result);

    List<LogEvent> events = sender.getEvents();
    await().until(() -> events.size(), is(4));
    server.stop();
    server.destroy();

    Assert.assertEquals(4, events.size());
    checkRequestOut(events.get(0));
    checkRequestIn(events.get(1));
    checkResponseOut(events.get(2));
    checkResponseIn(events.get(3));
}
 
Example 2
Source File: CodeFirstTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCXF1510() throws Exception {
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setServiceClass(NoRootBare.class);
    factory.setServiceBean(new NoRootBareImpl());
    factory.setAddress("local://localhost/testNoRootBare");
    Server server = factory.create();
    server.start();

    QName serviceName = new QName("http://service.jaxws.cxf.apache.org/", "NoRootBareService");
    QName portName = new QName("http://service.jaxws.cxf.apache.org/", "NoRootBarePort");

    ServiceImpl service = new ServiceImpl(getBus(), (URL)null, serviceName, null);
    service.addPort(portName, "http://schemas.xmlsoap.org/soap/",
                    "local://localhost/testNoRootBare");

    NoRootBare proxy = service.getPort(portName, NoRootBare.class);
    assertEquals("hello", proxy.echoString(new NoRootRequest("hello")).getMessage());
}
 
Example 3
Source File: StudentTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnMapDocLiteral() throws Exception {

    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceClass(StudentServiceDocLiteral.class);
    sf.setServiceBean(new StudentServiceDocLiteralImpl());
    sf.setAddress("local://StudentServiceDocLiteral");
    setupAegis(sf);
    Server server = sf.create();
    server.start();

    JaxWsProxyFactoryBean proxyFac = new JaxWsProxyFactoryBean();
    proxyFac.setAddress("local://StudentServiceDocLiteral");
    proxyFac.setBus(getBus());
    setupAegis(proxyFac.getClientFactoryBean());

    StudentServiceDocLiteral clientInterface = proxyFac.create(StudentServiceDocLiteral.class);
    Map<Long, Student> fullMap = clientInterface.getStudentsMap();
    assertNotNull(fullMap);
    Student one = fullMap.get(Long.valueOf(1));
    assertNotNull(one);
    assertEquals("Student1", one.getName());

}
 
Example 4
Source File: StudentTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapMap() throws Exception {

    ServerFactoryBean sf = new ServerFactoryBean();
    sf.setServiceClass(StudentServiceDocLiteral.class);
    sf.setServiceBean(new StudentServiceDocLiteralImpl());
    sf.setAddress("local://StudentServiceDocLiteral");
    setupAegis(sf);
    Server server = sf.create();
    server.start();

    ClientProxyFactoryBean proxyFac = new ClientProxyFactoryBean();
    proxyFac.setAddress("local://StudentServiceDocLiteral");
    proxyFac.setBus(getBus());
    setupAegis(proxyFac.getClientFactoryBean());
    //CHECKSTYLE:OFF
    HashMap<String, Student> mss = new HashMap<>();
    mss.put("Alice", new Student());
    HashMap<String, HashMap<String, Student>> mmss = new HashMap<>();
    mmss.put("Bob", mss);

    StudentServiceDocLiteral clientInterface = proxyFac.create(StudentServiceDocLiteral.class);
    clientInterface.takeMapMap(mmss);
    //CHECKSTYLE:ON
}
 
Example 5
Source File: StudentTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testReturnMap() throws Exception {

    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceClass(StudentService.class);
    sf.setServiceBean(new StudentServiceImpl());
    sf.setAddress("local://StudentService");
    setupAegis(sf);
    Server server = sf.create();
    server.start();

    JaxWsProxyFactoryBean proxyFac = new JaxWsProxyFactoryBean();
    proxyFac.setAddress("local://StudentService");
    proxyFac.setBus(getBus());
    setupAegis(proxyFac.getClientFactoryBean());

    StudentService clientInterface = proxyFac.create(StudentService.class);
    Map<Long, Student> fullMap = clientInterface.getStudentsMap();
    assertNotNull(fullMap);
    Student one = fullMap.get(Long.valueOf(1));
    assertNotNull(one);
    assertEquals("Student1", one.getName());

    Map<String, ?> wildMap = clientInterface.getWildcardMap();
    assertEquals("valuestring", wildMap.get("keystring"));
}
 
Example 6
Source File: ServerRegistryImpTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerRegistryPreShutdown() {
    ServerRegistryImpl serverRegistryImpl = new ServerRegistryImpl();
    Server server = new DummyServer(serverRegistryImpl);
    server.start();
    assertEquals("The serverList should have one service", serverRegistryImpl.serversList.size(), 1);
    serverRegistryImpl.preShutdown();
    assertEquals("The serverList should be clear ", serverRegistryImpl.serversList.size(), 0);
    serverRegistryImpl.postShutdown();
    assertEquals("The serverList should be clear ", serverRegistryImpl.serversList.size(), 0);
}
 
Example 7
Source File: RESTLoggingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBinary() throws IOException, InterruptedException {
    LoggingFeature loggingFeature = new LoggingFeature();
    TestEventSender sender = new TestEventSender();
    loggingFeature.setSender(sender);
    Server server = createService(SERVICE_URI_BINARY, new TestServiceRestBinary(), loggingFeature);
    server.start();
    WebClient client = createClient(SERVICE_URI_BINARY, loggingFeature);
    client.get(InputStream.class).close();
    loggingFeature.setLogBinary(true);
    client.get(InputStream.class).close();
    client.close();
    List<LogEvent> events = sender.getEvents();
    await().until(() -> events.size(), is(8));
    server.stop();
    server.destroy();

    Assert.assertEquals(8, events.size());
    
    // First call with binary logging false
    assertContentLogged(events.get(0));
    assertContentLogged(events.get(1));
    assertContentNotLogged(events.get(2));
    assertContentNotLogged(events.get(3));
    
    // Second call with binary logging true
    assertContentLogged(events.get(4));
    assertContentLogged(events.get(5));
    assertContentLogged(events.get(6));
    assertContentLogged(events.get(7));
}
 
Example 8
Source File: RESTLoggingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlf4j() throws IOException {
    LoggingFeature loggingFeature = new LoggingFeature();
    Server server = createService(SERVICE_URI, new TestServiceRest(), loggingFeature);
    server.start();
    WebClient client = createClient(SERVICE_URI, loggingFeature);
    String result = client.get(String.class);
    server.destroy();
    Assert.assertEquals("test1", result);
}
 
Example 9
Source File: StartEndpointCommand.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object execute() throws Exception {
    Bus b = getBus(busName);
    ServerRegistry reg = b.getExtension(ServerRegistry.class);
    List<Server> servers = reg.getServers();
    for (Server serv : servers) {
        if (endpoint.equals(serv.getEndpoint().getEndpointInfo().getName().getLocalPart())) {
            serv.start();
        }
    }
    return null;
}
 
Example 10
Source File: NoSpringServletClientTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void startServer() {
    ServerRegistry reg = serverBus.getExtension(ServerRegistry.class);
    List<Server> servers = reg.getServers();
    for (Server serv : servers) {
        serv.start();
    }
}
 
Example 11
Source File: EndpointCreationLoop3.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void iteration() {
    JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setAddress("http://localhost:9000/test");
    sf.setServiceClass(org.apache.cxf.systest.jaxb.service.TestServiceImpl.class);
    sf.setStart(false);

    Server server = sf.create();
    server.start();
    server.stop();
}
 
Example 12
Source File: WebServiceTaskTest.java    From activiti-in-action-codes with Apache License 2.0 5 votes vote down vote up
/**
 * 独立启动WebService服务
 */
public static void main(String[] args) {
	Counter counter = new CounterImpl();
	JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
	svrFactory.setServiceClass(Counter.class);
	svrFactory.setAddress("http://localhost:12345/counter");
	svrFactory.setServiceBean(counter);
	svrFactory.getInInterceptors().add(new LoggingInInterceptor());
	svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
	Server server = svrFactory.create();
	server.start();
}
 
Example 13
Source File: RestOrderServer.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // create dummy backend
    DummyOrderService dummy = new DummyOrderService();
    dummy.setupDummyOrders();

    // create CXF REST service and inject the dummy backend
    RestOrderService rest = new RestOrderService();
    rest.setOrderService(dummy);

    // setup Apache CXF REST server on port 9000
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(RestOrderService.class);
    sf.setResourceProvider(RestOrderService.class, new SingletonResourceProvider(rest));
    sf.setAddress("http://localhost:9000/");

    // create and start the CXF server (non blocking)
    Server server = sf.create();
    server.start();

    // keep the JVM running
    Console console = System.console();
    System.out.println("Server started on http://localhost:9000/");
    System.out.println("");

    // If you run the main class from IDEA/Eclipse then you may not have a console, which is null)
    if (console != null) {
        System.out.println("  Press ENTER to stop server");
        console.readLine();
    } else {
        System.out.println("  Stopping after 5 minutes or press ctrl + C to stop");
        Thread.sleep(5 * 60 * 1000);
    }

    // stop CXF server
    server.stop();
    System.exit(0);
}
 
Example 14
Source File: RestOrderServer.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // create dummy backend
    DummyOrderService dummy = new DummyOrderService();
    dummy.setupDummyOrders();

    // create CXF REST service and inject the dummy backend
    RestOrderService rest = new RestOrderService();
    rest.setOrderService(dummy);

    // setup Apache CXF REST server on port 9000
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(RestOrderService.class);
    sf.setResourceProvider(RestOrderService.class, new SingletonResourceProvider(rest));
    // to use jackson for json
    sf.setProvider(JacksonJsonProvider.class);
    sf.setAddress("http://localhost:9000/");

    // create and start the CXF server (non blocking)
    Server server = sf.create();
    server.start();

    // keep the JVM running
    Console console = System.console();
    System.out.println("Server started on http://localhost:9000/");
    System.out.println("");

    // If you run the main class from IDEA/Eclipse then you may not have a console, which is null)
    if (console != null) {
        System.out.println("  Press ENTER to stop server");
        console.readLine();
    } else {
        System.out.println("  Stopping after 5 minutes or press ctrl + C to stop");
        Thread.sleep(5 * 60 * 1000);
    }

    // stop CXF server
    server.stop();
    System.exit(0);
}
 
Example 15
Source File: MDBActivationWork.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * @param invoker
 * @param classLoader
 */
private void activate(MDBInvoker invoker, ClassLoader classLoader) {
    Class<?> serviceClass = null;
    if (spec.getServiceInterfaceClass() != null) {
        try {
            serviceClass = Class.forName(spec.getServiceInterfaceClass(),
                    false, classLoader);
        } catch (ClassNotFoundException e) {
            LOG.severe("Failed to activate service endpoint "
                    + spec.getDisplayName()
                    + " due to unable to endpoint listener.");
            return;
        }
    }

    Bus bus = null;
    if (spec.getBusConfigLocation() != null) {
        URL url = classLoader.getResource(spec.getBusConfigLocation());
        if (url == null) {
            LOG.warning("Unable to get bus configuration from "
                    + spec.getBusConfigLocation());
        } else {
            bus = new SpringBusFactory().createBus(url);
        }
    }

    if (bus == null) {
        bus = BusFactory.getDefaultBus();
    }

    Method method = null;

    try {
        Class<?> clazz = org.apache.cxf.jca.inbound.DispatchMDBMessageListener.class;
        method = clazz.getMethod(MESSAGE_LISTENER_METHOD, new Class[] {String.class});
    } catch (Exception ex) {
        LOG.severe("Failed to get method " + MESSAGE_LISTENER_METHOD
                   + " from class DispatchMDBMessageListener.");
    }

    Server server = createServer(bus, serviceClass, invoker);

    if (server == null) {
        LOG.severe("Failed to create CXF facade service endpoint.");
        return;
    }

    EndpointInfo ei = server.getEndpoint().getEndpointInfo();
    ei.setProperty(MESSAGE_ENDPOINT_FACTORY, endpointFactory);
    ei.setProperty(MDB_TRANSACTED_METHOD, method);

    server.start();

    // save the server for clean up later
    endpoints.put(spec.getDisplayName(), new InboundEndpoint(server, invoker));
}
 
Example 16
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 17
Source File: RestOrderServer.java    From camelinaction2 with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // create dummy backend
    DummyOrderService dummy = new DummyOrderService();
    dummy.setupDummyOrders();

    // create a Camel route that routes the REST services
    OrderRoute route = new OrderRoute();
    route.setOrderService(dummy);

    // create CamelContext and add the route
    CamelContext camel = new DefaultCamelContext();
    camel.addRoutes(route);

    // create a ProducerTemplate that the CXF REST service will use to integrate with Camel
    ProducerTemplate producer = camel.createProducerTemplate();

    // create CXF REST service and inject the Camel ProducerTemplate
    // which we use to call the Camel route
    RestOrderService rest = new RestOrderService();
    rest.setProducerTemplate(producer);

    // setup Apache CXF REST server on port 9000
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(RestOrderService.class);
    sf.setResourceProvider(RestOrderService.class, new SingletonResourceProvider(rest));
    // to use jackson for json
    sf.setProvider(JacksonJsonProvider.class);
    sf.setAddress("http://localhost:9000/");

    // create the CXF server
    Server server = sf.create();

    // start Camel and CXF (non blocking)
    camel.start();
    server.start();

    // keep the JVM running
    Console console = System.console();
    System.out.println("Server started on http://localhost:9000/");
    System.out.println("");

    // If you run the main class from IDEA/Eclipse then you may not have a console, which is null)
    if (console != null) {
        System.out.println("  Press ENTER to stop server");
        console.readLine();
    } else {
        System.out.println("  Stopping after 5 minutes or press ctrl + C to stop");
        Thread.sleep(5 * 60 * 1000);
    }

    // stop Camel and CXF server
    camel.stop();
    server.stop();
    System.exit(0);
}