1 15 16 package com.sun.facelets.mock; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.net.URI ; 23 import java.net.URL ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.HashSet ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import javax.servlet.RequestDispatcher ; 34 import javax.servlet.Servlet ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletException ; 37 38 43 public class MockServletContext implements ServletContext { 44 45 protected final Properties initParams = new Properties (); 46 47 protected final Logger log = Logger 48 .getLogger("facelets.mock.ServletContext"); 49 50 protected final Hashtable attributes = new Hashtable (); 51 52 protected final URI base; 53 54 public MockServletContext(URI base) { 55 this.base = base; 56 File f = new File (base); 57 if (!f.exists()) { 58 throw new IllegalArgumentException ("File: " + base.getPath() 59 + " doesn't exist"); 60 } 61 } 62 63 public ServletContext getContext(String name) { 64 throw new UnsupportedOperationException (); 65 } 66 67 public int getMajorVersion() { 68 return 2; 69 } 70 71 public int getMinorVersion() { 72 return 3; 73 } 74 75 public String getMimeType(String path) { 76 throw new UnsupportedOperationException (); 77 } 78 79 public Set getResourcePaths(String path) { 80 URI uri = this.resolve(path); 81 if (uri != null) { 82 File f = new File (uri); 83 if (f.exists() && f.isDirectory()) { 84 File [] c = f.listFiles(); 85 Set s = new HashSet (); 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 getResource(String path) throws MalformedURLException { 97 URI uri = this.resolve(path); 98 if (uri != null) { 99 File f = new File (uri); 100 if (f.exists()) { 101 return uri.toURL(); 102 } 103 } 104 return null; 105 } 106 107 public InputStream getResourceAsStream(String path) { 108 URI uri = this.resolve(path); 109 if (uri != null) { 110 try { 111 File f = new File (uri); 112 if (f.exists()) { 113 return uri.toURL().openStream(); 114 } 115 } catch (MalformedURLException e) { 116 this.log.severe(e.getMessage()); 117 return null; 118 } catch (IOException e) { 119 this.log.severe(e.getMessage()); 120 return null; 121 } 122 } 123 return null; 124 } 125 126 public RequestDispatcher getRequestDispatcher(String path) { 127 URI uri = this.resolve(path); 128 if (uri != null) { 129 File f = new File (uri); 130 if (f.exists()) { 131 try { 132 return new MockRequestDispatcher(uri.toURL()); 133 } catch (MalformedURLException e) { 134 this.log.severe(e.getMessage()); 135 return null; 136 } 137 } 138 139 } 140 return null; 141 } 142 143 public RequestDispatcher getNamedDispatcher(String fileName) { 144 throw new UnsupportedOperationException (); 145 } 146 147 public Servlet getServlet(String name) throws ServletException { 148 throw new UnsupportedOperationException (); 149 } 150 151 public Enumeration getServlets() { 152 throw new UnsupportedOperationException (); 153 } 154 155 public Enumeration getServletNames() { 156 throw new UnsupportedOperationException (); 157 } 158 159 public void log(String message) { 160 this.log.info(message); 161 } 162 163 public void log(Exception error, String message) { 164 this.log.log(Level.INFO, message, error); 165 166 } 167 168 public void log(String message, Throwable error) { 169 this.log.log(Level.INFO, message, error); 170 } 171 172 public String getRealPath(String path) { 173 URI uri = this.resolve(path); 174 if (uri != null) { 175 File f = new File (uri); 176 if (f.exists()) { 177 return f.getAbsolutePath(); 178 } 179 } 180 return null; 181 } 182 183 private final URI resolve(String path) { 184 if (path == null) { 185 throw new NullPointerException ("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 getServerInfo() { 197 return this.getClass().getName(); 198 } 199 200 public String getInitParameter(String name) { 201 return this.initParams.getProperty(name); 202 } 203 204 public Enumeration getInitParameterNames() { 205 return this.initParams.keys(); 206 } 207 208 public void setInitParameter(String name, String value) { 209 this.initParams.setProperty(name, value); 210 } 211 212 public Object getAttribute(String name) { 213 return this.attributes.get(name); 214 } 215 216 public Enumeration getAttributeNames() { 217 return this.attributes.keys(); 218 } 219 220 public void setAttribute(String name, Object value) { 221 this.attributes.put(name, value); 222 } 223 224 public void removeAttribute(String name) { 225 this.attributes.remove(name); 226 } 227 228 public String getServletContextName() { 229 return this.getClass().getName(); 230 } 231 232 } 233 | Popular Tags |