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