KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > ClientDeployer


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment;
23
24 // $Id: ClientDeployer.java 58275 2006-11-12 01:58:28Z scott.stark@jboss.org $
25

26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.management.MBeanServer JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.LinkRef JavaDoc;
37 import javax.naming.NamingException JavaDoc;
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 JavaDoc;
57
58 /**
59  * A deployer for j2ee application client jars
60  *
61  * @author Scott.Stark@jboss.org
62  * @author Thomas.Diesler@jboss.org
63  * @version $Revision: 58275 $
64  */

65 public class ClientDeployer extends AbstractSimpleDeployer
66 {
67    private static final String JavaDoc APPLICATION_CLIENT_XML = "application-client.xml";
68    private static final String JavaDoc JBOSS_CLIENT_XML = "jboss-client.xml";
69
70    private MBeanServer JavaDoc mbeanServer;
71
72    public MBeanServer JavaDoc getMbeanServer()
73    {
74       return mbeanServer;
75    }
76
77    public void setMbeanServer(MBeanServer JavaDoc server)
78    {
79       this.mbeanServer = server;
80    }
81
82    /**
83     * This method looks to the deployment for a META-INF/application-client.xml
84     * descriptor to identify a j2ee client jar.
85     */

86    private boolean accepts(DeploymentUnit unit) throws DeploymentException
87    {
88       boolean accepts = false;
89
90       // The jar must contain an META-INF/application-client.xml
91
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 JavaDoc root = DOMUtils.parse(dd.openStream());
98             String JavaDoc namespaceURI = root.getNamespaceURI();
99             // Accept the J2EE-1.4 namespace
100
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 JavaDoc ex)
105          {
106             DeploymentException.rethrowAsDeploymentException("Cannot parse " + APPLICATION_CLIENT_XML, ex);
107          }
108       }
109
110       return accepts;
111    }
112
113    @Override JavaDoc
114    public void deploy(DeploymentUnit unit) throws org.jboss.deployers.spi.DeploymentException
115    {
116       if (accepts(unit))
117       {
118          // Create the resource class loader
119
unit.createClassLoader(new RootURLClassLoaderFactory(unit));
120          
121          // TODO: externalize in parser deployer
122
ClientMetaData metaData = buildClientMetaData(unit);
123          unit.addAttachment(ClientMetaData.class, metaData);
124          
125          try
126          {
127             setupEnvironment(unit, metaData);
128          }
129          catch (Exception JavaDoc ex)
130          {
131             DeploymentException.rethrowAsDeploymentException("Failed to setup client ENC", ex);
132          }
133       }
134    }
135
136    @Override JavaDoc
137    public void undeploy(DeploymentUnit unit)
138    {
139       // Teardown the JNDI context
140
ClientMetaData metaData = unit.getAttachment(ClientMetaData.class);
141       if (metaData != null)
142       {
143          String JavaDoc appClientName = metaData.getJndiName();
144          log.info("Removing client ENC from: " + appClientName);
145          try
146          {
147             InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
148             Util.unbind(iniCtx, appClientName);
149          }
150          catch (NamingException JavaDoc 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 JavaDoc in = appClientVF.openStream();
168          XmlFileLoader xfl = new XmlFileLoader(true);
169          Element JavaDoc appClient = xfl.getDocument(in, "application-client.xml").getDocumentElement();
170          in.close();
171          metaData = new ClientMetaData();
172          metaData.setResourceClassLoader((URLClassLoader JavaDoc)unit.getClassLoader());
173          metaData.importClientXml(appClient);
174
175          // Look for a jboss-client.xml descriptor
176
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 JavaDoc jbossClient = xfl.getDocument(in, "jboss-client.xml").getDocumentElement();
184                in.close();
185                metaData.importJbossClientXml(jbossClient);
186             }
187          }
188       }
189       catch (IOException JavaDoc 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 JavaDoc
197    {
198       // Setup a JNDI context which contains
199
String JavaDoc appClientName = metaData.getJndiName();
200       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
201       Context JavaDoc envCtx = Util.createSubcontext(iniCtx, appClientName);
202       log.debug("Creating client ENC binding under: " + appClientName);
203       // Bind environment properties
204
Iterator JavaDoc 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       // Bind EJB references
213
HashMap JavaDoc 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             // Internal link
223
String JavaDoc linkName = ref.getLink();
224             String JavaDoc 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 JavaDoc 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 JavaDoc(jndiName));
234          }
235          else
236          {
237             // Bind the bean level ejb-ref/jndi-name
238
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 JavaDoc(ref.getJndiName()));
244          }
245       }
246
247       // Bind <service-ref> elements
248
Iterator JavaDoc serviceRefs = metaData.getServiceReferences().values().iterator();
249       ServiceRefHandler refHandler = ServiceRefHandlerFactory.newInstance();
250       if (refHandler != null && serviceRefs.hasNext())
251          refHandler.setupServiceRefEnvironment(envCtx, serviceRefs, unit);
252
253       // Bind resource references
254
HashMap JavaDoc resRefs = metaData.getResourceReferences();
255       i = resRefs.values().iterator();
256       while (i.hasNext())
257       {
258          ResourceRefMetaData ref = (ResourceRefMetaData)i.next();
259          String JavaDoc refName = ref.getRefName();
260          String JavaDoc jndiName = ref.getJndiName();
261
262          if (ref.getType().equals("java.net.URL"))
263          {
264             // URL bindings
265
String JavaDoc resURL = ref.getResURL();
266             if (resURL != null)
267             {
268                log.debug("Binding '" + refName + "' to URL: " + resURL);
269                URL JavaDoc url = new URL JavaDoc(resURL);
270                Util.bind(envCtx, refName, url);
271             }
272             else
273             {
274                log.debug("Linking '" + refName + "' to URL: " + resURL);
275                LinkRef JavaDoc urlLink = new LinkRef JavaDoc(jndiName);
276                Util.bind(envCtx, refName, urlLink);
277             }
278          }
279          else
280          {
281             // A resource link
282
log.debug("Binding resource: " + refName + " to JDNI as: " + jndiName);
283             Util.bind(envCtx, refName, new LinkRef JavaDoc(jndiName));
284          }
285       }
286
287       // Bind resource env references
288
HashMap JavaDoc envRefs = metaData.getResourceEnvReferences();
289       i = envRefs.values().iterator();
290       while (i.hasNext())
291       {
292          ResourceEnvRefMetaData resRef = (ResourceEnvRefMetaData)i.next();
293          String JavaDoc encName = resRef.getRefName();
294          String JavaDoc jndiName = resRef.getJndiName();
295          // Should validate the type...
296
log.debug("Binding env resource: " + encName + " to JDNI as: " + jndiName);
297          Util.bind(envCtx, encName, new LinkRef JavaDoc(jndiName));
298       }
299       log.info("Client ENC bound under: " + appClientName);
300    }
301
302    static class RootURLClassLoaderFactory implements ClassLoaderFactory
303    {
304       URL JavaDoc rootURL;
305       
306       RootURLClassLoaderFactory(DeploymentUnit unit)
307       {
308          try
309          {
310             rootURL = unit.getDeploymentContext().getRoot().toURL();
311          }
312          catch (Exception JavaDoc ex)
313          {
314             // ignore
315
}
316       }
317
318       public ClassLoader JavaDoc createClassLoader(DeploymentContext context) throws Exception JavaDoc
319       {
320          return new URLClassLoader JavaDoc(new URL JavaDoc[] {rootURL});
321       }
322
323       public void removeClassLoader(DeploymentContext context) throws Exception JavaDoc
324       {
325          // nothing to do
326
}
327    }
328 }
329
Popular Tags