|
| 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