Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@

<cse:rpc-schema schema-id="smartcare"
implementation="io.servicecomb.demo.pojo.server.SmartCareImpl"></cse:rpc-schema>
</beans>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<demo.main>io.servicecomb.springboot.pojo.client.PojoClient</demo.main>
</properties>
<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PojoClientTest {

public static final byte buffer[] = new byte[1024];

public static final String SPLITPARAM_RESPONSE_USER_SUFFIX = "(modified by MyHandler)";

static {
Arrays.fill(buffer, (byte) 1);
}
Expand Down Expand Up @@ -82,12 +84,15 @@ private static void testInputArray(Test test) {
private static void testSplitParam(Test test) {
User result = test.splitParam(1, new User());
LOGGER.info("split param result:{}", result);
TestMgr.check("User [name=nameA, users count:0, age=100, index=1]", result);
TestMgr.check("User [name=nameA, users count:0" + SPLITPARAM_RESPONSE_USER_SUFFIX
+ ", age=100, index=1]",
result);
}

private static void testCommonInvoke(String transport) {
Object result = InvokerUtils.syncInvoke("pojo", "server", "splitParam", new Object[] {2, new User()});
TestMgr.check("User [name=nameA, users count:0, age=100, index=2]", result);
TestMgr.check("User [name=nameA, users count:0" + SPLITPARAM_RESPONSE_USER_SUFFIX
+ ", age=100, index=2]", result);

result =
InvokerUtils.syncInvoke("pojo",
Expand All @@ -96,7 +101,8 @@ private static void testCommonInvoke(String transport) {
"server",
"splitParam",
new Object[] {3, new User()});
TestMgr.check("User [name=nameA, users count:0, age=100, index=3]", result);
TestMgr.check("User [name=nameA, users count:0" + SPLITPARAM_RESPONSE_USER_SUFFIX
+ ", age=100, index=3]", result);
}

private static void testEmpty(Test test) {
Expand All @@ -114,7 +120,7 @@ private static void testChinese(Test test) {
User user = new User();
user.setName("名字");
User result = test.splitParam(1, user);
TestMgr.check("名字, users count:0", result.getName());
TestMgr.check("名字, users count:0" + SPLITPARAM_RESPONSE_USER_SUFFIX, result.getName());
}

private static void testStringHaveSpace(Test test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-core</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-transport</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package io.servicecomb.demo.pojo.server;

import io.servicecomb.demo.helloworld.greeter.Hello;
import io.servicecomb.provider.pojo.RpcSchema;

@RpcSchema(schemaId = "helloworld.Greeter")
public class HelloImpl implements Hello {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.servicecomb.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.core.Handler;
import io.servicecomb.core.Invocation;
import io.servicecomb.demo.server.User;
import io.servicecomb.swagger.invocation.AsyncResponse;

public class MyHandler implements Handler {

private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);

public static final String SPLITPARAM_RESPONSE_USER_SUFFIX = "(modified by MyHandler)";

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
LOGGER.info("If you see this log, that means this demo project has been converted to ServiceComb framework.");

invocation.next(response -> {
if (invocation.getOperationMeta().getSchemaQualifiedName().equals("server.splitParam")) {
User user = response.getResult();
user.setName(user.getName() + SPLITPARAM_RESPONSE_USER_SUFFIX);
asyncResp.handle(response);
} else {
asyncResp.handle(response);
}
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017 Huawei Technologies Co., Ltd
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<config>
<handler id="myhandler" class="io.servicecomb.handler.MyHandler"/>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ cse:
address: http://127.0.0.1:30100
rest:
address: 0.0.0.0:8080
handler:
chain:
Provider:
default: myhandler
4 changes: 0 additions & 4 deletions demo/demo-spring-boot-transport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
</modules>

<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-provider</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
50 changes: 27 additions & 23 deletions spring-boot-starter/spring-boot-starter-transport/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.4.1-SNAPSHOT</version>
</parent>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.4.1-SNAPSHOT</version>
</parent>

<artifactId>spring-boot-starter-transport</artifactId>
<artifactId>spring-boot-starter-transport</artifactId>

<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-servicecomb</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>transport-rest-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>spring-boot-starter-servicecomb</artifactId>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>transport-rest-servlet</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.servicecomb</groupId>
<artifactId>foundation-test-scaffolding</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.netflix.config.DynamicPropertyFactory;

import io.servicecomb.transport.rest.servlet.RestServletInjector;
import io.servicecomb.transport.rest.servlet.ServletConfig;
import io.servicecomb.transport.rest.servlet.ServletUtils;

@Component
Expand All @@ -50,6 +55,11 @@ public void onStartup(ServletContext servletContext) throws ServletException {
// web container did not did listen now.
// so mock to listen, and then close.
try (ServerSocket ss = new ServerSocket(getPort(), 0, getAddress())) {
if (StringUtils.isEmpty(ServletConfig.getServletUrlPattern())) {
// ensure the servlet will be instantiated
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
configuration.setProperty(ServletConfig.KEY_SERVLET_URL_PATTERN, ServletConfig.DEFAULT_URL_PATTERN);
}
RestServletInjector.defaultInject(servletContext);
ServletUtils.saveUrlPrefix(servletContext);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2017 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicecomb.springboot.starter.transport;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.apache.commons.configuration.Configuration;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.netflix.config.DynamicPropertyFactory;

import io.servicecomb.foundation.common.net.IpPort;
import io.servicecomb.foundation.common.net.NetUtils;
import io.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
import io.servicecomb.transport.rest.servlet.RestServlet;
import io.servicecomb.transport.rest.servlet.RestServletInjector;
import io.servicecomb.transport.rest.servlet.ServletConfig;

@RunWith(PowerMockRunner.class)
@PrepareForTest({NetUtils.class})
public class TestRestServletInitializer {

private static final String LISTEN_ADDRESS = "127.0.0.1";

private static final int TEST_PORT = 8080;

@BeforeClass
public static void beforeClass() {
ArchaiusUtils.resetConfig();

DynamicPropertyFactory.getInstance();
}

@AfterClass
public static void afterClass() {
ArchaiusUtils.resetConfig();
}

@Before
public void setUp() throws Exception {
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
configuration.clearProperty(ServletConfig.KEY_SERVLET_URL_PATTERN);
configuration.setProperty(ServletConfig.KEY_CSE_REST_ADDRESS, LISTEN_ADDRESS);

PowerMockito.mockStatic(NetUtils.class);
PowerMockito.when(NetUtils.parseIpPortFromURI(anyString())).thenReturn(new IpPort(LISTEN_ADDRESS, TEST_PORT));
PowerMockito.when(NetUtils.canTcpListen(anyObject(), anyInt())).thenReturn(false);
}

@Test
public void testOnStartup() throws Exception {
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
String urlPattern = "/rest/*";
configuration.setProperty(ServletConfig.KEY_SERVLET_URL_PATTERN, urlPattern);

ServletContext servletContext = mock(ServletContext.class);
Dynamic dynamic = mock(Dynamic.class);
when(servletContext.addServlet(RestServletInjector.SERVLET_NAME, RestServlet.class)).thenReturn(dynamic);

RestServletInitializer restServletInitializer = new RestServletInitializer();
restServletInitializer.setPort(TEST_PORT);
restServletInitializer.onStartup(servletContext);

verify(dynamic).setAsyncSupported(true);
verify(dynamic).addMapping(urlPattern);
verify(dynamic).setLoadOnStartup(0);
}

@Test
public void testOnStartupWhenUrlPatternNotSet() throws ServletException {
ServletContext servletContext = mock(ServletContext.class);
Dynamic dynamic = mock(Dynamic.class);
when(servletContext.addServlet(RestServletInjector.SERVLET_NAME, RestServlet.class)).thenReturn(dynamic);

RestServletInitializer restServletInitializer = new RestServletInitializer();
restServletInitializer.setPort(TEST_PORT);
restServletInitializer.onStartup(servletContext);

verify(dynamic).setAsyncSupported(true);
verify(dynamic).addMapping(ServletConfig.DEFAULT_URL_PATTERN);
verify(dynamic).setLoadOnStartup(0);
}
}
Loading