001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.platform;
006:
007: import org.springframework.core.io.DefaultResourceLoader;
008: import org.springframework.core.io.Resource;
009: import java.io.File;
010: import java.io.IOException;
011: import java.util.Collections;
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.Set;
015: import java.util.TreeSet;
016: import java.util.logging.Logger;
017:
018: /**
019: * Manages resources in GeoServer.
020: * <p>
021: * The loader maintains a search path in which it will use to look up resources.
022: * The {@link #baseDirectory} is a member of this path.
023: * </p>
024: * <p>
025: * Files and directories created by the resource loader are made relative to
026: * {@link #baseDirectory}.
027: * </p>
028: * <p>
029: * <pre>
030: * <code>
031: * File dataDirectory = ...
032: * GeoServerResourceLoader loader = new GeoServerResourceLoader( dataDirectory );
033: * loader.addSearchLocation( new File( "/WEB-INF/" ) );
034: * loader.addSearchLocation( new File( "/data" ) );
035: * ...
036: * File catalog = loader.find( "catalog.xml" );
037: * </code>
038: * </pre>
039: * </p>
040: *
041: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
042: *
043: */
044: public class GeoServerResourceLoader extends DefaultResourceLoader {
045: private static final Logger LOGGER = org.geotools.util.logging.Logging
046: .getLogger("org.vfny.geoserver.global");
047:
048: /** "path" for resource lookups */
049: Set searchLocations;
050:
051: /**
052: * Base directory
053: */
054: File baseDirectory;
055:
056: /**
057: * Creates a new resource loader with no base directory.
058: * <p>
059: * Such a constructed resource loader is not capable of creating resources
060: * from relative paths.
061: * </p>
062: */
063: public GeoServerResourceLoader() {
064: searchLocations = new TreeSet();
065: }
066:
067: /**
068: * Creates a new resource loader.
069: *
070: * @param baseDirectory The directory in which
071: */
072: public GeoServerResourceLoader(File baseDirectory) {
073: this ();
074: this .baseDirectory = baseDirectory;
075: setSearchLocations(Collections.EMPTY_SET);
076: }
077:
078: /**
079: * Adds a location to the path used for resource lookups.
080: *
081: * @param A directory containing resources.
082: */
083: public void addSearchLocation(File searchLocation) {
084: searchLocations.add(searchLocation);
085: }
086:
087: /**
088: * Sets the search locations used for resource lookups.
089: *
090: * @param searchLocations A set of {@link File}.
091: */
092: public void setSearchLocations(Set searchLocations) {
093: this .searchLocations = new HashSet(searchLocations);
094:
095: //always add the base directory
096: if (baseDirectory != null) {
097: this .searchLocations.add(baseDirectory);
098: }
099: }
100:
101: /**
102: * @return The base directory.
103: */
104: public File getBaseDirectory() {
105: return baseDirectory;
106: }
107:
108: /**
109: * Sets the base directory.
110: *
111: * @param baseDirectory
112: */
113: public void setBaseDirectory(File baseDirectory) {
114: this .baseDirectory = baseDirectory;
115:
116: searchLocations.add(baseDirectory);
117: }
118:
119: /**
120: * Performs a resource lookup.
121: *
122: * @param location The name of the resource to lookup, can be absolute or
123: * relative.
124: *
125: * @return The file handle representing the resource, or null if the
126: * resource could not be found.
127: *
128: * @throws IOException In the event of an I/O error.
129: */
130: public File find(String location) throws IOException {
131: //first to an existance check
132: File file = new File(location);
133:
134: if (file.isAbsolute()) {
135: return file;
136: } else {
137: //try a relative url
138: for (Iterator f = searchLocations.iterator(); f.hasNext();) {
139: File base = (File) f.next();
140: file = new File(base, location);
141:
142: try {
143: if (file.exists()) {
144: return file;
145: }
146: } catch (SecurityException e) {
147: LOGGER
148: .warning("Failed attemp to check existance of "
149: + file.getAbsolutePath());
150: }
151: }
152: }
153:
154: Resource resource = getResource(location);
155:
156: if (resource.exists()) {
157: return resource.getFile();
158: }
159:
160: return null;
161: }
162:
163: /**
164: * Creates a new directory.
165: * <p>
166: * Relative paths are created relative to {@link #baseDirectory}.
167: * If {@link #baseDirectory} is not set, an IOException is thrown.
168: * </p>
169: * <p>
170: * If <code>location</code> already exists as a file, an IOException is thrown.
171: * </p>
172: * @param location Location of directory to create, either absolute or
173: * relative.
174: *
175: * @return The file handle of the created directory.
176: *
177: * @throws IOException
178: */
179: public File createDirectory(String location) throws IOException {
180: File file = find(location);
181:
182: if (file != null) {
183: if (!file.isDirectory()) {
184: String msg = location
185: + " already exists and is not directory";
186: throw new IOException(msg);
187: }
188: }
189:
190: file = new File(location);
191:
192: if (file.isAbsolute()) {
193: file.mkdir();
194:
195: return file;
196: }
197:
198: //no base directory set, cannot create a relative path
199: if (baseDirectory == null) {
200: String msg = "No base location set, could not create directory: "
201: + location;
202: throw new IOException(msg);
203: }
204:
205: file = new File(baseDirectory, location);
206: file.mkdir();
207:
208: return file;
209: }
210:
211: /**
212: * Creates a new file.
213: * <p>
214: * Relative paths are created relative to {@link #baseDirectory}.
215: * </p>
216: * If {@link #baseDirectory} is not set, an IOException is thrown.
217: * </p>
218: * <p>
219: * If <code>location</code> already exists as a directory, an IOException is thrown.
220: * </p>
221: * @param location Location of file to create, either absolute or relative.
222: *
223: * @return The file handle of the created file.
224: *
225: * @throws IOException In the event of an I/O error.
226: */
227: public File createFile(String location) throws IOException {
228: File file = find(location);
229:
230: if (file != null) {
231: if (file.isDirectory()) {
232: String msg = location
233: + " already exists and is a directory";
234: throw new IOException(msg);
235: }
236:
237: return file;
238: }
239:
240: file = new File(location);
241:
242: if (file.isAbsolute()) {
243: file.createNewFile();
244:
245: return file;
246: }
247:
248: //no base directory set, cannot create a relative path
249: if (baseDirectory == null) {
250: String msg = "No base location set, could not create file: "
251: + location;
252: throw new IOException(msg);
253: }
254:
255: file = new File(baseDirectory, location);
256: file.createNewFile();
257:
258: return file;
259: }
260: }
|