1 package servletunit; 2 3 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.Servlet ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import java.io.InputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.Set ; 34 35 38 public class ServletContextSimulator implements ServletContext 39 { 40 41 private Hashtable initParameters; 42 private Hashtable attributes; 43 private RequestDispatcherSimulator dispatcher = null; 44 private static Log logger = LogFactory.getLog( ServletContextSimulator.class ); 45 private File contextDirectory; 46 47 public ServletContextSimulator() { 48 this.initParameters = new Hashtable (); 49 this.attributes = new Hashtable (); 50 } 51 52 81 public Object getAttribute(String name) 82 { 83 return attributes.get(name); 84 } 85 86 99 public Enumeration getAttributeNames() 100 { 101 return attributes.keys(); 102 } 103 104 107 public ServletContext getContext(String uripath) 108 { 109 throw new UnsupportedOperationException ("getContext operation is not supported!"); 110 } 111 112 130 public String getInitParameter(String s) 131 { 132 return (String ) initParameters.get(s); 133 } 134 135 147 public Enumeration getInitParameterNames() 148 { 149 return initParameters.keys(); 150 } 151 152 163 public void setInitParameter(String key,String value) 164 { 165 initParameters.put(key,value); 166 } 167 168 177 public int getMajorVersion() 178 { 179 return 2; 180 } 181 182 185 public String getMimeType(String file) 186 { 187 throw new UnsupportedOperationException ("getMimeType operation is not supported!"); 188 } 189 190 199 public int getMinorVersion() 200 { 201 return 3; 202 } 203 204 public RequestDispatcher getNamedDispatcher(String s) 205 { 206 throw new UnsupportedOperationException ("getNamedDispatcher operation is not supported!"); 207 } 208 209 public String getRealPath(String path) 210 { 211 if ((contextDirectory == null) || (path == null)) 212 return null; 213 else 214 return (new File (contextDirectory, path)).getAbsolutePath(); 215 } 216 217 242 public RequestDispatcher getRequestDispatcher(String urlpath) 243 { 244 dispatcher = new RequestDispatcherSimulator(urlpath); 245 return dispatcher; 246 } 247 248 253 public RequestDispatcherSimulator getRequestDispatcherSimulator() { 254 return dispatcher; 255 } 256 257 260 public URL getResource(String path) throws MalformedURLException 261 { 262 try { 263 File file = getResourceAsFile(path); 264 265 if (file.exists()) { 266 return file.toURL(); 267 } 268 else { 269 if(!path.startsWith("/")){ 270 path = "/" + path; 271 } 272 return this.getClass().getResource(path); 273 } 274 } catch (Exception e) { 275 return null; 276 } 277 } 278 279 311 public InputStream getResourceAsStream(String path) 312 { 313 try { 314 File file = getResourceAsFile(path); 315 316 if (file.exists()) { 317 return new FileInputStream (file); 318 } 319 else { 320 if(!path.startsWith("/")){ 321 path = "/" + path; 322 } 323 return this.getClass().getResourceAsStream(path); 324 } 325 } catch (Exception e) { 326 System.out.println("caught error: " + e); 327 e.printStackTrace(); 328 return null; 329 } 330 } 331 332 344 public File getResourceAsFile(String path){ 345 File file = new File (path); 346 347 if (!file.exists()) 349 { 350 if(!path.startsWith("/")){ 351 path = "/" + path; 352 } 353 if((getContextDirectory() != null)) 354 { 355 file = new File (getContextDirectory().getAbsolutePath() + path); 356 }else{ 357 file = new File (new File (".").getAbsolutePath() + path); 359 } 360 } 361 return file; 362 363 } 364 365 368 public Set getResourcePaths() 369 { 370 throw new UnsupportedOperationException ("getResourcePaths operation is not supported!"); 371 } 372 373 391 public String getServerInfo() 392 { 393 return "MockServletEngine/1.9.5"; 394 } 395 396 399 public Servlet getServlet(String name) throws ServletException 400 { 401 throw new UnsupportedOperationException ("getServlet operation is not supported!"); 402 } 403 404 407 public String getServletContextName() 408 { 409 throw new UnsupportedOperationException ("getServletContextName operation is not supported!"); 410 } 411 412 415 public Enumeration getServletNames() 416 { 417 throw new UnsupportedOperationException ("getServletNames operation is not supported!"); 418 } 419 420 423 public Enumeration getServlets() 424 { 425 throw new UnsupportedOperationException ("getServlets operation is not supported!"); 426 } 427 428 438 public void log(Exception exception, String msg) 439 { 440 logger.info(msg + "\n" + exception.getClass() + " - " + exception.getMessage()); 441 } 442 443 455 public void log(String msg) 456 { 457 logger.info(msg); 458 } 459 460 474 public void log(String message, Throwable throwable) 475 { 476 logger.info(message + "\n" + throwable.getClass() + " - " + throwable.getMessage()); 477 } 478 479 494 public void removeAttribute(String name) 495 { 496 attributes.remove(name); 497 } 498 499 525 public void setAttribute(String name, Object object) 526 { 527 attributes.put(name,object); 528 } 529 530 533 public Set getResourcePaths(String path) { 534 throw new UnsupportedOperationException ("getResourcePaths operation is not supported!"); 535 } 536 537 541 public void setContextDirectory(File contextDirectory) { 542 this.contextDirectory = contextDirectory; 543 } 544 545 public File getContextDirectory() { 546 return contextDirectory; 547 } 548 549 550 } 551 | Popular Tags |