KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > mock > MockServletContext


1 /**
2  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3  * Licensed under the Common Development and Distribution License,
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.sun.com/cddl/
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package com.sun.facelets.mock;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.servlet.RequestDispatcher JavaDoc;
34 import javax.servlet.Servlet JavaDoc;
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37
38 /**
39  *
40  * @author Jacob Hookom
41  * @version $Id: MockServletContext.java,v 1.1 2005/07/18 08:25:42 jhook Exp $
42  */

43 public class MockServletContext implements ServletContext JavaDoc {
44
45     protected final Properties JavaDoc initParams = new Properties JavaDoc();
46
47     protected final Logger JavaDoc log = Logger
48             .getLogger("facelets.mock.ServletContext");
49
50     protected final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
51
52     protected final URI JavaDoc base;
53
54     public MockServletContext(URI JavaDoc base) {
55         this.base = base;
56         File JavaDoc f = new File JavaDoc(base);
57         if (!f.exists()) {
58             throw new IllegalArgumentException JavaDoc("File: " + base.getPath()
59                     + " doesn't exist");
60         }
61     }
62
63     public ServletContext JavaDoc getContext(String JavaDoc name) {
64         throw new UnsupportedOperationException JavaDoc();
65     }
66
67     public int getMajorVersion() {
68         return 2;
69     }
70
71     public int getMinorVersion() {
72         return 3;
73     }
74
75     public String JavaDoc getMimeType(String JavaDoc path) {
76         throw new UnsupportedOperationException JavaDoc();
77     }
78
79     public Set JavaDoc getResourcePaths(String JavaDoc path) {
80         URI JavaDoc uri = this.resolve(path);
81         if (uri != null) {
82             File JavaDoc f = new File JavaDoc(uri);
83             if (f.exists() && f.isDirectory()) {
84                 File JavaDoc[] c = f.listFiles();
85                 Set JavaDoc s = new HashSet JavaDoc();
86                 int start = f.getAbsolutePath().length();
87                 for (int i = 0; i < c.length; i++) {
88                     s.add(c[i].getAbsolutePath().substring(start));
89                 }
90                 return s;
91             }
92         }
93         return Collections.EMPTY_SET;
94     }
95
96     public URL JavaDoc getResource(String JavaDoc path) throws MalformedURLException JavaDoc {
97         URI JavaDoc uri = this.resolve(path);
98         if (uri != null) {
99             File JavaDoc f = new File JavaDoc(uri);
100             if (f.exists()) {
101                 return uri.toURL();
102             }
103         }
104         return null;
105     }
106
107     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
108         URI JavaDoc uri = this.resolve(path);
109         if (uri != null) {
110             try {
111                 File JavaDoc f = new File JavaDoc(uri);
112                 if (f.exists()) {
113                     return uri.toURL().openStream();
114                 }
115             } catch (MalformedURLException JavaDoc e) {
116                 this.log.severe(e.getMessage());
117                 return null;
118             } catch (IOException JavaDoc e) {
119                 this.log.severe(e.getMessage());
120                 return null;
121             }
122         }
123         return null;
124     }
125
126     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
127         URI JavaDoc uri = this.resolve(path);
128         if (uri != null) {
129             File JavaDoc f = new File JavaDoc(uri);
130             if (f.exists()) {
131                 try {
132                     return new MockRequestDispatcher(uri.toURL());
133                 } catch (MalformedURLException JavaDoc e) {
134                     this.log.severe(e.getMessage());
135                     return null;
136                 }
137             }
138
139         }
140         return null;
141     }
142
143     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc fileName) {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146
147     public Servlet JavaDoc getServlet(String JavaDoc name) throws ServletException JavaDoc {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150
151     public Enumeration JavaDoc getServlets() {
152         throw new UnsupportedOperationException JavaDoc();
153     }
154
155     public Enumeration JavaDoc getServletNames() {
156         throw new UnsupportedOperationException JavaDoc();
157     }
158
159     public void log(String JavaDoc message) {
160         this.log.info(message);
161     }
162
163     public void log(Exception JavaDoc error, String JavaDoc message) {
164         this.log.log(Level.INFO, message, error);
165
166     }
167
168     public void log(String JavaDoc message, Throwable JavaDoc error) {
169         this.log.log(Level.INFO, message, error);
170     }
171
172     public String JavaDoc getRealPath(String JavaDoc path) {
173         URI JavaDoc uri = this.resolve(path);
174         if (uri != null) {
175             File JavaDoc f = new File JavaDoc(uri);
176             if (f.exists()) {
177                 return f.getAbsolutePath();
178             }
179         }
180         return null;
181     }
182
183     private final URI JavaDoc resolve(String JavaDoc path) {
184         if (path == null) {
185             throw new NullPointerException JavaDoc("Path cannot be null");
186         }
187         if (path.charAt(0) == '/') {
188             if (path.length() > 1) {
189                 return this.base.resolve(path.substring(1));
190             }
191             return this.base;
192         }
193         return null;
194     }
195
196     public String JavaDoc getServerInfo() {
197         return this.getClass().getName();
198     }
199
200     public String JavaDoc getInitParameter(String JavaDoc name) {
201         return this.initParams.getProperty(name);
202     }
203
204     public Enumeration JavaDoc getInitParameterNames() {
205         return this.initParams.keys();
206     }
207     
208     public void setInitParameter(String JavaDoc name, String JavaDoc value) {
209         this.initParams.setProperty(name, value);
210     }
211
212     public Object JavaDoc getAttribute(String JavaDoc name) {
213         return this.attributes.get(name);
214     }
215
216     public Enumeration JavaDoc getAttributeNames() {
217         return this.attributes.keys();
218     }
219
220     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
221         this.attributes.put(name, value);
222     }
223
224     public void removeAttribute(String JavaDoc name) {
225         this.attributes.remove(name);
226     }
227
228     public String JavaDoc getServletContextName() {
229         return this.getClass().getName();
230     }
231
232 }
233
Popular Tags