1 29 30 package com.caucho.server.webapp; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.Path; 35 import com.caucho.vfs.Vfs; 36 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.Servlet ; 39 import javax.servlet.ServletContext ; 40 import javax.servlet.ServletContextAttributeEvent ; 41 import javax.servlet.ServletContextAttributeListener ; 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.net.URL ; 45 import java.util.ArrayList ; 46 import java.util.Collections ; 47 import java.util.Enumeration ; 48 import java.util.HashMap ; 49 import java.util.HashSet ; 50 import java.util.Set ; 51 import java.util.logging.Level ; 52 import java.util.logging.Logger ; 53 54 57 public class ServletContextImpl implements ServletContext { 58 static final Logger log = Log.open(ServletContextImpl.class); 59 static final L10N L = new L10N(ServletContextImpl.class); 60 61 private String _name; 62 63 private HashMap <String ,Object > _attributes = new HashMap <String ,Object >(); 64 65 private ArrayList <ServletContextAttributeListener > 66 _applicationAttributeListeners; 67 68 private HashMap <String ,String > _initParams = new HashMap <String ,String >(); 69 70 public Path getAppDir() 71 { 72 return Vfs.lookup(); 73 } 74 75 78 public void setDisplayName(String name) 79 { 80 _name = name; 81 } 82 83 86 public String getServletContextName() 87 { 88 return _name; 89 } 90 91 94 public String getContextPath() 95 { 96 return _name; 97 } 98 99 102 protected void addAttributeListener(ServletContextAttributeListener listener) 103 { 104 if (_applicationAttributeListeners == null) 105 _applicationAttributeListeners = new ArrayList <ServletContextAttributeListener >(); 106 107 _applicationAttributeListeners.add(listener); 108 } 109 110 113 public String getServerInfo() 114 { 115 return "Resin/" + com.caucho.Version.VERSION; 116 } 117 118 121 public int getMajorVersion() 122 { 123 return 2; 124 } 125 126 129 public int getMinorVersion() 130 { 131 return 5; 132 } 133 134 137 protected void setInitParameter(String name, String value) 138 { 139 _initParams.put(name, value); 140 } 141 142 145 public String getInitParameter(String name) 146 { 147 return (String ) _initParams.get(name); 148 } 149 150 153 public Enumeration getInitParameterNames() 154 { 155 return Collections.enumeration(_initParams.keySet()); 156 } 157 158 161 public Object getAttribute(String name) 162 { 163 synchronized (_attributes) { 164 Object value = _attributes.get(name); 165 166 return value; 167 } 168 } 169 170 173 public Enumeration getAttributeNames() 174 { 175 synchronized (_attributes) { 176 return Collections.enumeration(_attributes.keySet()); 177 } 178 } 179 180 186 public void setAttribute(String name, Object value) 187 { 188 Object oldValue; 189 190 synchronized (_attributes) { 191 if (value != null) 192 oldValue = _attributes.put(name, value); 193 else 194 oldValue = _attributes.remove(name); 195 } 196 197 if (_applicationAttributeListeners != null) { 199 ServletContextAttributeEvent event; 200 201 if (oldValue != null) 202 event = new ServletContextAttributeEvent (this, name, oldValue); 203 else 204 event = new ServletContextAttributeEvent (this, name, value); 205 206 for (int i = 0; i < _applicationAttributeListeners.size(); i++) { 207 ServletContextAttributeListener listener; 208 209 Object objListener = _applicationAttributeListeners.get(i); 210 listener = (ServletContextAttributeListener ) objListener; 211 212 try { 213 if (oldValue != null) 214 listener.attributeReplaced(event); 215 else 216 listener.attributeAdded(event); 217 } catch (Throwable e) { 218 log.log(Level.FINE, e.toString(), e); 219 } 220 } 221 } 222 } 223 224 229 public void removeAttribute(String name) 230 { 231 Object oldValue; 232 233 synchronized (_attributes) { 234 oldValue = _attributes.remove(name); 235 } 236 237 if (_applicationAttributeListeners != null) { 239 ServletContextAttributeEvent event; 240 241 event = new ServletContextAttributeEvent (this, name, oldValue); 242 243 for (int i = 0; i < _applicationAttributeListeners.size(); i++) { 244 ServletContextAttributeListener listener; 245 246 Object objListener = _applicationAttributeListeners.get(i); 247 listener = (ServletContextAttributeListener ) objListener; 248 249 try { 250 listener.attributeRemoved(event); 251 } catch (Throwable e) { 252 log.log(Level.FINE, e.toString(), e); 253 } 254 } 255 } 256 } 257 258 261 public String getRealPath(String uri) 262 { 263 return getAppDir().lookup("./" + uri).getNativePath(); 264 } 265 266 272 public URL getResource(String name) 273 throws java.net.MalformedURLException 274 { 275 if (! name.startsWith("/")) 276 throw new java.net.MalformedURLException (name); 277 278 String realPath = getRealPath(name); 279 280 Path path = getAppDir().lookupNative(realPath); 281 282 if (path.exists()) 283 return new URL (path.getURL()); 284 285 return null; 286 } 287 288 291 public InputStream getResourceAsStream(String uripath) 292 { 293 Path path = getAppDir().lookupNative(getRealPath(uripath)); 294 295 try { 296 if (path.canRead()) 297 return path.openRead(); 298 else 299 return null; 300 } catch (IOException e) { 301 log.log(Level.FINEST, e.toString(), e); 302 303 return null; 304 } 305 } 306 307 310 public Set getResourcePaths(String prefix) 311 { 312 if (! prefix.endsWith("/")) 313 prefix = prefix + "/"; 314 315 Path path = getAppDir().lookup(getRealPath(prefix)); 316 317 HashSet <String > set = new HashSet <String >(); 318 319 try { 320 String []list = path.list(); 321 322 for (int i = 0; i < list.length; i++) { 323 if (path.lookup(list[i]).isDirectory()) 324 set.add(prefix + list[i] + '/'); 325 else 326 set.add(prefix + list[i]); 327 } 328 } catch (IOException e) { 329 } 330 331 return set; 332 } 333 334 337 public ServletContext getContext(String uri) 338 { 339 return this; 340 } 341 342 345 public String getMimeType(String uri) 346 { 347 return null; 348 } 349 350 353 public RequestDispatcher getRequestDispatcher(String uri) 354 { 355 return null; 356 } 357 358 361 public RequestDispatcher getNamedDispatcher(String servletName) 362 { 363 return null; 364 } 365 366 369 370 375 public final void log(String message) 376 { 377 log(message, null); 378 } 379 380 383 public final void log(Exception e, String msg) 384 { 385 log(msg, e); 386 } 387 388 394 public void log(String message, Throwable e) 395 { 396 if (e != null) 397 log.log(Level.WARNING, message, e); 398 else 399 log.info(message); 400 } 401 402 406 public Servlet getServlet(String name) 407 { 408 throw new UnsupportedOperationException ("getServlet is deprecated"); 409 } 410 411 public Enumeration getServletNames() 412 { 413 throw new UnsupportedOperationException ("getServletNames is deprecated"); 414 } 415 416 public Enumeration getServlets() 417 { 418 throw new UnsupportedOperationException ("getServlets is deprecated"); 419 } 420 } 421 | Popular Tags |