1 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 ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.management.j2ee.statistics.Stats ; 34 import java.lang.reflect.Method ; 35 import java.util.HashMap ; 36 import java.util.Hashtable ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 40 48 public abstract class EJB extends J2EEManagedObject 49 implements EJBMBean 50 { 51 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 59 60 private static Logger log = Logger.getLogger(EJB.class); 61 62 65 protected ObjectName ejbContainerName; 66 67 protected String jndiName; 68 protected String localJndiName; 69 70 72 84 public static ObjectName create(MBeanServer mbeanServer, ObjectName ejbModuleName, 85 ObjectName ejbContainerName, int ejbType, String ejbName, 86 String jndiName, String localJndiName) 87 { 88 try 89 { 90 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 jsr77Name = ejb.getObjectName(); 113 mbeanServer.registerMBean(ejb, jsr77Name); 114 log.debug("Created JSR-77 EJB: " + jsr77Name); 115 return jsr77Name; 116 } 117 catch (Exception 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 mbeanServer, ObjectName jsr77Name) 125 { 126 try 127 { 128 mbeanServer.unregisterMBean(jsr77Name); 130 log.debug("Destroyed JSR-77 EJB: " + jsr77Name); 131 } 132 catch (javax.management.InstanceNotFoundException ignore) 133 { 134 } 135 catch (Exception e) 136 { 137 log.debug("Could not destroy JSR-77 EJB: " + jsr77Name, e); 138 } 139 } 140 141 143 153 public EJB(String ejbType, String ejbName, ObjectName ejbModuleName, 154 ObjectName ejbContainerName) 155 throws MalformedObjectNameException , 156 InvalidParentException 157 { 158 this(ejbType, ejbName, ejbModuleName, ejbContainerName, null, null); 159 } 160 161 175 public EJB(String ejbType, String ejbName, ObjectName ejbModuleName, 176 ObjectName ejbContainerName, String jndiName, String localJndiName) 177 throws MalformedObjectNameException , 178 InvalidParentException 179 { 180 super(ejbType, ejbName, ejbModuleName); 181 this.ejbContainerName = ejbContainerName; 182 this.jndiName = jndiName; 183 this.localJndiName = localJndiName; 184 } 185 186 188 194 public abstract Stats getstats(); 195 196 201 public abstract void resetStats(); 202 204 public String getJndiName() 205 { 206 return this.jndiName; 207 } 208 209 public String getLocalJndiName() 210 { 211 return this.localJndiName; 212 } 213 214 216 public String toString() 217 { 218 return "EJB { " + super.toString() + " } []"; 219 } 220 222 224 233 protected void updateCommonStats(EJBStatsImpl stats) 234 { 235 try 236 { 237 ObjectName containerName = getContainerName(); 238 CountStatisticImpl createCount = (CountStatisticImpl) stats.getCreateCount(); 239 Long creates = (Long ) server.getAttribute(containerName, "CreateCount"); 240 createCount.set(creates.longValue()); 241 CountStatisticImpl removeCount = (CountStatisticImpl) stats.getRemoveCount(); 242 Long removes = (Long ) server.getAttribute(containerName, "RemoveCount"); 243 removeCount.set(removes.longValue()); 244 245 InvocationStatistics times = (InvocationStatistics) server.getAttribute(containerName, "InvokeStats"); 247 HashMap timesMap = new HashMap (times.getStats()); 248 Iterator iter = timesMap.entrySet().iterator(); 249 while (iter.hasNext()) 250 { 251 Map.Entry entry = (Map.Entry ) iter.next(); 252 Method m = (Method ) 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 e) 261 { 262 log.debug("Failed to retrieve stats", e); 263 } 264 } 265 266 269 protected ObjectName getContainerName() 270 { 271 return this.ejbContainerName; 272 } 273 274 277 protected ObjectName getContainerCacheName() 278 { 279 ObjectName cacheName = null; 280 try 281 { 282 Hashtable props = ejbContainerName.getKeyPropertyList(); 283 props.put("plugin", "cache"); 284 cacheName = new ObjectName (ejbContainerName.getDomain(), props); 285 } 286 catch (MalformedObjectNameException e) 287 { 288 } 289 return cacheName; 290 } 291 292 295 protected ObjectName getContainerPoolName() 296 { 297 ObjectName poolName = null; 298 try 299 { 300 Hashtable props = ejbContainerName.getKeyPropertyList(); 301 props.put("plugin", "pool"); 302 poolName = new ObjectName (ejbContainerName.getDomain(), props); 303 } 304 catch (MalformedObjectNameException e) 305 { 306 } 307 return poolName; 308 } 309 310 313 protected Hashtable getParentKeys(ObjectName pParent) 314 { 315 Hashtable lReturn = new Hashtable (); 316 Hashtable lProperties = pParent.getKeyPropertyList(); 317 lReturn.put(J2EETypeConstants.EJBModule, lProperties.get("name")); 318 lReturn.put(J2EETypeConstants.J2EEApplication, lProperties.get(J2EETypeConstants.J2EEApplication)); 321 lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer)); 322 return lReturn; 323 } 324 325 327 329 } 330 | Popular Tags |