Skip to content

Commit 68561e3

Browse files
authored
Merge pull request #433 from apache/refactoring/430-Resolving-type-system-imports-through-SPIs-slows-things-down-too-much
Issue #430: Resolving type system imports through sp is slows things down too much
2 parents c7064c3 + b43e72e commit 68561e3

File tree

7 files changed

+101
-68
lines changed

7 files changed

+101
-68
lines changed

.asf.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
# limitations under the License.
1616
#
1717

18+
notifications:
19+
20+
21+
pullrequests: [email protected]
22+
23+
jira_options: link label
1824
github:
1925
description: "Apache UIMA Java SDK"
2026
homepage: https://uima.apache.org
2127
dependabot_alerts: true
2228
dependabot_updates: false
23-
notifications:
24-
25-
26-
pullrequests: [email protected]
27-
28-
jira_options: link label
2929
labels:
3030
- apache
3131
- uima

uima-bnd-plugin/src/main/java/org/apache/uima/tools/bnd/UimaBndPlugin.java

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.List;
27+
import java.util.Map;
2728
import java.util.regex.Pattern;
2829

2930
import org.apache.uima.UIMAFramework;
@@ -44,12 +45,14 @@
4445
import aQute.bnd.osgi.Analyzer;
4546
import aQute.bnd.osgi.Resource;
4647
import aQute.bnd.service.AnalyzerPlugin;
48+
import aQute.bnd.service.Plugin;
49+
import aQute.lib.converter.Converter;
50+
import aQute.service.reporter.Reporter;
4751

48-
@BndPlugin(name = "UIMA")
52+
@BndPlugin(name = "UIMA", parameters = UimaBndPlugin.Configuration.class)
4953
public class UimaBndPlugin
50-
implements AnalyzerPlugin
54+
implements AnalyzerPlugin, Plugin
5155
{
52-
5356
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
5457

5558
private static final Pattern XML_FILE = Pattern.compile(".*\\.xml");
@@ -58,6 +61,21 @@ public class UimaBndPlugin
5861

5962
private final static XMLParser PARSER = UIMAFramework.getXMLParser();
6063

64+
private Reporter reporter;
65+
private UimaBndPlugin.Configuration configuration;
66+
67+
@Override
68+
public void setProperties(Map<String, String> aMap) throws Exception
69+
{
70+
configuration = Converter.cnv(UimaBndPlugin.Configuration.class, aMap);
71+
}
72+
73+
@Override
74+
public void setReporter(Reporter aReporter)
75+
{
76+
reporter = aReporter;
77+
}
78+
6179
@Override
6280
public boolean analyzeJar(Analyzer analyzer) throws Exception
6381
{
@@ -69,37 +87,37 @@ public boolean analyzeJar(Analyzer analyzer) throws Exception
6987
for (var entry : resources.entrySet()) {
7088
var path = entry.getKey();
7189
var resource = entry.getValue();
72-
90+
7391
try {
7492
if (XML_FILE.matcher(path).matches()) {
75-
importsProcessed += analyzeXmlFile(analyzer, path, resource);
93+
importsProcessed += analyzeXmlFile(analyzer, path, resource, 0);
7694
}
7795
}
7896
catch (Exception e) {
7997
analyzer.error("Unexpected exception in processing resource (%s): %s", path, e);
8098
}
8199
}
82100
}
83-
101+
84102
LOG.info("UIMA bnd plugin processed {} imports", importsProcessed);
85103

86104
return false;
87105
}
88106

89-
private int analyzeXmlFile(Analyzer analyzer, String path, Resource resource) throws Exception
107+
private int analyzeXmlFile(Analyzer analyzer, String path, Resource resource, int level) throws Exception
90108
{
91109
var desc = readUimaDescriptor(resource);
92110
if (desc == null) {
93111
return 0;
94112
}
95113

96-
LOG.debug("Found {}: {}", desc.getClass().getSimpleName(), path);
114+
LOG.debug("{}Found {}: {}", repeat(" ", level), desc.getClass().getSimpleName(), path);
97115
var imports = getImportsFromDescriptor(desc);
98116

99117
var importsProcessed = 0;
100118
for (var imp : imports) {
101119
if (imp.getName() != null) {
102-
handleImportByName(analyzer, path, imp);
120+
handleImportByName(analyzer, path, imp, level);
103121
importsProcessed++;
104122
continue;
105123
}
@@ -108,22 +126,21 @@ private int analyzeXmlFile(Analyzer analyzer, String path, Resource resource) th
108126
handleImportByLocation(imp);
109127
continue;
110128
}
111-
129+
112130
LOG.warn(
113-
"Found UIMA type system import without name and location - ignoring, please fix your type system description");
131+
"Found UIMA import without name and location - ignoring, please fix your type system description");
114132
}
115-
133+
116134
return importsProcessed;
117135
}
118136

119137
private void handleImportByLocation(Import imp)
120138
{
121-
LOG.warn(
122-
"Found UIMA type system import by location: {} - ignoring, please only use import-by-name",
139+
LOG.warn("Ignoring UIMA import by location (please only use import-by-name): {}",
123140
imp.getLocation());
124141
}
125142

126-
private void handleImportByName(Analyzer analyzer, String path, Import imp)
143+
private void handleImportByName(Analyzer analyzer, String path, Import imp, int level) throws Exception
127144
{
128145
var tsdPackage = imp.getName();
129146
int lastSeparatorPosition = tsdPackage.lastIndexOf('.');
@@ -132,18 +149,29 @@ private void handleImportByName(Analyzer analyzer, String path, Import imp)
132149
tsdPackage = tsdPackage.substring(0, lastSeparatorPosition);
133150
}
134151

135-
LOG.debug("Found UIMA type system import by name: {}", tsdPackage);
136-
137152
var pack = analyzer.getPackageRef(tsdPackage);
138153
if (!QN.matcher(pack.getFQN()).matches()) {
139-
analyzer.warning("Type system import does not seem to refer to a package (%s): %s",
140-
path, pack);
154+
analyzer.warning("Import does not seem to refer to a package (%s): %s", path, pack);
141155
}
142156

143-
if (!analyzer.getReferred().containsKey(pack)) {
157+
var alreadyKnownImport = analyzer.getReferred().containsKey(pack);
158+
if (!alreadyKnownImport) {
144159
var attrs = new Attrs();
145160
analyzer.getReferred().put(pack, attrs);
146161
}
162+
163+
LOG.debug("{}Found UIMA import by name: {} {}", repeat(" ", level), tsdPackage, alreadyKnownImport ? "" : "(new)");
164+
165+
var importedResourcePath = imp.getName().replace('.', '/') + ".xml";
166+
var importedResource = analyzer.findResource(importedResourcePath);
167+
if (importedResource == null) {
168+
analyzer.warning("Imported resource not found on classpath: {}", importedResourcePath);
169+
return;
170+
}
171+
172+
if (configuration == null || configuration.transitive(false)) {
173+
analyzeXmlFile(analyzer, importedResourcePath, importedResource, level + 1);
174+
}
147175
}
148176

149177
private List<Import> getImportsFromDescriptor(XMLizable desc)
@@ -194,4 +222,22 @@ private static <T> List<T> asList(T[] aList)
194222

195223
return Arrays.asList(aList);
196224
}
225+
226+
private static String repeat(String aString, int aCount) {
227+
if (aCount == 0) {
228+
return "";
229+
}
230+
231+
var buf = new StringBuilder();
232+
for (var i = 0; i < aCount; i++) {
233+
buf.append(aString);
234+
}
235+
236+
return buf.toString();
237+
}
238+
239+
public static interface Configuration
240+
{
241+
boolean transitive(boolean aDefault);
242+
}
197243
}

uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void execute() throws MojoExecutionException {
149149
componentLoader = Util.getClassloader(project, includeScope);
150150

151151
// List of components that is later written to META-INF/org.apache.uima.fit/components.txt
152-
StringBuilder componentsManifest = new StringBuilder();
152+
var componentsManifest = new StringBuilder();
153153

154154
int countGenerated = 0;
155155
for (String file : files) {

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

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ private ClassLoaderUtils() {
3838
* <ol>
3939
* <li>The {@link UimaContext} in the {@link UimaContextHolder} of the current thread(if any)</li>
4040
* <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>
41+
* <li>The class loader through which UIMA was loaded.</li>
42+
* <li>Finally checks the system classloader</li>
4343
* </ol>
4444
*
4545
* @return a class loader or {@code null} if no suitable class loader could be found.
@@ -60,7 +60,27 @@ public static ClassLoader findClassLoader() {
6060
return uimaFITClassLoader;
6161
}
6262

63-
return getDefaultClassLoader();
63+
ClassLoader cl;
64+
65+
try {
66+
cl = UIMAFramework.class.getClassLoader();
67+
if (cl != null) {
68+
return cl;
69+
}
70+
} catch (Throwable ex) {
71+
// Fall-through
72+
}
73+
74+
try {
75+
cl = ClassLoader.getSystemClassLoader();
76+
if (cl != null) {
77+
return cl;
78+
}
79+
} catch (Throwable ex) {
80+
// Fall-through
81+
}
82+
83+
return null;
6484
}
6585

6686
/**
@@ -122,36 +142,4 @@ private static ClassLoader getExtensionClassLoader(ResourceManager aResMgr) {
122142

123143
return null;
124144
}
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-
}
157145
}

uimaj-core/src/main/java/org/apache/uima/resource/RelativePathResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public interface RelativePathResolver {
129129
* @return the absolute URL at which the file exists, <code>null</code> it none could be found.
130130
* @deprecated Use {@link #resolveRelativePath(String)} instead.
131131
*/
132-
@Deprecated
132+
@Deprecated(since = "3.6.0")
133133
URL resolveRelativePath(URL aUrl);
134134

135135
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.List;
3838
import java.util.StringTokenizer;
3939

40+
import org.apache.uima.UIMAFramework;
4041
import org.apache.uima.resource.RelativePathResolver;
4142

4243
/**
@@ -59,6 +60,8 @@ public RelativePathResolver_impl() {
5960
}
6061

6162
public RelativePathResolver_impl(ClassLoader aClassLoader) {
63+
mClassLoader = aClassLoader;
64+
6265
// initialize data path based on uima.datapath System property; if not
6366
// present fall back on user.dir
6467
String dataPath = null;
@@ -88,8 +91,6 @@ public RelativePathResolver_impl(ClassLoader aClassLoader) {
8891
mDataPath = null;
8992
mBaseUrls = null;
9093
}
91-
92-
mClassLoader = aClassLoader;
9394
}
9495

9596
@Override
@@ -376,9 +377,6 @@ public URL resolveRelativePath(URL aUrl) {
376377
return absURL;
377378
}
378379

379-
/**
380-
* @see org.apache.uima.resource.RelativePathResolver#setPathResolverClassLoader(java.lang.ClassLoader)
381-
*/
382380
@Override
383381
public void setPathResolverClassLoader(ClassLoader aClassLoader) {
384382
mClassLoader = aClassLoader;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public ResourceRegistration(Object aResource, ExternalResourceDescription aDescr
226226
* Cache of imported descriptor URLs from which the parsed objects in importCache were created, so
227227
* that these URLs are not re-parsed if the same URL is imported again.
228228
*/
229+
@Deprecated(since = "3.3.0")
229230
private Map<String, Set<String>> importUrlsCache = Collections.synchronizedMap(new HashMap<>());
230231

231232
/**

0 commit comments

Comments
 (0)