KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > editors > AdminAuthenticator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * AdminAuthenticator.java
21  *
22  */

23
24 package org.netbeans.modules.j2ee.sun.ide.editors;
25
26 import java.util.ResourceBundle JavaDoc;
27 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
28 import org.netbeans.modules.j2ee.sun.api.SunDeploymentManagerInterface;
29 import org.netbeans.modules.j2ee.sun.api.SunURIManager;
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.util.NbBundle;
34
35
36
37 /** Global password protected sites Authenticator for IDE
38  *
39  * @author Ludo, Petr Hrebejk
40  */

41
42 public class AdminAuthenticator extends java.net.Authenticator JavaDoc {
43     private SunDeploymentManagerInterface preferredSunDeploymentManagerInterface;
44     
45     public AdminAuthenticator( ) {
46         preferredSunDeploymentManagerInterface=null ;
47     }
48     
49     public AdminAuthenticator(SunDeploymentManagerInterface dm) {
50         preferredSunDeploymentManagerInterface=dm ;
51     }
52
53     protected java.net.PasswordAuthentication JavaDoc getPasswordAuthentication() {
54         java.net.InetAddress JavaDoc site = getRequestingSite();
55         ResourceBundle JavaDoc bundle = NbBundle.getBundle( AdminAuthenticator.class );
56         String JavaDoc host = site == null ? bundle.getString( "CTL_PasswordProtected" ) : site.getHostName(); // NOI18N
57
String JavaDoc title = getRequestingPrompt();
58         InstanceProperties ip = null;
59         String JavaDoc keyURI;
60         String JavaDoc name=""; // NOI18N
61
if (title.equals("admin-realm")){ // NOI18N so this is a request from the sun server
62
if (preferredSunDeploymentManagerInterface!=null){
63             //Make sure this is really the admin port and not the app port (see bug 85605
64
if ( preferredSunDeploymentManagerInterface.getPort()==getRequestingPort()){
65                 ip =SunURIManager.getInstanceProperties(
66                         preferredSunDeploymentManagerInterface.getPlatformRoot(),
67                         preferredSunDeploymentManagerInterface.getHost(),
68                         preferredSunDeploymentManagerInterface.getPort());
69             }
70         }
71 // else {
72
// keyURI=SunURIManager.SUNSERVERSURI+site.getHostName()+":"+getRequestingPort();
73
// ip= InstanceProperties.getInstanceProperties(keyURI);
74
//
75
// }
76
}
77         if (ip!=null){
78             title =ip.getProperty(InstanceProperties.DISPLAY_NAME_ATTR);
79             name = ip.getProperty(InstanceProperties.USERNAME_ATTR);
80
81         }
82         
83         
84         
85         PasswordPanel passwordPanel = new PasswordPanel(name);
86         
87         DialogDescriptor dd = new DialogDescriptor( passwordPanel, host );
88         passwordPanel.setPrompt(title );
89         java.awt.Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog( dd );
90         dialog.setVisible(true);
91         
92         if ( dd.getValue().equals( NotifyDescriptor.OK_OPTION ) ){
93             
94             if (ip!=null){
95                 String JavaDoc oldpass = ip.getProperty(InstanceProperties.PASSWORD_ATTR);
96                 ip.setProperty(InstanceProperties.USERNAME_ATTR, passwordPanel.getUsername());
97                 ip.setProperty(InstanceProperties.PASSWORD_ATTR, passwordPanel.getTPassword());
98                 if (preferredSunDeploymentManagerInterface!=null){
99                     preferredSunDeploymentManagerInterface.setUserName(passwordPanel.getUsername());
100                     preferredSunDeploymentManagerInterface.setPassword(passwordPanel.getTPassword());
101                 }
102                 ip.refreshServerInstance();
103                 
104                 if ("".equals(oldpass)){
105                     ip.setProperty(InstanceProperties.PASSWORD_ATTR ,oldpass);
106                     
107                 }
108             }
109             
110             return new java.net.PasswordAuthentication JavaDoc( passwordPanel.getUsername(), passwordPanel.getPassword() );
111         } else{
112             return null;
113         }
114     }
115     
116     
117     
118     /** Inner class for JPanel with Username & Password fields */
119     
120     static class PasswordPanel extends javax.swing.JPanel JavaDoc {
121         
122         private static final int DEFAULT_WIDTH = 200;
123         private static final int DEFAULT_HEIGHT = 0;
124         
125         /** Generated serialVersionUID */
126         static final long serialVersionUID = 1555749205340031767L;
127         
128         ResourceBundle JavaDoc bundle = org.openide.util.NbBundle.getBundle(AdminAuthenticator.class);
129         
130         /** Creates new form PasswordPanel */
131         public PasswordPanel(String JavaDoc userName) {
132             initComponents();
133             usernameField.setText(userName);
134             usernameField.setSelectionStart(0);
135             usernameField.setSelectionEnd(userName.length());
136             usernameField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_UserNameField"));
137             passwordField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PasswordField"));
138         }
139         
140         public java.awt.Dimension JavaDoc getPreferredSize() {
141             java.awt.Dimension JavaDoc sup = super.getPreferredSize();
142             return new java.awt.Dimension JavaDoc( Math.max(sup.width, DEFAULT_WIDTH), Math.max(sup.height, DEFAULT_HEIGHT ));
143         }
144         
145         /** This method is called from within the constructor to
146          * initialize the form.
147          * WARNING: Do NOT modify this code. The content of this method is
148          * always regenerated by the FormEditor.
149          */

150         private void initComponents() {
151             setLayout(new java.awt.BorderLayout JavaDoc());
152             
153             mainPanel = new javax.swing.JPanel JavaDoc();
154             mainPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
155             java.awt.GridBagConstraints JavaDoc gridBagConstraints1;
156             mainPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 0, 11)));
157             
158             promptLabel = new javax.swing.JLabel JavaDoc();
159             promptLabel.setHorizontalAlignment(0);
160             
161             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
162             gridBagConstraints1.gridwidth = 0;
163             gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
164             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
165             mainPanel.add(promptLabel, gridBagConstraints1);
166             
167             jLabel1 = new javax.swing.JLabel JavaDoc();
168             org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
169                     bundle.getString("LAB_AUTH_User_Name")); // NOI18N
170

171             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
172             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 5, 12);
173             gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
174             mainPanel.add(jLabel1, gridBagConstraints1);
175             
176             usernameField = new javax.swing.JTextField JavaDoc();
177             usernameField.setMinimumSize(new java.awt.Dimension JavaDoc(70, 20));
178             usernameField.setPreferredSize(new java.awt.Dimension JavaDoc(70, 20));
179             jLabel1.setLabelFor(usernameField);
180             
181             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
182             gridBagConstraints1.gridwidth = 0;
183             gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
184             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 5, 0);
185             gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
186             gridBagConstraints1.weightx = 1.0;
187             mainPanel.add(usernameField, gridBagConstraints1);
188             
189             jLabel2 = new javax.swing.JLabel JavaDoc();
190             org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
191                     bundle.getString("LAB_AUTH_Password")); // NOI18N
192

193             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
194             gridBagConstraints1.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
195             gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
196             mainPanel.add(jLabel2, gridBagConstraints1);
197             
198             passwordField = new javax.swing.JPasswordField JavaDoc();
199             passwordField.setMinimumSize(new java.awt.Dimension JavaDoc(70, 20));
200             passwordField.setPreferredSize(new java.awt.Dimension JavaDoc(70, 20));
201             jLabel2.setLabelFor(passwordField);
202             
203             gridBagConstraints1 = new java.awt.GridBagConstraints JavaDoc();
204             gridBagConstraints1.gridwidth = 0;
205             gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
206             gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
207             gridBagConstraints1.weightx = 1.0;
208             mainPanel.add(passwordField, gridBagConstraints1);
209             
210             add(mainPanel, "Center"); // NOI18N
211

212         }
213         
214         // Variables declaration - do not modify//GEN-BEGIN:variables
215
private javax.swing.JPanel JavaDoc mainPanel;
216         private javax.swing.JLabel JavaDoc promptLabel;
217         private javax.swing.JLabel JavaDoc jLabel1;
218         private javax.swing.JTextField JavaDoc usernameField;
219         private javax.swing.JLabel JavaDoc jLabel2;
220         private javax.swing.JPasswordField JavaDoc passwordField;
221         // End of variables declaration//GEN-END:variables
222

223         String JavaDoc getUsername( ) {
224             return usernameField.getText();
225         }
226
227         char[] getPassword( ) {
228             return passwordField.getPassword();
229         }
230
231         String JavaDoc getTPassword( ) {
232             return passwordField.getText();
233         }
234
235         void setPrompt( String JavaDoc prompt ) {
236             if ( prompt == null ) {
237                 promptLabel.setVisible( false );
238                 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_NbAuthenticatorPasswordPanel"));
239             }
240             else {
241                 promptLabel.setVisible( true );
242                 promptLabel.setText( prompt );
243                 getAccessibleContext().setAccessibleDescription(prompt);
244             }
245         }
246     }
247 }
248
Popular Tags