001: /**
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * Licensed under the Common Development and Distribution License,
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.sun.com/cddl/
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
012: * implied. See the License for the specific language governing
013: * permissions and limitations under the License.
014: */package com.sun.facelets.mock;
015:
016: import java.io.File;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.net.MalformedURLException;
020: import java.net.URI;
021: import java.net.URL;
022: import java.util.Collections;
023: import java.util.Enumeration;
024: import java.util.HashSet;
025: import java.util.Hashtable;
026: import java.util.Properties;
027: import java.util.Set;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import javax.servlet.RequestDispatcher;
032: import javax.servlet.Servlet;
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletException;
035:
036: /**
037: *
038: * @author Jacob Hookom
039: * @version $Id: MockServletContext.java,v 1.1 2005/07/18 08:25:42 jhook Exp $
040: */
041: public class MockServletContext implements ServletContext {
042:
043: protected final Properties initParams = new Properties();
044:
045: protected final Logger log = Logger
046: .getLogger("facelets.mock.ServletContext");
047:
048: protected final Hashtable attributes = new Hashtable();
049:
050: protected final URI base;
051:
052: public MockServletContext(URI base) {
053: this .base = base;
054: File f = new File(base);
055: if (!f.exists()) {
056: throw new IllegalArgumentException("File: "
057: + base.getPath() + " doesn't exist");
058: }
059: }
060:
061: public ServletContext getContext(String name) {
062: throw new UnsupportedOperationException();
063: }
064:
065: public int getMajorVersion() {
066: return 2;
067: }
068:
069: public int getMinorVersion() {
070: return 3;
071: }
072:
073: public String getMimeType(String path) {
074: throw new UnsupportedOperationException();
075: }
076:
077: public Set getResourcePaths(String path) {
078: URI uri = this .resolve(path);
079: if (uri != null) {
080: File f = new File(uri);
081: if (f.exists() && f.isDirectory()) {
082: File[] c = f.listFiles();
083: Set s = new HashSet();
084: int start = f.getAbsolutePath().length();
085: for (int i = 0; i < c.length; i++) {
086: s.add(c[i].getAbsolutePath().substring(start));
087: }
088: return s;
089: }
090: }
091: return Collections.EMPTY_SET;
092: }
093:
094: public URL getResource(String path) throws MalformedURLException {
095: URI uri = this .resolve(path);
096: if (uri != null) {
097: File f = new File(uri);
098: if (f.exists()) {
099: return uri.toURL();
100: }
101: }
102: return null;
103: }
104:
105: public InputStream getResourceAsStream(String path) {
106: URI uri = this .resolve(path);
107: if (uri != null) {
108: try {
109: File f = new File(uri);
110: if (f.exists()) {
111: return uri.toURL().openStream();
112: }
113: } catch (MalformedURLException e) {
114: this .log.severe(e.getMessage());
115: return null;
116: } catch (IOException e) {
117: this .log.severe(e.getMessage());
118: return null;
119: }
120: }
121: return null;
122: }
123:
124: public RequestDispatcher getRequestDispatcher(String path) {
125: URI uri = this .resolve(path);
126: if (uri != null) {
127: File f = new File(uri);
128: if (f.exists()) {
129: try {
130: return new MockRequestDispatcher(uri.toURL());
131: } catch (MalformedURLException e) {
132: this .log.severe(e.getMessage());
133: return null;
134: }
135: }
136:
137: }
138: return null;
139: }
140:
141: public RequestDispatcher getNamedDispatcher(String fileName) {
142: throw new UnsupportedOperationException();
143: }
144:
145: public Servlet getServlet(String name) throws ServletException {
146: throw new UnsupportedOperationException();
147: }
148:
149: public Enumeration getServlets() {
150: throw new UnsupportedOperationException();
151: }
152:
153: public Enumeration getServletNames() {
154: throw new UnsupportedOperationException();
155: }
156:
157: public void log(String message) {
158: this .log.info(message);
159: }
160:
161: public void log(Exception error, String message) {
162: this .log.log(Level.INFO, message, error);
163:
164: }
165:
166: public void log(String message, Throwable error) {
167: this .log.log(Level.INFO, message, error);
168: }
169:
170: public String getRealPath(String path) {
171: URI uri = this .resolve(path);
172: if (uri != null) {
173: File f = new File(uri);
174: if (f.exists()) {
175: return f.getAbsolutePath();
176: }
177: }
178: return null;
179: }
180:
181: private final URI resolve(String path) {
182: if (path == null) {
183: throw new NullPointerException("Path cannot be null");
184: }
185: if (path.charAt(0) == '/') {
186: if (path.length() > 1) {
187: return this .base.resolve(path.substring(1));
188: }
189: return this .base;
190: }
191: return null;
192: }
193:
194: public String getServerInfo() {
195: return this .getClass().getName();
196: }
197:
198: public String getInitParameter(String name) {
199: return this .initParams.getProperty(name);
200: }
201:
202: public Enumeration getInitParameterNames() {
203: return this .initParams.keys();
204: }
205:
206: public void setInitParameter(String name, String value) {
207: this .initParams.setProperty(name, value);
208: }
209:
210: public Object getAttribute(String name) {
211: return this .attributes.get(name);
212: }
213:
214: public Enumeration getAttributeNames() {
215: return this .attributes.keys();
216: }
217:
218: public void setAttribute(String name, Object value) {
219: this .attributes.put(name, value);
220: }
221:
222: public void removeAttribute(String name) {
223: this .attributes.remove(name);
224: }
225:
226: public String getServletContextName() {
227: return this.getClass().getName();
228: }
229:
230: }
|