You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/09/08 06:38:24 UTC

[dubbo] branch 3.0 updated: add customized class path for javassist. (#6705)

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

liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new b5bb147  add customized class path for javassist. (#6705)
b5bb147 is described below

commit b5bb1472496141dfc558a32b6d5b838d212e26b8
Author: ken.lj <ke...@gmail.com>
AuthorDate: Tue Sep 8 14:36:12 2020 +0800

    add customized class path for javassist. (#6705)
---
 .../dubbo/rpc/cluster/support/ClusterUtils.java    |  19 ++++
 .../cluster/support/ProviderURLMergeProcessor.java |  13 +++
 .../dubbo/common/bytecode/ClassGenerator.java      |  12 +--
 .../common/bytecode/CustomizedLoaderClassPath.java | 104 +++++++++++++++++++++
 .../org/apache/dubbo/config/ServiceConfig.java     |   4 +
 .../registry/integration/RegistryDirectory.java    |   2 +-
 .../org/apache/dubbo/rpc/support/RpcUtils.java     |  18 +++-
 .../dubbo/rpc/protocol/dubbo/DubboInvoker.java     |   2 +
 8 files changed, 166 insertions(+), 8 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java
index 3e5f1d2..b7a30ed 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ClusterUtils.java
@@ -17,9 +17,11 @@
 package org.apache.dubbo.rpc.cluster.support;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
 import org.apache.dubbo.remoting.Constants;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import static org.apache.dubbo.common.constants.CommonConstants.ALIVE_KEY;
@@ -117,4 +119,21 @@ public class ClusterUtils {
         return remoteUrl.clearParameters().addParameters(map);
     }
 
+    public static URL mergeProviderUrl(URL remoteUrl, Map<String, String> localMap) {
+
+        //urlprocessor => upc
+        List<ProviderURLMergeProcessor> providerURLMergeProcessors = ExtensionLoader.getExtensionLoader(ProviderURLMergeProcessor.class)
+                .getActivateExtension(remoteUrl, "upc");
+
+        if (providerURLMergeProcessors != null && providerURLMergeProcessors.size() > 0) {
+            for (ProviderURLMergeProcessor providerURLMergeProcessor : providerURLMergeProcessors) {
+                if (providerURLMergeProcessor.accept(remoteUrl, localMap)) {
+                    return providerURLMergeProcessor.mergeProviderUrl(remoteUrl, localMap);
+                }
+            }
+        }
+
+        return mergeUrl(remoteUrl, localMap);
+    }
+
 }
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ProviderURLMergeProcessor.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ProviderURLMergeProcessor.java
new file mode 100644
index 0000000..9c01aeb
--- /dev/null
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ProviderURLMergeProcessor.java
@@ -0,0 +1,13 @@
+package org.apache.dubbo.rpc.cluster.support;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.SPI;
+
+import java.util.Map;
+
+@SPI
+public interface ProviderURLMergeProcessor {
+    URL mergeProviderUrl(URL providerUrl, Map<String, String> localParametersMap);
+
+    boolean accept(URL providerUrl, Map<String, String> localParametersMap);
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
index c4ecea6..b81b7a3 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/ClassGenerator.java
@@ -16,6 +16,11 @@
  */
 package org.apache.dubbo.common.bytecode;
 
+import org.apache.dubbo.common.utils.ArrayUtils;
+import org.apache.dubbo.common.utils.ClassUtils;
+import org.apache.dubbo.common.utils.ReflectUtils;
+import org.apache.dubbo.common.utils.StringUtils;
+
 import javassist.CannotCompileException;
 import javassist.ClassPool;
 import javassist.CtClass;
@@ -24,12 +29,7 @@ import javassist.CtField;
 import javassist.CtMethod;
 import javassist.CtNewConstructor;
 import javassist.CtNewMethod;
-import javassist.LoaderClassPath;
 import javassist.NotFoundException;
-import org.apache.dubbo.common.utils.ArrayUtils;
-import org.apache.dubbo.common.utils.ClassUtils;
-import org.apache.dubbo.common.utils.ReflectUtils;
-import org.apache.dubbo.common.utils.StringUtils;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
@@ -91,7 +91,7 @@ public final class ClassGenerator {
         ClassPool pool = POOL_MAP.get(loader);
         if (pool == null) {
             pool = new ClassPool(true);
-            pool.appendClassPath(new LoaderClassPath(loader));
+            pool.appendClassPath(new CustomizedLoaderClassPath(loader));
             POOL_MAP.put(loader, pool);
         }
         return pool;
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/CustomizedLoaderClassPath.java b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/CustomizedLoaderClassPath.java
new file mode 100644
index 0000000..c529917
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/CustomizedLoaderClassPath.java
@@ -0,0 +1,104 @@
+/*
+ * Javassist, a Java-bytecode translator toolkit.
+ * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License.  Alternatively, the contents of this file may be used under
+ * the terms of the GNU Lesser General Public License Version 2.1 or later,
+ * or the Apache License Version 2.0.
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ */
+
+package org.apache.dubbo.common.bytecode;
+
+import javassist.ClassPath;
+
+import java.io.InputStream;
+import java.lang.ref.WeakReference;
+import java.net.URL;
+
+/**
+ * A class search-path representing a class loader.
+ *
+ * <p>It is used for obtaining a class file from the given
+ * class loader by <code>getResourceAsStream()</code>.
+ * The <code>LoaderClassPath</code> refers to the class loader through
+ * <code>WeakReference</code>.  If the class loader is garbage collected,
+ * the other search pathes are examined.
+ *
+ * <p>The given class loader must have both <code>getResourceAsStream()</code>
+ * and <code>getResource()</code>.
+ *
+ * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
+ * @author Shigeru Chiba
+ */
+public class CustomizedLoaderClassPath implements ClassPath {
+    private WeakReference clref;
+
+    /**
+     * Creates a search path representing a class loader.
+     */
+    public CustomizedLoaderClassPath(ClassLoader cl) {
+        clref = new WeakReference(cl);
+    }
+
+    public String toString() {
+        Object cl = null;
+        if (clref != null)
+            cl = clref.get();
+
+        return cl == null ? "<null>" : cl.toString();
+    }
+
+    /**
+     * Obtains a class file from the class loader.
+     * This method calls <code>getResourceAsStream(String)</code>
+     * on the class loader.
+     */
+    public InputStream openClassfile(String classname) {
+        String cname = classname.replace('.', '/') + ".class";
+        ClassLoader cl = (ClassLoader) clref.get();
+        if (cl == null) {
+            return null;        // not found
+        } else {
+            InputStream result = cl.getResourceAsStream(cname);
+            if (result == null && (cl != this.getClass().getClassLoader())) {
+                return this.getClass().getClassLoader().getResourceAsStream(cname);
+            }
+            return result;
+        }
+    }
+
+    /**
+     * Obtains the URL of the specified class file.
+     * This method calls <code>getResource(String)</code>
+     * on the class loader.
+     *
+     * @return null if the class file could not be found.
+     */
+    public URL find(String classname) {
+        String cname = classname.replace('.', '/') + ".class";
+        ClassLoader cl = (ClassLoader) clref.get();
+        if (cl == null) {
+            return null;        // not found
+        } else {
+            URL url = cl.getResource(cname);
+            if (url == null && (cl != this.getClass().getClassLoader())) {
+                return this.getClass().getClassLoader().getResource(cname);
+            }
+            return url;
+        }
+    }
+
+    /**
+     * Closes this class path.
+     */
+    public void close() {
+        clref = null;
+    }
+}
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index 37972c1..2afe2b1 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -201,6 +201,10 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
         serviceMetadata.setServiceInterfaceName(getInterface());
         serviceMetadata.setTarget(getRef());
 
+        if (!shouldExport()) {
+            return;
+        }
+
         if (shouldDelay()) {
             DELAY_EXPORT_EXECUTOR.schedule(this::doExport, getDelay(), TimeUnit.MILLISECONDS);
         } else {
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
index 4c65c6a..26c278e 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryDirectory.java
@@ -363,7 +363,7 @@ public class RegistryDirectory<T> extends DynamicDirectory<T> implements NotifyL
      * @return
      */
     private URL mergeUrl(URL providerUrl) {
-        providerUrl = ClusterUtils.mergeUrl(providerUrl, queryMap); // Merge the consumer side parameters
+        providerUrl = ClusterUtils.mergeProviderUrl(providerUrl, queryMap); // Merge the consumer side parameters
 
         providerUrl = overrideWithConfigurator(providerUrl);
 
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
index 3e8f81e..5e9e74a 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/RpcUtils.java
@@ -43,6 +43,7 @@ import static org.apache.dubbo.rpc.Constants.ASYNC_KEY;
 import static org.apache.dubbo.rpc.Constants.AUTO_ATTACH_INVOCATIONID_KEY;
 import static org.apache.dubbo.rpc.Constants.ID_KEY;
 import static org.apache.dubbo.rpc.Constants.RETURN_KEY;
+
 /**
  * RpcUtils
  */
@@ -100,7 +101,7 @@ public class RpcUtils {
      */
     public static void attachInvocationIdIfAsync(URL url, Invocation inv) {
         if (isAttachInvocationId(url, inv) && getInvocationId(inv) == null && inv instanceof RpcInvocation) {
-            ((RpcInvocation) inv).setAttachment(ID_KEY, String.valueOf(INVOKE_ID.getAndIncrement()));
+            inv.setAttachment(ID_KEY, String.valueOf(INVOKE_ID.getAndIncrement()));
         }
     }
 
@@ -156,6 +157,14 @@ public class RpcUtils {
 
     public static boolean isAsync(URL url, Invocation inv) {
         boolean isAsync;
+
+        if (inv instanceof RpcInvocation) {
+            RpcInvocation rpcInvocation = (RpcInvocation) inv;
+            if (rpcInvocation.getInvokeMode() != null) {
+                return rpcInvocation.getInvokeMode() == InvokeMode.ASYNC;
+            }
+        }
+
         if (Boolean.TRUE.toString().equals(inv.getAttachment(ASYNC_KEY))) {
             isAsync = true;
         } else {
@@ -189,6 +198,13 @@ public class RpcUtils {
     }
 
     public static InvokeMode getInvokeMode(URL url, Invocation inv) {
+        if (inv instanceof RpcInvocation) {
+            RpcInvocation rpcInvocation = (RpcInvocation) inv;
+            if (rpcInvocation.getInvokeMode() != null) {
+                return rpcInvocation.getInvokeMode();
+            }
+        }
+
         if (isReturnTypeFuture(inv)) {
             return InvokeMode.FUTURE;
         } else if (isAsync(url, inv)) {
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java
index c415ac4..4fc52f7 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboInvoker.java
@@ -48,6 +48,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_ATTACHMENT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.TIME_COUNTDOWN_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
 import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;
@@ -95,6 +96,7 @@ public class DubboInvoker<T> extends AbstractInvoker<T> {
         try {
             boolean isOneway = RpcUtils.isOneway(getUrl(), invocation);
             int timeout = calculateTimeout(invocation, methodName);
+            invocation.put(TIMEOUT_KEY, timeout);
             if (isOneway) {
                 boolean isSent = getUrl().getMethodParameter(methodName, Constants.SENT_KEY, false);
                 currentClient.send(inv, isSent);