1 22 package org.jboss.deployment; 23 24 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 import javax.management.MBeanServer ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.LinkRef ; 37 import javax.naming.NamingException ; 38 39 import org.jboss.deployers.plugins.deployer.AbstractSimpleDeployer; 40 import org.jboss.deployers.spi.DeploymentException; 41 import org.jboss.deployers.spi.classloader.ClassLoaderFactory; 42 import org.jboss.deployers.spi.deployer.DeploymentUnit; 43 import org.jboss.deployers.spi.structure.DeploymentContext; 44 import org.jboss.ejb.EjbUtil50; 45 import org.jboss.metadata.ClientMetaData; 46 import org.jboss.metadata.EjbRefMetaData; 47 import org.jboss.metadata.EnvEntryMetaData; 48 import org.jboss.metadata.ResourceEnvRefMetaData; 49 import org.jboss.metadata.ResourceRefMetaData; 50 import org.jboss.metadata.XmlFileLoader; 51 import org.jboss.naming.Util; 52 import org.jboss.util.xml.DOMUtils; 53 import org.jboss.virtual.VirtualFile; 54 import org.jboss.webservice.ServiceRefHandler; 55 import org.jboss.webservice.ServiceRefHandlerFactory; 56 import org.w3c.dom.Element ; 57 58 65 public class ClientDeployer extends AbstractSimpleDeployer 66 { 67 private static final String APPLICATION_CLIENT_XML = "application-client.xml"; 68 private static final String JBOSS_CLIENT_XML = "jboss-client.xml"; 69 70 private MBeanServer mbeanServer; 71 72 public MBeanServer getMbeanServer() 73 { 74 return mbeanServer; 75 } 76 77 public void setMbeanServer(MBeanServer server) 78 { 79 this.mbeanServer = server; 80 } 81 82 86 private boolean accepts(DeploymentUnit unit) throws DeploymentException 87 { 88 boolean accepts = false; 89 90 VirtualFile dd = unit.getMetaDataFile(APPLICATION_CLIENT_XML); 92 if (dd != null) 93 { 94 log.debug("Found application-client.xml file: " + unit.getName()); 95 try 96 { 97 Element root = DOMUtils.parse(dd.openStream()); 98 String namespaceURI = root.getNamespaceURI(); 99 accepts = "http://java.sun.com/xml/ns/j2ee".equals(namespaceURI); 101 if (accepts == false) 102 log.debug("Ignore application-client.xml with namespace: " + namespaceURI); 103 } 104 catch (IOException ex) 105 { 106 DeploymentException.rethrowAsDeploymentException("Cannot parse " + APPLICATION_CLIENT_XML, ex); 107 } 108 } 109 110 return accepts; 111 } 112 113 @Override 114 public void deploy(DeploymentUnit unit) throws org.jboss.deployers.spi.DeploymentException 115 { 116 if (accepts(unit)) 117 { 118 unit.createClassLoader(new RootURLClassLoaderFactory(unit)); 120 121 ClientMetaData metaData = buildClientMetaData(unit); 123 unit.addAttachment(ClientMetaData.class, metaData); 124 125 try 126 { 127 setupEnvironment(unit, metaData); 128 } 129 catch (Exception ex) 130 { 131 DeploymentException.rethrowAsDeploymentException("Failed to setup client ENC", ex); 132 } 133 } 134 } 135 136 @Override 137 public void undeploy(DeploymentUnit unit) 138 { 139 ClientMetaData metaData = unit.getAttachment(ClientMetaData.class); 141 if (metaData != null) 142 { 143 String appClientName = metaData.getJndiName(); 144 log.info("Removing client ENC from: " + appClientName); 145 try 146 { 147 InitialContext iniCtx = new InitialContext (); 148 Util.unbind(iniCtx, appClientName); 149 } 150 catch (NamingException ex) 151 { 152 log.error("Failed to remove client ENC", ex); 153 } 154 } 155 } 156 157 private ClientMetaData buildClientMetaData(DeploymentUnit unit) throws DeploymentException 158 { 159 ClientMetaData metaData = null; 160 try 161 { 162 VirtualFile appClientVF = unit.getMetaDataFile(APPLICATION_CLIENT_XML); 163 164 if (appClientVF == null) 165 throw new DeploymentException("No application-client.xml found"); 166 167 InputStream in = appClientVF.openStream(); 168 XmlFileLoader xfl = new XmlFileLoader(true); 169 Element appClient = xfl.getDocument(in, "application-client.xml").getDocumentElement(); 170 in.close(); 171 metaData = new ClientMetaData(); 172 metaData.setResourceClassLoader((URLClassLoader )unit.getClassLoader()); 173 metaData.importClientXml(appClient); 174 175 VirtualFile jbossClientVF = unit.getMetaDataFile(JBOSS_CLIENT_XML); 177 if( jbossClientVF != null ) 178 { 179 in = jbossClientVF.openStream(); 180 if (in != null) 181 { 182 xfl = new XmlFileLoader(true); 183 Element jbossClient = xfl.getDocument(in, "jboss-client.xml").getDocumentElement(); 184 in.close(); 185 metaData.importJbossClientXml(jbossClient); 186 } 187 } 188 } 189 catch (IOException e) 190 { 191 throw new DeploymentException("Failed to parse metadata", e); 192 } 193 return metaData; 194 } 195 196 private void setupEnvironment(DeploymentUnit unit, ClientMetaData metaData) throws Exception 197 { 198 String appClientName = metaData.getJndiName(); 200 InitialContext iniCtx = new InitialContext (); 201 Context envCtx = Util.createSubcontext(iniCtx, appClientName); 202 log.debug("Creating client ENC binding under: " + appClientName); 203 Iterator i = metaData.getEnvironmentEntries().iterator(); 205 while (i.hasNext()) 206 { 207 EnvEntryMetaData entry = (EnvEntryMetaData)i.next(); 208 log.debug("Binding env-entry: " + entry.getName() + " of type: " + entry.getType() + " to value:" + entry.getValue()); 209 EnvEntryMetaData.bindEnvEntry(envCtx, entry); 210 } 211 212 HashMap ejbRefs = metaData.getEjbReferences(); 214 i = ejbRefs.values().iterator(); 215 while (i.hasNext()) 216 { 217 EjbRefMetaData ref = (EjbRefMetaData)i.next(); 218 log.debug("Binding an EJBReference " + ref.getName()); 219 220 if (ref.getLink() != null) 221 { 222 String linkName = ref.getLink(); 224 String jndiName = EjbUtil50.findEjbLink(mbeanServer, unit, linkName); 225 226 log.debug("Binding " + ref.getName() + " to ejb-link: " + linkName + " -> " + jndiName); 227 if (jndiName == null) 228 { 229 String msg = "Failed to resolve ejb-link: " + linkName + " make by ejb-name: " + ref.getName(); 230 throw new DeploymentException(msg); 231 } 232 log.debug("Link resolved to:" + jndiName); 233 Util.bind(envCtx, ref.getName(), new LinkRef (jndiName)); 234 } 235 else 236 { 237 if (ref.getJndiName() == null) 239 { 240 throw new DeploymentException("ejb-ref " + ref.getName() + ", expected either ejb-link in ejb-jar.xml " + "or jndi-name in jboss.xml"); 241 } 242 log.debug("Binding " + ref.getName() + " to : " + ref.getJndiName()); 243 Util.bind(envCtx, ref.getName(), new LinkRef (ref.getJndiName())); 244 } 245 } 246 247 Iterator serviceRefs = metaData.getServiceReferences().values().iterator(); 249 ServiceRefHandler refHandler = ServiceRefHandlerFactory.newInstance(); 250 if (refHandler != null && serviceRefs.hasNext()) 251 refHandler.setupServiceRefEnvironment(envCtx, serviceRefs, unit); 252 253 HashMap resRefs = metaData.getResourceReferences(); 255 i = resRefs.values().iterator(); 256 while (i.hasNext()) 257 { 258 ResourceRefMetaData ref = (ResourceRefMetaData)i.next(); 259 String refName = ref.getRefName(); 260 String jndiName = ref.getJndiName(); 261 262 if (ref.getType().equals("java.net.URL")) 263 { 264 String resURL = ref.getResURL(); 266 if (resURL != null) 267 { 268 log.debug("Binding '" + refName + "' to URL: " + resURL); 269 URL url = new URL (resURL); 270 Util.bind(envCtx, refName, url); 271 } 272 else 273 { 274 log.debug("Linking '" + refName + "' to URL: " + resURL); 275 LinkRef urlLink = new LinkRef (jndiName); 276 Util.bind(envCtx, refName, urlLink); 277 } 278 } 279 else 280 { 281 log.debug("Binding resource: " + refName + " to JDNI as: " + jndiName); 283 Util.bind(envCtx, refName, new LinkRef (jndiName)); 284 } 285 } 286 287 HashMap envRefs = metaData.getResourceEnvReferences(); 289 i = envRefs.values().iterator(); 290 while (i.hasNext()) 291 { 292 ResourceEnvRefMetaData resRef = (ResourceEnvRefMetaData)i.next(); 293 String encName = resRef.getRefName(); 294 String jndiName = resRef.getJndiName(); 295 log.debug("Binding env resource: " + encName + " to JDNI as: " + jndiName); 297 Util.bind(envCtx, encName, new LinkRef (jndiName)); 298 } 299 log.info("Client ENC bound under: " + appClientName); 300 } 301 302 static class RootURLClassLoaderFactory implements ClassLoaderFactory 303 { 304 URL rootURL; 305 306 RootURLClassLoaderFactory(DeploymentUnit unit) 307 { 308 try 309 { 310 rootURL = unit.getDeploymentContext().getRoot().toURL(); 311 } 312 catch (Exception ex) 313 { 314 } 316 } 317 318 public ClassLoader createClassLoader(DeploymentContext context) throws Exception 319 { 320 return new URLClassLoader (new URL [] {rootURL}); 321 } 322 323 public void removeClassLoader(DeploymentContext context) throws Exception 324 { 325 } 327 } 328 } 329 | Popular Tags |