1 22 package org.jboss.web.tomcat.tc6; 23 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.lang.reflect.Method ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.servlet.ServletContext ; 34 35 import org.apache.catalina.Container; 36 import org.apache.catalina.Context; 37 import org.apache.catalina.Globals; 38 import org.apache.catalina.Lifecycle; 39 import org.apache.catalina.LifecycleException; 40 import org.apache.catalina.LifecycleListener; 41 import org.apache.catalina.Loader; 42 import org.apache.naming.resources.DirContextURLStreamHandler; 43 import org.jboss.mx.loading.RepositoryClassLoader; 44 import org.jboss.logging.Logger; 45 46 52 public class WebCtxLoader 53 implements Lifecycle, Loader 54 { 55 private static final Logger log = Logger.getLogger(WebCtxLoader.class); 56 59 protected ClassLoader encLoader; 60 63 protected ENCLoader ctxLoader; 64 67 protected RepositoryClassLoader delegate; 68 protected Container webContainer; 69 protected URL warURL; 70 protected TomcatInjectionContainer injectionContainer; 71 72 75 private ArrayList repositories = new ArrayList (); 76 77 82 public WebCtxLoader(ClassLoader encLoader) 83 { 84 this(encLoader, null); 85 } 86 public WebCtxLoader(ClassLoader encLoader, TomcatInjectionContainer container) 87 { 88 this.encLoader = encLoader; 89 this.ctxLoader = new ENCLoader(encLoader); 90 ClassLoader parent = encLoader; 91 while ((parent instanceof RepositoryClassLoader) == false && parent != null) 92 parent = parent.getParent(); 93 this.delegate = (RepositoryClassLoader) parent; 94 injectionContainer = container; 95 } 96 97 102 public void setClasspath(List <URL > classpath) 103 { 104 for(URL path : classpath) 105 { 106 delegate.addURL(path); 107 ctxLoader.addURLInternal(path); 108 } 109 } 110 111 117 public void setWarURL(URL warURL) throws MalformedURLException 118 { 119 this.warURL = warURL; 120 String path = warURL.getFile(); 121 File classesDir = new File (path, "WEB-INF/classes"); 122 if (classesDir.exists()) 123 { 124 delegate.addURL(classesDir.toURL()); 125 ctxLoader.addURLInternal(classesDir.toURL()); 126 } 127 File libDir = new File (path, "WEB-INF/lib"); 128 if (libDir.exists()) 129 { 130 File [] jars = libDir.listFiles(); 131 int length = jars != null ? jars.length : 0; 132 for (int j = 0; j < length; j++) 133 { 134 File jar = jars[j]; 135 if(jar.getAbsolutePath().endsWith(".jar")) 136 { 137 delegate.addURL(jar.toURL()); 138 ctxLoader.addURLInternal(jar.toURL()); 139 } 140 } 141 } 142 } 143 144 public void addLifecycleListener(LifecycleListener listener) 145 { 146 } 147 148 public LifecycleListener[] findLifecycleListeners() 149 { 150 return new LifecycleListener[0]; 151 } 152 153 public void removeLifecycleListener(LifecycleListener listener) 154 { 155 } 156 157 public void start() throws LifecycleException 158 { 159 if (this.ctxLoader == null) 161 throw new LifecycleException("WebCtxLoader cannot be restarted"); 162 163 setClassPath(); 164 if (injectionContainer != null) 165 { 166 log.debug("injectionContainer enabled and processing beginning with JBoss WebCtxLoader"); 167 injectionContainer.setClassLoader(getClassLoader()); 170 injectionContainer.processMetadata(); 171 } 172 ServletContext servletContext = ((Context ) webContainer).getServletContext(); 173 if (servletContext == null) 174 return; 175 } 176 177 public void stop() throws LifecycleException 178 { 179 DirContextURLStreamHandler.unbind(ctxLoader); 181 org.apache.commons.logging.LogFactory.release(ctxLoader); 182 org.apache.commons.logging.LogFactory.release(encLoader); 183 this.encLoader = null; 184 this.ctxLoader = null; 185 this.delegate = null; 186 this.repositories.clear(); 187 this.warURL = null; 188 this.webContainer = null; 189 } 190 191 public void backgroundProcess() 192 { 193 } 195 196 205 public ClassLoader getClassLoader() 206 { 207 return ctxLoader; 208 } 209 210 public Container getContainer() 211 { 212 return webContainer; 213 } 214 215 public void setContainer(Container container) 216 { 217 webContainer = container; 218 219 } 220 221 public boolean getDelegate() 222 { 223 return false; 224 } 225 226 public void setDelegate(boolean delegate) 227 { 228 } 229 230 public String getInfo() 231 { 232 return null; 233 } 234 235 public boolean getReloadable() 236 { 237 return false; 238 } 239 240 public void setReloadable(boolean reloadable) 241 { 242 } 243 244 public void addPropertyChangeListener(PropertyChangeListener listener) 245 { 246 } 247 248 public void addRepository(String repository) 249 { 250 if (repositories.contains(repository) == true) 251 return; 252 repositories.add(repository); 253 setClassPath(); 254 } 255 256 public String [] findRepositories() 257 { 258 String [] tmp = new String [repositories.size()]; 259 repositories.toArray(tmp); 260 return tmp; 261 } 262 263 public boolean modified() 264 { 265 return false; 266 } 267 268 public void removePropertyChangeListener(PropertyChangeListener listener) 269 { 270 } 271 272 276 private void setClassPath() 277 { 278 if (!(webContainer instanceof Context )) 280 return; 281 ServletContext servletContext = ((Context ) webContainer).getServletContext(); 282 if (servletContext == null) 283 return; 284 285 try 286 { 287 Method method = 288 webContainer.getClass().getMethod("getCompilerClasspath", null); 289 Object baseClasspath = method.invoke(webContainer, null); 290 if (baseClasspath != null) 291 { 292 servletContext.setAttribute(Globals.CLASS_PATH_ATTR, 293 baseClasspath.toString()); 294 return; 295 } 296 } 297 catch (Exception e) 298 { 299 e.printStackTrace(); 301 } 302 303 StringBuffer classpath = new StringBuffer (); 304 305 for (int i = 0; i < repositories.size(); i++) 307 { 308 String repository = repositories.get(i).toString(); 309 if (repository.startsWith("file://")) 310 repository = repository.substring(7); 311 else if (repository.startsWith("file:")) 312 repository = repository.substring(5); 313 else if (repository.startsWith("jndi:")) 314 repository = servletContext.getRealPath(repository.substring(5)); 315 else 316 continue; 317 if (repository == null) 318 continue; 319 if (i > 0) 320 classpath.append(File.pathSeparator); 321 classpath.append(repository); 322 } 323 324 servletContext.setAttribute(Globals.CLASS_PATH_ATTR, 326 classpath.toString()); 327 328 } 329 330 334 static class ENCLoader extends URLClassLoader 335 { 336 private URL [] urllist = new URL [0]; 337 338 ENCLoader(ClassLoader parent) 339 { 340 super(new URL [0], parent); 341 } 342 343 void addURLInternal(URL url) 344 { 345 URL [] result = new URL [urllist.length + 1]; 346 for (int i = 0; i < urllist.length; i++) 347 result[i] = urllist[i]; 348 result[urllist.length] = url; 349 urllist = result; 350 } 351 352 public URL [] getURLs() 353 { 354 return urllist; 355 } 356 } 357 } 358 | Popular Tags |