Skip to content

Instantly share code, notes, and snippets.

@dblevins
Created June 27, 2012 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblevins/3005397 to your computer and use it in GitHub Desktop.
Save dblevins/3005397 to your computer and use it in GitHub Desktop.
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