You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/06/12 20:40:32 UTC
[camel] 04/05: (chores) camel-base-engine: cleanup duplicated code in package scan resolvers
This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit e713eee9ff2477cef9f50f176639b8f2c7fd6d47
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Mon Jun 12 14:08:01 2023 +0200
(chores) camel-base-engine: cleanup duplicated code in package scan resolvers
---
.../camel/impl/engine/BasePackageScanResolver.java | 61 ++++++++++++++++++++++
.../engine/DefaultPackageScanClassResolver.java | 53 ++-----------------
.../engine/DefaultPackageScanResourceResolver.java | 53 ++-----------------
3 files changed, 69 insertions(+), 98 deletions(-)
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/BasePackageScanResolver.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/BasePackageScanResolver.java
index cf5a8893fc8..7d703de948c 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/BasePackageScanResolver.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/BasePackageScanResolver.java
@@ -17,7 +17,11 @@
package org.apache.camel.impl.engine;
import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
@@ -139,4 +143,61 @@ public abstract class BasePackageScanResolver extends ServiceSupport implements
return loader.getResources(packageName);
}
+ protected String parseUrlPath(URL url) {
+ String urlPath = url.getFile();
+ urlPath = URLDecoder.decode(urlPath, StandardCharsets.UTF_8);
+ if (log.isTraceEnabled()) {
+ log.trace("Decoded urlPath: {} with protocol: {}", urlPath, url.getProtocol());
+ }
+
+ // If it's a file in a directory, trim the stupid file: spec
+ if (urlPath.startsWith("file:")) {
+ // file path can be temporary folder which uses characters that the URLDecoder decodes wrong
+ // for example + being decoded to something else (+ can be used in temp folders on Mac OS)
+ // to remedy this then create new path without using the URLDecoder
+ try {
+ urlPath = new URI(url.getFile()).getPath();
+ } catch (URISyntaxException e) {
+ // fallback to use as it was given from the URLDecoder
+ // this allows us to work on Windows if users have spaces in paths
+ }
+
+ if (urlPath.startsWith("file:")) {
+ urlPath = urlPath.substring(5);
+ }
+ }
+
+ // osgi bundles should be skipped
+ if (url.toString().startsWith("bundle:") || urlPath.startsWith("bundle:")) {
+ log.trace("Skipping OSGi bundle: {}", url);
+ return null;
+ }
+
+ // bundle resource should be skipped
+ if (url.toString().startsWith("bundleresource:") || urlPath.startsWith("bundleresource:")) {
+ log.trace("Skipping bundleresource: {}", url);
+ return null;
+ }
+
+ // Else it's in a JAR, grab the path to the jar
+ if (urlPath.indexOf('!') > 0) {
+ urlPath = urlPath.substring(0, urlPath.indexOf('!'));
+ }
+ return urlPath;
+ }
+
+ protected Enumeration<URL> getUrls(String packageName, ClassLoader loader) {
+ Enumeration<URL> urls;
+ try {
+ urls = getResources(loader, packageName);
+ if (!urls.hasMoreElements()) {
+ log.trace("No URLs returned by classloader");
+ }
+ } catch (IOException ioe) {
+ log.warn("Cannot read package: {}", packageName, ioe);
+ return null;
+ }
+ return urls;
+ }
+
}
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanClassResolver.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanClassResolver.java
index f1d95ebda10..e5027274903 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanClassResolver.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanClassResolver.java
@@ -21,12 +21,8 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -168,14 +164,8 @@ public class DefaultPackageScanClassResolver extends BasePackageScanResolver
test, packageName, loader.getClass().getName());
}
- Enumeration<URL> urls;
- try {
- urls = getResources(loader, packageName);
- if (!urls.hasMoreElements()) {
- log.trace("No URLs returned by classloader");
- }
- } catch (IOException ioe) {
- log.warn("Cannot read package: {}", packageName, ioe);
+ Enumeration<URL> urls = getUrls(packageName, loader);
+ if (urls == null) {
return;
}
@@ -187,46 +177,11 @@ public class DefaultPackageScanClassResolver extends BasePackageScanResolver
url = customResourceLocator(url);
- String urlPath = url.getFile();
- urlPath = URLDecoder.decode(urlPath, StandardCharsets.UTF_8);
- if (log.isTraceEnabled()) {
- log.trace("Decoded urlPath: {} with protocol: {}", urlPath, url.getProtocol());
- }
-
- // If it's a file in a directory, trim the stupid file: spec
- if (urlPath.startsWith("file:")) {
- // file path can be temporary folder which uses characters that the URLDecoder decodes wrong
- // for example + being decoded to something else (+ can be used in temp folders on Mac OS)
- // to remedy this then create new path without using the URLDecoder
- try {
- urlPath = new URI(url.getFile()).getPath();
- } catch (URISyntaxException e) {
- // fallback to use as it was given from the URLDecoder
- // this allows us to work on Windows if users have spaces in paths
- }
-
- if (urlPath.startsWith("file:")) {
- urlPath = urlPath.substring(5);
- }
- }
-
- // osgi bundles should be skipped
- if (url.toString().startsWith("bundle:") || urlPath.startsWith("bundle:")) {
- log.trace("Skipping OSGi bundle: {}", url);
- continue;
- }
-
- // bundle resource should be skipped
- if (url.toString().startsWith("bundleresource:") || urlPath.startsWith("bundleresource:")) {
- log.trace("Skipping bundleresource: {}", url);
+ String urlPath = parseUrlPath(url);
+ if (urlPath == null) {
continue;
}
- // Else it's in a JAR, grab the path to the jar
- if (urlPath.indexOf('!') > 0) {
- urlPath = urlPath.substring(0, urlPath.indexOf('!'));
- }
-
log.trace("Scanning for classes in: {} matching criteria: {}", urlPath, test);
File file = new File(urlPath);
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
index cfcda0f5d0b..6fa8a18eaf9 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultPackageScanResourceResolver.java
@@ -21,12 +21,8 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
@@ -129,14 +125,8 @@ public class DefaultPackageScanResourceResolver extends BasePackageScanResolver
Set<Resource> resources,
String subPattern) {
- Enumeration<URL> urls;
- try {
- urls = getResources(classLoader, packageName);
- if (!urls.hasMoreElements()) {
- log.trace("No URLs returned by classloader");
- }
- } catch (IOException ioe) {
- log.warn("Cannot read package: {}", packageName, ioe);
+ Enumeration<URL> urls = getUrls(packageName, classLoader);
+ if (urls == null) {
return;
}
@@ -148,46 +138,11 @@ public class DefaultPackageScanResourceResolver extends BasePackageScanResolver
url = customResourceLocator(url);
- String urlPath = url.getFile();
- urlPath = URLDecoder.decode(urlPath, StandardCharsets.UTF_8);
- if (log.isTraceEnabled()) {
- log.trace("Decoded urlPath: {} with protocol: {}", urlPath, url.getProtocol());
- }
-
- // If it's a file in a directory, trim the stupid file: spec
- if (urlPath.startsWith("file:")) {
- // file path can be temporary folder which uses characters that the URLDecoder decodes wrong
- // for example + being decoded to something else (+ can be used in temp folders on Mac OS)
- // to remedy this then create new path without using the URLDecoder
- try {
- urlPath = new URI(url.getFile()).getPath();
- } catch (URISyntaxException e) {
- // fallback to use as it was given from the URLDecoder
- // this allows us to work on Windows if users have spaces in paths
- }
-
- if (urlPath.startsWith("file:")) {
- urlPath = urlPath.substring(5);
- }
- }
-
- // osgi bundles should be skipped
- if (url.toString().startsWith("bundle:") || urlPath.startsWith("bundle:")) {
- log.trace("Skipping OSGi bundle: {}", url);
+ String urlPath = parseUrlPath(url);
+ if (urlPath == null) {
continue;
}
- // bundle resource should be skipped
- if (url.toString().startsWith("bundleresource:") || urlPath.startsWith("bundleresource:")) {
- log.trace("Skipping bundleresource: {}", url);
- continue;
- }
-
- // Else it's in a JAR, grab the path to the jar
- if (urlPath.indexOf('!') > 0) {
- urlPath = urlPath.substring(0, urlPath.indexOf('!'));
- }
-
log.trace("Scanning for resources in: {} matching pattern: {}", urlPath, subPattern);
File file = new File(urlPath);