Skip to content

Commit 4cd9619

Browse files
Jeff-Lvbeiwei30
authored andcommitted
remove serviceclassHolder and also fix the issue #2637 (#2607)
* remove serviceclassHolder * [Dubbo-2637] fix the issue #2637 to make sure the properties load properly * ServiceConfig.java:33: Using the '.*' form of import should be avoided * replace the import rpc.* with rpc Exporter etc * replace the import rpc.* with rpc Exporter etc
1 parent bb9fd69 commit 4cd9619

File tree

9 files changed

+43
-65
lines changed

9 files changed

+43
-65
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ public class Constants {
635635

636636
public static final String MULTICAST = "multicast";
637637

638+
public static final String SERVICE_IMPL_CLASS = "service.classimpl";
639+
638640
/*
639641
* private Constants(){ }
640642
*/

dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.alibaba.dubbo.common.logger.Logger;
2222
import com.alibaba.dubbo.common.logger.LoggerFactory;
2323

24+
import java.io.File;
2425
import java.io.FileInputStream;
2526
import java.io.InputStream;
2627
import java.lang.management.ManagementFactory;
@@ -217,7 +218,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile)
217218
*/
218219
public static Properties loadProperties(String fileName, boolean allowMultiFile, boolean optional) {
219220
Properties properties = new Properties();
220-
if (fileName.startsWith("/")) {
221+
if (checkFileNameExist(fileName)) {
221222
try {
222223
FileInputStream input = new FileInputStream(fileName);
223224
try {
@@ -291,6 +292,16 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
291292
return properties;
292293
}
293294

295+
/**
296+
* check if the fileName can be found in filesystem
297+
* @param fileName
298+
* @return
299+
*/
300+
private static boolean checkFileNameExist(String fileName) {
301+
File file = new File(fileName);
302+
return file != null && file.exists() ? true : false;
303+
}
304+
294305
public static int getPid() {
295306
if (PID < 0) {
296307
try {

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import com.alibaba.dubbo.rpc.Invoker;
3535
import com.alibaba.dubbo.rpc.Protocol;
3636
import com.alibaba.dubbo.rpc.ProxyFactory;
37-
import com.alibaba.dubbo.rpc.ServiceClassHolder;
37+
import com.alibaba.dubbo.rpc.StaticContext;
3838
import com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory;
3939
import com.alibaba.dubbo.rpc.service.GenericService;
4040
import com.alibaba.dubbo.rpc.support.ProtocolUtils;
@@ -532,7 +532,7 @@ private void exportLocal(URL url) {
532532
.setProtocol(Constants.LOCAL_PROTOCOL)
533533
.setHost(LOCALHOST)
534534
.setPort(0);
535-
ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));
535+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(url.getServiceKey(), getServiceClass(ref));
536536
Exporter<?> exporter = protocol.export(
537537
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
538538
exporters.add(exporter);

dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void checkApplication1() throws Exception {
8787
interfaceConfig.checkApplication();
8888
ApplicationConfig appConfig = interfaceConfig.getApplication();
8989
TestCase.assertEquals("demo", appConfig.getName());
90-
TestCase.assertEquals("100", System.getProperty(Constants.SHUTDOWN_WAIT_KEY));
90+
TestCase.assertEquals("100", ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_KEY));
9191

9292
System.clearProperty(Constants.SHUTDOWN_WAIT_KEY);
9393
ConfigUtils.setProperties(null);
@@ -386,6 +386,7 @@ private void writeDubboProperties(String key, String value) {
386386
Properties properties = new Properties();
387387
properties.put(key, value);
388388
properties.store(os, "");
389+
os.flush();
389390
os.close();
390391
} catch (IOException e) {
391392
if (os != null) {

dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/ServiceClassHolder.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocol.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import com.alibaba.dubbo.remoting.http.servlet.BootstrapListener;
2424
import com.alibaba.dubbo.remoting.http.servlet.ServletManager;
2525
import com.alibaba.dubbo.rpc.RpcException;
26-
import com.alibaba.dubbo.rpc.ServiceClassHolder;
26+
import com.alibaba.dubbo.rpc.StaticContext;
2727
import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;
28-
2928
import org.apache.http.HeaderElement;
3029
import org.apache.http.HeaderElementIterator;
3130
import org.apache.http.HttpResponse;
@@ -83,7 +82,7 @@ public int getDefaultPort() {
8382
@Override
8483
protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
8584
String addr = getAddr(url);
86-
Class implClass = ServiceClassHolder.getInstance().popServiceClass();
85+
Class implClass = (Class) StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).get(url.getServiceKey());
8786
RestServer server = servers.get(addr);
8887
if (server == null) {
8988
server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, "jetty"));

dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import com.alibaba.dubbo.rpc.RpcContext;
2828
import com.alibaba.dubbo.rpc.RpcException;
2929
import com.alibaba.dubbo.rpc.RpcInvocation;
30-
import com.alibaba.dubbo.rpc.ServiceClassHolder;
30+
import com.alibaba.dubbo.rpc.StaticContext;
31+
3132
import org.hamcrest.CoreMatchers;
3233
import org.junit.After;
3334
import org.junit.Test;
@@ -48,7 +49,7 @@ public void tearDown() {
4849

4950
@Test
5051
public void testExport() {
51-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
52+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
5253

5354

5455
RpcContext.getContext().setAttachment("timeout", "200");
@@ -64,7 +65,7 @@ public void testExport() {
6465

6566
@Test
6667
public void testNettyServer() {
67-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
68+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
6869

6970
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
7071
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
@@ -79,7 +80,7 @@ public void testNettyServer() {
7980

8081
@Test(expected = RpcException.class)
8182
public void testServletWithoutWebConfig() {
82-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
83+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
8384

8485
URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");
8586

@@ -88,7 +89,7 @@ public void testServletWithoutWebConfig() {
8889

8990
@Test(expected = RpcException.class)
9091
public void testErrorHandler() {
91-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
92+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
9293

9394
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
9495
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
@@ -100,7 +101,7 @@ public void testErrorHandler() {
100101

101102
@Test
102103
public void testInvoke() {
103-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
104+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
104105

105106

106107
Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
@@ -113,7 +114,7 @@ public void testInvoke() {
113114

114115
@Test
115116
public void testFilter() {
116-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
117+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
117118

118119
URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
119120
.addParameter(Constants.EXTENSION_KEY, "com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter");
@@ -130,7 +131,7 @@ public void testFilter() {
130131

131132
@Test(expected = RuntimeException.class)
132133
public void testRegFail() {
133-
ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
134+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
134135

135136
URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
136137
protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));

dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protol/rest/RestProtocolTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616
*/
1717
package com.alibaba.dubbo.rpc.protol.rest;
1818

19+
import com.alibaba.dubbo.common.Constants;
1920
import com.alibaba.dubbo.common.URL;
2021
import com.alibaba.dubbo.common.extension.ExtensionLoader;
21-
import com.alibaba.dubbo.rpc.*;
22+
import com.alibaba.dubbo.rpc.Exporter;
23+
import com.alibaba.dubbo.rpc.Invoker;
24+
import com.alibaba.dubbo.rpc.Protocol;
25+
import com.alibaba.dubbo.rpc.ProxyFactory;
26+
import com.alibaba.dubbo.rpc.StaticContext;
27+
2228
import junit.framework.Assert;
2329
import org.junit.Test;
2430

@@ -32,10 +38,11 @@ public class RestProtocolTest {
3238

3339
@Test
3440
public void testRestProtocol() {
35-
ServiceClassHolder.getInstance().pushServiceClass(RestServiceImpl.class);
41+
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0");
42+
StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(url.getServiceKey(), RestServiceImpl.class);
3643
RestServiceImpl server = new RestServiceImpl();
3744
Assert.assertFalse(server.isCalled());
38-
URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0");
45+
3946
Exporter<RestService> exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url));
4047
Invoker<RestService> invoker = protocol.refer(RestService.class, url);
4148
RestService client = proxyFactory.getProxy(invoker);

dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
-->
18-
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
<beans xmlns="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1920
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
21+
xmlns:beans="http://www.springframework.org/schema/beans"
2022
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
21-
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
23+
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
2224

2325
<dubbo:application name="rest-consumer" owner="programmer" organization="dubbo"/>
2426

0 commit comments

Comments
 (0)