KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > AbstractResource


1 /*
2  * Copyright 2002-2007 the original author or authors.
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.springframework.core.io;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24
25 /**
26  * Convenience base class for {@link Resource} implementations,
27  * pre-implementing typical behavior.
28  *
29  * <p>The "exists" method will check whether a File or InputStream can
30  * be opened; "isOpen" will always return false; "getURL" and "getFile"
31  * throw an exception; and "toString" will return the description.
32  *
33  * @author Juergen Hoeller
34  * @since 28.12.2003
35  */

36 public abstract class AbstractResource implements Resource {
37
38     /**
39      * This implementation checks whether a File can be opened,
40      * falling back to whether an InputStream can be opened.
41      * This will cover both directories and content resources.
42      */

43     public boolean exists() {
44         // Try file existence: can we find the file in the file system?
45
try {
46             return getFile().exists();
47         }
48         catch (IOException JavaDoc ex) {
49             // Fall back to stream existence: can we open the stream?
50
try {
51                 InputStream JavaDoc is = getInputStream();
52                 is.close();
53                 return true;
54             }
55             catch (Throwable JavaDoc isEx) {
56                 return false;
57             }
58         }
59     }
60
61     /**
62      * This implementation always returns <code>false</code>.
63      */

64     public boolean isOpen() {
65         return false;
66     }
67
68     /**
69      * This implementation throws a FileNotFoundException, assuming
70      * that the resource cannot be resolved to a URL.
71      */

72     public URL JavaDoc getURL() throws IOException JavaDoc {
73         throw new FileNotFoundException JavaDoc(getDescription() + " cannot be resolved to URL");
74     }
75
76     /**
77      * This implementation throws a FileNotFoundException, assuming
78      * that the resource cannot be resolved to an absolute file path.
79      */

80     public File JavaDoc getFile() throws IOException JavaDoc {
81         throw new FileNotFoundException JavaDoc(getDescription() + " cannot be resolved to absolute file path");
82     }
83
84     /**
85      * This implementation throws a FileNotFoundException, assuming
86      * that relative resources cannot be created for this resource.
87      */

88     public Resource createRelative(String JavaDoc relativePath) throws IOException JavaDoc {
89         throw new FileNotFoundException JavaDoc("Cannot create a relative resource for " + getDescription());
90     }
91
92     /**
93      * This implementation always throws IllegalStateException,
94      * assuming that the resource does not carry a filename.
95      */

96     public String JavaDoc getFilename() throws IllegalStateException JavaDoc {
97         throw new IllegalStateException JavaDoc(getDescription() + " does not carry a filename");
98     }
99
100     /**
101      * This abstract method declaration shadows the method in the Resource interface.
102      * This is necessary to make the <code>toString</code> implementation in this
103      * class work on Sun's JDK 1.3 classic VM, which can't find the method when
104      * executing <code>toString</code> else. Furthermore, <code>getDescription</code>
105      * is also called from <code>equals</code> and <code>hashCode</code>
106      * @see Resource#getDescription()
107      * @see #toString()
108      * @see #equals(Object)
109      * @see #hashCode()
110      */

111     public abstract String JavaDoc getDescription();
112
113
114     /**
115      * This implementation returns the description of this resource.
116      * @see #getDescription()
117      */

118     public String JavaDoc toString() {
119         return getDescription();
120     }
121
122     /**
123      * This implementation compares description strings.
124      * @see #getDescription()
125      */

126     public boolean equals(Object JavaDoc obj) {
127         return (obj == this ||
128             (obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription())));
129     }
130
131     /**
132      * This implementation returns the description's hash code.
133      * @see #getDescription()
134      */

135     public int hashCode() {
136         return getDescription().hashCode();
137     }
138
139 }
140
Popular Tags