Skip to content

Instantly share code, notes, and snippets.

@mojavelinux
Created April 2, 2010 19:42
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 mojavelinux/353601 to your computer and use it in GitHub Desktop.
Save mojavelinux/353601 to your computer and use it in GitHub Desktop.
ClassLoader cl = new SecureClassLoader() {
@Override
protected URL findResource(final String name) {
final Asset a = archive.get(name);
if (a == null) {
return null;
}
try {
return new URL(null, "archive:/" + name, new URLStreamHandler() {
@Override
protected java.net.URLConnection openConnection(URL u) throws java.io.IOException {
return new URLConnection(u) {
@Override
public void connect() throws IOException {
}
@Override
public InputStream getInputStream()
throws IOException {
return a.openStream();
}
};
}
;
});
} catch (Exception e) {
return null;
}
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
Iterator<URL> it = new ArrayList<URL>(0).iterator();
URL resource = findResource(name);
if (resource != null) {
it = Arrays.asList(resource).iterator();
}
final Iterator<URL> i = it;
return new Enumeration<URL>() {
@Override
public boolean hasMoreElements() {
return i.hasNext();
}
@Override
public URL nextElement() {
return i.next();
}
};
}
};
Thread.currentThread().setContextClassLoader(cl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment