001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.monitor;
023:
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030: import javax.management.JMException;
031: import javax.management.MBeanRegistration;
032: import javax.management.MBeanServer;
033: import javax.management.ObjectInstance;
034: import javax.management.ObjectName;
035: import org.jboss.ejb.Container;
036: import org.jboss.ejb.EJBDeployer;
037: import org.jboss.ejb.EJBDeployerMBean;
038: import org.jboss.ejb.EjbModule;
039: import org.jboss.ejb.EntityContainer;
040: import org.jboss.ejb.InstanceCache;
041: import org.jboss.ejb.StatefulSessionContainer;
042: import org.jboss.logging.Logger;
043: import org.jboss.monitor.client.BeanCacheSnapshot;
044:
045: /**
046: *
047: * @see Monitorable
048: * @author <a href="mailto:simone.bordet@compaq.com">Simone Bordet</a>
049: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
050: * @version $Revision: 57209 $
051: */
052: public class BeanCacheMonitor implements BeanCacheMonitorMBean,
053: MBeanRegistration {
054: // Constants ----------------------------------------------------
055:
056: // Attributes ---------------------------------------------------
057: static Logger log = Logger.getLogger(BeanCacheMonitor.class);
058: MBeanServer m_mbeanServer;
059:
060: // Static -------------------------------------------------------
061:
062: // Constructors -------------------------------------------------
063: public BeanCacheMonitor() {
064: }
065:
066: // Public -------------------------------------------------------
067:
068: // MBeanRegistration implementation -----------------------------------
069: public ObjectName preRegister(MBeanServer server, ObjectName name)
070: throws Exception {
071: m_mbeanServer = server;
072: return name;
073: }
074:
075: public void postRegister(Boolean registrationDone) {
076: }
077:
078: public void preDeregister() throws Exception {
079: }
080:
081: public void postDeregister() {
082: }
083:
084: // CacheMonitorMBean implementation -----------------------------------
085: /**
086: * Describe <code>getSnapshots</code> method here.
087: *
088: * @return a <code>BeanCacheSnapshot[]</code> value
089: * @todo: convert to queries on object names of components.
090: */
091: public BeanCacheSnapshot[] getSnapshots() {
092: try {
093: Collection snapshots = listSnapshots();
094: BeanCacheSnapshot[] snapshotArray = new BeanCacheSnapshot[snapshots
095: .size()];
096: return (BeanCacheSnapshot[]) snapshots
097: .toArray(snapshotArray);
098: } catch (JMException e) {
099: log.error("Problem getting bean cache snapshots", e);
100: return null;
101: } // end of try-catch
102: }
103:
104: /**
105: * The <code>listSnapshots</code> method returns a collection
106: * of BeanSnapshots showing the
107: *
108: * @return a <code>Collection</code> value
109: * @exception JMException if an error occurs
110: */
111: public Collection listSnapshots() throws JMException {
112: ArrayList cacheSnapshots = new ArrayList();
113:
114: Collection ejbModules = m_mbeanServer.queryNames(
115: EjbModule.EJB_MODULE_QUERY_NAME, null);
116:
117: // For each application, getContainers()
118: for (Iterator i = ejbModules.iterator(); i.hasNext();) {
119: ObjectName ejbModule = (ObjectName) i.next();
120: String name = ejbModule.getKeyProperty("jndiName");
121:
122: // Loop on each container of the application
123: //Since we are just totaling everything, do a query on container object names
124:
125: Collection containers = (Collection) m_mbeanServer
126: .getAttribute(ejbModule, "Containers");
127: for (Iterator cs = containers.iterator(); cs.hasNext();) {
128: // Get the cache for each container
129: InstanceCache cache = null;
130: Object container = cs.next();
131: if (container instanceof EntityContainer) {
132: cache = ((EntityContainer) container)
133: .getInstanceCache();
134: } else if (container instanceof StatefulSessionContainer) {
135: cache = ((StatefulSessionContainer) container)
136: .getInstanceCache();
137: }
138:
139: // Take a cache snapshot
140: if (cache instanceof Monitorable) {
141: BeanCacheSnapshot snapshot = new BeanCacheSnapshot();
142: snapshot.m_application = name;
143: snapshot.m_container = ((Container) container)
144: .getBeanMetaData().getEjbName();
145: ((Monitorable) cache).sample(snapshot);
146: cacheSnapshots.add(snapshot);
147: }
148: }
149: }
150: return cacheSnapshots;
151: }
152:
153: // Inner classes -------------------------------------------------
154: }
|