Skip to content

Commit e333ce0

Browse files
authored
Merge pull request #401 from apache/bugfix/385-Allow-import-of-type-systems-published-through-SPI
Issue #385: Allow import of type systems published through SPI
2 parents 9d5b6e4 + 18dbf96 commit e333ce0

File tree

4 files changed

+169
-54
lines changed

4 files changed

+169
-54
lines changed

uimafit-core/src/main/java/org/apache/uima/fit/internal/ClassLoaderUtils.java

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
package org.apache.uima.fit.internal;
2020

2121
import org.apache.uima.UimaContext;
22-
import org.apache.uima.UimaContextAdmin;
2322
import org.apache.uima.UimaContextHolder;
2423
import org.apache.uima.resource.ResourceManager;
2524
import org.springframework.util.ClassUtils;
2625

2726
/**
2827
* INTERNAL API - Helper functions to obtain a suitable classloader.
28+
*
29+
* @deprecated Use {@link org.apache.uima.internal.util.ClassLoaderUtils} instead.
30+
* @forRemoval 4.0.0
2931
*/
32+
@Deprecated(since = "3.6.0")
3033
public final class ClassLoaderUtils {
3134
private ClassLoaderUtils() {
3235
// No instances
@@ -45,23 +48,7 @@ private ClassLoaderUtils() {
4548
* @return a classloader or {@code null} if no suitable classloader could be found.
4649
*/
4750
public static ClassLoader findClassloader() {
48-
ClassLoader uimaThreadContextClassLoader = getExtensionClassloader(
49-
UimaContextHolder.getContext());
50-
if (uimaThreadContextClassLoader != null) {
51-
return uimaThreadContextClassLoader;
52-
}
53-
54-
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
55-
if (contextClassLoader != null) {
56-
return contextClassLoader;
57-
}
58-
59-
ClassLoader uimaFITClassLoader = ClassLoaderUtils.class.getClassLoader();
60-
if (uimaFITClassLoader != null) {
61-
return uimaFITClassLoader;
62-
}
63-
64-
return ClassUtils.getDefaultClassLoader();
51+
return org.apache.uima.internal.util.ClassLoaderUtils.findClassLoader();
6552
}
6653

6754
/**
@@ -74,12 +61,7 @@ public static ClassLoader findClassloader() {
7461
* @return a classloader or {@code null} if no suitable classloader could be found.
7562
*/
7663
public static ClassLoader findClassloader(ResourceManager aResMgr) {
77-
ClassLoader resourceManagerExtensionClassloader = getExtensionClassloader(aResMgr);
78-
if (resourceManagerExtensionClassloader != null) {
79-
return resourceManagerExtensionClassloader;
80-
}
81-
82-
return findClassloader();
64+
return org.apache.uima.internal.util.ClassLoaderUtils.findClassLoader(aResMgr);
8365
}
8466

8567
/**
@@ -93,32 +75,6 @@ public static ClassLoader findClassloader(ResourceManager aResMgr) {
9375
* @return a classloader or {@code null} if no suitable classloader could be found.
9476
*/
9577
public static ClassLoader findClassloader(UimaContext aContext) {
96-
ClassLoader uimaContextExtensionClassloader = getExtensionClassloader(aContext);
97-
if (uimaContextExtensionClassloader != null) {
98-
return uimaContextExtensionClassloader;
99-
}
100-
101-
return findClassloader((ResourceManager) null);
102-
}
103-
104-
private static ClassLoader getExtensionClassloader(UimaContext aContext) {
105-
if (aContext instanceof UimaContextAdmin) {
106-
return getExtensionClassloader(((UimaContextAdmin) aContext).getResourceManager());
107-
}
108-
109-
return null;
110-
}
111-
112-
private static ClassLoader getExtensionClassloader(ResourceManager aResMgr) {
113-
if (aResMgr == null) {
114-
return null;
115-
}
116-
117-
ClassLoader cl = aResMgr.getExtensionClassLoader();
118-
if (cl != null) {
119-
return aResMgr.getExtensionClassLoader();
120-
}
121-
122-
return null;
78+
return org.apache.uima.internal.util.ClassLoaderUtils.findClassLoader(aContext);
12379
}
12480
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
}

uimaj-core/src/main/java/org/apache/uima/internal/util/Class_TCCL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//@formatter:off
3232
/**
3333
* Utilities supporting a unified approach to loading classes,
34-
* incorporating the resource manager's classloader if available,
34+
* incorporating the resource manager's class loader if available,
3535
* and making use of the Thread Context Class Loader (TCCL)
3636
*
3737
* For backwards compatibility, if a class is not found using the
@@ -108,7 +108,7 @@ public static <T> Class<T> forName(String className, Map<String, Object> additio
108108

109109
/**
110110
* @deprecated Method should not be used because if an extension classloader is set, the thread
111-
* context classloader. It will be ignored and will be removed in a future version.
111+
* context classloader will be ignored. It will be removed in a future version.
112112
* @forRemoval 4.0.0
113113
*/
114114
@Deprecated(since = "3.5.0")

uimaj-core/src/main/java/org/apache/uima/resource/metadata/impl/Import_impl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ServiceLoader;
2525

2626
import org.apache.uima.UIMAFramework;
27+
import org.apache.uima.internal.util.ClassLoaderUtils;
2728
import org.apache.uima.resource.ResourceManager;
2829
import org.apache.uima.resource.metadata.Import;
2930
import org.apache.uima.spi.TypeSystemProvider;
@@ -130,7 +131,8 @@ private URL findResouceUrlByName(ResourceManager aResourceManager, String name)
130131

131132
// If that fails, try loading through the SPIs
132133
if (url == null) {
133-
var providers = ServiceLoader.load(TypeSystemProvider.class);
134+
var cl = ClassLoaderUtils.findClassLoader(aResourceManager);
135+
var providers = ServiceLoader.load(TypeSystemProvider.class, cl);
134136
for (var provider : providers) {
135137
var maybeTypeSystemUrl = provider.findResourceUrl(name);
136138
if (maybeTypeSystemUrl.isPresent()) {

0 commit comments

Comments
 (0)