KickJava   Java API By Example, From Geeks To Geeks.

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


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.Button;
8 import org.dijon.Container;
9 import org.dijon.Dialog;
10 import org.dijon.DialogResource;
11 import org.dijon.Label;
12 import org.dijon.TextField;
13
14 import com.tc.util.event.UpdateEventListener;
15
16 import java.awt.BorderLayout JavaDoc;
17 import java.awt.Frame JavaDoc;
18 import java.awt.event.ActionEvent JavaDoc;
19 import java.awt.event.ActionListener JavaDoc;
20 import java.awt.event.HierarchyEvent JavaDoc;
21 import java.awt.event.HierarchyListener JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InterruptedIOException JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.management.remote.JMXConnector JavaDoc;
27 import javax.management.remote.JMXServiceURL JavaDoc;
28 import javax.swing.JPasswordField JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30 import javax.swing.SwingUtilities JavaDoc;
31 import javax.swing.Timer JavaDoc;
32
33 public final class ConnectDialog extends Dialog {
34   private static final long DEFAULT_CONNECT_TIMEOUT_MILLIS = 8000;
35   public static final long CONNECT_TIMEOUT_MILLIS = Long.getLong("com.tc.admin.connect-timeout",
36                                                                           DEFAULT_CONNECT_TIMEOUT_MILLIS).longValue();
37
38   private ServerConnectionManager m_connectManager;
39   private long m_timeout;
40   private ConnectionListener m_listener;
41   private JMXConnector JavaDoc m_jmxc;
42   private Thread JavaDoc m_mainThread;
43   private Thread JavaDoc m_connectThread;
44   private Timer JavaDoc m_timer;
45   private Exception JavaDoc m_error;
46   private Label m_label;
47   private Button m_cancelButton;
48   private final JTextField JavaDoc m_usernameField;
49   private final JPasswordField JavaDoc m_passwordField;
50   private final Button m_okButton;
51   private final Button m_authCancelButton;
52   private final Container m_emptyPanel;
53   private final Container m_authPanel;
54
55   public ConnectDialog(Frame JavaDoc parent, ServerConnectionManager scm, ConnectionListener listener) {
56     super(parent, true);
57
58     m_connectManager = scm;
59     m_timeout = CONNECT_TIMEOUT_MILLIS;
60     m_listener = listener;
61
62     AdminClientContext acc = AdminClient.getContext();
63     load((DialogResource) acc.topRes.child("ConnectDialog"));
64     m_label = (Label)findComponent("ConnectLabel");
65     m_label.setText("Connecting to "+scm+". Please wait...");
66     pack();
67
68     m_cancelButton = (Button) findComponent("CancelButton");
69     m_cancelButton.addActionListener(new ActionListener JavaDoc() {
70       public void actionPerformed(ActionEvent JavaDoc ae) {
71         m_cancelButton.setEnabled(false);
72         m_mainThread.interrupt();
73         m_jmxc = null;
74         ConnectDialog.this.setVisible(false);
75       }
76     });
77     getContentPane().addHierarchyListener(new HL());
78
79     int delay = 1000;
80     ActionListener JavaDoc taskPerformer = new ActionListener JavaDoc() {
81       public void actionPerformed(ActionEvent JavaDoc evt) {
82         setVisible(false);
83       }
84     };
85
86     m_emptyPanel = (Container) findComponent("EmptyPanel");
87     m_emptyPanel.setLayout(new BorderLayout JavaDoc());
88
89     m_authPanel = (Container) AdminClient.getContext().topRes.resolve("AuthPanel");
90
91     Container credentialsPanel = (Container) m_authPanel.findComponent("CredentialsPanel");
92     m_authPanel.setVisible(false);
93     this.m_usernameField = (JTextField JavaDoc) credentialsPanel.findComponent("UsernameField");
94     this.m_okButton = (Button) m_authPanel.findComponent("OKButton");
95     this.m_authCancelButton = (Button) m_authPanel.findComponent("CancelButton");
96
97     // must be found last because JPasswordField is not a Dijon Component
98
TextField passwordField = (TextField) credentialsPanel.findComponent("PasswordField");
99     Container passwdHolder = new Container();
100     passwdHolder.setLayout(new BorderLayout JavaDoc());
101     passwdHolder.add(m_passwordField = new JPasswordField JavaDoc());
102     credentialsPanel.replaceChild(passwordField, passwdHolder);
103
104     m_okButton.addActionListener(new ActionListener JavaDoc() {
105       public void actionPerformed(ActionEvent JavaDoc ae) {
106         final String JavaDoc username = m_usernameField.getText().trim();
107         final String JavaDoc password = new String JavaDoc(m_passwordField.getPassword()).trim();
108         SwingUtilities.invokeLater(new Thread JavaDoc() {
109           public void run() {
110             m_connectManager.setCredentials(username, password);
111             ((AuthenticatingJMXConnector) m_jmxc).handleOkClick(username, password);
112           }
113         });
114       }
115     });
116     m_authCancelButton.addActionListener(new ActionListener JavaDoc() {
117       public void actionPerformed(ActionEvent JavaDoc ae) {
118         m_cancelButton.doClick();
119       }
120     });
121
122     m_timer = new Timer JavaDoc(delay, taskPerformer);
123     m_timer.setRepeats(false);
124   }
125
126   private void disableAuthenticationDialog() {
127     m_usernameField.setEnabled(false);
128     m_passwordField.setEnabled(false);
129     m_emptyPanel.removeAll();
130     m_usernameField.setText("");
131     m_passwordField.setText("");
132     m_authPanel.setVisible(false);
133     m_authCancelButton.setVisible(false);
134     m_cancelButton.setVisible(true);
135     pack();
136     center(getOwner());
137   }
138
139   private void enableAuthenticationDialog() {
140     m_emptyPanel.add(m_authPanel);
141     m_cancelButton.setVisible(false);
142     m_authCancelButton.setVisible(true);
143     m_usernameField.setEnabled(true);
144     m_passwordField.setEnabled(true);
145     m_authPanel.setVisible(true);
146     m_authPanel.getRootPane().setDefaultButton(m_okButton);
147     pack();
148     center(getOwner());
149     m_usernameField.grabFocus();
150   }
151
152   public void setServerConnectionManager(ServerConnectionManager scm) {
153     m_connectManager = scm;
154     m_label.setText("Connecting to "+scm+". Please wait...");
155     pack();
156   }
157
158   public ServerConnectionManager getServerConnectionManager() {
159     return m_connectManager;
160   }
161
162   public void setTimeout(long millis) {
163     m_timeout = millis;
164   }
165
166   public long getTimeout() {
167     return m_timeout;
168   }
169
170   public void setConnectionListener(ConnectionListener listener) {
171     m_listener = listener;
172   }
173
174   public ConnectionListener getConnectionListener() {
175     return m_listener;
176   }
177
178   public JMXConnector JavaDoc getConnector() {
179     return m_jmxc;
180   }
181
182   public Exception JavaDoc getError() {
183     return m_error;
184   }
185
186   class HL implements HierarchyListener JavaDoc {
187     public void hierarchyChanged(HierarchyEvent JavaDoc e) {
188       long flags = e.getChangeFlags();
189
190       if ((flags & HierarchyEvent.SHOWING_CHANGED) != 0) {
191         if (isShowing()) {
192           m_cancelButton.setEnabled(true);
193           m_mainThread = new MainThread();
194           m_mainThread.start();
195         } else {
196           fireHandleConnect();
197         }
198       }
199     }
200   }
201
202   protected void fireHandleConnect() {
203     if (m_listener != null) {
204       try {
205         if (m_error == null) {
206           m_listener.handleConnection();
207         } else {
208           m_listener.handleException();
209         }
210       } catch (RuntimeException JavaDoc rte) {
211         rte.printStackTrace();
212       }
213     }
214   }
215
216   // --------------------------------------------------------------------------------
217

218   class MainThread extends Thread JavaDoc {
219
220     private boolean m_isConnecting = true;
221     private boolean m_join;
222     private final ConnectionTimer m_connectionTimer = new ConnectionTimer();
223
224     public void run() {
225       m_connectThread = new ConnectThread();
226       try {
227         m_error = null;
228         JMXServiceURL JavaDoc url = m_connectManager.getJMXServiceURL();
229         Map JavaDoc env = m_connectManager.getConnectionEnvironment();
230         m_jmxc = new AuthenticatingJMXConnector(url, env);
231         ((AuthenticatingJMXConnector) m_jmxc).addAuthenticationListener(new UpdateEventListener() {
232           public void handleUpdate(Object JavaDoc obj) {
233             m_connectionTimer.stopTimer();
234             m_connectionTimer.interrupt();
235             enableAuthenticationDialog();
236           }
237         });
238         ((AuthenticatingJMXConnector) m_jmxc).addCollapseListener(new UpdateEventListener() {
239           public void handleUpdate(Object JavaDoc obj) {
240             m_connectionTimer.setTimer();
241             disableAuthenticationDialog();
242           }
243         });
244         ((AuthenticatingJMXConnector) m_jmxc).addExceptionListener(new UpdateEventListener() {
245           public void handleUpdate(Object JavaDoc obj) {
246             m_connectionTimer.setTimer();
247             m_connectionTimer.interrupt();
248             disableAuthenticationDialog();
249           }
250         });
251
252         if (m_jmxc != null && m_error == null) {
253           m_connectThread.start();
254           m_connectionTimer.start();
255           synchronized (this) {
256             while (m_isConnecting)
257               wait();
258             if (m_join) m_connectThread.join(m_timeout);
259           }
260         }
261       } catch (IOException JavaDoc e) {
262         m_error = e;
263       } catch (InterruptedException JavaDoc e) {
264         m_connectThread.interrupt();
265         m_connectionTimer.interrupt();
266         disableAuthenticationDialog();
267         m_error = new InterruptedIOException JavaDoc("Interrupted");
268         return;
269       }
270
271       if (m_error == null && m_connectThread.isAlive()) {
272         m_connectThread.interrupt();
273         m_error = new InterruptedIOException JavaDoc("Connection timed out");
274       }
275
276       if (m_error != null) {
277         m_connectThread.interrupt();
278       }
279
280       m_timer.start();
281     }
282
283     private synchronized void connectionJoin() {
284       if(m_connectionTimer.isAlive()) {
285         m_connectionTimer.stopTimer();
286         m_connectionTimer.interrupt();
287       }
288
289       m_join = true;
290       m_isConnecting = false;
291       notifyAll();
292     }
293
294     private synchronized void connectionTimeout() {
295       m_isConnecting = false;
296       notifyAll();
297     }
298   }
299
300   // --------------------------------------------------------------------------------
301

302   class ConnectThread extends Thread JavaDoc {
303
304     public ConnectThread() {
305       setDaemon(true);
306     }
307
308     public void run() {
309       try {
310         m_jmxc.connect(m_connectManager.getConnectionEnvironment());
311         ((MainThread) m_mainThread).connectionJoin();
312       } catch (IOException JavaDoc e) {
313         m_error = e;
314       } catch (RuntimeException JavaDoc e) {
315         if (e instanceof AuthenticatingJMXConnector.AuthenticationException) { return; }
316         m_error = e;
317       }
318     }
319   }
320
321   // --------------------------------------------------------------------------------
322

323   private class ConnectionTimer extends Thread JavaDoc {
324
325     private boolean isSet;
326
327     private ConnectionTimer() {
328       setDaemon(true);
329     }
330
331     public void run() {
332       try {
333         startTimer();
334       } catch (InterruptedException JavaDoc e) {
335         // do nothing
336
}
337     }
338
339     private void startTimer() throws InterruptedException JavaDoc {
340       isSet = true;
341       try {
342         Thread.sleep(m_timeout);
343       } catch (InterruptedException JavaDoc e) {
344         // do nothing
345
}
346       if (isSet) {
347         ((MainThread) m_mainThread).connectionTimeout();
348         return;
349       }
350       while (!isSet) {
351         synchronized (this) {
352           wait();
353         }
354       }
355       ((MainThread) m_mainThread).connectionJoin();
356     }
357
358     private synchronized void setTimer() {
359       isSet = true;
360       notifyAll();
361     }
362
363     private synchronized void stopTimer() {
364       isSet = false;
365       notifyAll();
366     }
367   }
368
369   // --------------------------------------------------------------------------------
370

371   void tearDown() {
372     Map JavaDoc env = m_connectManager.getConnectionEnvironment();
373     if (env != null) {
374       env.clear();
375     }
376
377     m_connectManager = null;
378     m_listener = null;
379     m_jmxc = null;
380     m_mainThread = null;
381     m_connectThread = null;
382     m_cancelButton = null;
383     m_timer = null;
384     m_error = null;
385   }
386 }
387
Popular Tags