001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.providers.jsp;
006:
007: import java.io.InputStream;
008: import java.io.IOException;
009: import java.io.File;
010:
011: import java.net.URL;
012: import java.net.URLConnection;
013: import java.net.FileNameMap;
014: import java.net.MalformedURLException;
015:
016: import java.util.Collections;
017: import java.util.Enumeration;
018: import java.util.Hashtable;
019: import java.util.Vector;
020: import java.util.Set;
021: import java.util.logging.Logger;
022: import java.util.logging.Level;
023: import java.util.logging.LogRecord;
024:
025: import javax.servlet.Servlet;
026: import javax.servlet.ServletException;
027: import javax.servlet.ServletContext;
028: import javax.servlet.ServletConfig;
029: import javax.servlet.RequestDispatcher;
030:
031: import com.sun.portal.providers.jsp.jasper3.jasper.EmbededServletOptions;
032:
033: import com.sun.portal.providers.ProviderAdapter;
034: import com.sun.portal.providers.ProviderException;
035: import com.sun.portal.providers.context.ProviderContext;
036: import com.sun.portal.providers.context.ProviderContextException;
037:
038: import com.sun.portal.desktop.context.ProviderContextThreadLocalizer;
039: import com.sun.portal.desktop.context.ContextException;
040:
041: import com.sun.portal.desktop.DesktopServlet;
042: import com.sun.portal.desktop.DesktopRequestThreadLocalizer;
043: import com.sun.portal.log.common.PortalLogger;
044:
045: /*
046: * The JspServletEnvironment represents the environment in which the
047: * JSP is compiled and run. It is responsible for implementing the
048: * search algorithm for JSP files as well as the rest of the methods
049: * for the ServletContext and ServletConfig interfaces.
050: **/
051: class JspServletEnvironment implements ServletContext, ServletConfig {
052: Hashtable attributes = new Hashtable();
053: Hashtable initArgs = new Hashtable();
054: //ProviderAdapter provider = new ProviderAdapter();
055: EmbededServletOptions options;
056: String desktopType;
057: String locale;
058: String channelName;
059: String clientPath;
060: JSPProvider jspProvider;
061: private static Logger logger = PortalLogger
062: .getLogger(JspServletEnvironment.class);
063:
064: public JspServletEnvironment(JSPProvider jspProvider,
065: String channelName, String scratchdir, String desktopType,
066: String locale, String clientPath, String classPath) {
067: this .channelName = channelName;
068: this .desktopType = desktopType;
069: this .locale = locale;
070: this .clientPath = clientPath;
071: this .jspProvider = jspProvider;
072:
073: initArgs.put("scratchdir", scratchdir);
074: initArgs.put("classpath", classPath);
075:
076: options = new EmbededServletOptions(this , this );
077: }
078:
079: public EmbededServletOptions getOptions() {
080: return options;
081: }
082:
083: public Object getAttribute(String name) {
084: if (name
085: .equals("com.sun.portal.providers.jsp.jasper3.tomcat.jsp_classpath"))
086: return options.getClassPath();
087: else if (name
088: .equals("com.sun.portal.providers.jsp.jasper3.tomcat.classloader")) {
089: return this .getClass().getClassLoader();
090: } else {
091: return attributes.get(name);
092: }
093: }
094:
095: public Enumeration getAttributeNames() {
096: return attributes.keys();
097: }
098:
099: public void setAttribute(String name, Object object) {
100: attributes.put(name, object);
101: }
102:
103: public void removeAttribute(String name) {
104: attributes.remove(name);
105: }
106:
107: public ServletContext getContext(String path) {
108: throw new IllegalArgumentException(
109: "ServletContextImpl.getContext not supported");
110: }
111:
112: public int getMajorVersion() {
113: return 1;
114: }
115:
116: public int getMinorVersion() {
117: return 1;
118: }
119:
120: public String getMimeType(String filename) {
121: FileNameMap fnm = URLConnection.getFileNameMap();
122: return fnm.getContentTypeFor(filename);
123: }
124:
125: public String getRealPath(String path) {
126: String realPath = null;
127:
128: // seperate uri and query string
129: //
130: int index = path.indexOf('?');
131: String uri = path;
132: ProviderContext providerContext = ProviderContextThreadLocalizer
133: .get();
134:
135: if (index != -1) {
136: uri = path.substring(0, index);
137: }
138:
139: try {
140: URL url = getResource(uri);
141: if (url != null
142: && url.getProtocol().equalsIgnoreCase("file")) {
143: // take care of File.getAbsolutePath() troubles on
144: // jdk1.1.x/win
145:
146: //mjm realPath = JspFileUtil.patch(url.getFile());
147: realPath = url.getFile();
148: }
149: } catch (Exception e) {
150: if (logger.isLoggable(Level.INFO)) {
151: LogRecord record = new LogRecord(Level.INFO,
152: "PSDT_CSPPJ0016");
153: record.setLoggerName(logger.getName());
154: record.setParameters(new Object[] { path });
155: record.setThrown(e);
156: logger.log(record);
157: }
158: }
159: return realPath;
160: }
161:
162: public InputStream getResourceAsStream(String path) {
163: InputStream is = null;
164:
165: try {
166: URL url = getResource(path);
167: URLConnection con = url.openConnection();
168:
169: con.connect();
170:
171: is = con.getInputStream();
172: } catch (MalformedURLException e) {
173: if (logger.isLoggable(Level.FINEST)) {
174: LogRecord record = new LogRecord(Level.FINEST,
175: "PSDT_CSPPJ0017");
176: record.setLoggerName(logger.getName());
177: record.setParameters(new Object[] { path });
178: record.setThrown(e);
179: logger.log(record);
180: }
181: } catch (IOException e) {
182: if (logger.isLoggable(Level.FINEST)) {
183: LogRecord record = new LogRecord(Level.FINEST,
184: "PSDT_CSPPJ0017");
185: record.setLoggerName(logger.getName());
186: record.setParameters(new Object[] { path });
187: record.setThrown(e);
188: logger.log(record);
189: }
190: }
191:
192: return is;
193: }
194:
195: public URL getResource(String path) throws MalformedURLException {
196: if (path == null) {
197: logger.finest("PSDT_CSPPJ0018");
198: throw new NullPointerException(
199: "null path passed to getResource");
200: }
201:
202: // JSPProvider does not support web.xml. Returns null to indicate that
203: // such resource is not available. See bug 4631056.
204:
205: if (path.equals("/WEB-INF/web.xml")) {
206: return null;
207: }
208:
209: // build search path for JSP files
210: //
211: // desktopType_locale/channelName/clientPath
212: // desktopType_locale/channelName
213: // desktopType_locale
214: // desktopType/channelName/clientPath
215: // desktopType/channelName
216: // desktopType
217: // default_locale/channelName/clientPath
218: // default_locale/channelName
219: // default_locale
220: // default/channelName/clientPath
221: // default/channelName
222: // default
223: //
224: //changed to use FileLookup API.
225:
226: ProviderContext providerContext = ProviderContextThreadLocalizer
227: .get();
228: File jspFileHandle = null;
229: try {
230: jspFileHandle = jspProvider.getExistingJSPPath(
231: providerContext, channelName, path);
232: } catch (ProviderException pc) {
233: if (logger.isLoggable(Level.FINEST)) {
234: LogRecord record = new LogRecord(Level.FINEST,
235: "PSDT_CSPPJ0012");
236: record.setLoggerName(logger.getName());
237: record
238: .setParameters(new Object[] { channelName, path });
239: record.setThrown(pc);
240: logger.log(record);
241: }
242: throw new MalformedURLException(
243: "JspServletEnvironment.getResource(): ");
244: }
245:
246: if (jspFileHandle == null) {
247: if (logger.isLoggable(Level.FINEST)) {
248: LogRecord record = new LogRecord(Level.FINEST,
249: "PSDT_CSPPJ0019");
250: record.setLoggerName(logger.getName());
251: record
252: .setParameters(new Object[] { channelName, path });
253: logger.log(record);
254: }
255: throw new MalformedURLException(
256: "JspServletEnvironment.getResource(): not found for channelName="
257: + channelName + ", path=" + path);
258: }
259: return jspFileHandle.toURL();
260: }
261:
262: public RequestDispatcher getRequestDispatcher(String path) {
263: if (path == null || !path.startsWith("/")) {
264: if (logger.isLoggable(Level.FINEST)) {
265: LogRecord record = new LogRecord(Level.FINEST,
266: "PSDT_CSPPJ0020");
267: record.setLoggerName(logger.getName());
268: record.setParameters(new Object[] { path });
269: logger.log(record);
270: }
271: throw new IllegalArgumentException(
272: "getRequestDispatcher bad path");
273: }
274:
275: return new JspRequestDispatcher(path, this );
276: }
277:
278: public RequestDispatcher getNamedDispatcher(String name) {
279: return null;
280: }
281:
282: public String getServerInfo() {
283: return "JSPProvider";
284: }
285:
286: public void log(String msg) {
287: /*
288: ProviderContext providerContext = ProviderContextThreadLocalizer.get();
289: if ( providerContext.isDebugMessageEnabled() ) {
290: providerContext.debugMessage(msg);
291: }
292: */
293: }
294:
295: public String getInitParameter(String name) {
296: if (name.equals("staticContext")) {
297: String sContent = DesktopRequestThreadLocalizer
298: .getRequest().getContextPath();
299: return sContent;
300: } else {
301: return (String) initArgs.get(name);
302: }
303: }
304:
305: public Enumeration getInitParameterNames() {
306: return initArgs.keys();
307: }
308:
309: public void log(String msg, Throwable t) {
310: logger.log(Level.INFO, msg, t);
311: }
312:
313: /**
314: * methods added for Servlet 2.3
315: */
316: public String getServletContextName() {
317: return "JSPProvider";
318: }
319:
320: public Set getResourcePaths(String path) {
321: Set s = Collections.singleton(path);
322: return s;
323: }
324:
325: /**
326: *
327: * @deprecated This method is deprecated in the
328: * javax.servlet.ServletContext interface
329: */
330:
331: public void log(Exception e, String msg) {
332: log(msg, e);
333: }
334:
335: /**
336: *
337: * @deprecated This method is deprecated in the
338: * javax.servlet.ServletContext interface
339: */
340:
341: public Servlet getServlet(String name) throws ServletException {
342: return null;
343: }
344:
345: /**
346: * This method has been deprecated in the public api and always
347: * return an empty enumeration.
348: *
349: * @deprecated
350: */
351:
352: public Enumeration getServlets() {
353: // silly hack to get an empty enumeration
354: Vector v = new Vector();
355: return v.elements();
356: }
357:
358: /**
359: * This method has been deprecated in the public api and always
360: * return an empty enumeration.
361: *
362: * @deprecated
363: */
364:
365: public Enumeration getServletNames() {
366: // silly hack to get an empty enumeration
367: Vector v = new Vector();
368: return v.elements();
369: }
370:
371: /*
372: * ServletConfig methods
373: */
374: public ServletContext getServletContext() {
375: return this ;
376: }
377:
378: public String getServletName() {
379: return "JSPProvider";
380: }
381: }
|