ServerCheckWizardPanel.java Sample

This topic inludes the source code for the ServerCheckWizardPanel.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/ControlDevKit/ControlFeatures/insertWizard/ide/

Sample Source Code


001 package insertWizard.ide; 
002 
003 import weblogic.management.MBeanHome;
004 import java.awt.event.ActionListener;
005 import java.awt.GridBagConstraints;
006 import java.awt.GridBagLayout;
007 import java.awt.event.ActionEvent;
008 import javax.swing.JOptionPane;
009 import javax.swing.JPanel;
010 import javax.swing.JTextField;
011 import javax.swing.JPasswordField;
012 import javax.swing.JLabel;
013 import javax.swing.JButton;
014 import insertWizard.MBeanUtil;
015 import com.bea.ide.control.EditorContext;
016 import weblogic.management.configuration.ConfigurationMBean;
017 import javax.management.MBeanServer;
018 import weblogic.management.RemoteMBeanServer;
019 import javax.management.MBeanInfo;
020 import weblogic.management.configuration.WebServerMBean;
021 import java.util.Set;
022 import java.util.Iterator;
023 import java.awt.FocusTraversalPolicy;
024 import javax.swing.JComponent;
025 import java.awt.Component;
026 import java.awt.Container;
027 import java.net.MalformedURLException;
028 import java.net.URL;
029 
030 /*
031  * Represents the user interface for the ServerCheck control
032  * insert dialog. 
033  */
034 public class ServerCheckWizardPanel extends JPanel 
035     implements ActionListener
036 
037   JPanel m_pnlServerInfo;
038   JTextField m_txtUserName;
039   JPasswordField m_pssPassword;
040   JLabel m_lblPassword;
041   JLabel m_lblUserName;
042   JTextField m_txtServerURL;
043   JLabel m_lblServerURL;
044   JButton m_btnCheckConnection;
045   JTextField m_txtServerName;
046   JLabel m_lblServerName;
047     EditorContext _ctx;
048 
049     public ServerCheckWizardPanel()
050     {
051         initComponents();          
052     }
053 
054     public void setContext(EditorContext ctx)
055     {
056         _ctx = ctx;
057         m_pnlServerInfo.setFocusCycleRoot(true);
058         m_pnlServerInfo.setFocusTraversalPolicy(_newFtp);
059     }
060     /*
061      * Executes when the user clicks the "Check Connection" button
062      * in the insert dialog. If the connection does not succeed, 
063      * relevant messages are forwarded to the user from the MBean.
064      */
065     public void actionPerformed(ActionEvent e) {
066         MBeanHome mBeanFromUserData = null;
067         MBeanHome mBeanCurServer = null;        
068         String message="Connection Check:";
069         try {
070             if (!_ctx.ensureServerRunning())
071                 return;
072            mBeanCurServer = (MBeanHome)_ctx.getMBeanHome();
073             
074             RemoteMBeanServer mBS = mBeanCurServer.getMBeanServer();
075             message += "<br> Server Name current value is:  " + mBS.getServerName();
076                     
077             mBeanFromUserData = MBeanUtil.getMBean(getUserName(), getPassword()
078                 getServerURL(), getServerName());
079             message += "<br> Password access verified!";
080             this.showConnectionCheckResults(message);            
081         catch (IllegalArgumentException iae) {
082             message += "<br>" + iae.getMessage();
083             this.showConnectionCheckResults(message);
084         }
085         catch (Exception ex) {
086             ex.printStackTrace();
087             message += "<br>" + ex.getMessage();
088             this.showConnectionCheckResults(message);
089         }
090     }
091     
092     /*
093      * Displays a dialog with the results of the "Check Connection"
094      * button.
095      */
096     private void showConnectionCheckResults(String resultsMessage){
097             StringBuffer message = new StringBuffer();
098             message.append("<html><p>").append(resultsMessage).
099                 append("</p><p>").append("</p></html>");
100             JOptionPane.showMessageDialog(this, message, "Connection Check"
101                 JOptionPane.INFORMATION_MESSAGE);        
102     }
103     
104     
105     private FocusTraversalPolicy _newFtp = new FocusTraversalPolicy()
106         {
107             public Component getComponentAfter(Container focusCycleRoot, Component aComponent)
108             {                
109                 if (aComponent==m_btnCheckConnection)
110                     return m_txtServerName;
111                 if (aComponent==m_txtServerName)
112                     return m_txtServerURL;
113                 if (aComponent==m_txtServerURL)
114                     return m_txtUserName;
115                 if (aComponent==m_txtUserName)
116                     return m_pssPassword;
117                 if (aComponent==m_pssPassword)
118                     return m_btnCheckConnection;                
119                  return null;
120             }
121 
122             public Component getComponentBefore(Container focusCycleRoot, Component aComponent)
123             {
124                 if (aComponent==m_txtServerName)
125                     return m_btnCheckConnection;
126                 if (aComponent==m_btnCheckConnection)
127                     return m_pssPassword;
128                 if (aComponent==m_pssPassword)
129                     return m_txtUserName;
130                 if (aComponent==m_txtUserName)
131                     return m_txtServerURL;
132                 if (aComponent==m_txtServerURL)
133                     return m_txtServerName;              
134                  return null;
135             }
136 
137             public Component getDefaultComponent(Container focusCycleRoot)
138             {
139                   return m_btnCheckConnection;
140             }
141 
142             public Component getFirstComponent(Container focusCycleRoot)
143             {
144                   return m_txtServerName;                
145             }
146 
147             public Component getLastComponent(Container focusCycleRoot)
148             {
149                   return m_btnCheckConnection;                
150             }
151         };
152 
153     /*
154      * Assembles user interface components that make up the insert
155      * dialog.
156      */
157     private void initComponents() {
158       
159         m_pnlServerInfo = new JPanel();
160         m_txtUserName = new JTextField();
161         m_pssPassword = new JPasswordField();
162         m_lblPassword = new JLabel();
163         m_lblUserName = new JLabel();
164         m_txtServerURL = new JTextField();
165         m_lblServerURL = new JLabel();
166         m_btnCheckConnection = new JButton();
167         m_txtServerName = new JTextField();
168         m_lblServerName = new JLabel();
169              
170         GridBagConstraints gridBagConstraints;
171 
172         setLayout(new GridBagLayout());
173         setEnabled(true);
174                                 
175         m_lblServerName.setText("Server name:   ");      
176         m_lblServerName.setLabelFor(m_txtServerName);
177         m_lblServerName.setDisplayedMnemonic('S');
178                   
179         gridBagConstraints = new GridBagConstraints();
180         gridBagConstraints.gridx = 0;
181         gridBagConstraints.gridy = 7;
182         gridBagConstraints.anchor = GridBagConstraints.WEST;
183         gridBagConstraints.insets = new java.awt.Insets(0006);
184         add(m_lblServerName, gridBagConstraints);
185 
186         gridBagConstraints = new GridBagConstraints();
187         gridBagConstraints.gridx = 1;
188         gridBagConstraints.gridy = 7;
189         gridBagConstraints.gridwidth = 2;
190         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
191         gridBagConstraints.anchor = GridBagConstraints.WEST;
192         gridBagConstraints.weightx = 1.0;
193         gridBagConstraints.insets = new java.awt.Insets(6060);
194         add(m_txtServerName, gridBagConstraints);
195 
196     m_lblServerURL.setText("Server URL:       ");
197         m_lblServerURL.setLabelFor(m_txtServerURL);
198         m_lblServerURL.setDisplayedMnemonic('r');
199         m_txtServerURL.setFocusTraversalKeysEnabled(true);        
200         
201         gridBagConstraints = new GridBagConstraints();
202         gridBagConstraints.gridx = 0;
203         gridBagConstraints.gridy = 8;
204         gridBagConstraints.gridwidth = 2;
205         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
206         gridBagConstraints.anchor = GridBagConstraints.WEST;
207         gridBagConstraints.weightx = 1.0;
208         gridBagConstraints.insets = new java.awt.Insets(6060);
209         add(m_lblServerURL, gridBagConstraints);
210 
211         gridBagConstraints = new GridBagConstraints();
212         gridBagConstraints.gridx = 1;
213         gridBagConstraints.gridy = 8;
214         gridBagConstraints.gridwidth = 2;
215         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
216         gridBagConstraints.anchor = GridBagConstraints.WEST;
217         gridBagConstraints.weightx = 1.0;
218         gridBagConstraints.insets = new java.awt.Insets(6060);
219         add(m_txtServerURL, gridBagConstraints);
220 
221 
222     m_lblUserName.setText("WLS Admin user:  ");
223         m_lblUserName.setLabelFor(m_txtUserName);
224         m_lblUserName.setDisplayedMnemonic('A');
225         
226         gridBagConstraints = new GridBagConstraints();
227         gridBagConstraints.gridx = 0;
228         gridBagConstraints.gridy = 9;
229         gridBagConstraints.gridwidth = 2;
230         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
231         gridBagConstraints.anchor = GridBagConstraints.WEST;
232         gridBagConstraints.weightx = 1.0;
233         gridBagConstraints.insets = new java.awt.Insets(6060);
234         add(m_lblUserName, gridBagConstraints);
235 
236         gridBagConstraints = new GridBagConstraints();
237         gridBagConstraints.gridx = 1;
238         gridBagConstraints.gridy = 9;
239         gridBagConstraints.gridwidth = 2;
240         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
241         gridBagConstraints.anchor = GridBagConstraints.WEST;
242         gridBagConstraints.weightx = 1.0;
243         gridBagConstraints.insets = new java.awt.Insets(6060);
244         add(m_txtUserName, gridBagConstraints);
245 
246     m_lblPassword.setText("Password:    ");
247         m_lblPassword.setLabelFor(m_pssPassword);
248         m_lblPassword.setDisplayedMnemonic('P');
249         
250         gridBagConstraints = new GridBagConstraints();
251         gridBagConstraints.gridx = 0;
252         gridBagConstraints.gridy = 10;
253         gridBagConstraints.gridwidth = 2;
254         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
255         gridBagConstraints.anchor = GridBagConstraints.WEST;
256         gridBagConstraints.weightx = 1.0;
257         gridBagConstraints.insets = new java.awt.Insets(6060);
258         add(m_lblPassword, gridBagConstraints);
259 
260         gridBagConstraints = new GridBagConstraints();
261         gridBagConstraints.gridx = 1;
262         gridBagConstraints.gridy = 10;
263         gridBagConstraints.gridwidth = 2;
264         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
265         gridBagConstraints.anchor = GridBagConstraints.WEST;
266         gridBagConstraints.weightx = 1.0;
267         gridBagConstraints.insets = new java.awt.Insets(6060);
268         add(m_pssPassword, gridBagConstraints);
269 
270     m_btnCheckConnection.setText("Check Connection");
271         m_btnCheckConnection.setMnemonic('C');
272         m_btnCheckConnection.addActionListener(this);
273         gridBagConstraints = new GridBagConstraints();
274         gridBagConstraints.gridx = 1;
275         gridBagConstraints.gridy = 11;
276         gridBagConstraints.gridwidth = 2;
277         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
278         gridBagConstraints.anchor = GridBagConstraints.WEST;
279         gridBagConstraints.weightx = 1.0;
280         gridBagConstraints.insets = new java.awt.Insets(606250);
281         add(m_btnCheckConnection, gridBagConstraints);
282         
283         
284     }
285     
286     /*
287      * A set of accesses that simplify retrieving the values
288      * that the control's user has entered into the insert dialog.
289      */
290     
291     public String getPassword(){    
292         return m_pssPassword.getText();
293     }
294     
295     public String getUserName(){
296         return m_txtUserName.getText();
297     }
298     
299     public String getServerName(){
300         return m_txtServerName.getText();
301     }
302     
303     public String getServerURL() throws IllegalArgumentException
304     {
305         
306         try 
307         {
308             URL serverURL = new URL(m_txtServerURL.getText());
309             return serverURL.toString();
310         }
311         catch (MalformedURLException mfue)
312         {
313             throw new IllegalArgumentException("Server URL must be of form http://localhost:7001");
314         }
315     }
316 
317