use of java.util.ServiceLoader in project jdk8u_jdk by JetBrains.
the class Charset method providers.
// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown. Should be invoked with full privileges.
//
private static Iterator<CharsetProvider> providers() {
return new Iterator<CharsetProvider>() {
ClassLoader cl = ClassLoader.getSystemClassLoader();
ServiceLoader<CharsetProvider> sl = ServiceLoader.load(CharsetProvider.class, cl);
Iterator<CharsetProvider> i = sl.iterator();
CharsetProvider next = null;
private boolean getNext() {
while (next == null) {
try {
if (!i.hasNext())
return false;
next = i.next();
} catch (ServiceConfigurationError sce) {
if (sce.getCause() instanceof SecurityException) {
// Ignore security exceptions
continue;
}
throw sce;
}
}
return true;
}
public boolean hasNext() {
return getNext();
}
public CharsetProvider next() {
if (!getNext())
throw new NoSuchElementException();
CharsetProvider n = next;
next = null;
return n;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
use of java.util.ServiceLoader in project JMRI by JMRI.
the class StartupActionModelUtil method prepareActionsHashMap.
private void prepareActionsHashMap() {
if (this.actions == null) {
this.actions = new HashMap<>();
this.overrides = new HashMap<>();
ResourceBundle rb = ResourceBundle.getBundle("apps.ActionListBundle");
rb.keySet().stream().filter((key) -> (!key.isEmpty())).forEach((key) -> {
try {
Class<?> clazz = Class.forName(key);
ActionAttributes attrs = new ActionAttributes(rb.getString(key), clazz);
this.actions.put(clazz, attrs);
} catch (ClassNotFoundException ex) {
log.error("Did not find class \"{}\"", key);
}
});
ServiceLoader<StartupActionFactory> loader = ServiceLoader.load(StartupActionFactory.class);
loader.forEach(factory -> {
for (Class<?> clazz : factory.getActionClasses()) {
ActionAttributes attrs = new ActionAttributes(factory.getTitle(clazz), clazz);
this.actions.put(clazz, attrs);
for (String overridden : factory.getOverriddenClasses(clazz)) {
this.overrides.put(overridden, clazz);
}
}
});
// allow factories to be garbage collected
loader.reload();
}
}
Aggregations