1 19 23 24 package org.netbeans.modules.j2ee.sun.ide.editors; 25 26 import java.util.ResourceBundle ; 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 41 42 public class AdminAuthenticator extends java.net.Authenticator { 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 getPasswordAuthentication() { 54 java.net.InetAddress site = getRequestingSite(); 55 ResourceBundle bundle = NbBundle.getBundle( AdminAuthenticator.class ); 56 String host = site == null ? bundle.getString( "CTL_PasswordProtected" ) : site.getHostName(); String title = getRequestingPrompt(); 58 InstanceProperties ip = null; 59 String keyURI; 60 String name=""; if (title.equals("admin-realm")){ if (preferredSunDeploymentManagerInterface!=null){ 63 if ( preferredSunDeploymentManagerInterface.getPort()==getRequestingPort()){ 65 ip =SunURIManager.getInstanceProperties( 66 preferredSunDeploymentManagerInterface.getPlatformRoot(), 67 preferredSunDeploymentManagerInterface.getHost(), 68 preferredSunDeploymentManagerInterface.getPort()); 69 } 70 } 71 } 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 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 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 ( passwordPanel.getUsername(), passwordPanel.getPassword() ); 111 } else{ 112 return null; 113 } 114 } 115 116 117 118 119 120 static class PasswordPanel extends javax.swing.JPanel { 121 122 private static final int DEFAULT_WIDTH = 200; 123 private static final int DEFAULT_HEIGHT = 0; 124 125 126 static final long serialVersionUID = 1555749205340031767L; 127 128 ResourceBundle bundle = org.openide.util.NbBundle.getBundle(AdminAuthenticator.class); 129 130 131 public PasswordPanel(String 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 getPreferredSize() { 141 java.awt.Dimension sup = super.getPreferredSize(); 142 return new java.awt.Dimension ( Math.max(sup.width, DEFAULT_WIDTH), Math.max(sup.height, DEFAULT_HEIGHT )); 143 } 144 145 150 private void initComponents() { 151 setLayout(new java.awt.BorderLayout ()); 152 153 mainPanel = new javax.swing.JPanel (); 154 mainPanel.setLayout(new java.awt.GridBagLayout ()); 155 java.awt.GridBagConstraints gridBagConstraints1; 156 mainPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 0, 11))); 157 158 promptLabel = new javax.swing.JLabel (); 159 promptLabel.setHorizontalAlignment(0); 160 161 gridBagConstraints1 = new java.awt.GridBagConstraints (); 162 gridBagConstraints1.gridwidth = 0; 163 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 164 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 6, 0); 165 mainPanel.add(promptLabel, gridBagConstraints1); 166 167 jLabel1 = new javax.swing.JLabel (); 168 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, 169 bundle.getString("LAB_AUTH_User_Name")); 171 gridBagConstraints1 = new java.awt.GridBagConstraints (); 172 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 5, 12); 173 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 174 mainPanel.add(jLabel1, gridBagConstraints1); 175 176 usernameField = new javax.swing.JTextField (); 177 usernameField.setMinimumSize(new java.awt.Dimension (70, 20)); 178 usernameField.setPreferredSize(new java.awt.Dimension (70, 20)); 179 jLabel1.setLabelFor(usernameField); 180 181 gridBagConstraints1 = new java.awt.GridBagConstraints (); 182 gridBagConstraints1.gridwidth = 0; 183 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 184 gridBagConstraints1.insets = new java.awt.Insets (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 (); 190 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, 191 bundle.getString("LAB_AUTH_Password")); 193 gridBagConstraints1 = new java.awt.GridBagConstraints (); 194 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 12); 195 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 196 mainPanel.add(jLabel2, gridBagConstraints1); 197 198 passwordField = new javax.swing.JPasswordField (); 199 passwordField.setMinimumSize(new java.awt.Dimension (70, 20)); 200 passwordField.setPreferredSize(new java.awt.Dimension (70, 20)); 201 jLabel2.setLabelFor(passwordField); 202 203 gridBagConstraints1 = new java.awt.GridBagConstraints (); 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"); 212 } 213 214 private javax.swing.JPanel mainPanel; 216 private javax.swing.JLabel promptLabel; 217 private javax.swing.JLabel jLabel1; 218 private javax.swing.JTextField usernameField; 219 private javax.swing.JLabel jLabel2; 220 private javax.swing.JPasswordField passwordField; 221 223 String getUsername( ) { 224 return usernameField.getText(); 225 } 226 227 char[] getPassword( ) { 228 return passwordField.getPassword(); 229 } 230 231 String getTPassword( ) { 232 return passwordField.getText(); 233 } 234 235 void setPrompt( String 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 |