Skip to content

Instantly share code, notes, and snippets.

@nchapon
Created January 19, 2011 13:09
Show Gist options
  • Save nchapon/786143 to your computer and use it in GitHub Desktop.
Save nchapon/786143 to your computer and use it in GitHub Desktop.
Voodoo Code
public static URL getJarURLFromURLEntry(URL url, String entry) throws IllegalArgumentException {
URL jarUrl;
String file = url.getFile();
if ( ! entry.startsWith( "/" ) ) entry = "/" + entry;
file = file.substring( 0, file.length() - entry.length() );
if ( file.endsWith( "!" ) ) file = file.substring( 0, file.length() - 1 );
try {
String protocol = url.getProtocol();
if ( "jar".equals( protocol )
|| "wsjar".equals( protocol ) ) { //Websphere has it's own way
//Original URL is like jar:protocol
jarUrl = new URL( file );
if ( "file".equals( jarUrl.getProtocol() ) ) {
//not escaped, need to voodoo
if ( file.indexOf( ' ' ) != -1 ) {
//not escaped, need to voodoo
jarUrl = new File( jarUrl.getFile() ).toURI().toURL(); //goes by toURI to escape the path
}
} //otherwise left as is
}
else if ( "zip".equals( protocol ) //Weblogic has it's own way
|| "code-source".equals( url.getProtocol() ) //OC4J prevent ejb.jar access (ie everything without path)
|| "file".equals( protocol ) //if no wrapping is done
) {
//we have extracted the zip file, so it should be read as a file
if ( file.indexOf( ' ' ) != -1 ) {
//not escaped, need to voodoo
jarUrl = new File(file).toURI().toURL(); //goes by toURI to escape the path
}
else {
jarUrl = new File(file).toURL();
}
}
else {
jarUrl = new URL( protocol, url.getHost(), url.getPort(), file );
}
}
catch (MalformedURLException e) {
throw new IllegalArgumentException(
"Unable to determine JAR Url from " + url + ". Cause: " + e.getMessage()
);
}
log.trace("JAR URL from URL Entry: {} >> {}", url, jarUrl);
return jarUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment