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.dso;
005:
006: import com.tc.admin.AdminClient;
007: import com.tc.admin.AdminClientContext;
008: import com.tc.admin.ConnectionContext;
009: import com.tc.admin.common.ComponentNode;
010: import com.tc.admin.common.XTreeModel;
011: import com.tc.admin.common.XTreeNode;
012: import com.tc.stats.DSOMBean;
013:
014: import java.util.ArrayList;
015: import java.util.Arrays;
016:
017: import javax.management.Notification;
018: import javax.management.NotificationListener;
019: import javax.management.ObjectName;
020: import javax.swing.SwingUtilities;
021:
022: public class ClientsNode extends ComponentNode implements
023: NotificationListener {
024: private ConnectionContext m_cc;
025: private DSOClient[] m_clients;
026:
027: public ClientsNode(ConnectionContext cc) throws Exception {
028: super ();
029:
030: m_cc = cc;
031:
032: ObjectName dso = DSOHelper.getHelper().getDSOMBean(m_cc);
033: m_clients = ClientsHelper.getHelper().getClients(m_cc);
034:
035: for (int i = 0; i < m_clients.length; i++) {
036: add(new ClientTreeNode(m_cc, m_clients[i]));
037: }
038:
039: ClientsPanel panel = new ClientsPanel(m_clients);
040:
041: panel.setNode(this );
042: setLabel(AdminClient.getContext().getMessage("clients"));
043: setComponent(panel);
044:
045: m_cc.addNotificationListener(dso, this );
046: }
047:
048: public DSOClient[] getClients() {
049: return m_clients;
050: }
051:
052: public void handleNotification(final Notification notice,
053: final Object handback) {
054: String type = notice.getType();
055:
056: if (DSOMBean.CLIENT_ATTACHED.equals(type)
057: || DSOMBean.CLIENT_DETACHED.equals(type)) {
058: SwingUtilities.invokeLater(new Runnable() {
059: public void run() {
060: internalHandleNotification(notice, handback);
061: }
062: });
063: }
064: }
065:
066: private boolean haveClient(ObjectName objectName) {
067: if (m_clients == null)
068: return false;
069: for (DSOClient client : m_clients) {
070: if (client.getObjectName().equals(objectName)) {
071: return true;
072: }
073: }
074: return false;
075: }
076:
077: private void internalHandleNotification(Notification notice,
078: Object handback) {
079: String type = notice.getType();
080:
081: if (DSOMBean.CLIENT_ATTACHED.equals(type)) {
082: AdminClientContext acc = AdminClient.getContext();
083:
084: ObjectName clientObjectName = (ObjectName) notice
085: .getSource();
086: if (haveClient(clientObjectName))
087: return;
088:
089: acc.setStatus(acc.getMessage("dso.client.retrieving"));
090:
091: DSOClient client = new DSOClient(m_cc, clientObjectName);
092: ArrayList<DSOClient> list = new ArrayList<DSOClient>(Arrays
093: .asList(m_clients));
094:
095: list.add(client);
096: m_clients = list.toArray(new DSOClient[0]);
097:
098: ClientTreeNode ctn = new ClientTreeNode(m_cc, client);
099: XTreeModel model = getModel();
100: if (model != null) {
101: model.insertNodeInto(ctn, this , getChildCount());
102: } else {
103: add(ctn);
104: }
105:
106: ((ClientsPanel) getComponent()).add(client);
107:
108: acc.setStatus(acc.getMessage("dso.client.new") + client);
109: } else if (DSOMBean.CLIENT_DETACHED.equals(type)) {
110: AdminClientContext acc = AdminClient.getContext();
111:
112: acc.setStatus(acc.getMessage("dso.client.detaching"));
113:
114: int nodeIndex = -1;
115: ObjectName clientObjectName = (ObjectName) notice
116: .getSource();
117: ArrayList<DSOClient> list = new ArrayList<DSOClient>(Arrays
118: .asList(m_clients));
119: DSOClient client = null;
120:
121: for (int i = 0; i < list.size(); i++) {
122: client = list.get(i);
123:
124: if (client.getObjectName().equals(clientObjectName)) {
125: list.remove(client);
126: m_clients = list.toArray(new DSOClient[] {});
127: nodeIndex = i;
128: break;
129: }
130: }
131:
132: if (nodeIndex != -1) {
133: acc.controller
134: .remove((XTreeNode) getChildAt(nodeIndex));
135: ((ClientsPanel) getComponent()).remove(client);
136: }
137:
138: acc.setStatus(acc.getMessage("dso.client.detached")
139: + client);
140: }
141: }
142:
143: public void tearDown() {
144: try {
145: ObjectName dso = DSOHelper.getHelper().getDSOMBean(m_cc);
146: if (dso != null) {
147: m_cc.removeNotificationListener(dso, this );
148: }
149: } catch (Exception e) {/**/
150: }
151: m_cc = null;
152: m_clients = null;
153:
154: super.tearDown();
155: }
156: }
|