KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > BeanCacheMonitor


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.monitor;
23
24
25
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Set JavaDoc;
32 import javax.management.JMException JavaDoc;
33 import javax.management.MBeanRegistration JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.ObjectInstance JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import org.jboss.ejb.Container;
38 import org.jboss.ejb.EJBDeployer;
39 import org.jboss.ejb.EJBDeployerMBean;
40 import org.jboss.ejb.EjbModule;
41 import org.jboss.ejb.EntityContainer;
42 import org.jboss.ejb.InstanceCache;
43 import org.jboss.ejb.StatefulSessionContainer;
44 import org.jboss.logging.Logger;
45 import org.jboss.monitor.client.BeanCacheSnapshot;
46
47 /**
48  *
49  * @see Monitorable
50  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
51  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
52  * @version $Revision: 37459 $
53  */

54 public class BeanCacheMonitor
55    implements BeanCacheMonitorMBean, MBeanRegistration JavaDoc
56 {
57    // Constants ----------------------------------------------------
58

59    // Attributes ---------------------------------------------------
60
static Logger log = Logger.getLogger(BeanCacheMonitor.class);
61    MBeanServer JavaDoc m_mbeanServer;
62    // Static -------------------------------------------------------
63

64    // Constructors -------------------------------------------------
65
public BeanCacheMonitor()
66    {}
67    
68    // Public -------------------------------------------------------
69

70    // MBeanRegistration implementation -----------------------------------
71
public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
72    throws Exception JavaDoc
73    {
74       m_mbeanServer = server;
75       return name;
76    }
77    
78    public void postRegister(Boolean JavaDoc registrationDone)
79    {}
80    public void preDeregister() throws Exception JavaDoc
81    {}
82    public void postDeregister()
83    {}
84    
85    // CacheMonitorMBean implementation -----------------------------------
86
/**
87     * Describe <code>getSnapshots</code> method here.
88     *
89     * @return a <code>BeanCacheSnapshot[]</code> value
90     * @todo: convert to queries on object names of components.
91     */

92    public BeanCacheSnapshot[] getSnapshots()
93    {
94       try
95       {
96          Collection JavaDoc snapshots = listSnapshots();
97          BeanCacheSnapshot[] snapshotArray = new BeanCacheSnapshot[snapshots.size()];
98          return (BeanCacheSnapshot[])snapshots.toArray(snapshotArray);
99       }
100       catch (JMException JavaDoc e)
101       {
102          log.error("Problem getting bean cache snapshots", e);
103          return null;
104       } // end of try-catch
105
}
106
107    /**
108     * The <code>listSnapshots</code> method returns a collection
109     * of BeanSnapshots showing the
110     *
111     * @return a <code>Collection</code> value
112     * @exception JMException if an error occurs
113     */

114    public Collection JavaDoc listSnapshots() throws JMException JavaDoc
115    {
116       ArrayList JavaDoc cacheSnapshots = new ArrayList JavaDoc();
117
118       Collection JavaDoc ejbModules = m_mbeanServer.queryNames(EjbModule.EJB_MODULE_QUERY_NAME, null);
119       
120       // For each application, getContainers()
121
for (Iterator JavaDoc i = ejbModules.iterator(); i.hasNext(); )
122       {
123          ObjectName JavaDoc ejbModule = (ObjectName JavaDoc) i.next();
124          String JavaDoc name = ejbModule.getKeyProperty("jndiName");
125          
126          // Loop on each container of the application
127
//Since we are just totaling everything, do a query on container object names
128

129          Collection JavaDoc containers = (Collection JavaDoc)m_mbeanServer.getAttribute(ejbModule, "Containers");
130          for (Iterator JavaDoc cs = containers.iterator(); cs.hasNext();)
131          {
132             // Get the cache for each container
133
InstanceCache cache = null;
134             Object JavaDoc container = cs.next();
135             if (container instanceof EntityContainer)
136             {
137                cache = ((EntityContainer)container).getInstanceCache();
138             }
139             else if (container instanceof StatefulSessionContainer)
140             {
141                cache = ((StatefulSessionContainer)container).getInstanceCache();
142             }
143             
144             // Take a cache snapshot
145
if (cache instanceof Monitorable)
146             {
147                BeanCacheSnapshot snapshot = new BeanCacheSnapshot();
148                snapshot.m_application = name;
149                snapshot.m_container = ((Container)container).getBeanMetaData().getEjbName();
150                ((Monitorable)cache).sample(snapshot);
151                cacheSnapshots.add(snapshot);
152             }
153          }
154       }
155       return cacheSnapshots;
156    }
157    
158    // Inner classes -------------------------------------------------
159
}
160
161
Popular Tags