KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > deployers > Ejb3ClientDeployer


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, Red Hat Middleware LLC, 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.ejb3.deployers;
23
24 import java.io.InputStream JavaDoc;
25 import java.util.jar.Attributes JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.jar.Manifest JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NameNotFoundException JavaDoc;
33 import javax.naming.NamingException JavaDoc;
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 /**
48  * Deploys a client application jar.
49  *
50  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
51  * @version $Revision: $
52  */

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 JavaDoc server;
59    
60    public Ejb3ClientDeployer()
61    {
62       super(ApplicationClientDD.class);
63    }
64    
65    @Override JavaDoc
66    public void deploy(DeploymentUnit unit, ApplicationClientDD metaData) throws DeploymentException
67    {
68       log.debug("deploy " + unit.getName());
69       
70       String JavaDoc appClientName = getJndiName(unit, metaData);
71       
72       try
73       {
74          // I create the namespace here, because I destroy it in undeploy
75
InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
76          Context JavaDoc encCtx = Util.createSubcontext(iniCtx, appClientName);
77          log.debug("Creating client ENC binding under: " + appClientName);
78          
79          String JavaDoc mainClassName = getMainClassName(unit, true);
80          
81          Class JavaDoc<?> mainClass = loadClass(unit, mainClassName);
82          
83          ClientENCInjectionContainer container = new ClientENCInjectionContainer(unit, metaData, mainClass, appClientName, unit.getClassLoader(), encCtx);
84          
85          //di.deployedObject = container.getObjectName();
86
unit.addAttachment(ClientENCInjectionContainer.class, container);
87          getKernelAbstraction().install(container.getObjectName().getCanonicalName(), container.getDependencyPolicy(), container);
88       }
89       catch(Exception JavaDoc 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    /**
98     * If there is no deployment descriptor, or it doesn't specify a JNDI name, then we make up one.
99     * We use the basename from di.shortName.
100     *
101     * @param di
102     * @param dd
103     * @return a good JNDI name
104     */

105    private String JavaDoc getJndiName(DeploymentUnit unit, ApplicationClientDD dd)
106    {
107       String JavaDoc jndiName = dd.getJndiName();
108       if(jndiName != null)
109          return jndiName;
110       
111       String JavaDoc 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 JavaDoc("Expected either '.jar' or '.jar/' at the end of " + shortName);
118       
119       return jndiName;
120    }
121    
122 // public Kernel getKernel()
123
// {
124
// return kernel;
125
// }
126

127    private KernelAbstraction getKernelAbstraction()
128    {
129       return new MCKernelAbstraction(kernel, server);
130    }
131    
132    protected String JavaDoc getMainClassName(DeploymentUnit unit, boolean fail) throws Exception JavaDoc
133    {
134       VirtualFile file = unit.getMetaDataFile("MANIFEST.MF");
135       log.trace("parsing " + file);
136       // Default to the jboss client main
137
String JavaDoc mainClassName = "org.jboss.client.AppClientMain";
138
139       if(file == null)
140       {
141          return mainClassName;
142       }
143
144       try
145       {
146          InputStream JavaDoc is = file.openStream();
147          Manifest JavaDoc mf;
148          try
149          {
150             mf = new Manifest JavaDoc(is);
151          }
152          finally
153          {
154             is.close();
155          }
156          Attributes JavaDoc attrs = mf.getMainAttributes();
157          String JavaDoc 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 JavaDoc<?> loadClass(DeploymentUnit unit, String JavaDoc className) throws ClassNotFoundException JavaDoc
172    {
173       ClassLoader JavaDoc 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 JavaDoc server)
191    {
192       this.server = server;
193    }
194    
195    @Override JavaDoc
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 JavaDoc jndiName = getJndiName(unit, metaData);
205       log.debug("Removing client ENC from: " + jndiName);
206       try
207       {
208          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
209          Util.unbind(iniCtx, jndiName);
210       }
211       catch(NameNotFoundException JavaDoc e)
212       {
213          // make sure stop doesn't fail for no reason
214
log.debug("Could not find client ENC");
215       }
216       catch (NamingException JavaDoc e)
217       {
218          log.error("Failed to remove client ENC", e);
219       }
220    }
221
222 }
223
Popular Tags