/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
*/ | |
package org.apache.myfaces.config.util; | |
import java.io.IOException; | |
import java.net.JarURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.jar.JarFile; | |
/** | |
* | |
* @author Leonardo Uribe | |
*/ | |
public class JarUtils | |
{ | |
public static JarFile getJarFile(URL url) throws IOException | |
{ | |
URLConnection conn = url.openConnection(); | |
conn.setUseCaches(false); | |
conn.setDefaultUseCaches(false); | |
JarFile jarFile; | |
if (conn instanceof JarURLConnection) | |
{ | |
jarFile = ((JarURLConnection) conn).getJarFile(); | |
} | |
else | |
{ | |
jarFile = _getAlternativeJarFile(url); | |
} | |
return jarFile; | |
} | |
/** | |
* taken from org.apache.myfaces.view.facelets.util.Classpath | |
* | |
* For URLs to JARs that do not use JarURLConnection - allowed by the servlet spec - attempt to produce a JarFile | |
* object all the same. Known servlet engines that function like this include Weblogic and OC4J. This is not a full | |
* solution, since an unpacked WAR or EAR will not have JAR "files" as such. | |
*/ | |
private static JarFile _getAlternativeJarFile(URL url) throws IOException | |
{ | |
String urlFile = url.getFile(); | |
// Trim off any suffix - which is prefixed by "!/" on Weblogic | |
int separatorIndex = urlFile.indexOf("!/"); | |
// OK, didn't find that. Try the less safe "!", used on OC4J | |
if (separatorIndex == -1) | |
{ | |
separatorIndex = urlFile.indexOf('!'); | |
} | |
if (separatorIndex != -1) | |
{ | |
String jarFileUrl = urlFile.substring(0, separatorIndex); | |
// And trim off any "file:" prefix. | |
if (jarFileUrl.startsWith("file:")) | |
{ | |
jarFileUrl = jarFileUrl.substring("file:".length()); | |
} | |
return new JarFile(jarFileUrl); | |
} | |
return null; | |
} | |
} |