Skip to content

Commit adf019b

Browse files
Fixing ClassLoader Issue for IT's
1 parent ea3b7bc commit adf019b

3 files changed

Lines changed: 117 additions & 4 deletions

File tree

addons/hive-bridge/pom.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,14 @@
335335
</dependencies>
336336
<build>
337337
<plugins>
338-
<!-- Parent disables Failsafe forking (forkCount=0). Hive IT runs in Maven JVM + Hive TCCL
339-
breaks JAX-RS ClientBuilder; fork + unshaded v2 client on IT classpath fix LinkageError. -->
338+
<!-- Parent disables Failsafe forking (forkCount=0). Fork IT JVM; on Java 9+ use
339+
HiveITSystemClassLoader so Hive 3.1.x SessionState sees a URLClassLoader. -->
340340
<plugin>
341341
<groupId>org.apache.maven.plugins</groupId>
342342
<artifactId>maven-failsafe-plugin</artifactId>
343343
<configuration combine.self="merge">
344344
<forkCount>1</forkCount>
345345
<reuseForks>false</reuseForks>
346-
<useSystemClassLoader>false</useSystemClassLoader>
347346
<classpathDependencyExcludes>
348347
<classpathDependencyExclude>org.apache.atlas:atlas-client-v2-shaded</classpathDependencyExclude>
349348
<classpathDependencyExclude>org.glassfish.jersey.media:jersey-media-json-jackson</classpathDependencyExclude>
@@ -744,5 +743,22 @@
744743
</plugins>
745744
</build>
746745
</profile>
746+
<profile>
747+
<id>hive-it-jdk17</id>
748+
<activation>
749+
<jdk>[9,)</jdk>
750+
</activation>
751+
<build>
752+
<plugins>
753+
<plugin>
754+
<groupId>org.apache.maven.plugins</groupId>
755+
<artifactId>maven-failsafe-plugin</artifactId>
756+
<configuration combine.self="merge">
757+
<argLine combine.children="append">--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED -Djava.system.class.loader=org.apache.atlas.hive.HiveITSystemClassLoader</argLine>
758+
</configuration>
759+
</plugin>
760+
</plugins>
761+
</build>
762+
</profile>
747763
</profiles>
748764
</project>

addons/hive-bridge/src/test/java/org/apache/atlas/hive/HiveITBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public class HiveITBase {
107107
public void setUp() throws Exception {
108108
//Set-up hive session
109109
conf = new HiveConf();
110-
conf.setClassLoader(Thread.currentThread().getContextClassLoader());
111110
conf.set("hive.metastore.event.listeners", "");
112111

113112
// 'driver' using this configuration will be used for tests in HiveHookIT
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.atlas.hive;
20+
21+
import java.io.File;
22+
import java.net.MalformedURLException;
23+
import java.net.URL;
24+
import java.net.URLClassLoader;
25+
26+
/**
27+
* Test JVM system class loader for hive-bridge integration tests on Java 9+.
28+
* Hive 3.1.x {@code SessionState} casts {@code SessionState.class.getClassLoader()}
29+
* to {@link URLClassLoader}; the default application loader is not a URLClassLoader
30+
* on Java 9+, so tests install this loader via {@code -Djava.system.class.loader}.
31+
*/
32+
public final class HiveITSystemClassLoader extends URLClassLoader {
33+
private static final ClassLoader PLATFORM_CLASS_LOADER = ClassLoader.getPlatformClassLoader();
34+
35+
static {
36+
ClassLoader.registerAsParallelCapable();
37+
}
38+
39+
public HiveITSystemClassLoader(ClassLoader parent) {
40+
super(classpathUrls(), parent);
41+
}
42+
43+
@Override
44+
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
45+
synchronized (getClassLoadingLock(name)) {
46+
Class<?> loadedClass = findLoadedClass(name);
47+
48+
if (loadedClass == null) {
49+
if (isPlatformClass(name)) {
50+
loadedClass = PLATFORM_CLASS_LOADER.loadClass(name);
51+
} else {
52+
try {
53+
loadedClass = findClass(name);
54+
} catch (ClassNotFoundException e) {
55+
loadedClass = PLATFORM_CLASS_LOADER.loadClass(name);
56+
}
57+
}
58+
}
59+
60+
if (resolve) {
61+
resolveClass(loadedClass);
62+
}
63+
64+
return loadedClass;
65+
}
66+
}
67+
68+
private static boolean isPlatformClass(String name) {
69+
return name.startsWith("java.") || name.startsWith("jdk.");
70+
}
71+
72+
private static URL[] classpathUrls() {
73+
String classpath = System.getProperty("java.class.path");
74+
String[] entries = classpath.split(File.pathSeparator);
75+
URL[] urls = new URL[entries.length];
76+
int urlCount = 0;
77+
78+
for (String entry : entries) {
79+
if (entry.isEmpty()) {
80+
continue;
81+
}
82+
83+
try {
84+
urls[urlCount++] = new File(entry).toURI().toURL();
85+
} catch (MalformedURLException e) {
86+
throw new IllegalStateException("Invalid classpath entry: " + entry, e);
87+
}
88+
}
89+
90+
if (urlCount == urls.length) {
91+
return urls;
92+
}
93+
94+
URL[] trimmed = new URL[urlCount];
95+
System.arraycopy(urls, 0, trimmed, 0, urlCount);
96+
return trimmed;
97+
}
98+
}

0 commit comments

Comments
 (0)