KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > deployers > EjbDeployer


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.ejb.deployers;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import org.jboss.deployers.plugins.deployers.helpers.AbstractSimpleRealDeployer;
31 import org.jboss.deployers.spi.DeploymentException;
32 import org.jboss.deployers.spi.deployer.DeploymentUnit;
33 import org.jboss.ejb.EjbModule;
34 import org.jboss.metadata.ApplicationMetaData;
35 import org.jboss.mx.util.ObjectNameConverter;
36 import org.jboss.system.metadata.ServiceAttributeMetaData;
37 import org.jboss.system.metadata.ServiceConstructorMetaData;
38 import org.jboss.system.metadata.ServiceDependencyValueMetaData;
39 import org.jboss.system.metadata.ServiceMetaData;
40
41 /**
42  * A real deployer that translates ApplicationMetaData into ServiceMetaData for
43  * the ejb module service mbeans.
44  *
45  * @author Scott.Stark@jboss.org
46  * @version $Revision:$
47  */

48 public class EjbDeployer extends AbstractSimpleRealDeployer<ApplicationMetaData>
49 {
50    private String JavaDoc transactionManagerServiceName;
51    private String JavaDoc webServiceName;
52
53    /**
54     * A utility method that takes a deployment unit name and strips it down to the base war
55     * name without the .war suffix.
56     * @param name - the DeploymentUnit name.
57     */

58    public static String JavaDoc shortNameFromDeploymentName(String JavaDoc name)
59    {
60       String JavaDoc shortName = name;
61       String JavaDoc[] parts = name.split("/|\\.");
62       if( parts.length > 1 )
63       {
64          // If it ends in .war, use the previous part
65
if( parts[parts.length-1].equals("war") )
66             shortName = parts[parts.length-2];
67          // else use the last part
68
else
69             shortName = parts[parts.length-1];
70       }
71       return shortName;
72    }
73
74    public EjbDeployer()
75    {
76       super(ApplicationMetaData.class);
77    }
78
79    public String JavaDoc getTransactionManagerServiceName()
80    {
81       return transactionManagerServiceName;
82    }
83
84    public void setTransactionManagerServiceName(
85          String JavaDoc transactionManagerServiceName)
86    {
87       this.transactionManagerServiceName = transactionManagerServiceName;
88    }
89
90    public String JavaDoc getWebServiceName()
91    {
92       return webServiceName;
93    }
94
95    public void setWebServiceName(String JavaDoc webServiceName)
96    {
97       this.webServiceName = webServiceName;
98    }
99
100    @Override JavaDoc
101    public void deploy(DeploymentUnit unit, ApplicationMetaData deployment)
102       throws DeploymentException
103    {
104       // TODO: use BeanVerifier to validate the metadata in the DDs
105
if (deployment.getEjbVersion() > 2) return; // let EJB3 deployer handle this
106

107       ServiceMetaData ejbModule = new ServiceMetaData();
108       ejbModule.setCode(EjbModule.class.getName());
109
110       String JavaDoc moduleName = deployment.getJmxName();
111       if( moduleName == null )
112       {
113          String JavaDoc unitShortName = unit.getName();
114          if (unitShortName.endsWith("/"))
115          {
116             unitShortName = unitShortName.substring(0, unitShortName.length() - 1);
117          }
118
119          if(unitShortName.endsWith("!"))
120          {
121             unitShortName = unitShortName.substring(0, unitShortName.length() - 1);
122          }
123
124          unitShortName = unitShortName.substring(unitShortName.lastIndexOf("/") + 1);
125
126          moduleName = EjbModule.BASE_EJB_MODULE_NAME + ",module=" + unitShortName;
127       }
128
129       // Build an escaped JMX name including deployment shortname
130
ObjectName JavaDoc moduleObjectName = null;
131       try
132       {
133          moduleObjectName = ObjectNameConverter.convert(moduleName);
134       }
135       catch(MalformedObjectNameException JavaDoc e)
136       {
137          throw new DeploymentException("Failed to create EJB module " + unit.getName() +
138             ": malformed EjbModule name " + moduleName, e);
139       }
140
141       ejbModule.setObjectName(moduleObjectName);
142
143       ServiceConstructorMetaData ctor = new ServiceConstructorMetaData();
144       ctor.setSignature(
145          new String JavaDoc[]{DeploymentUnit.class.getName(), ApplicationMetaData.class.getName()}
146       );
147       ctor.setParameters(new Object JavaDoc[]{unit, deployment});
148       ejbModule.setConstructor(ctor);
149
150       // set attributes
151
List JavaDoc<ServiceAttributeMetaData> attrs = new ArrayList JavaDoc<ServiceAttributeMetaData>();
152       ServiceAttributeMetaData attr = new ServiceAttributeMetaData();
153       attr.setName("TransactionManagerFactory");
154       ServiceDependencyValueMetaData dependencyValue = new ServiceDependencyValueMetaData();
155       dependencyValue.setDependency(getTransactionManagerServiceName());
156       dependencyValue.setProxyType("attribute");
157       attr.setValue(dependencyValue);
158       attrs.add(attr);
159
160       ejbModule.setAttributes(attrs);
161
162       unit.addAttachment(ServiceMetaData.class, ejbModule);
163    }
164
165    @Override JavaDoc
166    public void undeploy(DeploymentUnit unit, ApplicationMetaData deployment)
167    {
168       // TODO Auto-generated method stub
169

170    }
171
172    /**
173     * Get the object name of the ServiceMetaData instance associated with
174     * the WebMetaData. This uses the pattern:
175     * "jboss.web.deployment:war="+metaData.getContextRoot()
176     *
177     * @param metaData - the web app metaData
178     * @return "jboss.web.deployment:war="+metaData.getContextRoot();
179     * @throws MalformedObjectNameException
180     */

181    protected ObjectName JavaDoc getObjectName(DeploymentUnit unit, ApplicationMetaData metaData)
182       throws MalformedObjectNameException JavaDoc
183    {
184       String JavaDoc name = metaData.getJmxName();
185       if( name == null )
186       {
187          name = EjbModule.BASE_EJB_MODULE_NAME + ",module="
188             + shortNameFromDeploymentName(unit.getName());
189       }
190
191       // Build an escaped JMX name including deployment shortname
192
ObjectName JavaDoc ejbModuleName = ObjectNameConverter.convert(name);
193       return ejbModuleName;
194    }
195
196    protected void deployEjbModule(DeploymentUnit unit, ApplicationMetaData metaData)
197       throws Exception JavaDoc
198    {
199       log.debug("deployWebModule");
200       try
201       {
202          ServiceMetaData ejbModule = new ServiceMetaData();
203          ObjectName JavaDoc objectName = getObjectName(unit, metaData);
204          ejbModule.setObjectName(objectName);
205          ejbModule.setCode(EjbModule.class.getName());
206          // EjbModule(DeploymentUnit)
207
ServiceConstructorMetaData constructor = new ServiceConstructorMetaData();
208          constructor.setSignature(new String JavaDoc[] { DeploymentUnit.class.getName()});
209          constructor.setParameters(new Object JavaDoc[] {unit, this, metaData});
210          ejbModule.setConstructor(constructor);
211
212          List JavaDoc<ServiceAttributeMetaData> attributes = new ArrayList JavaDoc<ServiceAttributeMetaData>();
213          // Add injection of the TM
214
ServiceAttributeMetaData tm = new ServiceAttributeMetaData();
215          tm.setName("txMgr");
216          String JavaDoc tmProxyType = "javax.transaction.TransactionManager";
217          ServiceDependencyValueMetaData tmDepends = new ServiceDependencyValueMetaData(transactionManagerServiceName, tmProxyType);
218          tm.setValue(tmDepends);
219          attributes.add(tm);
220          // Add injection of the WebServiceName
221
ServiceAttributeMetaData ws = new ServiceAttributeMetaData();
222          ws.setName("webServiceName");
223          ServiceDependencyValueMetaData wsDepends = new ServiceDependencyValueMetaData();
224          wsDepends.setDependency(webServiceName);
225          attributes.add(ws);
226          ejbModule.setAttributes(attributes);
227
228          // TODO could create multiple components for the deployment
229
unit.addAttachment(ServiceMetaData.class, ejbModule);
230       }
231       catch (Exception JavaDoc e)
232       {
233          throw DeploymentException.rethrowAsDeploymentException("Error creating rar deployment " + unit.getName(), e);
234       }
235
236    }
237 }
238
Popular Tags