54
54
import org .apache .uima .cas .CAS ;
55
55
import org .apache .uima .cas .impl .CASImpl ;
56
56
import org .apache .uima .internal .util .Class_TCCL ;
57
+ import org .apache .uima .internal .util .Misc ;
57
58
import org .apache .uima .internal .util .UIMAClassLoader ;
58
59
import org .apache .uima .it .pear_with_typesystem .spi .JCasClassProviderForTesting ;
59
60
import org .apache .uima .it .pear_with_typesystem .type .ComplexAnnotation ;
@@ -107,8 +108,8 @@ void setup() throws Exception
107
108
}
108
109
109
110
/**
110
- * PEAR use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local
111
- * classpath and available at compile time.
111
+ * PEAR use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local classpath
112
+ * and available at compile time.
112
113
*/
113
114
@ Test
114
115
void testScenario1 (@ TempDir File aTemp ) throws Exception
@@ -131,6 +132,8 @@ void testScenario1(@TempDir File aTemp) throws Exception
131
132
.hasSize (1 ) //
132
133
.allSatisfy (complexAnnotation -> assertThat (complexAnnotation )
133
134
.isInstanceOf (ComplexAnnotation .class ));
135
+
136
+ pearAnnotator .getUimaContextAdmin ().getResourceManager ().destroy ();
134
137
}
135
138
136
139
/**
@@ -161,14 +164,16 @@ void testScenario2(@TempDir File aTemp) throws Exception
161
164
.hasSize (1 ) //
162
165
.allSatisfy (complexAnnotation -> assertThat (complexAnnotation )
163
166
.isInstanceOf (ComplexAnnotation .class ));
167
+
168
+ pearAnnotator .getUimaContextAdmin ().getResourceManager ().destroy ();
164
169
}
165
170
166
171
/**
167
172
* PEAR can use {@link ComplexAnnotation} directly - we assume it is in the PEAR's local
168
173
* classpath and available at compile time.
169
174
* <p>
170
- * However, PEAR does not know about {@link ComplexAnnotationSubtype}. Yet, it will find
171
- * that the CAS contains an annotation of that type.
175
+ * However, PEAR does not know about {@link ComplexAnnotationSubtype}. Yet, it will find that
176
+ * the CAS contains an annotation of that type.
172
177
*/
173
178
@ Test
174
179
void testScenario3 (@ TempDir File aTemp ) throws Exception
@@ -190,6 +195,8 @@ void testScenario3(@TempDir File aTemp) throws Exception
190
195
assertThatExceptionOfType (AnalysisEngineProcessException .class ) //
191
196
.isThrownBy (() -> pearAnnotator .process (cas )) //
192
197
.withRootCauseInstanceOf (ClassCastException .class );
198
+
199
+ pearAnnotator .getUimaContextAdmin ().getResourceManager ().destroy ();
193
200
}
194
201
195
202
private AnalysisEngine installPear (File aTemp ,
@@ -245,15 +252,13 @@ public IsolatingResourceManager_impl(Map<String, Object> aResourceMap,
245
252
aParameterizedResourceInstanceMap );
246
253
}
247
254
248
- @ SuppressWarnings ("resource" )
249
255
@ Override
250
256
public synchronized void setExtensionClassPath (String aClasspath , boolean aResolveResource )
251
257
throws MalformedURLException
252
258
{
253
259
var parentCL = Class_TCCL .get_parent_cl ();
254
- var uimaCL = new UIMAClassLoader (aClasspath , Class_TCCL .get_parent_cl ());
255
- uimaCL = clConfigurer .apply (parentCL ,
256
- new IsolatingUIMAClassloader ("PEAR Classloader" , uimaCL ));
260
+ var uimaCL = clConfigurer .apply (parentCL , new IsolatingUIMAClassloader (
261
+ "PEAR Classloader" , parentCL , Misc .classpath2urls (aClasspath )));
257
262
setExtensionClassLoader (uimaCL , aResolveResource );
258
263
}
259
264
@@ -333,6 +338,16 @@ public IsolatingUIMAClassloader(String aName, ClassLoader aParent, URL... aClass
333
338
id = aName ;
334
339
}
335
340
341
+ @ Override
342
+ public void close () throws IOException
343
+ {
344
+ if (getParent () instanceof UIMAClassLoader uimaClassLoader ) {
345
+ uimaClassLoader .close ();
346
+ }
347
+
348
+ super .close ();
349
+ }
350
+
336
351
@ SafeVarargs
337
352
public final <T > IsolatingUIMAClassloader delegatingSPI (ClassLoader aDelegate ,
338
353
Class <T > aServiceInterface , Class <? extends T >... aServiceProviders )
@@ -513,26 +528,32 @@ protected Class<?> loadClass(String aName, boolean aResolve) throws ClassNotFoun
513
528
LOG .debug ("[{}] redefining class: {}" , id , aName );
514
529
515
530
String internalName = aName .replace ("." , "/" ) + ".class" ;
516
- InputStream is = getParent ().getResourceAsStream (internalName );
517
- if (is == null ) {
531
+ URL url = getParent ().getResource (internalName );
532
+ if (url == null ) {
518
533
throw new ClassNotFoundException (aName );
519
534
}
520
535
521
536
try {
522
- var buffer = new ByteArrayOutputStream ();
523
- is .transferTo (buffer );
524
- byte [] bytes = buffer .toByteArray ();
525
- Class <?> cls = defineClass (aName , bytes , 0 , bytes .length );
526
- if (cls .getPackage () == null ) {
527
- int packageSeparator = aName .lastIndexOf ('.' );
528
- if (packageSeparator != -1 ) {
529
- String packageName = aName .substring (0 , packageSeparator );
530
- definePackage (packageName , null , null , null , null , null , null ,
531
- null );
537
+ // Disable JAR cache so JUnit can delete the temporary folder after the test
538
+ var urlConnection = url .openConnection ();
539
+ urlConnection .setDefaultUseCaches (false );
540
+
541
+ try (InputStream is = urlConnection .getInputStream ()) {
542
+ var buffer = new ByteArrayOutputStream ();
543
+ is .transferTo (buffer );
544
+ byte [] bytes = buffer .toByteArray ();
545
+ Class <?> cls = defineClass (aName , bytes , 0 , bytes .length );
546
+ if (cls .getPackage () == null ) {
547
+ int packageSeparator = aName .lastIndexOf ('.' );
548
+ if (packageSeparator != -1 ) {
549
+ String packageName = aName .substring (0 , packageSeparator );
550
+ definePackage (packageName , null , null , null , null , null , null ,
551
+ null );
552
+ }
532
553
}
554
+ loadedClasses .put (aName , cls );
555
+ return cls ;
533
556
}
534
- loadedClasses .put (aName , cls );
535
- return cls ;
536
557
}
537
558
catch (IOException ex ) {
538
559
throw new ClassNotFoundException (
0 commit comments