From 47ef71f6bcfd0f4ef453c1cbdf8c9a69ed770686 Mon Sep 17 00:00:00 2001 From: wkclz Date: Sun, 17 Dec 2023 23:41:49 +0800 Subject: [PATCH 1/5] #2241 fix bug: when running with a fat jar, need to read entries with openConnection when running in IDE or a thin jar, it is ok, when running with a fat jar in server, new JarFile can not open and read the entries. --- .../main/java/io/kubernetes/client/util/ModelMapper.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index 1271a60ccc..02a81afa09 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -22,9 +22,9 @@ import java.io.File; import java.io.IOException; +import java.net.JarURLConnection; import java.net.URI; import java.net.URL; -import java.net.URLDecoder; import java.time.Duration; import java.util.ArrayList; import java.util.Enumeration; @@ -484,10 +484,8 @@ private static List getClassNamesFromPackage(ClassLoader classLoader, St } private static void processJarPackage(URL packageURL, String packageName, String pkg, ArrayList names) throws IOException { - String jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8"); - jarFileName = jarFileName.substring(5, jarFileName.indexOf("!")); - logger.info("Loading classes from jar {}", jarFileName); - try (JarFile jf = new JarFile(jarFileName)) { + logger.info("Loading classes from jar {}", packageURL.getFile()); + try (JarFile jf = ((JarURLConnection) packageURL.openConnection()).getJarFile()) { Enumeration jarEntries = jf.entries(); while (jarEntries.hasMoreElements()) { processJarEntry(jarEntries.nextElement(), packageName, pkg, names); From ec7e7b9b1f5d3f3ae6b51f4d317bb35f7552c491 Mon Sep 17 00:00:00 2001 From: wkclz Date: Thu, 28 Dec 2023 23:52:36 +0800 Subject: [PATCH 2/5] #2952 fix: packageURL must startwith java: or nested: In a fat jar, packageURL start with "nested:", not a fat jar, it start with "jar:". --- .../main/java/io/kubernetes/client/util/ModelMapper.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index 02a81afa09..2f6f4c99d3 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -484,7 +484,11 @@ private static List getClassNamesFromPackage(ClassLoader classLoader, St } private static void processJarPackage(URL packageURL, String packageName, String pkg, ArrayList names) throws IOException { - logger.info("Loading classes from jar {}", packageURL.getFile()); + String jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8"); + if (!jarFileName.startsWith("jar:") && !jarFileName.startsWith("nested:")) { + logger.error("Loading classes from jar with error packageURL: {}", jarFileName); + } + logger.info("Loading classes from jar {}", jarFileName); try (JarFile jf = ((JarURLConnection) packageURL.openConnection()).getJarFile()) { Enumeration jarEntries = jf.entries(); while (jarEntries.hasMoreElements()) { From 88ac227456b5fc48fd6bf7e3dc36060b37bf3319 Mon Sep 17 00:00:00 2001 From: wkclz Date: Fri, 29 Dec 2023 11:30:02 +0800 Subject: [PATCH 3/5] #2952 packageURL is not jar or nested, it would be return packageURL is not jar or nested, it would be return --- util/src/main/java/io/kubernetes/client/util/ModelMapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index 2f6f4c99d3..e51bfaf3f7 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -487,6 +487,7 @@ private static void processJarPackage(URL packageURL, String packageName, String String jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8"); if (!jarFileName.startsWith("jar:") && !jarFileName.startsWith("nested:")) { logger.error("Loading classes from jar with error packageURL: {}", jarFileName); + return; } logger.info("Loading classes from jar {}", jarFileName); try (JarFile jf = ((JarURLConnection) packageURL.openConnection()).getJarFile()) { From de71d98acb020d7e2496e2156c428c7682a9cafa Mon Sep 17 00:00:00 2001 From: wkclz Date: Mon, 1 Jan 2024 21:49:01 +0800 Subject: [PATCH 4/5] #2952 re import URLDecoder re import URLDecoder --- util/src/main/java/io/kubernetes/client/util/ModelMapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index e51bfaf3f7..2a1691facf 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -25,6 +25,7 @@ import java.net.JarURLConnection; import java.net.URI; import java.net.URL; +import java.net.URLDecoder; import java.time.Duration; import java.util.ArrayList; import java.util.Enumeration; From f69bd5ea2e0470f2120816909b852a647c26d8ef Mon Sep 17 00:00:00 2001 From: wangkc Date: Wed, 3 Jan 2024 23:20:54 +0800 Subject: [PATCH 5/5] the packageURL is startWith 'file:' when client is a file in target (unit test) --- .../kubernetes/client/util/ModelMapper.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index 2a1691facf..995b151e2d 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -486,17 +486,26 @@ private static List getClassNamesFromPackage(ClassLoader classLoader, St private static void processJarPackage(URL packageURL, String packageName, String pkg, ArrayList names) throws IOException { String jarFileName = URLDecoder.decode(packageURL.getFile(), "UTF-8"); - if (!jarFileName.startsWith("jar:") && !jarFileName.startsWith("nested:")) { + JarFile jf = null; + // jar: client in repository; nested: client in a fat jar + if (jarFileName.startsWith("jar:") || jarFileName.startsWith("nested:")) { + jf = ((JarURLConnection) packageURL.openConnection()).getJarFile(); + } + // file: client is a file in target (unit test) + if (jarFileName.startsWith("file:") ) { + jarFileName = jarFileName.substring(5, jarFileName.indexOf("!")); + jf = new JarFile(jarFileName); + } + if (jf == null) { logger.error("Loading classes from jar with error packageURL: {}", jarFileName); return; } logger.info("Loading classes from jar {}", jarFileName); - try (JarFile jf = ((JarURLConnection) packageURL.openConnection()).getJarFile()) { - Enumeration jarEntries = jf.entries(); - while (jarEntries.hasMoreElements()) { - processJarEntry(jarEntries.nextElement(), packageName, pkg, names); - } + Enumeration jarEntries = jf.entries(); + while (jarEntries.hasMoreElements()) { + processJarEntry(jarEntries.nextElement(), packageName, pkg, names); } + jf.close(); } private static void processJarEntry(JarEntry jarEntry, String packageName, String pkg, ArrayList names) {