View Javadoc

1   /*
2    * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A resource representing a file on the local filesystem.
29   */
30  public class FilesystemResource implements Resource {
31      
32      /** The file represented by this resource. */
33      private File resource;
34      
35      /**
36       * Constructor.
37       *
38       * @param resourcePath the path to the file for this resource
39       * 
40       * @throws ResourceException thrown if the resource path is null or empty
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      /** {@inheritDoc} */
51      public boolean exists() throws ResourceException {
52          return resource.exists();
53      }
54  
55      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
74      public String getLocation() {
75          return resource.getAbsolutePath();
76      }
77      
78      /** {@inheritDoc} */
79      public String toString() {
80          return getLocation();
81      }
82      
83      /** {@inheritDoc} */
84      public int hashCode() {
85          return getLocation().hashCode();
86      }
87      
88      /** {@inheritDoc} */
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 }