001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import com.caucho.util.Alarm;
033: import com.caucho.util.CharBuffer;
034: import com.caucho.util.L10N;
035: import com.caucho.util.LruCache;
036: import com.caucho.util.QDate;
037:
038: import java.io.*;
039: import java.util.ArrayList;
040: import java.util.Map;
041:
042: /**
043: * The classpath scheme.
044: */
045: public class ClasspathPath extends FilesystemPath {
046: protected static L10N L = new L10N(ClasspathPath.class);
047:
048: /**
049: * Creates a new classpath sub path.
050: *
051: * @param root the classpath filesystem root
052: * @param userPath the argument to the calling lookup()
053: * @param newAttributes any attributes passed to http
054: * @param path the full normalized path
055: * @param query any query string
056: */
057: public ClasspathPath(FilesystemPath root, String userPath,
058: String path) {
059: super (root, userPath, path);
060:
061: if (_root == null)
062: _root = this ;
063: }
064:
065: /**
066: * Lookup the actual path relative to the filesystem root.
067: *
068: * @param userPath the user's path to lookup()
069: * @param attributes the user's attributes to lookup()
070: * @param path the normalized path
071: *
072: * @return the selected path
073: */
074: public Path fsWalk(String userPath, Map<String, Object> attributes,
075: String path) {
076: return new ClasspathPath(_root, userPath, path);
077: }
078:
079: /**
080: * Returns the scheme, http.
081: */
082: public String getScheme() {
083: return "classpath";
084: }
085:
086: /**
087: * Returns true if the file exists.
088: */
089: public boolean exists() {
090: ClassLoader loader = Thread.currentThread()
091: .getContextClassLoader();
092:
093: return loader.getResource(getPath()) != null;
094: }
095:
096: /**
097: * Returns true if the file exists.
098: */
099: public boolean isFile() {
100: return exists();
101: }
102:
103: /**
104: * Returns true if the file is readable.
105: */
106: public boolean canRead() {
107: return exists();
108: }
109:
110: /**
111: * Returns the last modified time.
112: */
113: public boolean isDirectory() {
114: return false;
115: }
116:
117: /**
118: * Returns a read stream for a GET request.
119: */
120: public StreamImpl openReadImpl() throws IOException {
121: ClassLoader loader = Thread.currentThread()
122: .getContextClassLoader();
123:
124: String path = getPath();
125: if (path.startsWith("/"))
126: path = path.substring(1);
127:
128: InputStream is = loader.getResourceAsStream(path);
129:
130: if (is == null)
131: throw new FileNotFoundException(path);
132:
133: return new VfsStream(is, null);
134: }
135:
136: /**
137: * Returns the string form of the http path.
138: */
139: public String toString() {
140: return getURL();
141: }
142: }
|