KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > EJBModule


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.management.j2ee;
23
24 import org.jboss.logging.Logger;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MalformedObjectNameException JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.security.InvalidParameterException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 /**
39  * Root class of the JBoss JSR-77 implementation of EJBModule.
40  *
41  * @author Andreas Schaefer.
42  * @author Scott.Stark@jboss.org
43  * @author thomas.diesler@jboss.org
44  * @version $Revision: 40550 $
45  * @jmx:mbean extends="org.jboss.management.j2ee.EventProvider, org.jboss.management.j2ee.J2EEModuleMBean"
46  */

47 public class EJBModule
48         extends J2EEModule
49         implements EJBModuleMBean
50 {
51    private static final String JavaDoc[] eventTypes = {NotificationConstants.OBJECT_CREATED,
52                                                NotificationConstants.OBJECT_DELETED};
53
54    // Attributes ----------------------------------------------------
55
private static Logger log = Logger.getLogger(EJBModule.class);
56
57    // list of object names as strings
58
private List JavaDoc mEJBs = new ArrayList JavaDoc();
59
60    private ObjectName JavaDoc moduleServiceName;
61    private String JavaDoc mJBossDD;
62    private String JavaDoc mJAWSDD;
63    private String JavaDoc mCMPDD;
64
65    /**
66     * The JSR77 ObjectNames of fake J2EEApplications created by standalone jars
67     */

68    private static final Map JavaDoc fakeJ2EEApps = new HashMap JavaDoc();
69
70    // Static --------------------------------------------------------
71

72    /**
73     * Creates the JSR-77 EJBModule
74     *
75     * @param mbeanServer MBeanServer the EJBModule is created on
76     * @param earName the ear name unless null which indicates a standalone module (no EAR)
77     * @param jarName the ejb.jar name
78     * @param pURL URL path to the local deployment of the module (where to find the DD file)
79     * @param moduleServiceName ObjectName of the EjbModule service to start and stop the module
80     * @return the JSR77 ObjectName of the EJBModule
81     */

82    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer,
83                                    String JavaDoc earName,
84                                    String JavaDoc jarName,
85                                    URL JavaDoc pURL,
86                                    ObjectName JavaDoc moduleServiceName)
87    {
88       String JavaDoc lDD = null;
89       String JavaDoc lJBossDD = null;
90       String JavaDoc lJAWSDD = null;
91       String JavaDoc lCMPDD = null;
92       ObjectName JavaDoc lParent = null;
93       ObjectName JavaDoc lCreated = null;
94       ObjectName JavaDoc jsr77Name = null;
95       // Get the J2EEServer name
96
ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(mbeanServer);
97       try
98       {
99          Hashtable JavaDoc props = j2eeServerName.getKeyPropertyList();
100          String JavaDoc j2eeServer = props.get(J2EEManagedObject.TYPE) + "=" +
101                  props.get("name");
102
103          if (earName == null)
104          {
105             // If there is no ear use the J2EEServer as the parent
106
lParent = j2eeServerName;
107          }
108          else
109          {
110             // Query for the J2EEApplication matching earName
111
ObjectName JavaDoc lApplicationQuery = new ObjectName JavaDoc(J2EEDomain.getDomainName() + ":" +
112                     J2EEManagedObject.TYPE + "=" + J2EETypeConstants.J2EEApplication + "," +
113                     "name=" + earName + "," +
114                     j2eeServer + "," +
115                     "*");
116             Set JavaDoc parentApps = mbeanServer.queryNames(lApplicationQuery, null);
117
118             if (parentApps.isEmpty())
119             {
120                lCreated = J2EEApplication.create(mbeanServer,
121                        earName,
122                        null);
123                lParent = lCreated;
124             } // end of if ()
125
else if (parentApps.size() == 1)
126             {
127                lParent = (ObjectName JavaDoc) parentApps.iterator().next();
128             } // end of if ()
129
}
130
131          // Get the J2EE deployement descriptor
132
lDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.EJB);
133          // Get the JBoss deployement descriptor
134
lJBossDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.JBOSS);
135          // Get the JAWS deployement descriptor
136
lJAWSDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.JAWS);
137          // Get the CMP 2.0 deployement descriptor
138
lCMPDD = J2EEDeployedObject.getDeploymentDescriptor(pURL, J2EEDeployedObject.CMP);
139       }
140       catch (Exception JavaDoc e)
141       {
142          log.debug("Could not create JSR-77 EJBModule: " + jarName, e);
143          return null;
144       }
145
146       try
147       {
148          // Get JVM of the j2eeServer
149
String JavaDoc[] jvms = (String JavaDoc[]) mbeanServer.getAttribute(j2eeServerName,
150                  "javaVMs");
151
152          EJBModule ejbModule = new EJBModule(jarName, lParent,
153                  jvms,
154                  lDD,
155                  moduleServiceName,
156                  lJBossDD,
157                  lJAWSDD,
158                  lCMPDD);
159          jsr77Name = ejbModule.getObjectName();
160          mbeanServer.registerMBean(ejbModule, jsr77Name);
161          // If we created our parent, if we have to delete it in destroy.
162
if (lCreated != null)
163          {
164             fakeJ2EEApps.put(jsr77Name, lCreated);
165          }
166          log.debug("Created JSR-77 EJBModule: " + jsr77Name);
167       }
168       catch (Exception JavaDoc e)
169       {
170          log.error("Could not create JSR-77 EJBModule: " + jarName, e);
171       }
172
173       return jsr77Name;
174    }
175
176    /**
177     * Destroyes the given JSR-77 EJB-Module
178     *
179     * @param mbeanServer The JMX MBeanServer the desired EJB-Module is registered on
180     * @param jsr77Name the JSR77 EJBModule component ObjectName
181     */

182    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
183    {
184       try
185       {
186          log.debug("destroy(), remove EJB-Module: " + jsr77Name);
187          mbeanServer.unregisterMBean(jsr77Name);
188
189          ObjectName JavaDoc jsr77ParentName = (ObjectName JavaDoc) fakeJ2EEApps.get(jsr77Name);
190          if (jsr77ParentName != null)
191          {
192             log.debug("Remove fake JSR-77 parent Application: " + jsr77ParentName);
193             J2EEApplication.destroy(mbeanServer, jsr77ParentName);
194          }
195       }
196       catch (Exception JavaDoc e)
197       {
198          log.debug("Could not destroy JSR-77 EJBModule: " + jsr77Name, e);
199       }
200    }
201
202    // Constructors --------------------------------------------------
203

204    /**
205     * Constructor taking the Name of this Object
206     *
207     * @param jarName the ejb jar name which must not be null
208     * @param jsr77ParentName ObjectName of the Parent this Module belongs
209     * too. Either it is a J2EEApplication or J2EEServer
210     * if a standalone module.
211     * @param pJVMs Array of ObjectNames of the JVM this module is deployed on
212     * @param pDeploymentDescriptor Content of the module deployment descriptor
213     * @param moduleServiceName ObjectName of the service this Managed Object represent
214     * used for state management (start and stop)
215     * @throws MalformedObjectNameException If name or application name is incorrect
216     * @throws InvalidParameterException If the given Name is null
217     */

218    public EJBModule(String JavaDoc jarName,
219                     ObjectName JavaDoc jsr77ParentName,
220                     String JavaDoc[] pJVMs,
221                     String JavaDoc pDeploymentDescriptor,
222                     ObjectName JavaDoc moduleServiceName,
223                     String JavaDoc pJBossDD,
224                     String JavaDoc pJAWSDD,
225                     String JavaDoc pCMPDD)
226            throws
227            MalformedObjectNameException JavaDoc,
228            InvalidParentException
229    {
230       super(J2EETypeConstants.EJBModule, jarName, jsr77ParentName, pJVMs, pDeploymentDescriptor);
231       this.moduleServiceName = moduleServiceName;
232       mJBossDD = (pJBossDD == null ? "" : pJBossDD);
233       mJAWSDD = (pJAWSDD == null ? "" : pJAWSDD);
234       mCMPDD = (pCMPDD == null ? "" : pCMPDD);
235    }
236
237    // Public --------------------------------------------------------
238

239    /**
240     * @jmx:managed-attribute
241     */

242    public String JavaDoc[] getejbs()
243    {
244       return (String JavaDoc[]) mEJBs.toArray(new String JavaDoc[mEJBs.size()]);
245    }
246
247    /**
248     * @jmx:managed-operation
249     */

250    public String JavaDoc getejb(int pIndex)
251    {
252       if (pIndex >= 0 && pIndex < mEJBs.size())
253       {
254          return (String JavaDoc) mEJBs.get(pIndex);
255       }
256       else
257       {
258          return null;
259       }
260    }
261
262    /**
263     * @return JBoss Deployment Descriptor
264     * @jmx:managed-attribute
265     */

266    public String JavaDoc getjbossDeploymentDescriptor()
267    {
268       return mJBossDD;
269    }
270
271    /**
272     * @return JAWS Deployment Descriptor
273     * @jmx:managed-attribute
274     */

275    public String JavaDoc getjawsDeploymentDescriptor()
276    {
277       return mJAWSDD;
278    }
279
280    /**
281     * @return CMP 2.0 Deployment Descriptor
282     * @jmx:managed-attribute
283     */

284    public String JavaDoc getcmpDeploymentDescriptor()
285    {
286       return mCMPDD;
287    }
288
289    // J2EEManagedObjectMBean implementation -------------------------
290

291    public void addChild(ObjectName JavaDoc pChild)
292    {
293       String JavaDoc lType = J2EEManagedObject.getType(pChild);
294       if (J2EETypeConstants.EntityBean.equals(lType) ||
295               J2EETypeConstants.StatelessSessionBean.equals(lType) ||
296               J2EETypeConstants.StatefulSessionBean.equals(lType) ||
297               J2EETypeConstants.MessageDrivenBean.equals(lType)
298       )
299       {
300          mEJBs.add(pChild.getCanonicalName());
301       }
302    }
303
304    public void removeChild(ObjectName JavaDoc pChild)
305    {
306       String JavaDoc lType = J2EEManagedObject.getType(pChild);
307       if (J2EETypeConstants.EntityBean.equals(lType) ||
308               J2EETypeConstants.StatelessSessionBean.equals(lType) ||
309               J2EETypeConstants.StatefulSessionBean.equals(lType) ||
310               J2EETypeConstants.MessageDrivenBean.equals(lType)
311       )
312       {
313          mEJBs.remove(pChild.getCanonicalName());
314       }
315    }
316
317    public void postCreation()
318    {
319       sendNotification(NotificationConstants.OBJECT_CREATED, "EJB Module created");
320    }
321
322    public void preDestruction()
323    {
324       sendNotification(NotificationConstants.OBJECT_DELETED, "EJB Module destroyed");
325    }
326
327    // javax.managment.j2ee.EventProvider implementation -------------
328

329    public String JavaDoc[] getEventTypes()
330    {
331       return eventTypes;
332    }
333
334    public String JavaDoc getEventType(int index)
335    {
336       String JavaDoc type = null;
337       if (index >= 0 && index < eventTypes.length)
338       {
339          type = eventTypes[index];
340       }
341       return type;
342    }
343
344    // Object overrides ---------------------------------------------------
345

346    public String JavaDoc toString()
347    {
348       return "EJBModule[ " + super.toString() +
349               ", EJBs: " + mEJBs +
350               ", JBoss-DD: " + mJBossDD +
351               ", JAWS-DD: " + mJAWSDD +
352               ", CMP-2.0-DD: " + mCMPDD +
353               " ]";
354    }
355
356    // Package protected ---------------------------------------------
357

358    // Protected -----------------------------------------------------
359

360    /**
361     * @param jsr77ParentName the WebModule parent's JSR77 ObjectName
362     * @return A hashtable with the J2EE-Application and J2EE-Server as parent
363     */

364    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc jsr77ParentName)
365    {
366       Hashtable JavaDoc parentKeys = new Hashtable JavaDoc();
367       Hashtable JavaDoc parentProps = jsr77ParentName.getKeyPropertyList();
368       String JavaDoc parentName = (String JavaDoc) parentProps.get("name");
369       String JavaDoc j2eeType = (String JavaDoc) parentProps.get(J2EEManagedObject.TYPE);
370
371       // Check if parent is a J2EEServer or J2EEApplication
372
if (j2eeType.equals(J2EETypeConstants.J2EEApplication) == false)
373       {
374          // J2EEServer
375
parentKeys.put(J2EETypeConstants.J2EEServer, parentName);
376          parentKeys.put(J2EETypeConstants.J2EEApplication, "null");
377       }
378       else
379       {
380          // J2EEApplication
381
parentKeys.put(J2EETypeConstants.J2EEApplication, parentName);
382          String JavaDoc j2eeServerName = (String JavaDoc) parentProps.get(J2EETypeConstants.J2EEServer);
383          parentKeys.put(J2EETypeConstants.J2EEServer, j2eeServerName);
384       }
385
386       return parentKeys;
387    }
388
389    // Private -------------------------------------------------------
390

391    // Inner classes -------------------------------------------------
392
}
393
394 /*
395 vim:ts=3:sw=3:et
396 */

397
Popular Tags