|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.uima.internal.util; |
| 20 | + |
| 21 | +import org.apache.uima.UIMAFramework; |
| 22 | +import org.apache.uima.UimaContext; |
| 23 | +import org.apache.uima.UimaContextAdmin; |
| 24 | +import org.apache.uima.UimaContextHolder; |
| 25 | +import org.apache.uima.resource.ResourceManager; |
| 26 | + |
| 27 | +/** |
| 28 | + * INTERNAL API - Helper functions to obtain a suitable class loader. |
| 29 | + */ |
| 30 | +public final class ClassLoaderUtils { |
| 31 | + private ClassLoaderUtils() { |
| 32 | + // No instances |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Looks up a suitable class loader in the following order: |
| 37 | + * |
| 38 | + * <ol> |
| 39 | + * <li>The {@link UimaContext} in the {@link UimaContextHolder} of the current thread(if any)</li> |
| 40 | + * <li>The current thread-context class loader (if any)</li> |
| 41 | + * <li>The class loader through which uimaFIT (i.e. this class) was loaded.</li> |
| 42 | + * <li>For backwards compatibility then delegates to {@link #getDefaultClassLoader()}</li> |
| 43 | + * </ol> |
| 44 | + * |
| 45 | + * @return a class loader or {@code null} if no suitable class loader could be found. |
| 46 | + */ |
| 47 | + public static ClassLoader findClassLoader() { |
| 48 | + var uimaThreadContextClassLoader = getExtensionClassLoader(UimaContextHolder.getContext()); |
| 49 | + if (uimaThreadContextClassLoader != null) { |
| 50 | + return uimaThreadContextClassLoader; |
| 51 | + } |
| 52 | + |
| 53 | + var contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 54 | + if (contextClassLoader != null) { |
| 55 | + return contextClassLoader; |
| 56 | + } |
| 57 | + |
| 58 | + var uimaFITClassLoader = ClassLoaderUtils.class.getClassLoader(); |
| 59 | + if (uimaFITClassLoader != null) { |
| 60 | + return uimaFITClassLoader; |
| 61 | + } |
| 62 | + |
| 63 | + return getDefaultClassLoader(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Looks up a suitable class loader in the following order: |
| 68 | + * |
| 69 | + * <ol> |
| 70 | + * <li>The extension class loader of the given {@link ResourceManager}</li> |
| 71 | + * <li>See {@link #findClassLoader()}</li> |
| 72 | + * </ol> |
| 73 | + * |
| 74 | + * @return a class loader or {@code null} if no suitable class loader could be found. |
| 75 | + */ |
| 76 | + public static ClassLoader findClassLoader(ResourceManager aResMgr) { |
| 77 | + var resourceManagerExtensionClassloader = getExtensionClassLoader(aResMgr); |
| 78 | + if (resourceManagerExtensionClassloader != null) { |
| 79 | + return resourceManagerExtensionClassloader; |
| 80 | + } |
| 81 | + |
| 82 | + return findClassLoader(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Looks up a suitable class loader in the following order: |
| 87 | + * |
| 88 | + * <ol> |
| 89 | + * <li>The extension class loader of the {@link ResourceManager} associated with the given |
| 90 | + * {@link UimaContext} (if any)</li> |
| 91 | + * <li>See {@link #findClassLoader(ResourceManager)}</li> |
| 92 | + * </ol> |
| 93 | + * |
| 94 | + * @return a class loader or {@code null} if no suitable class loader could be found. |
| 95 | + */ |
| 96 | + public static ClassLoader findClassLoader(UimaContext aContext) { |
| 97 | + var uimaContextExtensionClassloader = getExtensionClassLoader(aContext); |
| 98 | + if (uimaContextExtensionClassloader != null) { |
| 99 | + return uimaContextExtensionClassloader; |
| 100 | + } |
| 101 | + |
| 102 | + return findClassLoader((ResourceManager) null); |
| 103 | + } |
| 104 | + |
| 105 | + private static ClassLoader getExtensionClassLoader(UimaContext aContext) { |
| 106 | + if (aContext instanceof UimaContextAdmin) { |
| 107 | + return getExtensionClassLoader(((UimaContextAdmin) aContext).getResourceManager()); |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + private static ClassLoader getExtensionClassLoader(ResourceManager aResMgr) { |
| 114 | + if (aResMgr == null) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + var cl = aResMgr.getExtensionClassLoader(); |
| 119 | + if (cl != null) { |
| 120 | + return cl; |
| 121 | + } |
| 122 | + |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + private static ClassLoader getDefaultClassLoader() { |
| 127 | + ClassLoader cl; |
| 128 | + try { |
| 129 | + cl = Thread.currentThread().getContextClassLoader(); |
| 130 | + if (cl != null) { |
| 131 | + return cl; |
| 132 | + } |
| 133 | + } catch (Throwable ex) { |
| 134 | + // Fall-through |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + cl = UIMAFramework.class.getClassLoader(); |
| 139 | + if (cl != null) { |
| 140 | + return cl; |
| 141 | + } |
| 142 | + } catch (Throwable ex) { |
| 143 | + // Fall-through |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + cl = ClassLoader.getSystemClassLoader(); |
| 148 | + if (cl != null) { |
| 149 | + return cl; |
| 150 | + } |
| 151 | + } catch (Throwable ex) { |
| 152 | + // Fall-through |
| 153 | + } |
| 154 | + |
| 155 | + return null; |
| 156 | + } |
| 157 | +} |
0 commit comments