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