001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.admin;
006:
007: import org.dijon.Container;
008: import org.dijon.ContainerResource;
009:
010: import com.tc.admin.common.StatusRenderer;
011: import com.tc.admin.common.StatusView;
012: import com.tc.admin.common.XContainer;
013: import com.tc.admin.common.XObjectTable;
014: import com.tc.config.schema.L2Info;
015:
016: import java.awt.Frame;
017: import java.awt.Toolkit;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.ActionListener;
020: import java.io.IOException;
021: import java.util.Date;
022:
023: import javax.management.remote.JMXConnector;
024: import javax.swing.Icon;
025: import javax.swing.ImageIcon;
026: import javax.swing.JButton;
027: import javax.swing.JOptionPane;
028: import javax.swing.JTable;
029: import javax.swing.JTextField;
030: import javax.swing.SwingUtilities;
031: import javax.swing.table.TableColumnModel;
032:
033: public class ServerPanel extends XContainer {
034: private AdminClientContext m_acc;
035: private ServerNode m_serverNode;
036: private JTextField m_hostField;
037: private JTextField m_portField;
038: private JButton m_connectButton;
039: static private ImageIcon m_connectIcon;
040: static private ImageIcon m_disconnectIcon;
041: private Container m_runtimeInfoPanel;
042: private StatusView m_statusView;
043: private JButton m_shutdownButton;
044: private ProductInfoPanel m_productInfoPanel;
045: private ProductInfoPanel m_altProductInfoPanel; // Displayed if the RuntimeInfoPanel is not.
046: private XObjectTable m_clusterMemberTable;
047: private ClusterMemberTableModel m_clusterMemberTableModel;
048:
049: static {
050: m_connectIcon = new ImageIcon(ServerPanel.class
051: .getResource("/com/tc/admin/icons/disconnect_co.gif"));
052: m_disconnectIcon = new ImageIcon(ServerPanel.class
053: .getResource("/com/tc/admin/icons/newex_wiz.gif"));
054: }
055:
056: public ServerPanel(ServerNode serverNode) {
057: super (serverNode);
058:
059: m_serverNode = serverNode;
060: m_acc = AdminClient.getContext();
061:
062: load((ContainerResource) m_acc.topRes
063: .getComponent("ServerPanel"));
064:
065: m_hostField = (JTextField) findComponent("HostField");
066: m_portField = (JTextField) findComponent("PortField");
067: m_connectButton = (JButton) findComponent("ConnectButton");
068: m_runtimeInfoPanel = (Container) findComponent("RuntimeInfoPanel");
069: m_statusView = (StatusView) findComponent("StatusIndicator");
070: m_shutdownButton = (JButton) findComponent("ShutdownButton");
071: m_productInfoPanel = (ProductInfoPanel) findComponent("ProductInfoPanel");
072: m_clusterMemberTable = (XObjectTable) findComponent("ClusterMembersTable");
073: m_clusterMemberTableModel = new ClusterMemberTableModel();
074: m_clusterMemberTable.setModel(m_clusterMemberTableModel);
075: TableColumnModel colModel = m_clusterMemberTable
076: .getColumnModel();
077: colModel.getColumn(0).setCellRenderer(
078: new ClusterMemberStatusRenderer());
079: colModel.getColumn(2).setCellRenderer(
080: new XObjectTable.PortNumberRenderer());
081:
082: m_statusView.setLabel("Not connected");
083: m_runtimeInfoPanel.setVisible(false);
084:
085: m_hostField.addActionListener(new ActionListener() {
086: public void actionPerformed(ActionEvent ae) {
087: String host = m_hostField.getText().trim();
088:
089: m_serverNode.setHost(host);
090: m_acc.controller.nodeChanged(m_serverNode);
091: m_acc.controller.updateServerPrefs();
092: }
093: });
094:
095: m_portField.addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent ae) {
097: String port = m_portField.getText().trim();
098:
099: try {
100: m_serverNode.setPort(Integer.parseInt(port));
101: m_acc.controller.nodeChanged(m_serverNode);
102: m_acc.controller.updateServerPrefs();
103: } catch (Exception e) {
104: Toolkit.getDefaultToolkit().beep();
105: m_acc.controller.log("'" + port + "' not a number");
106: m_portField.setText(Integer.toString(m_serverNode
107: .getPort()));
108: }
109: }
110: });
111:
112: m_connectButton.addActionListener(new ActionListener() {
113: public void actionPerformed(ActionEvent ae) {
114: if (m_serverNode.isConnected()) {
115: disconnect();
116: } else {
117: connect();
118: }
119: }
120: });
121:
122: m_hostField.setText(m_serverNode.getHost());
123: m_portField.setText(Integer.toString(m_serverNode.getPort()));
124:
125: m_shutdownButton.setAction(m_serverNode.getShutdownAction());
126:
127: setupConnectButton();
128: }
129:
130: void setupConnectButton() {
131: String label;
132: Icon icon;
133: boolean enabled;
134:
135: if (m_serverNode.isConnected()) {
136: label = "Disconnect";
137: icon = m_disconnectIcon;
138: enabled = true;
139: } else {
140: label = "Connect...";
141: icon = m_connectIcon;
142: enabled = !m_serverNode.isAutoConnect();
143: }
144:
145: m_connectButton.setText(label);
146: m_connectButton.setIcon(icon);
147: m_connectButton.setEnabled(enabled);
148: }
149:
150: JButton getConnectButton() {
151: return m_connectButton;
152: }
153:
154: private void connect() {
155: m_serverNode.connect();
156: }
157:
158: void activated() {
159: m_hostField.setEditable(false);
160: m_portField.setEditable(false);
161:
162: setupConnectButton();
163:
164: Date activateDate = new Date(m_serverNode.getActivateTime());
165: String activateTime = activateDate.toString();
166: String statusMsg = "Activated at " + activateTime;
167:
168: setStatusLabel(statusMsg);
169: m_acc.controller.addServerLog(m_serverNode
170: .getConnectionContext());
171: if (!isRuntimeInfoShowing()) {
172: showRuntimeInfo();
173: }
174:
175: m_acc.controller.setStatus(m_serverNode + " activated at "
176: + activateTime);
177: }
178:
179: /**
180: * The only differences between activated() and started() is the status message and the serverlog is only added in
181: * activated() under the presumption that a non-active server won't be saying anything.
182: */
183: void started() {
184: m_hostField.setEditable(false);
185: m_portField.setEditable(false);
186:
187: Date startDate = new Date(m_serverNode.getStartTime());
188: String startTime = startDate.toString();
189: String statusMsg = "Started at " + startTime;
190:
191: setupConnectButton();
192: setStatusLabel(statusMsg);
193: if (!isRuntimeInfoShowing()) {
194: showRuntimeInfo();
195: }
196:
197: m_acc.controller.setStatus("Started " + m_serverNode + " at "
198: + startTime);
199: }
200:
201: void passiveUninitialized() {
202: m_hostField.setEditable(false);
203: m_portField.setEditable(false);
204:
205: String startTime = new Date().toString();
206: String statusMsg = "Initializing at " + startTime;
207:
208: setupConnectButton();
209: setStatusLabel(statusMsg);
210: if (!isRuntimeInfoShowing()) {
211: showRuntimeInfo();
212: }
213:
214: m_acc.controller.setStatus("Initializing " + m_serverNode
215: + " at " + startTime);
216: }
217:
218: void passiveStandby() {
219: m_hostField.setEditable(false);
220: m_portField.setEditable(false);
221:
222: String startTime = new Date().toString();
223: String statusMsg = "Standing by at " + startTime;
224:
225: setupConnectButton();
226: setStatusLabel(statusMsg);
227: if (!isRuntimeInfoShowing()) {
228: showRuntimeInfo();
229: }
230:
231: m_acc.controller.setStatus(m_serverNode + " standing by at "
232: + startTime);
233: }
234:
235: private void disconnect() {
236: m_serverNode.disconnect();
237: }
238:
239: void disconnected() {
240: m_hostField.setEditable(true);
241: m_portField.setEditable(true);
242:
243: String startTime = new Date().toString();
244: String statusMsg = "Disconnected at " + startTime;
245:
246: setupConnectButton();
247: setStatusLabel(statusMsg);
248: hideRuntimeInfo();
249:
250: m_acc.controller.removeServerLog(m_serverNode
251: .getConnectionContext());
252: m_acc.controller.setStatus(m_serverNode + " disconnected at "
253: + startTime);
254: }
255:
256: void setStatusLabel(String msg) {
257: m_statusView.setLabel(msg);
258: m_statusView.setIndicator(m_serverNode.getServerStatusColor());
259: }
260:
261: boolean isRuntimeInfoShowing() {
262: return m_runtimeInfoPanel.isVisible()
263: || (m_altProductInfoPanel != null && m_altProductInfoPanel
264: .isVisible());
265: }
266:
267: private void showRuntimeInfo() {
268: L2Info[] clusterMembers = m_serverNode.getClusterMembers();
269:
270: m_clusterMemberTableModel.clear();
271:
272: if (clusterMembers.length > 1) {
273: Container parent;
274:
275: if (m_altProductInfoPanel != null
276: && (parent = (Container) m_altProductInfoPanel
277: .getParent()) != null) {
278: parent.replaceChild(m_altProductInfoPanel,
279: m_runtimeInfoPanel);
280: }
281:
282: m_productInfoPanel.init(m_serverNode.getProductInfo());
283: m_runtimeInfoPanel.setVisible(true);
284:
285: for (int i = 0; i < clusterMembers.length; i++) {
286: addClusterMember(clusterMembers[i]);
287: }
288: m_clusterMemberTableModel.fireTableDataChanged();
289: } else {
290: if (m_altProductInfoPanel == null) {
291: m_altProductInfoPanel = new ProductInfoPanel();
292: }
293:
294: Container parent;
295: if ((parent = (Container) m_runtimeInfoPanel.getParent()) != null) {
296: parent.replaceChild(m_runtimeInfoPanel,
297: m_altProductInfoPanel);
298: }
299:
300: m_altProductInfoPanel.init(m_serverNode.getProductInfo());
301: m_altProductInfoPanel.setVisible(true);
302: }
303:
304: revalidate();
305: repaint();
306: }
307:
308: private void hideRuntimeInfo() {
309: m_clusterMemberTableModel.clear();
310: m_runtimeInfoPanel.setVisible(false);
311: if (m_altProductInfoPanel != null) {
312: m_altProductInfoPanel.setVisible(false);
313: }
314: revalidate();
315: repaint();
316: }
317:
318: private class ClusterMemberStatusRenderer extends StatusRenderer {
319: ClusterMemberStatusRenderer() {
320: super ();
321: }
322:
323: public void setValue(JTable table, int row, int col) {
324: ServerConnectionManager member = m_clusterMemberTableModel
325: .getClusterMemberAt(row);
326:
327: m_label.setText(member.getName());
328: m_indicator.setBackground(ServerNode
329: .getServerStatusColor(member));
330: }
331: }
332:
333: private class ClusterMemberListener implements ConnectionListener {
334: ServerConnectionManager m_scm;
335: ConnectDialog m_cd;
336:
337: void setServerConnectionManager(ServerConnectionManager scm) {
338: m_scm = scm;
339: }
340:
341: public void handleConnection() {
342: SwingUtilities.invokeLater(new Runnable() {
343: public void run() {
344: if (m_clusterMemberTableModel != null) {
345: int count = m_clusterMemberTableModel
346: .getRowCount();
347: m_clusterMemberTableModel.fireTableRowsUpdated(
348: 0, count - 1);
349: }
350: }
351: });
352: }
353:
354: class ConnectDialogListener implements ConnectionListener {
355: public void handleConnection() {
356: JMXConnector jmxc;
357: if ((jmxc = m_cd.getConnector()) != null) {
358: try {
359: m_scm.setJMXConnector(jmxc);
360: m_scm.setAutoConnect(true);
361: } catch (IOException ioe) {/**/
362: }
363: }
364: }
365:
366: public void handleException() {
367: final Exception error = m_cd.getError();
368:
369: m_acc.log("Failed to connect to '" + m_scm + "' ["
370: + error.getMessage() + "]");
371:
372: if (error instanceof SecurityException) {
373: SwingUtilities.invokeLater(new Runnable() {
374: public void run() {
375: AdminClientPanel topPanel = (AdminClientPanel) ServerPanel.this
376: .getAncestorOfClass(AdminClientPanel.class);
377: Frame frame = (Frame) topPanel
378: .getAncestorOfClass(java.awt.Frame.class);
379: String msg = "Failed to connect to '"
380: + m_scm + "'\n\n"
381: + error.getMessage()
382: + "\n\nTry again to connect?";
383:
384: int result = JOptionPane.showConfirmDialog(
385: frame, msg, frame.getTitle(),
386: JOptionPane.YES_NO_OPTION);
387: if (result == JOptionPane.OK_OPTION) {
388: m_cd.center(frame);
389: m_cd.setVisible(true);
390: }
391: }
392: });
393: }
394: }
395: }
396:
397: void connect() {
398: AdminClientPanel topPanel = (AdminClientPanel) ServerPanel.this
399: .getAncestorOfClass(AdminClientPanel.class);
400: Frame frame = (Frame) topPanel
401: .getAncestorOfClass(java.awt.Frame.class);
402:
403: m_cd = m_serverNode
404: .getConnectDialog(new ConnectDialogListener());
405: m_cd.setServerConnectionManager(m_scm);
406: m_cd.center(frame);
407: m_cd.setVisible(true);
408: }
409:
410: public void handleException() {
411: if (m_cd != null && m_cd.isVisible())
412: return;
413:
414: SwingUtilities.invokeLater(new Runnable() {
415: public void run() {
416: Exception e = m_scm.getConnectionException();
417: if (e instanceof SecurityException) {
418: connect();
419: }
420:
421: if (m_clusterMemberTableModel != null) {
422: int count = m_clusterMemberTableModel
423: .getRowCount();
424: m_clusterMemberTableModel.fireTableRowsUpdated(
425: 0, count - 1);
426: }
427: }
428: });
429: }
430: }
431:
432: void addClusterMember(L2Info clusterMember) {
433: ClusterMemberListener cml = new ClusterMemberListener();
434: ServerConnectionManager scm = new ServerConnectionManager(
435: clusterMember, false, cml);
436: String[] creds = ServerConnectionManager
437: .getCachedCredentials(scm);
438:
439: if (creds == null) {
440: creds = m_serverNode.getServerConnectionManager()
441: .getCredentials();
442: }
443: if (creds != null) {
444: scm.setCredentials(creds[0], creds[1]);
445: }
446: cml.setServerConnectionManager(scm);
447: scm.setAutoConnect(true);
448:
449: m_clusterMemberTableModel.addClusterMember(scm);
450: }
451:
452: ServerConnectionManager[] getClusterMembers() {
453: int count = m_clusterMemberTableModel.getRowCount();
454: ServerConnectionManager[] result = new ServerConnectionManager[count];
455:
456: for (int i = 0; i < count; i++) {
457: result[i] = m_clusterMemberTableModel.getClusterMemberAt(i);
458: }
459:
460: return result;
461: }
462:
463: public void tearDown() {
464: super.tearDown();
465:
466: m_statusView.tearDown();
467: m_productInfoPanel.tearDown();
468: m_clusterMemberTableModel.tearDown();
469:
470: m_acc = null;
471: m_serverNode = null;
472: m_hostField = null;
473: m_portField = null;
474: m_connectButton = null;
475: m_runtimeInfoPanel = null;
476: m_statusView = null;
477: m_productInfoPanel = null;
478: m_clusterMemberTable = null;
479: m_clusterMemberTableModel = null;
480: }
481: }
|