From 5602191629356dc7b5e1d97137767ad3a67a1e47 Mon Sep 17 00:00:00 2001 From: "Owen.Cai" <89424516@qq.com> Date: Thu, 11 Mar 2021 20:45:35 +0800 Subject: [PATCH] [dubbo-6756]ssl support load from classpath (#6756) * ssl support load from classpath * delete no need import * check dubbo compile bug --- .../common/constants/CommonConstants.java | 3 ++ .../apache/dubbo/common/utils/IOUtils.java | 38 +++++++++++++++++++ .../org/apache/dubbo/config/SslConfig.java | 28 +++++++------- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java index 4d81bf9fbe7..316f00b8ca1 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java @@ -365,5 +365,8 @@ public interface CommonConstants { String CLUSTER_REDIS = "cluster"; + /** Pseudo URL prefix for loading from the class path: "classpath:". */ + String CLASSPATH_URL_PREFIX = "classpath:"; + String DEFAULT_VERSION = "0.0.0"; } diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java index 02ec00bdcab..1e44924f7f7 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java @@ -16,6 +16,7 @@ */ package org.apache.dubbo.common.utils; +import org.apache.dubbo.common.constants.CommonConstants; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -30,6 +31,9 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.io.FileNotFoundException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -228,4 +232,38 @@ public static void appendLines(File file, String[] lines) throws IOException { writeLines(new FileOutputStream(file, true), lines); } + + /** + * use like spring code + * @param resourceLocation + * @return + */ + public static URL getURL(String resourceLocation) throws FileNotFoundException { + Assert.notNull(resourceLocation, "Resource location must not be null"); + if (resourceLocation.startsWith(CommonConstants.CLASSPATH_URL_PREFIX)) { + String path = resourceLocation.substring(CommonConstants.CLASSPATH_URL_PREFIX.length()); + ClassLoader cl = ClassUtils.getClassLoader(); + URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); + if (url == null) { + String description = "class path resource [" + path + "]"; + throw new FileNotFoundException(description + + " cannot be resolved to URL because it does not exist"); + } + return url; + } + try { + // try URL + return new URL(resourceLocation); + } + catch (MalformedURLException ex) { + // no URL -> treat as file path + try { + return new File(resourceLocation).toURI().toURL(); + } + catch (MalformedURLException ex2) { + throw new FileNotFoundException("Resource location [" + resourceLocation + + "] is neither a URL not a well-formed file path"); + } + } + } } \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java index 3be7d3ef5c0..83f86594c71 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/SslConfig.java @@ -18,10 +18,10 @@ import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.utils.IOUtils; import org.apache.dubbo.config.support.Parameter; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.util.concurrent.atomic.AtomicBoolean; @@ -120,9 +120,9 @@ public void setClientTrustCertCollectionPath(String clientTrustCertCollectionPat this.clientTrustCertCollectionPath = clientTrustCertCollectionPath; } - public InputStream getServerKeyCertChainPathStream() throws FileNotFoundException { + public InputStream getServerKeyCertChainPathStream() throws IOException { if (serverKeyCertChainPath != null) { - serverKeyCertChainPathStream = new FileInputStream(serverKeyCertChainPath); + serverKeyCertChainPathStream = IOUtils.getURL(serverKeyCertChainPath).openStream(); } return serverKeyCertChainPathStream; } @@ -131,9 +131,9 @@ public void setServerKeyCertChainPathStream(InputStream serverKeyCertChainPathSt this.serverKeyCertChainPathStream = serverKeyCertChainPathStream; } - public InputStream getServerPrivateKeyPathStream() throws FileNotFoundException { + public InputStream getServerPrivateKeyPathStream() throws IOException { if (serverPrivateKeyPath != null) { - serverPrivateKeyPathStream = new FileInputStream(serverPrivateKeyPath); + serverPrivateKeyPathStream = IOUtils.getURL(serverPrivateKeyPath).openStream(); } return serverPrivateKeyPathStream; } @@ -142,9 +142,9 @@ public void setServerPrivateKeyPathStream(InputStream serverPrivateKeyPathStream this.serverPrivateKeyPathStream = serverPrivateKeyPathStream; } - public InputStream getServerTrustCertCollectionPathStream() throws FileNotFoundException { + public InputStream getServerTrustCertCollectionPathStream() throws IOException { if (serverTrustCertCollectionPath != null) { - serverTrustCertCollectionPathStream = new FileInputStream(serverTrustCertCollectionPath); + serverTrustCertCollectionPathStream = IOUtils.getURL(serverTrustCertCollectionPath).openStream(); } return serverTrustCertCollectionPathStream; } @@ -153,9 +153,9 @@ public void setServerTrustCertCollectionPathStream(InputStream serverTrustCertCo this.serverTrustCertCollectionPathStream = serverTrustCertCollectionPathStream; } - public InputStream getClientKeyCertChainPathStream() throws FileNotFoundException { + public InputStream getClientKeyCertChainPathStream() throws IOException { if (clientKeyCertChainPath != null) { - clientKeyCertChainPathStream = new FileInputStream(clientKeyCertChainPath); + clientKeyCertChainPathStream = IOUtils.getURL(clientKeyCertChainPath).openStream(); } return clientKeyCertChainPathStream; } @@ -164,9 +164,9 @@ public void setClientKeyCertChainPathStream(InputStream clientKeyCertChainPathSt this.clientKeyCertChainPathStream = clientKeyCertChainPathStream; } - public InputStream getClientPrivateKeyPathStream() throws FileNotFoundException { + public InputStream getClientPrivateKeyPathStream() throws IOException { if (clientPrivateKeyPath != null) { - clientPrivateKeyPathStream = new FileInputStream(clientPrivateKeyPath); + clientPrivateKeyPathStream = IOUtils.getURL(clientPrivateKeyPath).openStream(); } return clientPrivateKeyPathStream; } @@ -175,9 +175,9 @@ public void setClientPrivateKeyPathStream(InputStream clientPrivateKeyPathStream this.clientPrivateKeyPathStream = clientPrivateKeyPathStream; } - public InputStream getClientTrustCertCollectionPathStream() throws FileNotFoundException { + public InputStream getClientTrustCertCollectionPathStream() throws IOException { if (clientTrustCertCollectionPath != null) { - clientTrustCertCollectionPathStream = new FileInputStream(clientTrustCertCollectionPath); + clientTrustCertCollectionPathStream = IOUtils.getURL(clientTrustCertCollectionPath).openStream(); } return clientTrustCertCollectionPathStream; }