KickJava   Java API By Example, From Geeks To Geeks.

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


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.invocation.InvocationStatistics;
25 import org.jboss.logging.Logger;
26 import org.jboss.management.j2ee.statistics.CountStatisticImpl;
27 import org.jboss.management.j2ee.statistics.EJBStatsImpl;
28 import org.jboss.management.j2ee.statistics.TimeStatisticImpl;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.j2ee.statistics.Stats JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Root class of the JBoss JSR-77.3.10 EJB model
42  *
43  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
44  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
45  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
46  * @version $Revision: 40550 $
47  */

48 public abstract class EJB extends J2EEManagedObject
49    implements EJBMBean
50 {
51    // Constants -----------------------------------------------------
52
public static final int ENTITY_BEAN = 0;
53    public static final int STATEFUL_SESSION_BEAN = 1;
54    public static final int STATELESS_SESSION_BEAN = 2;
55    public static final int MESSAGE_DRIVEN_BEAN = 3;
56
57    // Attributes ----------------------------------------------------
58

59    /** The logger */
60    private static Logger log = Logger.getLogger(EJB.class);
61
62    /**
63     * The ObjectName of the ejb container MBean
64     */

65    protected ObjectName JavaDoc ejbContainerName;
66    
67    protected String JavaDoc jndiName;
68    protected String JavaDoc localJndiName;
69
70    // Static --------------------------------------------------------
71

72    /**
73     * Create a JSR77 EJB submodel.
74     *
75     * @param mbeanServer the MBeanServer to use for mbean creation
76     * @param ejbModuleName the name of the JSR77 EJBModule mbean
77     * @param ejbContainerName the name of the JBoss ejb container mbean
78     * @param ejbType an EJB.XXX_BEAN type constant value
79     * @param ejbName the bean ejb-name
80     * @param jndiName the jndi name of the remote home binding if one exists, or null
81     * @param localJndiName the jndi name of the local home binding if one exists, or null
82     * @return the ObjectName of the JSR77 EJB mbean
83     */

84    public static ObjectName JavaDoc create(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc ejbModuleName,
85       ObjectName JavaDoc ejbContainerName, int ejbType, String JavaDoc ejbName,
86       String JavaDoc jndiName, String JavaDoc localJndiName)
87    {
88       try
89       {
90          // Now create the EJB mbean
91
EJB ejb = null;
92          switch (ejbType)
93          {
94             case ENTITY_BEAN:
95                ejb = new EntityBean(ejbName, ejbModuleName, ejbContainerName,
96                  jndiName, localJndiName);
97                break;
98             case STATEFUL_SESSION_BEAN:
99                ejb = new StatefulSessionBean(ejbName, ejbModuleName,
100                   ejbContainerName, jndiName, localJndiName);
101                break;
102             case STATELESS_SESSION_BEAN:
103                ejb = new StatelessSessionBean(ejbName, ejbModuleName,
104                   ejbContainerName, jndiName, localJndiName);
105                break;
106             case MESSAGE_DRIVEN_BEAN:
107                ejb = new MessageDrivenBean(ejbName, ejbModuleName,
108                   ejbContainerName, localJndiName);
109                break;
110          }
111
112          ObjectName JavaDoc jsr77Name = ejb.getObjectName();
113          mbeanServer.registerMBean(ejb, jsr77Name);
114          log.debug("Created JSR-77 EJB: " + jsr77Name);
115          return jsr77Name;
116       }
117       catch (Exception JavaDoc e)
118       {
119          log.debug("Could not create JSR-77 EJB: " + ejbName, e);
120          return null;
121       }
122    }
123
124    public static void destroy(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc jsr77Name)
125    {
126       try
127       {
128          // Now remove the EJB
129
mbeanServer.unregisterMBean(jsr77Name);
130          log.debug("Destroyed JSR-77 EJB: " + jsr77Name);
131       }
132       catch (javax.management.InstanceNotFoundException JavaDoc ignore)
133       {
134       }
135       catch (Exception JavaDoc e)
136       {
137          log.debug("Could not destroy JSR-77 EJB: " + jsr77Name, e);
138       }
139    }
140
141    // Constructors --------------------------------------------------
142

143    /**
144     * Create a EJB model
145     *
146     * @param ejbType the EJB.EJB_TYPES string
147     * @param ejbName the ejb-name from the deployment
148     * @param ejbModuleName the JSR-77 EJBModule name for this bean
149     * @param ejbContainerName the JMX name of the JBoss ejb container MBean
150     * @throws MalformedObjectNameException
151     * @throws InvalidParentException
152     */

153    public EJB(String JavaDoc ejbType, String JavaDoc ejbName, ObjectName JavaDoc ejbModuleName,
154               ObjectName JavaDoc ejbContainerName)
155            throws MalformedObjectNameException JavaDoc,
156            InvalidParentException
157    {
158       this(ejbType, ejbName, ejbModuleName, ejbContainerName, null, null);
159    }
160    
161    /**
162     * Create a EJB model
163     *
164     * @param ejbType the EJB.EJB_TYPES string
165     * @param ejbName the ejb-name from the deployment
166     * @param ejbModuleName the JSR-77 EJBModule name for this bean
167     * @param ejbContainerName the JMX name of the JBoss ejb container MBean
168     * @param jndiName the jndi name of the remote home binding is one exists,
169     * null if there is no remote home.
170     * @param localJndiName the jndi name of the local home binding is one exists,
171     * null if there is no local home.
172     * @throws MalformedObjectNameException
173     * @throws InvalidParentException
174     */

175    public EJB(String JavaDoc ejbType, String JavaDoc ejbName, ObjectName JavaDoc ejbModuleName,
176               ObjectName JavaDoc ejbContainerName, String JavaDoc jndiName, String JavaDoc localJndiName)
177            throws MalformedObjectNameException JavaDoc,
178            InvalidParentException
179    {
180       super(ejbType, ejbName, ejbModuleName);
181       this.ejbContainerName = ejbContainerName;
182       this.jndiName = jndiName;
183       this.localJndiName = localJndiName;
184    }
185
186    // Begin StatisticsProvider interface methods
187

188    /**
189     * Obtain the Stats from the StatisticsProvider.
190     *
191     * @return An EJBStats subclass
192     * @jmx:managed-attribute
193     */

194    public abstract Stats JavaDoc getstats();
195
196    /**
197     * Reset all statistics in the StatisticsProvider
198     *
199     * @jmx:managed-operation
200     */

201    public abstract void resetStats();
202    // End StatisticsProvider interface methods
203

204    public String JavaDoc getJndiName()
205    {
206       return this.jndiName;
207    }
208    
209    public String JavaDoc getLocalJndiName()
210    {
211       return this.localJndiName;
212    }
213
214    // java.lang.Object overrides --------------------------------------
215

216    public String JavaDoc toString()
217    {
218       return "EJB { " + super.toString() + " } []";
219    }
220    // Package protected ---------------------------------------------
221

222    // Protected -----------------------------------------------------
223

224    /**
225     * Obtain the Stats from the StatisticsProvider. This method simply
226     * updates the statistics common to all EJBs:
227     * CreateCount
228     * RemoveCount
229     * InvocationTimes
230     * <p/>
231     * It should be invoked to update these common statistics.
232     */

233    protected void updateCommonStats(EJBStatsImpl stats)
234    {
235       try
236       {
237          ObjectName JavaDoc containerName = getContainerName();
238          CountStatisticImpl createCount = (CountStatisticImpl) stats.getCreateCount();
239          Long JavaDoc creates = (Long JavaDoc) server.getAttribute(containerName, "CreateCount");
240          createCount.set(creates.longValue());
241          CountStatisticImpl removeCount = (CountStatisticImpl) stats.getRemoveCount();
242          Long JavaDoc removes = (Long JavaDoc) server.getAttribute(containerName, "RemoveCount");
243          removeCount.set(removes.longValue());
244
245          // Now build a TimeStatistics for every
246
InvocationStatistics times = (InvocationStatistics) server.getAttribute(containerName, "InvokeStats");
247          HashMap JavaDoc timesMap = new HashMap JavaDoc(times.getStats());
248          Iterator JavaDoc iter = timesMap.entrySet().iterator();
249          while (iter.hasNext())
250          {
251             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
252             Method JavaDoc m = (Method JavaDoc) entry.getKey();
253             InvocationStatistics.TimeStatistic stat = (InvocationStatistics.TimeStatistic) entry.getValue();
254             TimeStatisticImpl tstat = new TimeStatisticImpl(m.getName(), StatisticsConstants.MILLISECOND,
255                     "The timing information for the given method");
256             tstat.set(stat.count, stat.minTime, stat.maxTime, stat.totalTime);
257             stats.addStatistic(m.getName(), tstat);
258          }
259       }
260       catch (Exception JavaDoc e)
261       {
262          log.debug("Failed to retrieve stats", e);
263       }
264    }
265
266    /**
267     * @return the JMX name of the EJB container
268     */

269    protected ObjectName JavaDoc getContainerName()
270    {
271       return this.ejbContainerName;
272    }
273
274    /**
275     * @return the JMX name of the EJB container cache
276     */

277    protected ObjectName JavaDoc getContainerCacheName()
278    {
279       ObjectName JavaDoc cacheName = null;
280       try
281       {
282          Hashtable JavaDoc props = ejbContainerName.getKeyPropertyList();
283          props.put("plugin", "cache");
284          cacheName = new ObjectName JavaDoc(ejbContainerName.getDomain(), props);
285       }
286       catch (MalformedObjectNameException JavaDoc e)
287       {
288       }
289       return cacheName;
290    }
291
292    /**
293     * @return the JMX name of the EJB container pool
294     */

295    protected ObjectName JavaDoc getContainerPoolName()
296    {
297       ObjectName JavaDoc poolName = null;
298       try
299       {
300          Hashtable JavaDoc props = ejbContainerName.getKeyPropertyList();
301          props.put("plugin", "pool");
302          poolName = new ObjectName JavaDoc(ejbContainerName.getDomain(), props);
303       }
304       catch (MalformedObjectNameException JavaDoc e)
305       {
306       }
307       return poolName;
308    }
309
310    /**
311     * @return A hashtable with the EJB-Module, J2EE-Application and J2EE-Server as parent
312     */

313    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
314    {
315       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
316       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
317       lReturn.put(J2EETypeConstants.EJBModule, lProperties.get("name"));
318       // J2EE-Application and J2EE-Server is already parent of J2EE-Application therefore lookup
319
// the name by the J2EE-Server type
320
lReturn.put(J2EETypeConstants.J2EEApplication, lProperties.get(J2EETypeConstants.J2EEApplication));
321       lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
322       return lReturn;
323    }
324
325    // Private -------------------------------------------------------
326

327    // Inner classes -------------------------------------------------
328

329 }
330
Popular Tags