Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JAX-RS ServiceLoader - JS
@ApplicationPath("api")
public class PerformaApiApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final ClassLoader loader = this.getClass().getClassLoader();
final Set<Class<?>> services = new HashSet<Class<?>>();
for (String className : readClasses()) {
try {
final Class<?> clazz = loader.loadClass(className);
services.add(clazz);
} catch (Throwable t) {
throw new RuntimeException("Could not load: "+className, t);
}
}
return services;
}
private static Set<String> readClasses() {
final Set<String> classNames = new HashSet<String>();
try {
byte[] data = IOUtils.toByteArray(PerformaApiApplication.class.getResourceAsStream("/a.bin"));
data = ZlibUtils.inflate(data);
String str = new String(data);
BufferedReader br = new BufferedReader(new StringReader(str));
String line = null;
while ((line = br.readLine()) != null) {
if (line.trim().length() > 0) {
String[] keyval = line.split("=");
if (keyval.length == 2) {
String route = keyval[0];
String clazz = keyval[1];
classNames.add(clazz);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return classNames;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment