in src/main/java/org/apache/neethi/util/Service.java [70:147]
public static synchronized <T> List<? extends T> providers(Class<T> cls) {
String serviceFile = "META-INF/services/" + cls.getName();
List<T> l = cast(instanceMap.get(serviceFile));
if (l != null) {
return l;
}
l = new ArrayList<T>();
instanceMap.put(serviceFile, l);
ClassLoader cl = null;
try {
cl = cls.getClassLoader();
} catch (SecurityException se) {
// Ooops! can't get his class loader.
}
// Can always request your own class loader. But it might be 'null'.
if (cl == null) {
cl = Service.class.getClassLoader();
}
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
// No class loader so we can't find 'serviceFile'.
if (cl == null) {
return l;
}
Enumeration<URL> e;
try {
e = cl.getResources(serviceFile);
} catch (IOException ioe) {
return l;
}
while (e.hasMoreElements()) {
URL u = e.nextElement();
try (Reader r = new InputStreamReader(u.openStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(r)) {
String line = br.readLine();
while (line != null) {
try {
// First strip any comment...
int idx = line.indexOf('#');
if (idx != -1) {
line = line.substring(0, idx);
}
// Trim whitespace.
line = line.trim();
// If nothing left then loop around...
if (line.length() == 0) {
line = br.readLine();
continue;
}
// Try and load the class
Object obj = cl.loadClass(line).newInstance();
// stick it into our vector...
l.add(cls.cast(obj));
} catch (Exception ex) {
// Just try the next line
}
line = br.readLine();
}
} catch (Exception ex) {
// Just try the next file...
} catch (LinkageError le) {
// Just try the next file...
}
}
return l;
}