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.trinidad.util;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import java.net.JarURLConnection;
25 import java.net.URL;
26 import java.net.URLConnection;
27
28 public final class URLUtils
29 {
30 private URLUtils()
31 {
32 }
33
34 public static long getLastModified(URL url) throws IOException
35 {
36 if ("file".equals(url.getProtocol()))
37 {
38 String externalForm = url.toExternalForm();
39 // Remove the "file:"
40 File file = new File(externalForm.substring(5));
41
42 return file.lastModified();
43 }
44 else
45 {
46 return getLastModified(url.openConnection());
47 }
48 }
49
50 public static long getLastModified(URLConnection connection) throws IOException
51 {
52 long modified;
53 if (connection instanceof JarURLConnection)
54 {
55 // The following hack is required to work-around a JDK bug.
56 // getLastModified() on a JAR entry URL delegates to the actual JAR file
57 // rather than the JAR entry.
58 // This opens internally, and does not close, an input stream to the JAR
59 // file.
60 // In turn, you cannot close it by yourself, because it's internal.
61 // The work-around is to get the modification date of the JAR file
62 // manually,
63 // and then close that connection again.
64
65 URL jarFileUrl = ((JarURLConnection) connection).getJarFileURL();
66 URLConnection jarFileConnection = jarFileUrl.openConnection();
67
68 try
69 {
70 modified = jarFileConnection.getLastModified();
71 }
72 finally
73 {
74 try
75 {
76 jarFileConnection.getInputStream().close();
77 }
78 catch (Exception exception)
79 {
80 // Ignored
81 }
82 }
83 }
84 else
85 {
86 modified = connection.getLastModified();
87 }
88
89 return modified;
90 }
91 }