KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > admin > ServerPanel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.admin;
6
7 import org.dijon.Container;
8 import org.dijon.ContainerResource;
9
10 import com.tc.admin.common.StatusRenderer;
11 import com.tc.admin.common.StatusView;
12 import com.tc.admin.common.XContainer;
13 import com.tc.admin.common.XObjectTable;
14 import com.tc.config.schema.L2Info;
15
16 import java.awt.Frame JavaDoc;
17 import java.awt.Toolkit JavaDoc;
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23
24 import javax.management.remote.JMXConnector JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JOptionPane JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
32 import javax.swing.table.TableColumnModel JavaDoc;
33
34 public class ServerPanel extends XContainer {
35   private AdminClientContext m_acc;
36   private ServerNode m_serverNode;
37   private JTextField JavaDoc m_hostField;
38   private JTextField JavaDoc m_portField;
39   private JButton JavaDoc m_connectButton;
40   static private ImageIcon JavaDoc m_connectIcon;
41   static private ImageIcon JavaDoc m_disconnectIcon;
42   private Container m_runtimeInfoPanel;
43   private StatusView m_statusView;
44   private ProductInfoPanel m_productInfoPanel;
45   private ProductInfoPanel m_altProductInfoPanel; // Displayed if the RuntimeInfoPanel is not.
46
private XObjectTable m_clusterMemberTable;
47   private ClusterMemberTableModel m_clusterMemberTableModel;
48
49   static {
50     m_connectIcon = new ImageIcon JavaDoc(ServerPanel.class.getResource("/com/tc/admin/icons/disconnect_co.gif"));
51     m_disconnectIcon = new ImageIcon JavaDoc(ServerPanel.class.getResource("/com/tc/admin/icons/newex_wiz.gif"));
52   }
53
54   public ServerPanel(ServerNode serverNode) {
55     super(serverNode);
56
57     m_serverNode = serverNode;
58     m_acc = AdminClient.getContext();
59
60     load((ContainerResource) m_acc.topRes.getComponent("ServerPanel"));
61
62     m_hostField = (JTextField JavaDoc) findComponent("HostField");
63     m_portField = (JTextField JavaDoc) findComponent("PortField");
64     m_connectButton = (JButton JavaDoc) findComponent("ConnectButton");
65     m_runtimeInfoPanel = (Container) findComponent("RuntimeInfoPanel");
66     m_statusView = (StatusView) findComponent("StatusIndicator");
67     m_productInfoPanel = (ProductInfoPanel) findComponent("ProductInfoPanel");
68     m_clusterMemberTable = (XObjectTable) findComponent("ClusterMembersTable");
69     m_clusterMemberTableModel = new ClusterMemberTableModel();
70     m_clusterMemberTable.setModel(m_clusterMemberTableModel);
71     TableColumnModel JavaDoc colModel = m_clusterMemberTable.getColumnModel();
72     colModel.getColumn(0).setCellRenderer(new ClusterMemberStatusRenderer());
73     colModel.getColumn(2).setCellRenderer(new XObjectTable.PortNumberRenderer());
74
75     m_statusView.setLabel("Not connected");
76     m_runtimeInfoPanel.setVisible(false);
77
78     m_hostField.addActionListener(new ActionListener JavaDoc() {
79       public void actionPerformed(ActionEvent JavaDoc ae) {
80         String JavaDoc host = m_hostField.getText().trim();
81
82         m_serverNode.setHost(host);
83         m_acc.controller.nodeChanged(m_serverNode);
84         m_acc.controller.updateServerPrefs();
85       }
86     });
87
88     m_portField.addActionListener(new ActionListener JavaDoc() {
89       public void actionPerformed(ActionEvent JavaDoc ae) {
90         String JavaDoc port = m_portField.getText().trim();
91
92         try {
93           m_serverNode.setPort(Integer.parseInt(port));
94           m_acc.controller.nodeChanged(m_serverNode);
95           m_acc.controller.updateServerPrefs();
96         } catch (Exception JavaDoc e) {
97           Toolkit.getDefaultToolkit().beep();
98           m_acc.controller.log("'" + port + "' not a number");
99           m_portField.setText(Integer.toString(m_serverNode.getPort()));
100         }
101       }
102     });
103
104     m_connectButton.addActionListener(new ActionListener JavaDoc() {
105       public void actionPerformed(ActionEvent JavaDoc ae) {
106         if (m_serverNode.isConnected()) {
107           disconnect();
108         } else {
109           connect();
110         }
111       }
112     });
113
114     m_hostField.setText(m_serverNode.getHost());
115     m_portField.setText(Integer.toString(m_serverNode.getPort()));
116
117     setupConnectButton();
118   }
119
120   void setupConnectButton() {
121     String JavaDoc label;
122     Icon JavaDoc icon;
123     boolean enabled;
124
125     if (m_serverNode.isConnected()) {
126       label = "Disconnect";
127       icon = m_disconnectIcon;
128       enabled = true;
129     } else {
130       label = "Connect...";
131       icon = m_connectIcon;
132       enabled = !m_serverNode.isAutoConnect();
133     }
134
135     m_connectButton.setText(label);
136     m_connectButton.setIcon(icon);
137     m_connectButton.setEnabled(enabled);
138   }
139
140   JButton JavaDoc getConnectButton() {
141     return m_connectButton;
142   }
143
144   private void connect() {
145     m_serverNode.connect();
146   }
147
148   void activated() {
149     m_hostField.setEditable(false);
150     m_portField.setEditable(false);
151
152     setupConnectButton();
153
154     Date JavaDoc activateDate = new Date JavaDoc(m_serverNode.getActivateTime());
155     String JavaDoc activateTime = DateFormat.getTimeInstance().format(activateDate);
156     String JavaDoc statusMsg = "Activated at " + activateTime;
157
158     setStatusLabel(statusMsg);
159     m_acc.controller.addServerLog(m_serverNode.getConnectionContext());
160     if(!isRuntimeInfoShowing()) {
161       showRuntimeInfo();
162     }
163
164     m_acc.controller.setStatus(m_serverNode + " activated at " + activateTime);
165   }
166
167   /**
168    * The only differences between activated() and started() is the status message and the serverlog is only added in
169    * activated() under the presumption that a non-active server won't be saying anything.
170    */

171   void started() {
172     m_hostField.setEditable(false);
173     m_portField.setEditable(false);
174
175     Date JavaDoc startDate = new Date JavaDoc(m_serverNode.getStartTime());
176     String JavaDoc startTime = DateFormat.getTimeInstance().format(startDate);
177     String JavaDoc statusMsg = "Started at " + startTime;
178
179     setupConnectButton();
180     setStatusLabel(statusMsg);
181     if(!isRuntimeInfoShowing()) {
182       showRuntimeInfo();
183     }
184
185     m_acc.controller.setStatus("Started " + m_serverNode + " at " + startTime);
186   }
187
188   void passiveUninitialized() {
189     m_hostField.setEditable(false);
190     m_portField.setEditable(false);
191
192     Date JavaDoc now = new Date JavaDoc();
193     String JavaDoc startTime = DateFormat.getTimeInstance().format(now);
194     String JavaDoc statusMsg = "Initializing at " + startTime;
195
196     setupConnectButton();
197     setStatusLabel(statusMsg);
198     if(!isRuntimeInfoShowing()) {
199       showRuntimeInfo();
200     }
201
202     m_acc.controller.setStatus("Initializing " + m_serverNode + " at " + startTime);
203   }
204
205   void passiveStandby() {
206     m_hostField.setEditable(false);
207     m_portField.setEditable(false);
208
209     Date JavaDoc now = new Date JavaDoc();
210     String JavaDoc startTime = DateFormat.getTimeInstance().format(now);
211     String JavaDoc statusMsg = "Standing by at " + startTime;
212
213     setupConnectButton();
214     setStatusLabel(statusMsg);
215     if(!isRuntimeInfoShowing()) {
216       showRuntimeInfo();
217     }
218
219     m_acc.controller.setStatus(m_serverNode + " standing by at " + startTime);
220   }
221
222   private void disconnect() {
223     m_serverNode.disconnect();
224   }
225
226   void disconnected() {
227     m_hostField.setEditable(true);
228     m_portField.setEditable(true);
229
230     String JavaDoc startTime = DateFormat.getTimeInstance().format(new Date JavaDoc());
231     String JavaDoc statusMsg = "Disconnected at " + startTime;
232
233     setupConnectButton();
234     setStatusLabel(statusMsg);
235     hideRuntimeInfo();
236
237     m_acc.controller.removeServerLog(m_serverNode.getConnectionContext());
238     m_acc.controller.setStatus(m_serverNode + " disconnected at " + startTime);
239   }
240
241   void setStatusLabel(String JavaDoc msg) {
242     m_statusView.setLabel(msg);
243     m_statusView.setIndicator(m_serverNode.getServerStatusColor());
244   }
245
246   private boolean isRuntimeInfoShowing() {
247     return m_runtimeInfoPanel.isVisible() ||
248       (m_altProductInfoPanel != null && m_altProductInfoPanel.isVisible());
249   }
250   
251   private void showRuntimeInfo() {
252     L2Info[] clusterMembers = m_serverNode.getClusterMembers();
253
254     m_clusterMemberTableModel.clear();
255
256     if (clusterMembers.length > 1) {
257       Container parent;
258
259       if (m_altProductInfoPanel != null && (parent = (Container) m_altProductInfoPanel.getParent()) != null) {
260         parent.replaceChild(m_altProductInfoPanel, m_runtimeInfoPanel);
261       }
262
263       m_productInfoPanel.init(m_serverNode.getProductInfo());
264       m_runtimeInfoPanel.setVisible(true);
265
266       for (int i = 0; i < clusterMembers.length; i++) {
267         addClusterMember(clusterMembers[i]);
268       }
269       m_clusterMemberTableModel.fireTableDataChanged();
270     } else {
271       if (m_altProductInfoPanel == null) {
272         m_altProductInfoPanel = new ProductInfoPanel();
273       }
274
275       Container parent;
276       if ((parent = (Container) m_runtimeInfoPanel.getParent()) != null) {
277         parent.replaceChild(m_runtimeInfoPanel, m_altProductInfoPanel);
278       }
279
280       m_altProductInfoPanel.init(m_serverNode.getProductInfo());
281       m_altProductInfoPanel.setVisible(true);
282     }
283
284     revalidate();
285     repaint();
286   }
287
288   private void hideRuntimeInfo() {
289     m_clusterMemberTableModel.clear();
290     m_runtimeInfoPanel.setVisible(false);
291     if(m_altProductInfoPanel != null) {
292       m_altProductInfoPanel.setVisible(false);
293     }
294     revalidate();
295     repaint();
296   }
297
298   private class ClusterMemberStatusRenderer extends StatusRenderer {
299     ClusterMemberStatusRenderer() {
300       super();
301     }
302
303     public void setValue(JTable JavaDoc table, int row, int col) {
304       ServerConnectionManager member = m_clusterMemberTableModel.getClusterMemberAt(row);
305
306       m_label.setText(member.getName());
307       m_indicator.setBackground(ServerNode.getServerStatusColor(member));
308     }
309   }
310
311   private class ClusterMemberListener implements ConnectionListener {
312     ServerConnectionManager m_scm;
313     ConnectDialog m_cd;
314     
315     void setServerConnectionManager(ServerConnectionManager scm) {
316       m_scm = scm;
317     }
318     
319     public void handleConnection() {
320       SwingUtilities.invokeLater(new Runnable JavaDoc() {
321         public void run() {
322           if (m_clusterMemberTableModel != null) {
323             int count = m_clusterMemberTableModel.getRowCount();
324             m_clusterMemberTableModel.fireTableRowsUpdated(0, count - 1);
325           }
326         }
327       });
328     }
329
330     class ConnectDialogListener implements ConnectionListener {
331       public void handleConnection() {
332         JMXConnector JavaDoc jmxc;
333         if ((jmxc = m_cd.getConnector()) != null) {
334           try {
335             m_scm.setJMXConnector(jmxc);
336             m_scm.setAutoConnect(true);
337           } catch (IOException JavaDoc ioe) {/**/}
338         }
339       }
340       
341       public void handleException() {
342         final Exception JavaDoc error = m_cd.getError();
343         
344         m_acc.log("Failed to connect to '"+m_scm+"' ["+error.getMessage()+"]");
345
346         if(error instanceof SecurityException JavaDoc) {
347           SwingUtilities.invokeLater(new Runnable JavaDoc() {
348             public void run() {
349               AdminClientPanel topPanel = (AdminClientPanel) ServerPanel.this.getAncestorOfClass(AdminClientPanel.class);
350               Frame JavaDoc frame = (Frame JavaDoc) topPanel.getAncestorOfClass(java.awt.Frame JavaDoc.class);
351               String JavaDoc msg = "Failed to connect to '"+m_scm+"'\n\n"+error.getMessage()+"\n\nTry again to connect?";
352               
353               int result = JOptionPane.showConfirmDialog(frame, msg, frame.getTitle(), JOptionPane.YES_NO_OPTION);
354               if(result == JOptionPane.OK_OPTION) {
355                 m_cd.center(frame);
356                 m_cd.setVisible(true);
357               }
358             }
359           });
360         }
361       }
362     }
363   
364     void connect() {
365       AdminClientPanel topPanel = (AdminClientPanel) ServerPanel.this.getAncestorOfClass(AdminClientPanel.class);
366       Frame JavaDoc frame = (Frame JavaDoc) topPanel.getAncestorOfClass(java.awt.Frame JavaDoc.class);
367
368       m_cd = m_serverNode.getConnectDialog(new ConnectDialogListener());
369       m_cd.setServerConnectionManager(m_scm);
370       m_cd.center(frame);
371       m_cd.setVisible(true);
372     }
373     
374     public void handleException() {
375       if(m_cd != null && m_cd.isVisible()) return;
376       
377       SwingUtilities.invokeLater(new Runnable JavaDoc() {
378         public void run() {
379           Exception JavaDoc e = m_scm.getConnectionException();
380           if(e instanceof SecurityException JavaDoc) {
381             connect();
382           }
383
384           if (m_clusterMemberTableModel != null) {
385             int count = m_clusterMemberTableModel.getRowCount();
386             m_clusterMemberTableModel.fireTableRowsUpdated(0, count - 1);
387           }
388         }
389       });
390     }
391   }
392
393   void addClusterMember(L2Info clusterMember) {
394     ClusterMemberListener cml = new ClusterMemberListener();
395     ServerConnectionManager scm = new ServerConnectionManager(clusterMember, false, cml);
396     String JavaDoc[] creds = ServerConnectionManager.getCachedCredentials(scm);
397     
398     if(creds == null) {
399       creds = m_serverNode.getServerConnectionManager().getCredentials();
400     }
401     if(creds != null) {
402       scm.setCredentials(creds[0], creds[1]);
403     }
404     cml.setServerConnectionManager(scm);
405     scm.setAutoConnect(true);
406     
407     m_clusterMemberTableModel.addClusterMember(scm);
408   }
409
410   ServerConnectionManager[] getClusterMembers() {
411     int count = m_clusterMemberTableModel.getRowCount();
412     ServerConnectionManager[] result = new ServerConnectionManager[count];
413     
414     for(int i = 0; i < count; i++) {
415       result[i] = m_clusterMemberTableModel.getClusterMemberAt(i);
416     }
417     
418     return result;
419   }
420   
421   public void tearDown() {
422     super.tearDown();
423
424     m_statusView.tearDown();
425     m_productInfoPanel.tearDown();
426     m_clusterMemberTableModel.tearDown();
427
428     m_acc = null;
429     m_serverNode = null;
430     m_hostField = null;
431     m_portField = null;
432     m_connectButton = null;
433     m_runtimeInfoPanel = null;
434     m_statusView = null;
435     m_productInfoPanel = null;
436     m_clusterMemberTable = null;
437     m_clusterMemberTableModel = null;
438   }
439 }
440
Popular Tags