001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.jsfsupport.container;
042:
043: import java.io.InputStream;
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.util.Enumeration;
048: import java.util.Hashtable;
049: import java.util.Set;
050:
051: import javax.servlet.RequestDispatcher;
052: import javax.servlet.Servlet;
053: import javax.servlet.ServletContext;
054: import javax.servlet.ServletException;
055:
056: import org.openide.modules.InstalledFileLocator;
057:
058: /**
059: * <p>Provides a <code>ServletContext</code> (v 2.3) object for design-time use</p>
060: *
061: * @author Robert Brewin
062: * @version 1.0
063: */
064: public class RaveServletContext implements ServletContext {
065:
066: /**
067: * Storage for attributes
068: */
069: private Hashtable attributes = new Hashtable();
070:
071: /**
072: * Storage for parameters
073: */
074: private Hashtable parameters = new Hashtable();
075:
076: /**
077: * Default constructor for a RaveServletContext object
078: */
079: public RaveServletContext() {
080: }
081:
082: // --------------------------------------------------------- Public Methods
083:
084: /**
085: * Add an initialization parameter
086: *
087: * @param name - a <code>String</code> specifying the name of the parameter
088: * @param value - a <code>String</code> representing the value for this parameter
089: */
090: public void addInitParameter(String name, String value) {
091: parameters.put(name, value);
092: }
093:
094: // ------------------------------------------------- ServletContext Methods
095:
096: /**
097: * Returns the servlet container attribute with the given name, or null if
098: * there is no attribute by that name. An attribute allows a servlet container
099: * to give the servlet additional information not already provided by this
100: * interface. See your server documentation for information about its attributes.
101: * A list of supported attributes can be retrieved using {@link getAttributeNames}.
102: *
103: * The attribute is returned as a {@link java.lang.Object} or some subclass.
104: * Attribute names should follow the same convention as package names. The
105: * Java Servlet API specification reserves names matching java.*, javax.*, and sun.*.
106: *
107: * @param name - a String specifying the name of the attribute
108: *
109: * @return an Object containing the value of the attribute, or null
110: * if no attribute exists matching the given name
111: */
112: public Object getAttribute(String name) {
113: return (attributes.get(name));
114: }
115:
116: /**
117: * Binds an object to a given attribute name in this servlet context. If the name specified
118: * is already used for an attribute, this method will replace the attribute with the new to
119: * the new attribute.
120: *
121: * @param name - a <code>String</code> specifying the name of the attribute
122: * @param object - an <code>Object</code> representing the attribute to be bound
123: */
124: public void setAttribute(String name, Object value) {
125: attributes.put(name, value);
126: }
127:
128: /**
129: * Returns an Enumeration containing the attribute names available within
130: * this servlet context. Use the {@link getAttribute(java.lang.String)} method
131: * with an attribute name to get the value of an attribute.
132: *
133: * @return an <code>Enumeration</code> of attribute names
134: */
135: public Enumeration getAttributeNames() {
136: return attributes.keys();
137: }
138:
139: /**
140: * Removes the attribute with the given name from the servlet context
141: *
142: * @param name - a <code>String</code> which contains the name of the attribute to remove
143: */
144: public void removeAttribute(String name) {
145: attributes.remove(name);
146: }
147:
148: /**
149: * Returns a <code>ServletContext</code> object that corresponds to a
150: * specified URL on the server. In the <em>Mock</em> environment, this
151: * method throws an <code>UnsupportedOperationException</code> as there
152: * is only one context available
153: *
154: * @param uripath - a <code>String</code> specifying the context path of
155: * another web application in the container.
156: */
157: public ServletContext getContext(String uripath) {
158: throw new UnsupportedOperationException();
159: }
160:
161: /**
162: * Returns a <code>String</code> containing the value of the named context-wide
163: * initialization parameter, or <code>null</code> if the parameter does not exist.
164: *
165: * @param name - a String containing the name of the parameter whose value is
166: * requested
167: *
168: * @return a <code>String</code> containing at least the servlet container name
169: * and version number
170: */
171: public String getInitParameter(String name) {
172: return (String) parameters.get(name);
173: }
174:
175: /**
176: * Returns the names of the context's initialization parameters as an
177: * <code>Enumeration</code> of <code>String</code> objects, or an empty
178: * <code>Enumeration</code> if the context has no initialization parameters.
179: *
180: * @return <code>Enumeration</code> of <code>String</code> objects containing
181: * the names of the context's initialization parameters
182: */
183: public Enumeration getInitParameterNames() {
184: return parameters.keys();
185: }
186:
187: /**
188: * Returns the major version of the Java Servlet API that this servlet container
189: * supports
190: *
191: * @return <em>2</em>
192: */
193: public int getMajorVersion() {
194: return 2;
195: }
196:
197: /**
198: * Returns the minor version of the Java Servlet API that this servlet container
199: * supports
200: *
201: * @return <em>3</em>
202: */
203: public int getMinorVersion() {
204: return 3;
205: }
206:
207: /**
208: * Returns the MIME type of the specified file, or null if the MIME type is not known.
209: *
210: * TODO: Return a valid MIME type... at present, this method throws an UnsupportedOperationException
211: *
212: * @param path - a <code>String</code> specifying the name of a file
213: *
214: * @return a <code>String</code> specifying the file's MIME type
215: */
216: public String getMimeType(String path) {
217: throw new UnsupportedOperationException();
218: }
219:
220: /**
221: * Returns a <code>RequestDispatcher</code> object that acts as a wrapper for the
222: * named servlet. This method is <em>unsupported</em> in the Mock environment and
223: * returns an <code>UnsupportedOperationException</code>
224: */
225: public RequestDispatcher getNamedDispatcher(String name) {
226: throw new UnsupportedOperationException();
227: }
228:
229: /**
230: * Returns a <code>RequestDispatcher</code> object that acts as a wrapper for the
231: * named servlet. This method is <em>unsupported</em> in the Mock environment and
232: * returns an <code>UnsupportedOperationException</code>
233: */
234: public RequestDispatcher getRequestDispatcher(String path) {
235: throw new UnsupportedOperationException();
236: }
237:
238: /**
239: * Returns a <code>String</code> containing the real path for a given virtual path.
240: * This method will attempt to find the resource using the current thread's
241: * context class loader. This method will work only when the path specified
242: * is a file path.
243: */
244: public String getRealPath(String path) {
245: URL url = Thread.currentThread().getContextClassLoader()
246: .getResource(path);
247: if (url == null)
248: return null;
249: String str = url.getPath();
250: if (str.startsWith("file:")) {
251: int i = 5;
252: while (str.charAt(i) == '/')
253: i++;
254: str = str.substring(i);
255:
256: }
257: return str;
258: }
259:
260: /**
261: * Returns a URL to the resource that is mapped to a specified path.
262: * This method returns urls for specific resources available in the Mock jar
263: */
264: public URL getResource(String resource)
265: throws MalformedURLException {
266: URL url = null;
267: ClassLoader loader = getClass().getClassLoader();
268: if (!resource.endsWith(".jar")) {
269: String fqpath = "com/sun/rave/jsfsupp/container" + resource;
270: url = loader.getResource(fqpath);
271: } else {
272: // TODO: BOB
273: // If the location of where we install component libraries changes (likely),
274: // the hard-coded relative path below needs to change
275: File file = InstalledFileLocator.getDefault().locate(
276: "/modules/autoload/ext/" + resource, null, false);
277: url = file.toURL();
278: }
279: return url;
280: }
281:
282: /**
283: * Returns a resource as an <code>InputStream</code> that is mapped to a specified path.
284: * This method returns streams for specific resources available in the Mock jar
285: */
286: public InputStream getResourceAsStream(String path) {
287: ClassLoader loader = getClass().getClassLoader();
288: String fqpath = "com/sun/rave/jsfsupp/container" + path;
289: return loader.getResourceAsStream(fqpath);
290: }
291:
292: /**
293: * Returns a directory-like listing of all the paths to resources.
294: * This method is <em>unsupported</em> in the Mock environment and returns null
295: */
296: public Set getResourcePaths(String path) {
297: // TODO: BOB
298: // For TP, there is likely only one library, and it's hard-coded below
299: // The Set returned from this method should be populated by some resource
300: // set which has the list of added component libraries post-TP
301: Set set = new java.util.HashSet();
302: set.add("jsfcl.jar");
303: return set;
304: }
305:
306: /**
307: * Returns the name of this web application associated with this context
308: */
309: public String getServletContextName() {
310: return "RaveServletContext";
311: }
312:
313: /**
314: * Returns the name and version of the servlet container on which the servlet is
315: * running.
316: */
317: public String getServerInfo() {
318: return "RaveServletContext";
319: }
320:
321: /**
322: * Deprecated in Servlet APIs, included for compliation purposes only.
323: * This method is <em>unsupported</em> in the Mock environment and returns an
324: * <code>UnsupportedOperationException</code>
325: */
326: public Servlet getServlet(String name) throws ServletException {
327: throw new UnsupportedOperationException();
328: //!CQ could return the one servlet that we could know about
329: }
330:
331: /**
332: * Deprecated in Servlet APIs, included for compliation purposes only.
333: * This method is <em>unsupported</em> in the Mock environment and returns an
334: * <code>UnsupportedOperationException</code>
335: */
336: public Enumeration getServlets() {
337: throw new UnsupportedOperationException();
338: //!CQ could return the one servlet that we could know about
339: }
340:
341: /**
342: * Deprecated in Servlet APIs, included for compliation purposes only.
343: * This method is <em>unsupported</em> in the Mock environment and returns an
344: * <code>UnsupportedOperationException</code>
345: */
346: public Enumeration getServletNames() {
347: throw new UnsupportedOperationException();
348: //!CQ could return the one servlet that we could know about
349: }
350:
351: /**
352: * Writes the specified message to a servlet log file. Currently,
353: * his method is <em>unsupported</em> in the Mock environment and returns an
354: * <code>UnsupportedOperationException</code>
355: */
356: public void log(String message) {
357: throw new UnsupportedOperationException();
358: }
359:
360: /**
361: * Deprecated in Servlet APIs, included for compliation purposes only.
362: * This method is <em>unsupported</em> in the Mock environment and returns an
363: * <code>UnsupportedOperationException</code>
364: */
365: public void log(Exception exception, String message) {
366: throw new UnsupportedOperationException();
367: }
368:
369: /**
370: * Writes the specified message and stack trace to a servlet log file. Currently,
371: * his method is <em>unsupported</em> in the Mock environment and returns an
372: * <code>UnsupportedOperationException</code>
373: */
374: public void log(String message, Throwable exception) {
375: throw new UnsupportedOperationException();
376: }
377:
378: public String getContextPath() {
379: return "";
380: }
381: }
|