001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2005-2007 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.tui;
011
012import java.io.InputStream;
013import java.net.MalformedURLException;
014import java.net.URL;
015import java.util.*;
016import javax.servlet.*;
017
018/**
019 * Partial implementation of the {@link ServletContext} where just
020 * enough is present to allow for communication between Mondrian's
021 * XMLA code and other code in the same JVM.
022 *
023 * <p>Currently it is used in both the CmdRunner and in XMLA JUnit tests.
024 * If you need to add to this implementation, please do so.
025 *
026 * @author Richard M. Emberson
027 */
028public class MockServletContext implements ServletContext {
029
030    public static final String PARAM_DATASOURCES_CONFIG = "DataSourcesConfig";
031    public static final String PARAM_CHAR_ENCODING = "CharacterEncoding";
032    public static final String PARAM_CALLBACKS = "Callbacks";
033
034    private Map<String, URL> resources;
035    private Map<String, Object> attributes;
036    private int majorVersion;
037    private int minorVersion;
038    private Properties parameters;
039
040    public MockServletContext() {
041        this.majorVersion = 1;
042        this.minorVersion = 1;
043        this.resources = Collections.emptyMap();
044        this.attributes = Collections.emptyMap();
045        this.parameters = new Properties();
046    }
047
048
049    /**
050     * Returns a ServletContext object that corresponds to a specified URL on
051     * the server.
052     *
053     */
054    public ServletContext getContext(String s) {
055        // TODO
056        return null;
057    }
058
059    /**
060     * Returns the major version of the Java Servlet API that this servlet
061     * container supports.
062     *
063     */
064    public int getMajorVersion() {
065        return this.majorVersion;
066    }
067
068    /**
069     * Returns the minor version of the Servlet API that this servlet container
070     * supports.
071     *
072     */
073    public int getMinorVersion() {
074        return this.minorVersion;
075    }
076
077    /**
078     * Returns the MIME type of the specified file, or null if the MIME type is
079     * not known.
080     *
081     */
082    public String getMimeType(String s) {
083        // TODO
084        return null;
085    }
086
087    /**
088     *
089     *
090     */
091    public Set getResourcePaths(String s) {
092        // TODO
093        return null;
094    }
095
096    /**
097     * Returns a URL to the resource that is mapped to a specified path.
098     */
099    public URL getResource(String name) throws MalformedURLException {
100        if (!resources.containsKey(name)) {
101            addResource(name, new URL("file://" + name));
102        }
103        return resources.get(name);
104    }
105
106    /**
107     *  Returns the resource located at the named path as an InputStream object.
108     *
109     */
110    public InputStream getResourceAsStream(String s) {
111        // TODO
112        return null;
113    }
114
115    /**
116     *  Returns a RequestDispatcher object that acts as a wrapper for the
117     *  resource located at the given path.
118     *
119     */
120    public RequestDispatcher getRequestDispatcher(String s) {
121        // TODO
122        return null;
123    }
124
125    /**
126     * Returns a RequestDispatcher object that acts as a wrapper for the named
127     * servlet.
128     *
129     */
130    public RequestDispatcher getNamedDispatcher(String s) {
131        // TODO
132        return null;
133    }
134
135    public Servlet getServlet(String s) throws ServletException {
136        // method is deprecated as of Servlet API 2.1
137        return null;
138    }
139
140    public Enumeration getServlets() {
141        // method is deprecated as of Servlet API 2.1
142        return null;
143    }
144
145    public Enumeration getServletNames() {
146        // method is deprecated as of Servlet API 2.1
147        return null;
148    }
149
150    /**
151     * Writes the specified message to a servlet log file, usually an event log.
152     *
153     */
154    public void log(String s) {
155        // TODO
156    }
157
158    /**
159     * Deprecated. As of Java Servlet API 2.1, use log(String message, Throwable
160     * throwable) instead.
161     *
162     * This method was originally defined to write an exception's stack trace
163     * and an explanatory error message to the servlet log file.
164     *
165     * @deprecated Method log is deprecated
166     */
167    public void log(Exception exception, String s) {
168        log(s, exception);
169    }
170
171    /**
172     *  Writes an explanatory message and a stack trace for a given Throwable
173     *  exception to the servlet log file.
174     *
175     */
176    public void log(String s, Throwable throwable) {
177        // TODO
178    }
179
180    /**
181     * Returns a String containing the real path for a given virtual path.
182     *
183     */
184    public String getRealPath(String path) {
185        return path;
186    }
187
188    /**
189     * Returns the name and version of the servlet container on which the
190     * servlet is running.
191     *
192     */
193    public String getServerInfo() {
194        // TODO
195        return null;
196    }
197
198    /**
199     * Returns a String containing the value of the named context-wide
200     * initialization parameter, or null if the parameter does not exist.
201     *
202     */
203    public String getInitParameter(String name) {
204        return parameters.getProperty(name);
205    }
206
207    /**
208     * Returns the names of the context's initialization parameters as an
209     * Enumeration of String objects, or an empty Enumeration if the context has
210     * no initialization parameters.
211     *
212     */
213    public Enumeration getInitParameterNames() {
214        return parameters.propertyNames();
215    }
216
217    /**
218     *
219     *
220     */
221    public Object getAttribute(String s) {
222        return this.attributes.get(s);
223    }
224
225    /**
226     * Returns an Enumeration containing the attribute names available within
227     * this servlet context.
228     *
229     */
230    public Enumeration getAttributeNames() {
231        // TODO
232        return Collections.enumeration(this.attributes.keySet());
233    }
234
235    /**
236     *  Binds an object to a given attribute name in this servlet context.
237     *
238     */
239    public void setAttribute(String s, Object obj) {
240        if (this.attributes == Collections.EMPTY_MAP) {
241            this.attributes = new HashMap<String, Object>();
242        }
243        this.attributes.put(s, obj);
244    }
245
246    /**
247     *  Removes the attribute with the given name from the servlet context.
248     *
249     */
250    public void removeAttribute(String s) {
251        this.attributes.remove(s);
252    }
253
254    /**
255     *
256     *
257     */
258    public String getServletContextName() {
259        // TODO
260        return null;
261    }
262
263
264
265
266    /////////////////////////////////////////////////////////////////////////
267    //
268    // implementation access
269    //
270    /////////////////////////////////////////////////////////////////////////
271    public void setMajorVersion(int majorVersion) {
272        this.majorVersion = majorVersion;
273    }
274    public void setMinorVersion(int minorVersion) {
275        this.minorVersion = minorVersion;
276    }
277    public void addResource(String name, URL url) {
278        if (this.resources == Collections.EMPTY_MAP) {
279            this.resources = new HashMap<String, URL>();
280        }
281        this.resources.put(name, url);
282    }
283    public void addInitParameter(String name, String value) {
284        if (value != null) {
285            this.parameters.setProperty(name, value);
286        }
287    }
288}
289
290// End MockServletContext.java