1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.util.resource;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22
23 import org.joda.time.DateTime;
24 import org.opensaml.xml.util.DatatypeHelper;
25
26
27
28
29
30
31
32 public class ClasspathResource implements Resource {
33
34
35 private URL resource;
36
37
38 private DateTime lastModTime;
39
40
41
42
43
44
45
46
47 public ClasspathResource(String path) throws ResourceException {
48 if (DatatypeHelper.isEmpty(path)) {
49 throw new ResourceException("Resource path may not be null or empty");
50 }
51
52 resource = getClass().getResource(path);
53 if (resource == null) {
54 throw new ResourceException("Classpath resource does not exist: " + path);
55 }
56
57 lastModTime = new DateTime();
58 }
59
60
61 public boolean exists() throws ResourceException {
62 if(resource != null){
63 return true;
64 }
65
66 return false;
67 }
68
69
70 public InputStream getInputStream() throws ResourceException {
71 try {
72 return resource.openStream();
73 } catch (IOException e) {
74 throw new ResourceException("Unable to open resource: " + resource);
75 }
76 }
77
78
79 public DateTime getLastModifiedTime() throws ResourceException {
80 return lastModTime;
81 }
82
83
84 public String getLocation() {
85 return resource.toString();
86 }
87
88
89 public String toString() {
90 return getLocation();
91 }
92
93
94 public int hashCode() {
95 return getLocation().hashCode();
96 }
97
98
99 public boolean equals(Object o) {
100 if(o == this){
101 return true;
102 }
103
104 if(o instanceof ClasspathResource){
105 return getLocation().equals(((ClasspathResource)o).getLocation());
106 }
107
108 return false;
109 }
110 }