1 22 package org.jboss.ejb3.deployers; 23 24 import java.io.InputStream ; 25 import java.util.jar.Attributes ; 26 import java.util.jar.JarFile ; 27 import java.util.jar.Manifest ; 28 29 import javax.management.MBeanServer ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.NamingException ; 34 35 import org.jboss.deployers.plugins.deployers.helpers.AbstractSimpleRealDeployer; 36 import org.jboss.deployers.spi.DeploymentException; 37 import org.jboss.deployers.spi.deployer.DeploymentUnit; 38 import org.jboss.ejb3.KernelAbstraction; 39 import org.jboss.ejb3.MCKernelAbstraction; 40 import org.jboss.ejb3.clientmodule.ClientENCInjectionContainer; 41 import org.jboss.ejb3.metamodel.ApplicationClientDD; 42 import org.jboss.kernel.Kernel; 43 import org.jboss.logging.Logger; 44 import org.jboss.naming.Util; 45 import org.jboss.virtual.VirtualFile; 46 47 53 public class Ejb3ClientDeployer extends AbstractSimpleRealDeployer<ApplicationClientDD> 54 { 55 private static final Logger log = Logger.getLogger(Ejb3ClientDeployer.class); 56 57 private Kernel kernel; 58 private MBeanServer server; 59 60 public Ejb3ClientDeployer() 61 { 62 super(ApplicationClientDD.class); 63 } 64 65 @Override 66 public void deploy(DeploymentUnit unit, ApplicationClientDD metaData) throws DeploymentException 67 { 68 log.debug("deploy " + unit.getName()); 69 70 String appClientName = getJndiName(unit, metaData); 71 72 try 73 { 74 InitialContext iniCtx = new InitialContext (); 76 Context encCtx = Util.createSubcontext(iniCtx, appClientName); 77 log.debug("Creating client ENC binding under: " + appClientName); 78 79 String mainClassName = getMainClassName(unit, true); 80 81 Class <?> mainClass = loadClass(unit, mainClassName); 82 83 ClientENCInjectionContainer container = new ClientENCInjectionContainer(unit, metaData, mainClass, appClientName, unit.getClassLoader(), encCtx); 84 85 unit.addAttachment(ClientENCInjectionContainer.class, container); 87 getKernelAbstraction().install(container.getObjectName().getCanonicalName(), container.getDependencyPolicy(), container); 88 } 89 catch(Exception e) 90 { 91 log.error("Could not deploy " + unit.getName(), e); 92 undeploy(unit, metaData); 93 throw new DeploymentException("Could not deploy " + unit.getName(), e); 94 } 95 } 96 97 105 private String getJndiName(DeploymentUnit unit, ApplicationClientDD dd) 106 { 107 String jndiName = dd.getJndiName(); 108 if(jndiName != null) 109 return jndiName; 110 111 String shortName = unit.getDeploymentContext().getRoot().getName(); 112 if(shortName.endsWith(".jar/")) 113 jndiName = shortName.substring(0, shortName.length() - 5); 114 else if(shortName.endsWith(".jar")) 115 jndiName = shortName.substring(0, shortName.length() - 4); 116 else 117 throw new IllegalStateException ("Expected either '.jar' or '.jar/' at the end of " + shortName); 118 119 return jndiName; 120 } 121 122 127 private KernelAbstraction getKernelAbstraction() 128 { 129 return new MCKernelAbstraction(kernel, server); 130 } 131 132 protected String getMainClassName(DeploymentUnit unit, boolean fail) throws Exception 133 { 134 VirtualFile file = unit.getMetaDataFile("MANIFEST.MF"); 135 log.trace("parsing " + file); 136 String mainClassName = "org.jboss.client.AppClientMain"; 138 139 if(file == null) 140 { 141 return mainClassName; 142 } 143 144 try 145 { 146 InputStream is = file.openStream(); 147 Manifest mf; 148 try 149 { 150 mf = new Manifest (is); 151 } 152 finally 153 { 154 is.close(); 155 } 156 Attributes attrs = mf.getMainAttributes(); 157 String className = attrs.getValue(Attributes.Name.MAIN_CLASS); 158 if(className != null) 159 { 160 mainClassName = className; 161 } 162 163 return mainClassName; 164 } 165 finally 166 { 167 file.close(); 168 } 169 } 170 171 private Class <?> loadClass(DeploymentUnit unit, String className) throws ClassNotFoundException 172 { 173 ClassLoader old = Thread.currentThread().getContextClassLoader(); 174 try 175 { 176 Thread.currentThread().setContextClassLoader(unit.getClassLoader()); 177 return Thread.currentThread().getContextClassLoader().loadClass(className); 178 } 179 finally 180 { 181 Thread.currentThread().setContextClassLoader(old); 182 } 183 } 184 185 public void setKernel(Kernel kernel) 186 { 187 this.kernel = kernel; 188 } 189 190 public void setMbeanServer(MBeanServer server) 191 { 192 this.server = server; 193 } 194 195 @Override 196 public void undeploy(DeploymentUnit unit, ApplicationClientDD metaData) 197 { 198 log.debug("undeploy " + unit.getName()); 199 200 ClientENCInjectionContainer container = unit.getAttachment(ClientENCInjectionContainer.class); 201 if(container != null) 202 getKernelAbstraction().uninstall(container.getObjectName().getCanonicalName()); 203 204 String jndiName = getJndiName(unit, metaData); 205 log.debug("Removing client ENC from: " + jndiName); 206 try 207 { 208 InitialContext iniCtx = new InitialContext (); 209 Util.unbind(iniCtx, jndiName); 210 } 211 catch(NameNotFoundException e) 212 { 213 log.debug("Could not find client ENC"); 215 } 216 catch (NamingException e) 217 { 218 log.error("Failed to remove client ENC", e); 219 } 220 } 221 222 } 223 | Popular Tags |