You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/10/31 06:48:32 UTC

[incubator-dubbo] branch 2.6.x updated: remove serviceclassHolder and also fix the issue #2637 (#2607)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch 2.6.x
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/2.6.x by this push:
     new 4cd9619  remove serviceclassHolder and also fix the issue #2637 (#2607)
4cd9619 is described below

commit 4cd96191303475bf4b7840db0d4b605bf21f50e9
Author: Jeff-Lv <lr...@gmail.com>
AuthorDate: Wed Oct 31 14:48:24 2018 +0800

    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
---
 .../java/com/alibaba/dubbo/common/Constants.java   |  2 +
 .../alibaba/dubbo/common/utils/ConfigUtils.java    | 13 ++++++-
 .../com/alibaba/dubbo/config/ServiceConfig.java    |  4 +-
 .../dubbo/config/AbstractInterfaceConfigTest.java  |  3 +-
 .../com/alibaba/dubbo/rpc/ServiceClassHolder.java  | 45 ----------------------
 .../dubbo/rpc/protocol/rest/RestProtocol.java      |  5 +--
 .../dubbo/rpc/protocol/rest/RestProtocolTest.java  | 17 ++++----
 .../dubbo/rpc/protol/rest/RestProtocolTest.java    | 13 +++++--
 .../alibaba/dubbo/examples/rest/rest-consumer.xml  |  6 ++-
 9 files changed, 43 insertions(+), 65 deletions(-)

diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java
index 404fe8f..02d70ea 100644
--- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java
+++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/Constants.java
@@ -635,6 +635,8 @@ public class Constants {
 
     public static final String MULTICAST = "multicast";
 
+    public static final String SERVICE_IMPL_CLASS = "service.classimpl";
+
     /*
      * private Constants(){ }
      */
diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java
index 8c4ac69..d08c2be 100644
--- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java
+++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java
@@ -21,6 +21,7 @@ import com.alibaba.dubbo.common.extension.ExtensionLoader;
 import com.alibaba.dubbo.common.logger.Logger;
 import com.alibaba.dubbo.common.logger.LoggerFactory;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.lang.management.ManagementFactory;
@@ -217,7 +218,7 @@ public class ConfigUtils {
      */
     public static Properties loadProperties(String fileName, boolean allowMultiFile, boolean optional) {
         Properties properties = new Properties();
-        if (fileName.startsWith("/")) {
+        if (checkFileNameExist(fileName)) {
             try {
                 FileInputStream input = new FileInputStream(fileName);
                 try {
@@ -291,6 +292,16 @@ public class ConfigUtils {
         return properties;
     }
 
+    /**
+     * check if the fileName can be found in filesystem
+     * @param fileName
+     * @return
+     */
+    private static boolean checkFileNameExist(String fileName) {
+        File file = new File(fileName);
+        return file != null && file.exists() ? true : false;
+    }
+
     public static int getPid() {
         if (PID < 0) {
             try {
diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java
index ec6dcd9..846b216 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java
@@ -34,7 +34,7 @@ import com.alibaba.dubbo.rpc.Exporter;
 import com.alibaba.dubbo.rpc.Invoker;
 import com.alibaba.dubbo.rpc.Protocol;
 import com.alibaba.dubbo.rpc.ProxyFactory;
-import com.alibaba.dubbo.rpc.ServiceClassHolder;
+import com.alibaba.dubbo.rpc.StaticContext;
 import com.alibaba.dubbo.rpc.cluster.ConfiguratorFactory;
 import com.alibaba.dubbo.rpc.service.GenericService;
 import com.alibaba.dubbo.rpc.support.ProtocolUtils;
@@ -532,7 +532,7 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
                     .setProtocol(Constants.LOCAL_PROTOCOL)
                     .setHost(LOCALHOST)
                     .setPort(0);
-            ServiceClassHolder.getInstance().pushServiceClass(getServiceClass(ref));
+            StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(url.getServiceKey(), getServiceClass(ref));
             Exporter<?> exporter = protocol.export(
                     proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
             exporters.add(exporter);
diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java
index f34c31f..4f08d29 100644
--- a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java
+++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java
@@ -87,7 +87,7 @@ public class AbstractInterfaceConfigTest {
             interfaceConfig.checkApplication();
             ApplicationConfig appConfig = interfaceConfig.getApplication();
             TestCase.assertEquals("demo", appConfig.getName());
-            TestCase.assertEquals("100", System.getProperty(Constants.SHUTDOWN_WAIT_KEY));
+            TestCase.assertEquals("100", ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_KEY));
 
             System.clearProperty(Constants.SHUTDOWN_WAIT_KEY);
             ConfigUtils.setProperties(null);
@@ -386,6 +386,7 @@ public class AbstractInterfaceConfigTest {
             Properties properties = new Properties();
             properties.put(key, value);
             properties.store(os, "");
+            os.flush();
             os.close();
         } catch (IOException e) {
             if (os != null) {
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/ServiceClassHolder.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/ServiceClassHolder.java
deleted file mode 100644
index b480e14..0000000
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/ServiceClassHolder.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 com.alibaba.dubbo.rpc;
-
-/**
- * TODO this is just a workaround for rest protocol, and now we just ensure it works in the most common dubbo usages
- *
- */
-public class ServiceClassHolder {
-
-    private static final ServiceClassHolder INSTANCE = new ServiceClassHolder();
-
-    private final ThreadLocal<Class> holder  = new ThreadLocal<Class>();
-
-    public static ServiceClassHolder getInstance() {
-        return INSTANCE;
-    }
-
-    private ServiceClassHolder() {
-    }
-
-    public Class popServiceClass() {
-        Class clazz = holder.get();
-        holder.remove();
-        return clazz;
-    }
-
-    public void pushServiceClass(Class clazz) {
-        holder.set(clazz);
-    }
-}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocol.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocol.java
index bf8f8e9..78b5deb 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocol.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocol.java
@@ -23,9 +23,8 @@ import com.alibaba.dubbo.remoting.http.HttpBinder;
 import com.alibaba.dubbo.remoting.http.servlet.BootstrapListener;
 import com.alibaba.dubbo.remoting.http.servlet.ServletManager;
 import com.alibaba.dubbo.rpc.RpcException;
-import com.alibaba.dubbo.rpc.ServiceClassHolder;
+import com.alibaba.dubbo.rpc.StaticContext;
 import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;
-
 import org.apache.http.HeaderElement;
 import org.apache.http.HeaderElementIterator;
 import org.apache.http.HttpResponse;
@@ -83,7 +82,7 @@ public class RestProtocol extends AbstractProxyProtocol {
     @Override
     protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException {
         String addr = getAddr(url);
-        Class implClass = ServiceClassHolder.getInstance().popServiceClass();
+        Class implClass = (Class) StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).get(url.getServiceKey());
         RestServer server = servers.get(addr);
         if (server == null) {
             server = serverFactory.createServer(url.getParameter(Constants.SERVER_KEY, "jetty"));
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java
index 089af0a..6a2c637 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/RestProtocolTest.java
@@ -27,7 +27,8 @@ import com.alibaba.dubbo.rpc.Result;
 import com.alibaba.dubbo.rpc.RpcContext;
 import com.alibaba.dubbo.rpc.RpcException;
 import com.alibaba.dubbo.rpc.RpcInvocation;
-import com.alibaba.dubbo.rpc.ServiceClassHolder;
+import com.alibaba.dubbo.rpc.StaticContext;
+
 import org.hamcrest.CoreMatchers;
 import org.junit.After;
 import org.junit.Test;
@@ -48,7 +49,7 @@ public class RestProtocolTest {
 
     @Test
     public void testExport() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
 
         RpcContext.getContext().setAttachment("timeout", "200");
@@ -64,7 +65,7 @@ public class RestProtocolTest {
 
     @Test
     public void testNettyServer() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
         URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
         Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
@@ -79,7 +80,7 @@ public class RestProtocolTest {
 
     @Test(expected = RpcException.class)
     public void testServletWithoutWebConfig() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
         URL servletUrl = exportUrl.addParameter(Constants.SERVER_KEY, "servlet");
 
@@ -88,7 +89,7 @@ public class RestProtocolTest {
 
     @Test(expected = RpcException.class)
     public void testErrorHandler() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
         URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty");
         Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
@@ -100,7 +101,7 @@ public class RestProtocolTest {
 
     @Test
     public void testInvoke() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
 
         Exporter<IDemoService> exporter = protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, exportUrl));
@@ -113,7 +114,7 @@ public class RestProtocolTest {
 
     @Test
     public void testFilter() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
         URL nettyUrl = exportUrl.addParameter(Constants.SERVER_KEY, "netty")
                 .addParameter(Constants.EXTENSION_KEY, "com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter");
@@ -130,7 +131,7 @@ public class RestProtocolTest {
 
     @Test(expected = RuntimeException.class)
     public void testRegFail() {
-        ServiceClassHolder.getInstance().pushServiceClass(DemoService.class);
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(exportUrl.getServiceKey(), DemoService.class);
 
         URL nettyUrl = exportUrl.addParameter(Constants.EXTENSION_KEY, "com.not.existing.Filter");
         protocol.export(proxy.getInvoker(new DemoService(), IDemoService.class, nettyUrl));
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protol/rest/RestProtocolTest.java
index 4764cf6..fce656e 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protol/rest/RestProtocolTest.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protol/rest/RestProtocolTest.java
@@ -16,9 +16,15 @@
  */
 package com.alibaba.dubbo.rpc.protol.rest;
 
+import com.alibaba.dubbo.common.Constants;
 import com.alibaba.dubbo.common.URL;
 import com.alibaba.dubbo.common.extension.ExtensionLoader;
-import com.alibaba.dubbo.rpc.*;
+import com.alibaba.dubbo.rpc.Exporter;
+import com.alibaba.dubbo.rpc.Invoker;
+import com.alibaba.dubbo.rpc.Protocol;
+import com.alibaba.dubbo.rpc.ProxyFactory;
+import com.alibaba.dubbo.rpc.StaticContext;
+
 import junit.framework.Assert;
 import org.junit.Test;
 
@@ -32,10 +38,11 @@ public class RestProtocolTest {
 
     @Test
     public void testRestProtocol() {
-        ServiceClassHolder.getInstance().pushServiceClass(RestServiceImpl.class);
+        URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0");
+        StaticContext.getContext(Constants.SERVICE_IMPL_CLASS).put(url.getServiceKey(), RestServiceImpl.class);
         RestServiceImpl server = new RestServiceImpl();
         Assert.assertFalse(server.isCalled());
-        URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say1?version=1.0.0");
+
         Exporter<RestService> exporter = protocol.export(proxyFactory.getInvoker(server, RestService.class, url));
         Invoker<RestService> invoker = protocol.refer(RestService.class, url);
         RestService client = proxyFactory.getProxy(invoker);
diff --git a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml
index bacd513..920a95d 100644
--- a/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml
+++ b/dubbo-test/dubbo-test-examples/src/main/java/com/alibaba/dubbo/examples/rest/rest-consumer.xml
@@ -15,10 +15,12 @@
   See the License for the specific language governing permissions and
   limitations under the License.
   -->
-<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
+       xmlns:beans="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
-	http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
+	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">
 
     <dubbo:application name="rest-consumer" owner="programmer" organization="dubbo"/>