1 19 20 package com.sslexplorer.server.jetty; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Collections ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.mortbay.http.ResourceCache; 38 import org.mortbay.jetty.servlet.WebApplicationContext; 39 import org.mortbay.util.Resource; 40 41 import com.sslexplorer.server.Main; 42 43 68 public class CustomWebApplicationContext extends WebApplicationContext { 69 70 final static Log log = LogFactory.getLog(CustomWebApplicationContext.class); 71 private List <ResourceCache> resourceCaches; 72 private String additionalClasspath; 73 private List <ResourceCache> reverseCaches; 74 private Map <String , ResourceCache> resourceCacheMap; 75 private ResourceCache mainWebappResourceCache; 76 private Map <String , CacheState> cacheState; 77 78 84 public CustomWebApplicationContext(boolean useDevConfig) throws Exception { 85 super("webapp"); 86 additionalClasspath = ""; 87 resourceCacheMap = new HashMap <String , ResourceCache>(); 88 reverseCaches = new ArrayList <ResourceCache>(); 89 cacheState = new HashMap <String , CacheState>(); 90 setContextPath("/"); 91 setDefaultsDescriptor("/com/sslexplorer/boot/webdefault.xml"); 92 setDisplayName("SSL-Explorer"); 93 setTempDirectory(new File ("tmp")); 94 95 setResourceAlias("/defaultStyle.css", "/css/defaultStyle.jsp"); 96 97 if (useDevConfig) { 98 setClassLoader(Main.class.getClassLoader()); 99 } 100 setWelcomeFiles(new String [] { "showHome.do" }); 101 setConfigurationClassNames(new String []{"org.mortbay.jetty.servlet.XMLConfiguration", "org.mortbay.jetty.servlet.JettyWebConfiguration", 102 "com.sslexplorer.server.jetty.JspPrecompileConfiguration" }); 103 resourceCaches = new ArrayList <ResourceCache>(); 104 } 105 106 111 public Collection <ResourceCache> getResourceCaches() { 112 return resourceCaches; 113 } 114 115 126 public void addResourceCache(ResourceCache cache) { 127 resourceCaches.add(cache); 128 reverseCaches.clear(); 129 cacheState.clear(); 130 reverseCaches.addAll(resourceCaches); 131 Collections.reverse(reverseCaches); 132 } 133 134 145 public void removeResourceCache(ResourceCache cache) { 146 resourceCaches.remove(cache); 147 reverseCaches.clear(); 148 cacheState.clear(); 149 reverseCaches.addAll(resourceCaches); 150 Collections.reverse(reverseCaches); 151 } 152 153 protected void addComponent(Object o) 154 { 155 if(o instanceof ResourceCache) { 156 mainWebappResourceCache = ((ResourceCache)o); 157 } 158 super.addComponent(o); 159 } 160 161 166 public Resource getResource(String pathInContext) throws IOException { 167 boolean fullResourceCache = System.getProperty("sslexplorer.fullResourceCache", 168 String.valueOf(!(System.getProperty("sslexplorer.useDevConfig", "false").equals("true")))).equals("true"); 169 Resource r; 170 if (log.isDebugEnabled()) 171 log.debug("Request for " + pathInContext); 172 173 if(pathInContext.indexOf("//WEB-INF") != -1) { 175 return null; 176 } 177 178 182 if(fullResourceCache && cacheState.containsKey(pathInContext)) { 183 r = cacheState.get(pathInContext).getResource(); 184 if (log.isDebugEnabled()) 185 if(r == null) 186 log.debug("Resource " + pathInContext + " is permanently as missing."); 187 else 188 log.debug("Resource " + pathInContext + " found in permanent cache"); 189 return r; 190 } 191 192 197 198 ResourceCache o = fullResourceCache ? null : (ResourceCache) resourceCacheMap.get(pathInContext); 199 if (o == null) { 200 201 207 if (log.isDebugEnabled()) 208 log.debug("Resource " + pathInContext + " not found in any resource cache, checking in plugins"); 209 210 for (Iterator i = reverseCaches.iterator();i.hasNext();) { 211 ResourceCache cache = (ResourceCache)i.next(); 212 r = cache.getResource(pathInContext); 213 if(r != null && r.exists() && !r.isDirectory()) { 214 if(fullResourceCache) { 215 if (log.isDebugEnabled()) 216 log.debug(" Found in " + cache.getBaseResource().toString()); 217 cacheState.put(pathInContext, new CacheState(CacheState.FOUND, pathInContext, r)); 218 } 219 else { 220 if (log.isDebugEnabled()) 221 log.debug(" Found in " + cache.getBaseResource().toString()); 222 resourceCacheMap.put(pathInContext, cache); 223 } 224 return r; 225 } 226 } 227 228 231 if (log.isDebugEnabled()) 232 log.debug(" Not found"); 233 } else { 234 238 r = o.getResource(pathInContext); 239 if (r != null && r.exists() && !r.isDirectory() ) { 240 if (log.isDebugEnabled()) 241 log.debug(" Found in " + o.getBaseResource().toString()); 242 return r; 243 } 244 } 245 246 if (log.isDebugEnabled()) 247 log.debug("Checking for alias in plugins"); 248 String resourceAlias = getResourceAlias(pathInContext); 249 if (resourceAlias != null) { 250 251 256 257 if (log.isDebugEnabled()) 258 log.debug(" Found alias of " + resourceAlias + ", checking in plugins"); 259 for (Iterator i = reverseCaches.iterator();i.hasNext();) { 260 ResourceCache cache = (ResourceCache)i.next(); 261 r = cache.getResource(resourceAlias); 262 263 267 268 if(r !=null && r.exists() && !r.isDirectory()) { 269 if(fullResourceCache) { 270 if (log.isDebugEnabled()) 271 log.debug(" Found in " + cache.getBaseResource().toString()); 272 cacheState.put(pathInContext, new CacheState(CacheState.FOUND, pathInContext, r)); 273 return r; 274 } 275 else { 276 if (log.isDebugEnabled()) 277 log.debug(" Found in " + cache.getBaseResource().toString()); 278 resourceCacheMap.put(pathInContext, cache); 279 return r; 280 } 281 } 282 } 283 if (log.isDebugEnabled()) 284 log.debug(" Not found"); 285 } 286 287 291 292 if (log.isDebugEnabled()) 293 log.debug("Passing to main webapp"); 294 r = super.getResource(pathInContext); 295 if(r != null && r.exists() && !r.isDirectory()) { 296 297 301 302 if (log.isDebugEnabled()) 303 log.debug(" Found in main webapp"); 304 305 if(fullResourceCache) { 306 cacheState.put(pathInContext, new CacheState(CacheState.FOUND, pathInContext, r)); 307 } 308 else { 309 resourceCacheMap.put(pathInContext, mainWebappResourceCache); 310 } 311 return r; 312 } 313 else { 314 if(fullResourceCache) { 315 if (log.isDebugEnabled()) 316 log.debug(" Not found, caching as missing"); 317 cacheState.put(pathInContext, new CacheState(CacheState.MISSING, pathInContext, r)); 318 } 319 } 320 321 322 323 if (log.isDebugEnabled()) 324 log.debug(" Found in main webapp"); 325 326 return r; 327 } 328 329 344 public void addContextLoaderURL(URL url) { 345 if (url.getProtocol().equals("file")) { 346 additionalClasspath = additionalClasspath 347 + (additionalClasspath.length() != 0 ? System.getProperty("path.separator") : "") + url.getFile(); 348 } 349 doAddContextLoaderURL(url); 350 } 351 352 private void doAddContextLoaderURL(URL u) { 353 try { 354 URLClassLoader sysloader = (URLClassLoader ) getClassLoader(); 355 Class sysclass = URLClassLoader .class; 356 Method method = sysclass.getDeclaredMethod("addURL", new Class [] { URL .class }); 357 method.setAccessible(true); 358 method.invoke(sysloader, new Object [] { u }); 359 if(log.isInfoEnabled()) 360 log.info(u.toExternalForm() + " added to context classloader"); 361 } catch (Exception e) { 362 log.error("Failed to add to classpath.", e); 363 } 364 } 365 } | Popular Tags |