001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell;
017:
018: import com.google.gwt.dev.cfg.ModuleDef;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.net.MalformedURLException;
024: import java.net.URL;
025: import java.util.Enumeration;
026: import java.util.Set;
027:
028: import javax.servlet.RequestDispatcher;
029: import javax.servlet.Servlet;
030: import javax.servlet.ServletContext;
031: import javax.servlet.ServletException;
032:
033: /**
034: * ServletContext proxy that implements the getResource and getResourceAsStream
035: * members so that they can work with the {@link GWTShellServlet}.
036: */
037: class HostedModeServletContextProxy implements ServletContext {
038: private final ServletContext context;
039: private final ModuleDef moduleDef;
040: private final File outputDir;
041:
042: HostedModeServletContextProxy(ServletContext context,
043: ModuleDef moduleDef, File outputDir) {
044: this .context = context;
045: this .moduleDef = moduleDef;
046: this .outputDir = outputDir;
047: }
048:
049: /**
050: * @param arg0
051: * @return
052: * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
053: */
054: public Object getAttribute(String arg0) {
055: return context.getAttribute(arg0);
056: }
057:
058: /**
059: * @return
060: * @see javax.servlet.ServletContext#getAttributeNames()
061: */
062: public Enumeration<String> getAttributeNames() {
063: return context.getAttributeNames();
064: }
065:
066: /**
067: * @param arg0
068: * @return
069: * @see javax.servlet.ServletContext#getContext(java.lang.String)
070: */
071: public ServletContext getContext(String arg0) {
072: return context.getContext(arg0);
073: }
074:
075: /**
076: * @param arg0
077: * @return
078: * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
079: */
080: public String getInitParameter(String arg0) {
081: return context.getInitParameter(arg0);
082: }
083:
084: /**
085: * @return
086: * @see javax.servlet.ServletContext#getInitParameterNames()
087: */
088: public Enumeration<?> getInitParameterNames() {
089: return context.getInitParameterNames();
090: }
091:
092: /**
093: * @return
094: * @see javax.servlet.ServletContext#getMajorVersion()
095: */
096: public int getMajorVersion() {
097: return context.getMajorVersion();
098: }
099:
100: /**
101: * @param arg0
102: * @return
103: * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
104: */
105: public String getMimeType(String arg0) {
106: return context.getMimeType(arg0);
107: }
108:
109: /**
110: * @return
111: * @see javax.servlet.ServletContext#getMinorVersion()
112: */
113: public int getMinorVersion() {
114: return context.getMinorVersion();
115: }
116:
117: /**
118: * @param arg0
119: * @return
120: * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
121: */
122: public RequestDispatcher getNamedDispatcher(String arg0) {
123: return context.getNamedDispatcher(arg0);
124: }
125:
126: /**
127: * @param arg0
128: * @return
129: * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
130: */
131: public String getRealPath(String arg0) {
132: return context.getRealPath(arg0);
133: }
134:
135: /**
136: * @param arg0
137: * @return
138: * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
139: */
140: public RequestDispatcher getRequestDispatcher(String arg0) {
141: return context.getRequestDispatcher(arg0);
142: }
143:
144: /**
145: * @param arg0
146: * @return
147: * @throws MalformedURLException
148: * @see javax.servlet.ServletContext#getResource(java.lang.String)
149: */
150: public URL getResource(String path) throws MalformedURLException {
151: String moduleContext = "/" + moduleDef.getName() + "/";
152: if (!path.startsWith(moduleContext)) {
153: // This path is in a different context; just return null
154: return null;
155: }
156:
157: // Try to get the resource from the application's public path
158: URL url = moduleDef.findPublicFile(path.substring(moduleContext
159: .length()));
160: if (url == null) {
161: // Otherwise try the path but rooted in the output directory
162: File requestedFile = new File(outputDir, path.substring(1));
163: if (requestedFile.exists()) {
164: url = requestedFile.toURI().toURL();
165: }
166: }
167:
168: return url;
169: }
170:
171: /**
172: * @param arg0
173: * @return
174: * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
175: */
176: public InputStream getResourceAsStream(String arg0) {
177: URL url;
178: try {
179: url = getResource(arg0);
180: if (url != null) {
181: return url.openStream();
182: }
183: } catch (MalformedURLException e) {
184: // Ignore the exception; return null
185: } catch (IOException e) {
186: // Ignore the exception; return null
187: }
188:
189: return null;
190: }
191:
192: /**
193: *
194: * @param path
195: * @return
196: * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
197: */
198: public Set<String> getResourcePaths(String path) {
199: return context.getResourcePaths(path);
200: }
201:
202: /**
203: * @return
204: * @see javax.servlet.ServletContext#getServerInfo()
205: */
206: public String getServerInfo() {
207: return context.getServerInfo();
208: }
209:
210: /**
211: * @param arg0
212: * @return
213: * @throws ServletException
214: * @deprecated
215: * @see javax.servlet.ServletContext#getServlet(java.lang.String)
216: */
217: @Deprecated
218: public Servlet getServlet(String arg0) throws ServletException {
219: return context.getServlet(arg0);
220: }
221:
222: /**
223: * @return
224: * @see javax.servlet.ServletContext#getServletContextName()
225: */
226: public String getServletContextName() {
227: return context.getServletContextName();
228: }
229:
230: /**
231: * @return
232: * @deprecated
233: * @see javax.servlet.ServletContext#getServletNames()
234: */
235: @Deprecated
236: public Enumeration<String> getServletNames() {
237: return context.getServletNames();
238: }
239:
240: /**
241: * @return
242: * @deprecated
243: * @see javax.servlet.ServletContext#getServlets()
244: */
245: @Deprecated
246: public Enumeration<Servlet> getServlets() {
247: return context.getServlets();
248: }
249:
250: /**
251: * @param arg0
252: * @param arg1
253: * @deprecated
254: * @see javax.servlet.ServletContext#log(java.lang.Exception,
255: * java.lang.String)
256: */
257: @Deprecated
258: public void log(Exception arg0, String arg1) {
259: context.log(arg0, arg1);
260: }
261:
262: /**
263: * @param arg0
264: * @see javax.servlet.ServletContext#log(java.lang.String)
265: */
266: public void log(String arg0) {
267: context.log(arg0);
268: }
269:
270: /**
271: * @param arg0
272: * @param arg1
273: * @see javax.servlet.ServletContext#log(java.lang.String,java.lang.Throwable)
274: */
275: public void log(String arg0, Throwable arg1) {
276: context.log(arg0, arg1);
277: }
278:
279: /**
280: * @param arg0
281: * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
282: */
283: public void removeAttribute(String arg0) {
284: context.removeAttribute(arg0);
285: }
286:
287: /**
288: * @param arg0
289: * @param arg1
290: * @see javax.servlet.ServletContext#setAttribute(java.lang.String,java.lang.Object)
291: */
292: public void setAttribute(String arg0, Object arg1) {
293: context.setAttribute(arg0, arg1);
294: }
295: }
|