Skip to content

Commit

Permalink
Merge branch 'release/3.6.x'
Browse files Browse the repository at this point in the history
* release/3.6.x:
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  Issue #431: Issue using SPI-enabled type systems embedded into PEARs
  • Loading branch information
reckart committed Dec 19, 2024
2 parents 1092a99 + 296b614 commit e4f6ebb
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.uima.UIMAFramework.getXMLParser;
import static org.apache.uima.fit.internal.MetaDataUtil.scanDescriptors;
import static org.apache.uima.fit.internal.ReflectionUtil.getInheritableAnnotation;
import static org.apache.uima.internal.util.ServiceLoaderUtil.loadServicesSafely;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -31,7 +32,6 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.WeakHashMap;

import org.apache.uima.fit.descriptor.FsIndex;
Expand Down Expand Up @@ -335,7 +335,7 @@ static void loadFsIndexCollectionsFromScannedLocations(List<FsIndexDescription>
static void loadFsIndexCollectionsfromSPIs(List<FsIndexDescription> fsIndexList) {
var loaded = Collections.newSetFromMap(new IdentityHashMap<>());

ServiceLoader.load(FsIndexCollectionProvider.class).forEach(provider -> {
loadServicesSafely(FsIndexCollectionProvider.class).forEach(provider -> {
for (var fsIdxCol : provider.listFsIndexCollections()) {
loaded.add(fsIdxCol);
fsIndexList.addAll(asList(fsIdxCol.getFsIndexes()));
Expand All @@ -344,7 +344,7 @@ static void loadFsIndexCollectionsfromSPIs(List<FsIndexDescription> fsIndexList)
}
});

ServiceLoader.load(TypeSystemProvider.class).forEach(provider -> {
loadServicesSafely(TypeSystemProvider.class).forEach(provider -> {
for (var fsIdxCol : provider.listFsIndexCollections()) {
if (loaded.contains(fsIdxCol)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.uima.UIMAFramework.getXMLParser;
import static org.apache.uima.fit.internal.MetaDataUtil.scanDescriptors;
import static org.apache.uima.fit.util.CasUtil.UIMA_BUILTIN_JCAS_PREFIX;
import static org.apache.uima.internal.util.ServiceLoaderUtil.loadServicesSafely;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -29,7 +30,6 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.WeakHashMap;

import org.apache.uima.fit.internal.ClassLoaderUtils;
Expand Down Expand Up @@ -170,15 +170,15 @@ static void loadTypePrioritiesFromScannedLocations(List<TypePriorities> typePrio
static void loadTypePrioritiesFromSPIs(List<TypePriorities> typePrioritiesList) {
var loaded = Collections.newSetFromMap(new IdentityHashMap<>());

ServiceLoader.load(TypePrioritiesProvider.class).forEach(provider -> {
loadServicesSafely(TypePrioritiesProvider.class).forEach(provider -> {
for (var desc : provider.listTypePriorities()) {
loaded.add(desc);
typePrioritiesList.add(desc);
LOG.debug("Loaded legacy SPI-provided type priorities at [{}]", desc.getSourceUrlString());
}
});

ServiceLoader.load(TypeSystemProvider.class).forEach(provider -> {
loadServicesSafely(TypeSystemProvider.class).forEach(provider -> {
for (var desc : provider.listTypePriorities()) {
if (loaded.contains(desc)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.apache.uima.UIMAFramework.getXMLParser;
import static org.apache.uima.fit.internal.MetaDataUtil.scanDescriptors;
import static org.apache.uima.internal.util.ServiceLoaderUtil.loadServicesSafely;
import static org.apache.uima.util.CasCreationUtils.mergeTypeSystems;

import java.io.IOException;
Expand All @@ -28,7 +29,6 @@
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.ServiceLoader;
import java.util.WeakHashMap;

import org.apache.uima.fit.internal.ClassLoaderUtils;
Expand Down Expand Up @@ -173,15 +173,15 @@ static void loadTypeSystemDescriptionsFromScannedLocations(List<TypeSystemDescri
static void loadTypeSystemDescriptionsFromSPIs(List<TypeSystemDescription> tsdList) {
var loaded = Collections.newSetFromMap(new IdentityHashMap<>());

ServiceLoader.load(TypeSystemDescriptionProvider.class).forEach(provider -> {
loadServicesSafely(TypeSystemDescriptionProvider.class).forEach(provider -> {
for (var desc : provider.listTypeSystemDescriptions()) {
loaded.add(desc);
tsdList.add(desc);
LOG.debug("Loaded legacy SPI-provided type system at [{}]", desc.getSourceUrlString());
}
});

ServiceLoader.load(TypeSystemProvider.class).forEach(provider -> {
loadServicesSafely(TypeSystemProvider.class).forEach(provider -> {
for (var desc : provider.listTypeSystemDescriptions()) {
if (loaded.contains(desc)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
package org.apache.uima.fit.validation;

import static java.util.Arrays.stream;
import static java.util.ServiceLoader.load;
import static java.util.stream.StreamSupport.stream;
import static org.apache.uima.internal.util.ServiceLoaderUtil.loadServicesSafely;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -219,7 +218,7 @@ public Validator.Builder includingByType(Class<?>... types) {
}

private Validator.Builder autoDetectChecks() {
stream(load(ValidationCheck.class).spliterator(), false).forEachOrdered(checks::add);
loadServicesSafely(ValidationCheck.class).forEachOrdered(checks::add);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static java.lang.invoke.MethodHandles.lookup;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Collections.emptyMap;
import static org.apache.uima.internal.util.ServiceLoaderUtil.loadServicesSafely;

import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
Expand All @@ -40,7 +41,6 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;

import org.apache.uima.UIMAFramework;
import org.apache.uima.UIMARuntimeException;
Expand Down Expand Up @@ -1009,14 +1009,14 @@ static Map<String, Class<? extends TOP>> loadJCasClassesFromSPI(ClassLoader cl)

var spiJCasClasses = new LinkedHashMap<String, Class<? extends TOP>>();

ServiceLoader.load(JCasClassProvider.class, cl).forEach(provider -> {
loadServicesSafely(JCasClassProvider.class, cl).forEach(provider -> {
var list = provider.listJCasClasses();
if (list != null) {
list.forEach(item -> spiJCasClasses.put(item.getName(), item));
}
});

ServiceLoader.load(TypeSystemProvider.class, cl).forEach(provider -> {
loadServicesSafely(TypeSystemProvider.class, cl).forEach(provider -> {
var list = provider.listJCasClasses();
if (list != null) {
list.forEach(item -> spiJCasClasses.put(item.getName(), item));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.uima.internal.util;

import java.lang.invoke.MethodHandles;
import java.util.Collection;
import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceLoaderUtil {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final int MAX_BROKEN_SERVICES = 16;

public static <T> Stream<T> loadServicesSafely(Class<T> aService) {
var cl = ClassLoaderUtils.findClassLoader();
return loadServicesSafely(aService, cl, null);
}

public static <T> Stream<T> loadServicesSafely(Class<T> aService, ClassLoader aClassLoader) {
return loadServicesSafely(aService, aClassLoader, null);
}

static <T> Stream<T> loadServicesSafely(Class<T> aService, ClassLoader aClassLoader,
Collection<Throwable> aErrorCollector) {
var loader = ServiceLoader.load(aService, aClassLoader);
return StreamSupport.stream(
new ServiceLoaderSpliterator<T>(aService, loader.iterator(), aErrorCollector), false);
}

private static class ServiceLoaderSpliterator<T> implements Spliterator<T> {

private final Logger log;
private final Iterator<T> serviceIterator;
private final Class<T> service;
private final Collection<Throwable> errorCollector;

public ServiceLoaderSpliterator(Class<T> aService, Iterator<T> aIterator,
Collection<Throwable> aErrorCollector) {
serviceIterator = aIterator;
service = aService;
errorCollector = aErrorCollector;
log = aErrorCollector == null ? LOG : null;
}

@Override
public boolean tryAdvance(final Consumer<? super T> aAction) {
int i = MAX_BROKEN_SERVICES;
while (i-- > 0) {
try {
if (serviceIterator.hasNext()) {
aAction.accept(serviceIterator.next());
return true;
}
} catch (ServiceConfigurationError | LinkageError e) {
handle(e);
} catch (Throwable e) {
handle(e);
throw e;
}
}
return false;
}

private void handle(Throwable e) {
if (log != null) {
log.warn("Unable to load service class for service {}", service, e);
}
if (errorCollector != null) {
errorCollector.add(e);
}
}

@Override
public Spliterator<T> trySplit() {
return null;
}

@Override
public long estimateSize() {
return Long.MAX_VALUE;
}

@Override
public int characteristics() {
return NONNULL | IMMUTABLE;
}
}
}
Loading

0 comments on commit e4f6ebb

Please sign in to comment.