001: /*
002: * $Id$ $Revision$ $Date$
003: *
004: * ==============================================================================
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package wicket.util.file;
018:
019: import java.io.File;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.servlet.ServletContext;
027:
028: import wicket.util.string.StringList;
029:
030: /**
031: * Mantains a list of folders as a path.
032: *
033: * @author Johan Compagner
034: */
035: public final class WebApplicationPath implements IResourcePath {
036: /** The list of urls in the path */
037: private final List webappPaths = new ArrayList();
038:
039: /** The list of folders in the path */
040: private final List folders = new ArrayList();
041:
042: private final ServletContext servletContext;
043:
044: /**
045: * Constructor
046: *
047: * @param servletContext
048: * The webapplication context where the resources must be loaded
049: * from
050: */
051: public WebApplicationPath(ServletContext servletContext) {
052: this .servletContext = servletContext;
053: }
054:
055: /**
056: * @param folder
057: * add a path that is lookup through the servlet context
058: */
059: public void add(String folder) {
060: final Folder f = new Folder(folder);
061: if (f.exists()) {
062: folders.add(f);
063: } else {
064: if (!folder.startsWith("/")) {
065: folder = "/" + folder;
066: }
067: if (!folder.endsWith("/")) {
068: folder += "/";
069: }
070: webappPaths.add(folder);
071: }
072: }
073:
074: /**
075: * Looks for a given pathname along this path
076: *
077: * @param pathname
078: * The filename with possible path
079: * @return The file located on the path
080: */
081: public URL find(final String pathname) {
082: for (final Iterator iterator = folders.iterator(); iterator
083: .hasNext();) {
084: Folder folder = (Folder) iterator.next();
085: File file = new File(folder, pathname);
086: if (file.exists()) {
087: try {
088: return file.toURI().toURL();
089: } catch (MalformedURLException ex) {
090: // ignore
091: }
092: }
093: }
094: for (final Iterator iterator = webappPaths.iterator(); iterator
095: .hasNext();) {
096: final String path = (String) iterator.next();
097: try {
098: final URL file = servletContext.getResource(path
099: + pathname);
100: if (file != null) {
101: return file;
102: }
103: } catch (Exception ex) {
104: // ignore, file couldn't be found
105: }
106: }
107:
108: return null;
109: }
110:
111: /**
112: * @see java.lang.Object#toString()
113: */
114: public String toString() {
115: return "[folders = " + StringList.valueOf(folders)
116: + ", webapppaths: " + StringList.valueOf(webappPaths)
117: + "]";
118: }
119: }
|