View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config.util;
20  
21  import java.io.IOException;
22  import java.net.JarURLConnection;
23  import java.net.URL;
24  import java.net.URLConnection;
25  import java.util.jar.JarFile;
26  
27  /**
28   *
29   * @author Leonardo Uribe
30   */
31  public class JarUtils
32  {
33  
34      public static JarFile getJarFile(URL url) throws IOException
35      {
36          URLConnection conn = url.openConnection();
37          conn.setUseCaches(false);
38          conn.setDefaultUseCaches(false);
39  
40          JarFile jarFile;
41          if (conn instanceof JarURLConnection)
42          {
43              jarFile = ((JarURLConnection) conn).getJarFile();
44          }
45          else
46          {
47              jarFile = _getAlternativeJarFile(url);
48          }
49          return jarFile;
50      }
51      
52      /**
53       * taken from org.apache.myfaces.view.facelets.util.Classpath
54       * 
55       * For URLs to JARs that do not use JarURLConnection - allowed by the servlet spec - attempt to produce a JarFile
56       * object all the same. Known servlet engines that function like this include Weblogic and OC4J. This is not a full
57       * solution, since an unpacked WAR or EAR will not have JAR "files" as such.
58       */
59      private static JarFile _getAlternativeJarFile(URL url) throws IOException
60      {
61          String urlFile = url.getFile();
62  
63          // Trim off any suffix - which is prefixed by "!/" on Weblogic
64          int separatorIndex = urlFile.indexOf("!/");
65  
66          // OK, didn't find that. Try the less safe "!", used on OC4J
67          if (separatorIndex == -1)
68          {
69              separatorIndex = urlFile.indexOf('!');
70          }
71  
72          if (separatorIndex != -1)
73          {
74              String jarFileUrl = urlFile.substring(0, separatorIndex);
75              // And trim off any "file:" prefix.
76              if (jarFileUrl.startsWith("file:"))
77              {
78                  jarFileUrl = jarFileUrl.substring("file:".length());
79              }
80  
81              return new JarFile(jarFileUrl);
82          }
83  
84          return null;
85      }
86  
87  }