1 7 package org.jboss.web.tomcat.tc5; 8 9 import java.beans.PropertyChangeListener ; 10 import java.net.URL ; 11 import java.net.MalformedURLException ; 12 import java.net.URLClassLoader ; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.lang.reflect.Method ; 16 import java.util.ArrayList ; 17 import java.util.Enumeration ; 18 import javax.servlet.ServletContext ; 19 20 import org.apache.catalina.Lifecycle; 21 import org.apache.catalina.Loader; 22 import org.apache.catalina.LifecycleListener; 23 import org.apache.catalina.LifecycleException; 24 import org.apache.catalina.Container; 25 import org.apache.catalina.Context; 26 import org.apache.catalina.Globals; 27 import org.apache.naming.resources.DirContextURLStreamHandler; 28 29 import org.jboss.mx.loading.RepositoryClassLoader; 30 31 37 public class WebCtxLoader 38 implements Lifecycle, Loader 39 { 40 43 protected ClassLoader encLoader; 44 47 protected ENCLoader ctxLoader; 48 51 protected RepositoryClassLoader delegate; 52 protected Container webContainer; 53 protected URL warURL; 54 55 58 private ArrayList repositories = new ArrayList (); 59 60 65 WebCtxLoader(ClassLoader encLoader) 66 { 67 this.encLoader = encLoader; 68 this.ctxLoader = new ENCLoader(encLoader); 69 ClassLoader parent = encLoader; 70 while ((parent instanceof RepositoryClassLoader) == false && parent != null) 71 parent = parent.getParent(); 72 this.delegate = (RepositoryClassLoader) parent; 73 } 74 75 public void setWarURL(URL warURL) throws MalformedURLException 76 { 77 this.warURL = warURL; 78 String path = warURL.getFile(); 79 File classesDir = new File (path, "WEB-INF/classes"); 80 if (classesDir.exists()) 81 { 82 delegate.addURL(classesDir.toURL()); 83 ctxLoader.addURLInternal(classesDir.toURL()); 84 } 85 File libDir = new File (path, "WEB-INF/lib"); 86 if (libDir.exists()) 87 { 88 File [] jars = libDir.listFiles(); 89 int length = jars != null ? jars.length : 0; 90 for (int j = 0; j < length; j++) 91 { 92 delegate.addURL(jars[j].toURL()); 93 ctxLoader.addURLInternal(jars[j].toURL()); 94 } 95 } 96 } 97 98 public void addLifecycleListener(LifecycleListener listener) 99 { 100 } 101 102 public LifecycleListener[] findLifecycleListeners() 103 { 104 return new LifecycleListener[0]; 105 } 106 107 public void removeLifecycleListener(LifecycleListener listener) 108 { 109 } 110 111 public void start() throws LifecycleException 112 { 113 setClassPath(); 114 ServletContext servletContext = ((Context ) webContainer).getServletContext(); 115 if (servletContext == null) 116 return; 117 } 118 119 public void stop() throws LifecycleException 120 { 121 DirContextURLStreamHandler.unbind(ctxLoader); 123 org.apache.commons.logging.LogFactory.release(ctxLoader); 124 org.apache.commons.logging.LogFactory.release(encLoader); 125 this.encLoader = null; 126 this.ctxLoader = null; 127 this.delegate = null; 128 this.repositories.clear(); 129 this.warURL = null; 130 this.webContainer = null; 131 } 132 133 public void backgroundProcess() 134 { 135 } 137 138 147 public ClassLoader getClassLoader() 148 { 149 return ctxLoader; 150 } 151 152 public Container getContainer() 153 { 154 return webContainer; 155 } 156 157 public void setContainer(Container container) 158 { 159 webContainer = container; 160 161 } 162 163 public boolean getDelegate() 164 { 165 return false; 166 } 167 168 public void setDelegate(boolean delegate) 169 { 170 } 171 172 public String getInfo() 173 { 174 return null; 175 } 176 177 public boolean getReloadable() 178 { 179 return false; 180 } 181 182 public void setReloadable(boolean reloadable) 183 { 184 } 185 186 public void addPropertyChangeListener(PropertyChangeListener listener) 187 { 188 } 189 190 public void addRepository(String repository) 191 { 192 if (repositories.contains(repository) == true) 193 return; 194 repositories.add(repository); 195 setClassPath(); 196 } 197 198 public String [] findRepositories() 199 { 200 String [] tmp = new String [repositories.size()]; 201 repositories.toArray(tmp); 202 return tmp; 203 } 204 205 public boolean modified() 206 { 207 return false; 208 } 209 210 public void removePropertyChangeListener(PropertyChangeListener listener) 211 { 212 } 213 214 218 private void setClassPath() 219 { 220 if (!(webContainer instanceof Context )) 222 return; 223 ServletContext servletContext = ((Context ) webContainer).getServletContext(); 224 if (servletContext == null) 225 return; 226 227 try 228 { 229 Method method = 230 webContainer.getClass().getMethod("getCompilerClasspath", null); 231 Object baseClasspath = method.invoke(webContainer, null); 232 if (baseClasspath != null) 233 { 234 servletContext.setAttribute(Globals.CLASS_PATH_ATTR, 235 baseClasspath.toString()); 236 return; 237 } 238 } 239 catch (Exception e) 240 { 241 e.printStackTrace(); 243 } 244 245 StringBuffer classpath = new StringBuffer (); 246 247 for (int i = 0; i < repositories.size(); i++) 249 { 250 String repository = repositories.get(i).toString(); 251 if (repository.startsWith("file://")) 252 repository = repository.substring(7); 253 else if (repository.startsWith("file:")) 254 repository = repository.substring(5); 255 else if (repository.startsWith("jndi:")) 256 repository = servletContext.getRealPath(repository.substring(5)); 257 else 258 continue; 259 if (repository == null) 260 continue; 261 if (i > 0) 262 classpath.append(File.pathSeparator); 263 classpath.append(repository); 264 } 265 266 servletContext.setAttribute(Globals.CLASS_PATH_ATTR, 268 classpath.toString()); 269 270 } 271 272 276 static class ENCLoader extends URLClassLoader 277 { 278 private URL [] urllist = new URL [0]; 279 280 ENCLoader(ClassLoader parent) 281 { 282 super(new URL [0], parent); 283 } 284 285 void addURLInternal(URL url) 286 { 287 URL [] result = new URL [urllist.length + 1]; 288 for (int i = 0; i < urllist.length; i++) 289 result[i] = urllist[i]; 290 result[urllist.length] = url; 291 urllist = result; 292 } 293 294 public URL [] getURLs() 295 { 296 return urllist; 297 } 298 } 299 } 300 | Popular Tags |