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 extends AbstractFilteredResource {
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 super();
49
50 if (DatatypeHelper.isEmpty(path)) {
51 throw new ResourceException("Resource path may not be null or empty");
52 }
53
54 resource = getClass().getResource(path);
55 if (resource == null) {
56 throw new ResourceException("Classpath resource does not exist: " + path);
57 }
58
59 lastModTime = new DateTime();
60 }
61
62
63
64
65
66
67
68
69
70 public ClasspathResource(String path, ResourceFilter resourceFilter) throws ResourceException {
71 super(resourceFilter);
72
73 if (DatatypeHelper.isEmpty(path)) {
74 throw new ResourceException("Resource path may not be null or empty");
75 }
76
77 resource = getClass().getResource(path);
78 if (resource == null) {
79 throw new ResourceException("Classpath resource does not exist: " + path);
80 }
81
82 lastModTime = new DateTime();
83 }
84
85
86 public boolean exists() throws ResourceException {
87 if (resource != null) {
88 return true;
89 }
90
91 return false;
92 }
93
94
95 public InputStream getInputStream() throws ResourceException {
96 try {
97 InputStream ins = resource.openStream();
98 if (getResourceFilter() != null) {
99 return getResourceFilter().applyFilter(ins);
100 } else {
101 return ins;
102 }
103 } catch (IOException e) {
104 throw new ResourceException("Unable to open resource: " + resource);
105 }
106 }
107
108
109 public DateTime getLastModifiedTime() throws ResourceException {
110 return lastModTime;
111 }
112
113
114 public String getLocation() {
115 return resource.toString();
116 }
117
118
119 public String toString() {
120 return getLocation();
121 }
122
123
124 public int hashCode() {
125 return getLocation().hashCode();
126 }
127
128
129 public boolean equals(Object o) {
130 if (o == this) {
131 return true;
132 }
133
134 if (o instanceof ClasspathResource) {
135 return getLocation().equals(((ClasspathResource) o).getLocation());
136 }
137
138 return false;
139 }
140 }