Created
June 27, 2012 17:00
-
-
Save dblevins/3005397 to your computer and use it in GitHub Desktop.
JAX-RS ServiceLoader - JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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