1 16 17 package org.directwebremoting.util; 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 import java.util.Set ; 29 30 import javax.servlet.RequestDispatcher ; 31 import javax.servlet.Servlet ; 32 import javax.servlet.ServletContext ; 33 34 40 public class FakeServletContext implements ServletContext 41 { 42 46 public FakeServletContext() 47 { 48 this(""); 49 } 50 51 55 public FakeServletContext(String resourceBasePath) 56 { 57 this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); 58 59 String tempDir = System.getProperty("java.io.tmpdir"); 61 if (tempDir != null) 62 { 63 attributes.put("javax.servlet.context.tempdir", new File (tempDir)); 64 } 65 } 66 67 73 protected String getResourceLocation(String path) 74 { 75 String output = path; 76 if (!output.startsWith("/")) 77 { 78 output = "/" + output; 79 } 80 output = resourceBasePath + output; 81 82 return output; 83 } 84 85 public ServletContext getContext(String name) 86 { 87 throw new UnsupportedOperationException ("getContext"); 88 } 89 90 93 public int getMajorVersion() 94 { 95 return 2; 96 } 97 98 101 public int getMinorVersion() 102 { 103 return 4; 104 } 105 106 109 public String getMimeType(String filePath) 110 { 111 throw new UnsupportedOperationException ("getMimeType"); 112 } 113 114 117 public Set getResourcePaths(String path) 118 { 119 throw new UnsupportedOperationException (); 120 } 121 122 125 public URL getResource(String path) throws MalformedURLException 126 { 127 throw new UnsupportedOperationException (); 128 } 129 130 133 public InputStream getResourceAsStream(String path) 134 { 135 throw new UnsupportedOperationException (); 136 } 137 138 141 public RequestDispatcher getRequestDispatcher(String path) 142 { 143 if (!path.startsWith("/")) 144 { 145 throw new IllegalArgumentException ("RequestDispatcher path at ServletContext level must start with '/'"); 146 } 147 return new FakeRequestDispatcher(path); 148 } 149 150 153 public RequestDispatcher getNamedDispatcher(String path) 154 { 155 throw new UnsupportedOperationException ("getNamedDispatcher"); 156 } 157 158 161 public Servlet getServlet(String name) 162 { 163 throw new UnsupportedOperationException ("getServlet"); 164 } 165 166 169 public Enumeration getServlets() 170 { 171 throw new UnsupportedOperationException ("getServlets"); 172 } 173 174 177 public Enumeration getServletNames() 178 { 179 throw new UnsupportedOperationException ("getServletNames"); 180 } 181 182 185 public void log(String message) 186 { 187 log.info(message); 188 } 189 190 193 public void log(Exception ex, String message) 194 { 195 log.warn(message, ex); 196 } 197 198 201 public void log(String message, Throwable ex) 202 { 203 log.warn(message, ex); 204 } 205 206 209 public String getRealPath(String path) 210 { 211 throw new UnsupportedOperationException (); 212 } 213 214 217 public String getServerInfo() 218 { 219 return "FakeServletContext"; 220 } 221 222 225 public String getInitParameter(String name) 226 { 227 return initParameters.getProperty(name); 228 } 229 230 235 public void addInitParameter(String name, String value) 236 { 237 initParameters.setProperty(name, value); 238 } 239 240 243 public Enumeration getInitParameterNames() 244 { 245 return initParameters.keys(); 246 } 247 248 251 public Object getAttribute(String name) 252 { 253 return attributes.get(name); 254 } 255 256 259 public Enumeration getAttributeNames() 260 { 261 return Collections.enumeration(attributes.keySet()); 262 } 263 264 267 public void setAttribute(String name, Object value) 268 { 269 if (value != null) 270 { 271 attributes.put(name, value); 272 } 273 else 274 { 275 attributes.remove(name); 276 } 277 } 278 279 282 public void removeAttribute(String name) 283 { 284 attributes.remove(name); 285 } 286 287 292 public void setServletContextName(String servletContextName) 293 { 294 this.servletContextName = servletContextName; 295 } 296 297 300 public String getServletContextName() 301 { 302 return servletContextName; 303 } 304 305 309 public String getContextPath() 310 { 311 throw new UnsupportedOperationException ("getContextPath"); 312 } 313 314 317 private static final Logger log = Logger.getLogger(FakeServletContext.class); 318 319 322 private final String resourceBasePath; 323 324 327 private final Properties initParameters = new Properties (); 328 329 332 private final Map attributes = new HashMap (); 333 334 337 private String servletContextName = "FakeServletContext"; 338 } 339 | Popular Tags |