1 19 package org.netbeans.modules.j2ee.sun.ide.j2ee.ui; 20 21 import java.awt.Component ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.ObjectInputStream ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.net.InetAddress ; 27 import java.text.MessageFormat ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.MissingResourceException ; 31 import java.util.NoSuchElementException ; 32 import java.util.Set ; 33 import javax.swing.JComponent ; 34 import javax.swing.event.ChangeEvent ; 35 import javax.swing.event.ChangeListener ; 36 import org.netbeans.api.progress.ProgressHandle; 37 import org.netbeans.api.progress.ProgressHandleFactory; 38 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException; 39 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 40 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 41 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 42 import org.netbeans.modules.j2ee.sun.ide.editors.AdminAuthenticator; 43 import org.netbeans.modules.j2ee.sun.ide.j2ee.RunTimeDDCatalog; 44 import org.netbeans.modules.j2ee.sun.ide.j2ee.Utils; 45 import org.netbeans.modules.j2ee.sun.ide.j2ee.db.ExecSupport; 46 import org.netbeans.modules.j2ee.sun.ide.j2ee.db.RegisterPointbase; 47 import org.openide.DialogDisplayer; 48 import org.openide.ErrorManager; 49 import org.openide.NotifyDescriptor; 50 import org.openide.WizardDescriptor; 51 import org.openide.filesystems.FileObject; 52 import org.openide.filesystems.FileUtil; 53 import org.openide.util.Cancellable; 54 import org.openide.util.NbBundle; 55 56 public final class AddDomainWizardIterator implements 57 WizardDescriptor.InstantiatingIterator,ChangeListener { 58 59 private int index; 60 61 private WizardDescriptor.Panel[] panels = null; 62 63 final static String USER_NAME = "username"; final static String PASSWORD = "password"; final static String BLANK = ""; final static String HOST = "host"; final static String PORT = "port"; final static String CREATE_LOCALLY = "create_locally"; final static String DOMAIN_FILE = "domain_file"; final static String PLATFORM_LOCATION = "platform_location"; final static String INSTALL_LOCATION = "install_location"; final static String DOMAIN = "domain"; final static String INSTANCE_PORT = "instance_port"; final static String JMS_PORT = "jms_port"; final static String ORB_LISTENER_PORT = "orb_listener_port"; final static String ORB_SSL_PORT = "orb_ssl_port"; final static String HTTP_SSL_PORT = "http_ssl_port"; final static String ORB_MUTUAL_AUTH_PORT = "orb_mutual_auth_port"; final static String ADMIN_JMX_PORT = "admin_jmx_port"; final static String PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; final static String TYPE = "type"; final static String PROP_DISPLAY_NAME = "ServInstWizard_displayName"; 84 85 private AddDomainHostPortPanel hppanel = 86 new AddDomainHostPortPanel(); 87 private AddDomainDirectoryPanel domainDirPanel = 88 new AddDomainDirectoryPanel(false); 89 private AddDomainDirectoryPanel personalDirPanel = 90 new AddDomainDirectoryPanel(true); 91 private AddDomainPlatformPanel platformPanel = 92 new AddDomainPlatformPanel(); 93 private AddDomainNamePasswordPanel unamePanel = 94 new AddDomainNamePasswordPanel(); 95 private AddDomainPortsDefPanel portsPanel = 96 new AddDomainPortsDefPanel(); 97 98 private WizardDescriptor.Panel[] defaultFlow = { 99 platformPanel, unamePanel 100 }; 101 102 private WizardDescriptor.Panel[] remoteFlow = { 103 platformPanel, hppanel, unamePanel 104 }; 105 106 private WizardDescriptor.Panel[] localFlow = { 107 platformPanel, domainDirPanel, unamePanel 108 }; 109 110 private WizardDescriptor.Panel[] personalFlow = { 111 platformPanel, personalDirPanel, unamePanel, portsPanel 112 }; 113 117 private WizardDescriptor.Panel[] getPanels() { 118 if (panels == null) { 119 decoratePanels(remoteFlow); 120 decoratePanels(localFlow); 121 decoratePanels(personalFlow); 122 decoratePanels(defaultFlow); 123 platformPanel.addChangeListener(this); 124 panels = defaultFlow; 125 } 126 return panels; 127 } 128 129 public WizardDescriptor.Panel current() { 130 return getPanels()[index]; 131 } 132 133 public String name() { 134 return index + 1 + ". from " + getPanels().length; 135 } 136 137 public boolean hasNext() { 138 return index < getPanels().length - 1; 139 } 140 141 public boolean hasPrevious() { 142 return index > 0; 143 } 144 145 public void nextPanel() { 146 if (!hasNext()) { 147 throw new NoSuchElementException (); 148 } 149 index++; 150 } 151 152 public void previousPanel() { 153 if (!hasPrevious()) { 154 throw new NoSuchElementException (); 155 } 156 index--; 157 } 158 159 private transient Set listeners = new HashSet (1); 164 public final void addChangeListener(ChangeListener l) { 165 synchronized (listeners) { 166 listeners.add(l); 167 } 168 } 169 public final void removeChangeListener(ChangeListener l) { 170 synchronized (listeners) { 171 listeners.remove(l); 172 } 173 } 174 protected final void fireChangeEvent() { 175 Iterator it; 176 synchronized (listeners) { 177 it = new HashSet (listeners).iterator(); 178 } 179 ChangeEvent ev = new ChangeEvent (this); 180 while (it.hasNext()) { 181 ChangeListener l = (ChangeListener ) it.next(); 182 l.stateChanged(ev); 183 } 184 } 185 186 public void stateChanged(ChangeEvent e) { 187 if (wizard.getProperty(TYPE) == REMOTE) { panels = remoteFlow; 189 decoratePanels(remoteFlow); 190 fireChangeEvent(); 191 } else if (wizard.getProperty(TYPE) == DEFAULT) { panels = defaultFlow; 193 decoratePanels(defaultFlow); 194 fireChangeEvent(); 195 } else if (wizard.getProperty(TYPE) == LOCAL) { panels = localFlow; 197 decoratePanels(localFlow); 198 fireChangeEvent(); 199 } else if (wizard.getProperty(TYPE) == PERSONAL) { panels = personalFlow; 201 decoratePanels(personalFlow); 202 fireChangeEvent(); 203 } else { 204 System.out.println("THIS CANNOT BE TRUE!"); 205 panels = defaultFlow; 206 fireChangeEvent(); 207 } 208 } 209 210 private void decoratePanels(WizardDescriptor.Panel[] pnls) { 211 String [] steps = new String [pnls.length]; 212 for (int i = 0; i < pnls.length; i++) { 213 Component c = pnls[i].getComponent(); 214 steps[i] = c.getName(); 216 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 218 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); 221 jc.putClientProperty("WizardPanel_contentData", steps); jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE); 226 jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE); 229 jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE); 232 } 233 } 234 235 } 236 237 private void readObject(ObjectInputStream in) throws 238 IOException , ClassNotFoundException { 239 in.defaultReadObject(); 240 listeners = new HashSet (1); 241 } 242 243 public void uninitialize(WizardDescriptor wizard) { 244 } 245 246 private WizardDescriptor wizard; 247 248 public void initialize(WizardDescriptor wizard) { 249 this.wizard = wizard; 250 } 251 252 public java.util.Set instantiate() { 253 InstanceProperties ip = createInstance(); 254 Set result = new HashSet (); 255 if (ip != null) { 256 result.add(ip); 257 } 258 return result; 259 } 260 261 private void queryForNameAndWord() { 262 PasswordPanel pp = new PasswordPanel(); 263 pp.setPrompt(NbBundle.getMessage(AddDomainWizardIterator.class, 264 "PROMPT_USERNAME_PASSWORD")); org.openide.DialogDescriptor dd = new org.openide.DialogDescriptor(pp, 266 NbBundle.getMessage(AddDomainWizardIterator.class, 267 "TITLE_USERNAME_PASSWORD")); java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(dd); 269 d.setVisible(true); 270 if (dd.getValue() != NotifyDescriptor.CANCEL_OPTION) { 271 wizard.putProperty(USER_NAME,pp.getUsername()); 272 wizard.putProperty(PASSWORD,pp.getTPassword()); 273 } else { 274 wizard.putProperty(USER_NAME,BLANK); 275 wizard.putProperty(PASSWORD,BLANK); 276 } 277 } 278 279 InstanceProperties createInstance(){ 280 InstanceProperties retVal = null; 281 try { 282 if (isValidHost((String ) wizard.getProperty(HOST))) { 283 String uname = (String ) wizard.getProperty(USER_NAME); 284 if (null == uname) { 285 uname = BLANK; 286 } 287 String password = (String ) wizard.getProperty(PASSWORD); 288 if (null == password) { 289 password = BLANK; 290 } 291 if (wizard.getProperty(TYPE) == PERSONAL) { 292 if (uname.trim().length() < 1 || 294 password.trim().length() < 1) { 295 queryForNameAndWord(); 296 } 297 if (((String )wizard.getProperty(USER_NAME)).trim().length() < 1 || 298 ((String )wizard.getProperty(PASSWORD)).trim().length() < 8 ) { 299 DialogDisplayer.getDefault().notify( 300 new NotifyDescriptor.Message(NbBundle.getMessage(AddDomainWizardIterator.class, 301 "ERR_Illegal_Values"))); return null; 303 } 304 CreateDomain cd = new CreateDomain(((String )wizard.getProperty(USER_NAME)).trim(), 305 ((String )wizard.getProperty(PASSWORD)).trim()); 306 cd.start(); 307 } else { 308 retVal = createIP(uname,password); 309 } 310 } 311 } catch (InstanceCreationException e){ 312 NotifyDescriptor d = new NotifyDescriptor.Message( 313 e.getLocalizedMessage(), 314 NotifyDescriptor.INFORMATION_MESSAGE); 315 d.setTitle(NbBundle.getMessage(AddDomainWizardIterator.class, 316 "LBL_RegServerFailed")); DialogDisplayer.getDefault().notify(d); 318 } 319 return retVal; 320 } 321 322 private InstanceProperties createIP(final String uname, final String password) throws IllegalStateException , InstanceCreationException { 323 InstanceProperties retVal; 324 String domainDir = (String ) wizard.getProperty(INSTALL_LOCATION); 325 String displayName = 326 (String )wizard.getProperty(PROP_DISPLAY_NAME); 327 InstanceProperties instanceProperties = 328 SunURIManager.createInstanceProperties( 329 (File ) wizard.getProperty(PLATFORM_LOCATION), 330 (String ) wizard.getProperty(HOST), 331 (String )wizard.getProperty(PORT), 332 uname.trim(), password.trim() , displayName ); 333 instanceProperties.setProperty("httpportnumber", (String ) wizard.getProperty(PORT)); 335 instanceProperties.setProperty("DOMAIN", (String ) wizard.getProperty(DOMAIN)); 337 instanceProperties.setProperty("LOCATION", domainDir); 339 if (wizard.getProperty(TYPE) != REMOTE) { 340 RegisterPointbase.getDefault().register((File ) wizard.getProperty(PLATFORM_LOCATION)); 341 } 342 RunTimeDDCatalog.getRunTimeDDCatalog().refresh(); 343 wizard.putProperty(USER_NAME,BLANK); 344 wizard.putProperty(PASSWORD,BLANK); 345 retVal = instanceProperties; 346 return retVal; 347 } 348 349 private boolean isValidHost(String hostname){ 350 InetAddress addr = null; 351 try{ 352 addr = InetAddress.getByName(hostname); 353 }catch(java.net.UnknownHostException ex){ 354 String mess = MessageFormat.format( 355 NbBundle.getMessage(AddDomainWizardIterator.class, 356 "MSG_UnknownHost"), new Object []{hostname}); Util.showInformationWhenHolding(mess, 358 NbBundle.getMessage(AddDomainWizardIterator.class, 359 "LBL_UnknownHost")); } 361 return addr != null; 362 } 363 364 private class CreateDomain extends Thread { 365 366 private String uname; 367 368 private String pword; 369 370 CreateDomain(String uname, String pword) { 371 this.uname = uname; 372 this.pword = pword; 373 } 374 375 public void run() { 376 Process process = null; 377 File irf = (File ) wizard.getProperty(PLATFORM_LOCATION); 379 if (null != irf && irf.exists()) { 380 PDCancel pdcan = null; 381 String installRoot = irf.getAbsolutePath(); 382 String asadminCmd = installRoot + File.separator + 383 "bin" + File.separator + 385 "asadmin"; 387 if (File.separator.equals("\\")) { asadminCmd = asadminCmd + ".bat"; } 390 String domain = (String ) wizard.getProperty(DOMAIN); 391 String domainDir = (String ) wizard.getProperty(INSTALL_LOCATION); 392 File passWordFile = Utils.createTempPasswordFile(pword, "changeit"); if (passWordFile==null){ 394 return; 395 } 396 String arrnd[] = new String [] { asadminCmd, 397 "create-domain", "--domaindir", domainDir, 400 "--adminport", (String ) wizard.getProperty(PORT), 402 "--adminuser", uname, 404 "--passwordfile", passWordFile.getAbsolutePath(), 406 "--instanceport", (String ) wizard.getProperty(INSTANCE_PORT), 408 "--domainproperties", "jms.port="+ ((String )wizard.getProperty(JMS_PORT)).trim()+ 411 ":orb.listener.port="+ ((String )wizard.getProperty(ORB_LISTENER_PORT)).trim()+ 413 ":http.ssl.port="+ ((String )wizard.getProperty(HTTP_SSL_PORT)).trim()+ 415 ":orb.ssl.port="+ ((String )wizard.getProperty(ORB_SSL_PORT)).trim()+ 417 ":orb.mutualauth.port="+ ((String )wizard.getProperty(ORB_MUTUAL_AUTH_PORT)).trim()+ 419 ":domain.jmxPort="+ ((String )wizard.getProperty(ADMIN_JMX_PORT)).trim(), 421 domain 422 }; 423 ProgressHandle ph = null; 424 try { 425 ExecSupport ee= new ExecSupport(); 426 process= Runtime.getRuntime().exec(arrnd); 427 pdcan = new PDCancel(process, domainDir+File.separator+domain); 428 ph = ProgressHandleFactory.createHandle( 429 NbBundle.getMessage(AddDomainWizardIterator.class,"LBL_Creating_personal_domain"), 430 pdcan); 431 ph.start(); 432 433 ee.displayProcessOutputs(process, 434 NbBundle.getMessage(this.getClass(), "LBL_outputtab"), NbBundle.getMessage(this.getClass(), "LBL_RunningCreateDomainCommand") ); 437 } catch (MissingResourceException ex) { 438 Util.showInformation(ex.getLocalizedMessage()); 439 } catch (IOException ex) { 440 Util.showInformation(ex.getLocalizedMessage()); 441 } catch (InterruptedException ex) { 442 Util.showInformation(ex.getLocalizedMessage()); 443 } catch (RuntimeException ex) { 444 Util.showInformation(ex.getLocalizedMessage()); 445 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 447 ex); 448 } 449 int retVal = 0; 450 if (null != process) { 451 try { 452 retVal = process.waitFor(); 453 } catch (InterruptedException ie) { 454 retVal = -1; 455 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 456 ie); 457 } 458 } 459 if (null != ph) { 460 ph.finish(); 461 } 462 463 if (null != pdcan) { 464 if (0 != retVal && pdcan.notFired()) { 465 Util.showError(NbBundle.getMessage(this.getClass(), 466 "WARN_DELETE_INSTANCE", (String )wizard.getProperty(PROP_DISPLAY_NAME)), 468 NbBundle.getMessage(this.getClass(), 469 "WARN_DELETE_INSTANCE_TITLE")); } else if (pdcan.notFired()) { 471 try { 472 createIP(uname,pword); 473 } catch (InstanceCreationException ex) { 474 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 475 ex); 476 } catch (IllegalStateException ex) { 477 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 478 ex); 479 } 480 } 481 } 482 } 483 } 484 } 485 486 static class PDCancel implements Cancellable { 487 488 private Process p; 489 490 private String dirname; 491 492 private boolean notFired = true; 493 494 PDCancel(Process p, String newDirName) { 495 this.p = p; 496 this.dirname = newDirName; 497 } 498 499 synchronized public boolean notFired() { 500 return notFired; 501 } 502 503 synchronized public boolean cancel() { 504 notFired = false; 505 p.destroy(); 506 File domainDir = new File (dirname); 507 if (domainDir.exists()) { 508 FileObject fo = FileUtil.toFileObject(domainDir); 509 try { 510 fo.delete(); 511 } catch (IOException ex) { 512 Util.showError(NbBundle.getMessage(AddDomainWizardIterator.class, "ERR_Failed_cleanup", dirname)); 513 } 514 } 515 return true; 516 } 517 518 } 519 520 521 static class PasswordPanel extends javax.swing.JPanel { 522 523 private static final int DEFAULT_WIDTH = 200; 524 private static final int DEFAULT_HEIGHT = 0; 525 526 527 static final long serialVersionUID = 1555749205340031767L; 528 529 java.util.ResourceBundle bundle = org.openide.util.NbBundle.getBundle(AdminAuthenticator.class); 530 531 532 public PasswordPanel() { 533 initComponents(); 534 535 usernameField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_UserNameField")); 536 passwordField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PasswordField")); 537 } 538 539 public java.awt.Dimension getPreferredSize() { 540 java.awt.Dimension sup = super.getPreferredSize(); 541 return new java.awt.Dimension ( Math.max(sup.width, DEFAULT_WIDTH), Math.max(sup.height, DEFAULT_HEIGHT )); 542 } 543 544 549 private void initComponents() { 550 setLayout(new java.awt.BorderLayout ()); 551 552 mainPanel = new javax.swing.JPanel (); 553 mainPanel.setLayout(new java.awt.GridBagLayout ()); 554 java.awt.GridBagConstraints gridBagConstraints1; 555 mainPanel.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 0, 11))); 556 557 promptLabel = new javax.swing.JLabel (); 558 promptLabel.setHorizontalAlignment(0); 559 560 gridBagConstraints1 = new java.awt.GridBagConstraints (); 561 gridBagConstraints1.gridwidth = 0; 562 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 563 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 6, 0); 564 mainPanel.add(promptLabel, gridBagConstraints1); 565 566 jLabel1 = new javax.swing.JLabel (); 567 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, 568 bundle.getString("LAB_AUTH_User_Name")); gridBagConstraints1 = new java.awt.GridBagConstraints (); 570 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 5, 12); 571 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 572 mainPanel.add(jLabel1, gridBagConstraints1); 573 574 usernameField = new javax.swing.JTextField (); 575 usernameField.setMinimumSize(new java.awt.Dimension (70, 20)); 576 usernameField.setPreferredSize(new java.awt.Dimension (70, 20)); 577 jLabel1.setLabelFor(usernameField); 578 579 gridBagConstraints1 = new java.awt.GridBagConstraints (); 580 gridBagConstraints1.gridwidth = 0; 581 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 582 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 5, 0); 583 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 584 gridBagConstraints1.weightx = 1.0; 585 mainPanel.add(usernameField, gridBagConstraints1); 586 587 jLabel2 = new javax.swing.JLabel (); 588 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, 589 bundle.getString("LAB_AUTH_Password")); gridBagConstraints1 = new java.awt.GridBagConstraints (); 591 gridBagConstraints1.insets = new java.awt.Insets (0, 0, 0, 12); 592 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 593 mainPanel.add(jLabel2, gridBagConstraints1); 594 595 passwordField = new javax.swing.JPasswordField (); 596 passwordField.setMinimumSize(new java.awt.Dimension (70, 20)); 597 passwordField.setPreferredSize(new java.awt.Dimension (70, 20)); 598 jLabel2.setLabelFor(passwordField); 599 600 gridBagConstraints1 = new java.awt.GridBagConstraints (); 601 gridBagConstraints1.gridwidth = 0; 602 gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL; 603 gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST; 604 gridBagConstraints1.weightx = 1.0; 605 mainPanel.add(passwordField, gridBagConstraints1); 606 607 add(mainPanel, "Center"); 609 } 610 611 private javax.swing.JPanel mainPanel; 613 private javax.swing.JLabel promptLabel; 614 private javax.swing.JLabel jLabel1; 615 private javax.swing.JTextField usernameField; 616 private javax.swing.JLabel jLabel2; 617 private javax.swing.JPasswordField passwordField; 618 620 String getUsername( ) { 621 return usernameField.getText(); 622 } 623 624 char[] getPassword( ) { 625 return passwordField.getPassword(); 626 } 627 628 String getTPassword( ) { 629 return new String (passwordField.getPassword()); 630 } 631 632 void setPrompt( String prompt ) { 633 if ( prompt == null ) { 634 promptLabel.setVisible( false ); 635 getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_NbAuthenticatorPasswordPanel")); 636 } else { 637 promptLabel.setVisible( true ); 638 promptLabel.setText( prompt ); 639 getAccessibleContext().setAccessibleDescription(prompt); 640 } 641 } 642 } 643 644 static InstanceType PERSONAL = new InstanceType(); 645 static InstanceType REMOTE = new InstanceType(); 646 static InstanceType LOCAL = new InstanceType(); 647 static InstanceType DEFAULT = new InstanceType(); 648 649 650 static class InstanceType { 651 } 652 } 653 654 | Popular Tags |