1 22 package org.jboss.web.tomcat.tc6.deployers; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import org.apache.catalina.Loader; 30 import org.jboss.deployers.plugins.deployer.AbstractSimpleDeployer; 31 import org.jboss.deployers.spi.DeploymentException; 32 import org.jboss.deployers.spi.classloader.ClassLoaderFactory; 33 import org.jboss.deployers.spi.deployer.DeploymentUnit; 34 import org.jboss.deployers.spi.structure.DeploymentContext; 35 import org.jboss.metadata.WebMetaData; 36 import org.jboss.virtual.VirtualFile; 37 import org.jboss.web.tomcat.tc6.TomcatInjectionContainer; 38 import org.jboss.web.tomcat.tc6.WebAppLoader; 39 import org.jboss.web.tomcat.tc6.WebCtxLoader; 40 41 49 public class WarClassLoaderDeployer extends AbstractSimpleDeployer 50 implements ClassLoaderFactory 51 { 52 53 private boolean java2ClassLoadingCompliance = false; 54 private boolean useJBossWebLoader; 55 56 private String [] filteredPackages; 57 58 public WarClassLoaderDeployer() 59 { 60 super.setRelativeOrder(CLASSLOADER_DEPLOYER - 1); 62 } 63 64 public boolean isJava2ClassLoadingCompliance() 65 { 66 return java2ClassLoadingCompliance; 67 } 68 public void setJava2ClassLoadingCompliance(boolean flag) 69 { 70 this.java2ClassLoadingCompliance = flag; 71 } 72 73 public String [] getFilteredPackages() 74 { 75 return filteredPackages; 76 } 77 public void setFilteredPackages(String [] pkgs) 78 { 79 this.filteredPackages = pkgs; 80 } 81 82 public boolean isUseJBossWebLoader() 83 { 84 return useJBossWebLoader; 85 } 86 public void setUseJBossWebLoader(boolean useJBossWebLoader) 87 { 88 this.useJBossWebLoader = useJBossWebLoader; 89 } 90 91 92 @Override 93 public void deploy(DeploymentUnit unit) throws DeploymentException 94 { 95 WebMetaData metaData = unit.getAttachment(WebMetaData.class); 96 if( metaData != null ) 97 { 98 unit.addAttachment(ClassLoaderFactory.class, this); 99 } 100 } 101 102 public ClassLoader createClassLoader(DeploymentContext context) throws Exception 103 { 104 ClassLoader loader = null; 105 try 106 { 107 DeploymentUnit unit = context.getDeploymentUnit(); 108 WebMetaData metaData = unit.getAttachment(WebMetaData.class); 109 loader = createClassLoader(metaData, context); 110 if( loader != null ) 111 context.setClassLoader(loader); 112 } 113 catch(Exception e) 114 { 115 log.warn("", e); 116 } 117 return loader; 118 } 119 120 public void removeClassLoader(DeploymentContext context) throws Exception 121 { 122 123 } 124 125 128 protected ClassLoader createClassLoader(WebMetaData metaData, DeploymentContext context) 129 throws Exception 130 { 131 ClassLoader loader = null; 132 if (metaData != null) 133 { 134 ClassLoader parent = context.getClassLoader(); 135 if( parent == null ) 136 parent = context.getTopLevel().getClassLoader(); 137 if( parent == null ) 138 parent = Thread.currentThread().getContextClassLoader(); 139 Loader webLoader = getWebLoader(parent, metaData, context.getClassPath()); 140 context.getDeploymentUnit().addAttachment(Loader.class, webLoader); 141 loader = webLoader.getClassLoader(); 142 } 143 return loader; 144 } 145 146 155 protected Loader getWebLoader(ClassLoader loader, WebMetaData metaData, 156 List <VirtualFile> classpath) 157 throws Exception 158 { 159 Loader webLoader = null; 160 TomcatInjectionContainer injectionContainer = null; 161 if (useJBossWebLoader) 162 { 163 WebCtxLoader jbossLoader = new WebCtxLoader(loader, injectionContainer); 164 ArrayList <URL > cp = new ArrayList <URL >(); 165 for(VirtualFile vf : classpath) 166 { 167 try 168 { 169 URL path = vf.toURL(); 170 cp.add(path); 171 } 172 catch(Exception e) 173 { 174 log.debug("Ignoring path element: "+vf, e); 175 } 176 } 177 jbossLoader.setClasspath(cp); 178 webLoader = jbossLoader; 179 } 180 else 181 { 182 WebAppLoader jbossLoader = new WebAppLoader(loader, filteredPackages, injectionContainer); 183 jbossLoader.setDelegate(java2ClassLoadingCompliance); 184 jbossLoader.start(); 185 webLoader = jbossLoader; 186 } 187 return webLoader; 188 } 189 190 } 191 | Popular Tags |