1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.resourceloader.classpath.service.impl.devmode;
35
36 import info.magnolia.resourceloader.classpath.hierarchy.ClasspathFile;
37 import info.magnolia.resourceloader.classpath.service.impl.URLConnectionUtil;
38
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.net.URL;
42 import java.net.URLConnection;
43
44 import org.reflections.util.ClasspathHelper;
45 import org.reflections.vfs.Vfs;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.common.base.Objects;
50
51
52
53
54
55
56 public class DevelopmentModeClasspathFile implements ClasspathFile {
57
58 private static final Logger log = LoggerFactory.getLogger(DevelopmentModeClasspathFile.class);
59
60 private URL classpathLocation;
61 private Vfs.File jarFile;
62
63 public DevelopmentModeClasspathFile(URL classpathLocation, Vfs.File jarFile) {
64 this.classpathLocation = classpathLocation;
65 this.jarFile = jarFile;
66 }
67
68 @Override
69 public String getName() {
70 return jarFile.getName();
71 }
72
73 @Override
74 public String getAbsolutePath() {
75 return "/" + jarFile.getRelativePath();
76 }
77
78 @Override
79 public InputStream openStream() throws IOException {
80 return jarFile.openInputStream();
81 }
82
83 @Override
84 public long getLastModified() {
85 URLConnection urlConnection = null;
86 try {
87
88
89 final URL resourceUrl = ClasspathHelper.contextClassLoader().getResource(jarFile.getRelativePath());
90 if (resourceUrl != null) {
91 urlConnection = resourceUrl.openConnection();
92 } else {
93
94
95 urlConnection = classpathLocation.openConnection();
96 }
97
98 return urlConnection.getLastModified();
99 } catch (IOException e) {
100 log.warn("Failed to check the last modification date of a resource at {}, problems with establishing the connection with the jar file containing the resource [{}] resource: {}. Returning -1", getAbsolutePath(), classpathLocation, e.getMessage());
101 return -1;
102 } finally {
103 URLConnectionUtil.closeURLConnection(urlConnection);
104 }
105 }
106
107 @Override
108 public boolean equals(Object o) {
109 if (this == o) return true;
110 if (o == null || getClass() != o.getClass()) return false;
111
112 DevelopmentModeClasspathFile that = (DevelopmentModeClasspathFile) o;
113
114 return Objects.equal(getAbsolutePath(), that.getAbsolutePath());
115 }
116
117 @Override
118 public int hashCode() {
119 return Objects.hashCode(getAbsolutePath());
120 }
121 }