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.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.InputStream;
23
24 import org.joda.time.DateTime;
25 import org.opensaml.xml.util.DatatypeHelper;
26
27
28
29
30 public class FilesystemResource implements Resource {
31
32
33 private File resource;
34
35
36
37
38
39
40
41
42 public FilesystemResource(String resourcePath) throws ResourceException{
43 if(DatatypeHelper.isEmpty(resourcePath)){
44 throw new ResourceException("Resource path may not be null or empty");
45 }
46
47 resource = new File(resourcePath);
48 }
49
50
51 public boolean exists() throws ResourceException {
52 return resource.exists();
53 }
54
55
56 public InputStream getInputStream() throws ResourceException {
57 try{
58 return new FileInputStream(resource);
59 }catch(FileNotFoundException e){
60 throw new ResourceException("Resource file does not exist: " + resource.getAbsolutePath());
61 }
62 }
63
64
65 public DateTime getLastModifiedTime() throws ResourceException {
66 if(!resource.exists()){
67 throw new ResourceException("Resource file does not exist: " + resource.getAbsolutePath());
68 }
69
70 return new DateTime(resource.lastModified());
71 }
72
73
74 public String getLocation() {
75 return resource.getAbsolutePath();
76 }
77
78
79 public String toString() {
80 return getLocation();
81 }
82
83
84 public int hashCode() {
85 return getLocation().hashCode();
86 }
87
88
89 public boolean equals(Object o) {
90 if(o == this){
91 return true;
92 }
93
94 if(o instanceof FilesystemResource){
95 return getLocation().equals(((ClasspathResource)o).getLocation());
96 }
97
98 return false;
99 }
100 }