01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ModificationTimeClasspath.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.resources;
09:
10: import com.uwyn.rife.resources.exceptions.CantFindResourceJarEntryException;
11: import com.uwyn.rife.resources.exceptions.CouldntAccessResourceFileException;
12: import com.uwyn.rife.resources.exceptions.CouldntAccessResourceJarException;
13: import com.uwyn.rife.resources.exceptions.ResourceFinderErrorException;
14: import com.uwyn.rife.resources.exceptions.UnsupportedResourceProtocolException;
15: import com.uwyn.rife.tools.StringUtils;
16: import java.io.File;
17: import java.io.IOException;
18: import java.net.URL;
19: import java.net.URLDecoder;
20: import java.util.jar.JarEntry;
21: import java.util.jar.JarFile;
22:
23: public class ModificationTimeClasspath {
24: public static long getModificationTime(URL resource)
25: throws ResourceFinderErrorException {
26: if (null == resource) {
27: return -1;
28: }
29:
30: long modification_time = -1;
31:
32: String resource_protocol = resource.getProtocol();
33: String resource_filename = URLDecoder
34: .decode(resource.getFile());
35:
36: // handle Jetty's custom tx protocol
37: if (resource_protocol.equals("tx")) {
38: resource_protocol = "file";
39: resource_filename = StringUtils.stripFromFront(
40: resource_filename, "file:");
41: }
42:
43: if (resource_protocol.equals("jar")) {
44: String prefix = "file:";
45: String jar_filename = resource_filename.substring(prefix
46: .length(), resource_filename.indexOf('!'));
47: String jar_entryname = resource_filename
48: .substring(resource_filename.indexOf('!') + 2);
49: File jar_regularfile = new File(jar_filename);
50: if (jar_regularfile.exists() && jar_regularfile.canRead()) {
51: try {
52: JarFile jar_file = new JarFile(jar_regularfile);
53: JarEntry jar_entry = jar_file
54: .getJarEntry(jar_entryname);
55: if (null != jar_entry) {
56: modification_time = jar_entry.getTime();
57: } else {
58: throw new CantFindResourceJarEntryException(
59: jar_filename, jar_entryname, null);
60: }
61: } catch (IOException e) {
62: throw new CantFindResourceJarEntryException(
63: jar_filename, jar_entryname, e);
64: }
65: } else {
66: throw new CouldntAccessResourceJarException(
67: jar_filename, jar_entryname);
68: }
69: } else if (resource_protocol.equals("file")) {
70: File resource_file = new File(resource_filename);
71: if (resource_file.exists() && resource_file.canRead()) {
72: modification_time = resource_file.lastModified();
73: } else {
74: throw new CouldntAccessResourceFileException(
75: resource_filename);
76: }
77: }
78: // support orion's classloader resource url
79: else if (resource_protocol.equals("classloader")) {
80: modification_time = -1;
81: }
82: // support weblogic's classloader resource url
83: else if (resource_protocol.equals("zip")) {
84: modification_time = -1;
85: } else {
86: throw new UnsupportedResourceProtocolException(
87: resource_filename, resource_protocol);
88: }
89:
90: return modification_time;
91: }
92: }
|