001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.sessions;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.ConnectionContext;
008: import com.tc.admin.common.ComponentNode;
009: import com.tc.admin.common.XAbstractAction;
010: import com.tc.admin.dso.ClassesHelper;
011: import com.tc.management.TerracottaManagement;
012: import com.tc.management.exposed.SessionsProductMBean;
013:
014: import java.awt.event.ActionEvent;
015: import java.awt.event.KeyEvent;
016: import java.awt.event.MouseEvent;
017: import java.io.IOException;
018:
019: import javax.management.MBeanServerNotification;
020: import javax.management.Notification;
021: import javax.management.NotificationListener;
022: import javax.management.ObjectName;
023: import javax.swing.JPopupMenu;
024: import javax.swing.KeyStroke;
025: import javax.swing.tree.DefaultTreeModel;
026:
027: public class SessionsNode extends ComponentNode implements
028: NotificationListener {
029: private ConnectionContext m_cc;
030: private JPopupMenu m_popupMenu;
031: private RefreshAction m_refreshAction;
032:
033: private static final String REFRESH_ACTION = "RefreshAction";
034:
035: public SessionsNode(ConnectionContext cc, ObjectName[] beanNames) {
036: super ();
037:
038: m_cc = cc;
039:
040: try {
041: ObjectName mbsd = cc
042: .queryName("JMImplementation:type=MBeanServerDelegate");
043:
044: if (mbsd != null) {
045: cc.addNotificationListener(mbsd, this );
046: }
047: } catch (Exception ioe) {
048: ioe.printStackTrace();
049: }
050:
051: for (int i = 0; i < beanNames.length; i++) {
052: try {
053: addBean(beanNames[i]);
054: } catch (IOException ioe) {
055: ioe.printStackTrace();
056: }
057: }
058:
059: setLabel("Session Statistics");
060:
061: initMenu();
062: }
063:
064: private void addBean(ObjectName name) throws IOException {
065: SessionsProductMBean bean = (SessionsProductMBean) TerracottaManagement
066: .findMBean(name, SessionsProductMBean.class, m_cc.mbsc);
067: SessionsProductNode node = new SessionsProductNode(m_cc, bean,
068: name);
069: DefaultTreeModel model = getModel();
070:
071: if (model != null) {
072: model.insertNodeInto(node, this , getChildCount());
073: } else {
074: add(node);
075: }
076: }
077:
078: public void handleNotification(Notification notification,
079: Object handback) {
080: if (notification instanceof MBeanServerNotification) {
081: MBeanServerNotification mbsn = (MBeanServerNotification) notification;
082: String type = notification.getType();
083: ObjectName name = mbsn.getMBeanName();
084:
085: if (type
086: .equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
087: if (SessionsHelper.getHelper().isSessionsProductMBean(
088: name)) {
089: try {
090: addBean(name);
091: } catch (IOException ioe) {
092: ioe.printStackTrace();
093: }
094: }
095: }
096: }
097: }
098:
099: private void initMenu() {
100: m_refreshAction = new RefreshAction();
101:
102: m_popupMenu = new JPopupMenu("SessionNode Actions");
103: m_popupMenu.add(m_refreshAction);
104:
105: addActionBinding(REFRESH_ACTION, m_refreshAction);
106: }
107:
108: public JPopupMenu getPopupMenu() {
109: return m_popupMenu;
110: }
111:
112: public void refresh() {
113: /**/
114: }
115:
116: private class RefreshAction extends XAbstractAction {
117: private RefreshAction() {
118: super ();
119:
120: setName(AdminClient.getContext().getMessage("refresh.name"));
121: setSmallIcon(ClassesHelper.getHelper().getRefreshIcon());
122: setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
123: true));
124: }
125:
126: public void actionPerformed(ActionEvent ae) {
127: refresh();
128: }
129: }
130:
131: public void nodeClicked(MouseEvent me) {
132: m_refreshAction.actionPerformed(null);
133: }
134: }
|