1 19 package org.netbeans.modules.j2ee.weblogic9.ui.wizard; 20 21 import java.io.*; 22 import java.util.*; 23 import java.awt.*; 24 import java.awt.event.*; 25 import javax.swing.*; 26 import javax.swing.event.*; 27 import javax.xml.parsers.*; 28 import org.w3c.dom.*; 29 import org.xml.sax.*; 30 31 import org.openide.*; 32 import org.openide.util.*; 33 34 41 public class ServerPropertiesPanel extends JPanel implements WizardDescriptor.Panel { 42 46 private final static String PROP_ERROR_MESSAGE = "WizardPanel_errorMessage"; 48 51 private transient WizardDescriptor wizardDescriptor; 52 53 56 private transient WLInstantiatingIterator instantiatingIterator; 57 58 68 public ServerPropertiesPanel(String [] steps, int index, ChangeListener listener, WLInstantiatingIterator instantiatingIterator) { 69 this.instantiatingIterator = instantiatingIterator; 71 72 putClientProperty("WizardPanel_contentData", steps); putClientProperty("WizardPanel_contentSelectedIndex", new Integer (index)); 77 addChangeListener(listener); 79 80 setName(steps[index]); 82 83 init(); 85 } 86 87 92 public HelpCtx getHelp() { 93 return new HelpCtx("j2eeplugins_registering_app_server_weblogic_properties"); } 95 96 102 public Component getComponent() { 103 return this; 104 } 105 106 112 public boolean isValid() { 113 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, ""); 116 if (serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL"))) { if (!isValidDomainRoot(domainPathField.getText())) { 120 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_DOMAIN_ROOT")); return false; 122 } 123 } 124 125 if (hostField.getText().trim().equals("")) { 127 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_HOST")); } 129 130 if (!portField.getText().trim().matches("[0-9]+")) { 132 wizardDescriptor.putProperty(PROP_ERROR_MESSAGE, NbBundle.getMessage(ServerPropertiesPanel.class, "ERR_INVALID_PORT")); } 134 135 137 instantiatingIterator.setDomainRoot(domainPathField.getText()); 139 instantiatingIterator.setHost(hostField.getText()); 140 instantiatingIterator.setPort(portField.getText()); 141 instantiatingIterator.setUsername(usernameField.getText()); 142 instantiatingIterator.setPassword(new String (passwordField.getPassword())); 143 instantiatingIterator.setIsLocal(serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL")) ? "true" : "false"); 145 return true; 147 } 148 149 154 private boolean isValidDomainRoot(String path) { 155 158 String [] children = { 161 "servers", "config", "config/config.xml", "init-info/domain-info.xml", }; 166 boolean is90 = hasChildren(path, children); 167 String [] children90b = { 168 "servers", "config", "config/config.xml", "domain-info.xml", }; 173 boolean is90b = hasChildren(path, children90b); 174 return is90 || is90b; 175 } 176 177 182 private boolean hasChildren(String parent, String [] children) { 183 if (parent == null) { 185 return false; 186 } 187 188 if (children == null) { 190 return true; 191 } 192 193 for (int i = 0; i < children.length; i++) { 196 if (!(new File(parent + File.separator + children[i]).exists())) { 197 return false; 198 } 199 } 200 201 return true; 203 } 204 205 private JLabel domainPathLabel; 209 private JLabel hostLabel; 210 private JLabel portLabel; 211 private JLabel userNameLabel; 212 private JLabel passwordLabel; 213 private JPasswordField passwordField; 214 private JTextField domainPathField; 215 private JTextField hostField; 216 private JTextField portField; 217 private JTextField usernameField; 218 private JPanel formattingPanel; 219 private JComboBox serverTypeCombo; 220 private JComboBox localInstancesCombo; 221 private JLabel localInstanceLabel; 222 private JLabel serverTypeLabel; 223 224 227 private void init() { 228 GridBagConstraints gridBagConstraints; 231 232 domainPathLabel = new JLabel(); 234 domainPathField = new JTextField(); 235 hostLabel = new JLabel(); 236 hostField = new JTextField(); 237 portLabel = new JLabel(); 238 portField = new JTextField(); 239 userNameLabel = new JLabel(); 240 usernameField = new JTextField(); 241 passwordLabel = new JLabel(); 242 passwordField = new JPasswordField(); 243 formattingPanel = new JPanel(); 244 serverTypeCombo = new JComboBox(new Object [] {NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL")}); localInstanceLabel = new JLabel(); 246 localInstancesCombo = new JComboBox(new InstancesModel(getServerInstances())); 247 serverTypeLabel = new JLabel(); 248 249 setLayout(new GridBagLayout()); 251 252 254 272 localInstanceLabel.setLabelFor(localInstancesCombo); 274 localInstanceLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_LOCAL_INSTANCE")); gridBagConstraints = new GridBagConstraints(); 276 gridBagConstraints.gridx = 0; 277 gridBagConstraints.gridy = 1; 278 gridBagConstraints.anchor = GridBagConstraints.EAST; 279 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 280 add(localInstanceLabel, gridBagConstraints); 281 282 localInstancesCombo.addActionListener(new InstanceSelectionListener()); 284 gridBagConstraints = new GridBagConstraints(); 285 gridBagConstraints.gridx = 1; 286 gridBagConstraints.gridy = 1; 287 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 288 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 289 add(localInstancesCombo, gridBagConstraints); 290 localInstancesCombo.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_localInstancesCombo")); 292 domainPathLabel.setLabelFor(domainPathField); 294 domainPathLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_DOMAIN_LOCATION")); gridBagConstraints = new GridBagConstraints(); 296 gridBagConstraints.gridx = 0; 297 gridBagConstraints.gridy = 2; 298 gridBagConstraints.anchor = GridBagConstraints.EAST; 299 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 300 add(domainPathLabel, gridBagConstraints); 301 302 domainPathField.setColumns(20); 304 domainPathField.setText(""); domainPathField.setEditable(false); 306 gridBagConstraints = new GridBagConstraints(); 307 gridBagConstraints.gridx = 1; 308 gridBagConstraints.gridy = 2; 309 gridBagConstraints.weightx = 1.0; 310 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 311 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 312 add(domainPathField, gridBagConstraints); 313 domainPathField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_domainPathField")); 315 hostLabel.setLabelFor(hostField); 317 hostLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_HOST")); gridBagConstraints = new GridBagConstraints(); 319 gridBagConstraints.gridx = 0; 320 gridBagConstraints.gridy = 3; 321 gridBagConstraints.anchor = GridBagConstraints.EAST; 322 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 323 add(hostLabel, gridBagConstraints); 324 325 hostField.setColumns(20); 327 hostField.setText(""); hostField.addKeyListener(new KeyListener()); 329 gridBagConstraints = new GridBagConstraints(); 330 gridBagConstraints.gridx = 1; 331 gridBagConstraints.gridy = 3; 332 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 333 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 334 add(hostField, gridBagConstraints); 335 hostField.setEditable(false); 336 hostField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_hostField")); 338 portLabel.setLabelFor(portField); 340 portLabel.setText(NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_PORT")); gridBagConstraints = new GridBagConstraints(); 342 gridBagConstraints.gridx = 0; 343 gridBagConstraints.gridy = 4; 344 gridBagConstraints.anchor = GridBagConstraints.EAST; 345 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 346 add(portLabel, gridBagConstraints); 347 348 portField.setColumns(20); 350 portField.setText(""); portField.addKeyListener(new KeyListener()); 352 gridBagConstraints = new GridBagConstraints(); 353 gridBagConstraints.gridx = 1; 354 gridBagConstraints.gridy = 4; 355 gridBagConstraints.anchor = GridBagConstraints.WEST; 356 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 357 add(portField, gridBagConstraints); 358 portField.setEditable(false); 359 portField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_portField")); 361 userNameLabel.setLabelFor(usernameField); 363 org.openide.awt.Mnemonics.setLocalizedText(userNameLabel, NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_USERNAME")); 365 gridBagConstraints = new GridBagConstraints(); 366 gridBagConstraints.gridx = 0; 367 gridBagConstraints.gridy = 5; 368 gridBagConstraints.anchor = GridBagConstraints.EAST; 369 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 370 add(userNameLabel, gridBagConstraints); 371 372 usernameField.setColumns(20); 374 usernameField.setText(""); usernameField.addKeyListener(new KeyListener()); 376 gridBagConstraints = new GridBagConstraints(); 377 gridBagConstraints.gridx = 1; 378 gridBagConstraints.gridy = 5; 379 gridBagConstraints.anchor = GridBagConstraints.WEST; 380 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 381 add(usernameField, gridBagConstraints); 382 usernameField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_usernameField")); 384 passwordLabel.setLabelFor(passwordField); 386 org.openide.awt.Mnemonics.setLocalizedText(passwordLabel, NbBundle.getMessage(ServerPropertiesPanel.class, "LBL_PASSWORD")); 388 gridBagConstraints = new GridBagConstraints(); 389 gridBagConstraints.gridx = 0; 390 gridBagConstraints.gridy = 6; 391 gridBagConstraints.anchor = GridBagConstraints.EAST; 392 gridBagConstraints.insets = new Insets(0, 0, 5, 0); 393 add(passwordLabel, gridBagConstraints); 394 395 passwordField.setColumns(20); 397 passwordField.addKeyListener(new KeyListener()); 398 gridBagConstraints = new GridBagConstraints(); 399 gridBagConstraints.gridx = 1; 400 gridBagConstraints.gridy = 6; 401 gridBagConstraints.anchor = GridBagConstraints.WEST; 402 gridBagConstraints.insets = new Insets(0, 10, 5, 0); 403 add(passwordField, gridBagConstraints); 404 passwordField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ServerPropertiesPanel.class, "ACSD_ServerPropertiesPanel_passwordField")); 406 gridBagConstraints = new GridBagConstraints(); 408 gridBagConstraints.gridx = 0; 409 gridBagConstraints.gridy = 7; 410 gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; 411 gridBagConstraints.weighty = 1.0; 412 add(formattingPanel, gridBagConstraints); 413 } 414 415 423 private String [] getRegisteredDomains(String serverRoot){ 424 Vector result = new Vector(); 426 427 if (serverRoot == null) { 429 return new String [0]; 430 } 431 432 String domainListFile = "/common/nodemanager/nodemanager.domains"; 435 File file = new File(serverRoot + domainListFile); 437 LineNumberReader lnr = null; 438 439 try { 441 lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(file))); 443 444 String line; 446 while ((line = lnr.readLine()) != null){ 447 if (line.startsWith("#")) { 449 continue; 450 } 451 452 String path = line.split("=")[1].replaceAll("\\\\\\\\", "/").replaceAll("\\\\:", ":"); 455 result.add(path); 457 } 458 } catch (FileNotFoundException e) { 459 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 460 } catch (IOException e) { 461 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 462 } finally { 463 try { 464 if (lnr != null) { 466 lnr.close(); 467 } 468 } catch (IOException e) { 469 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 470 } 471 } 472 473 return (String []) result.toArray(new String [result.size()]); 475 } 476 477 482 private Vector getServerInstances(){ 483 Vector result = new Vector(); 485 486 String [] domains = getRegisteredDomains(instantiatingIterator.getServerRoot()); 488 489 for (int i = 0; i < domains.length; i++) { 491 String configPath = domains[i] + "/config/config.xml"; 494 InputStream inputStream = null; 496 Document document = null; 497 498 try { 499 inputStream = new FileInputStream(new File(configPath)); 501 502 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); 504 505 Element root = document.getDocumentElement(); 507 508 NodeList children = root.getChildNodes(); 510 511 for (int j = 0; j < children.getLength(); j++) { 513 Node child = children.item(j); 514 if (child.getNodeName().matches("(?:[a-z]+\\:)?server")) { NodeList nl = child.getChildNodes(); 518 519 String name = ""; 521 String port = ""; 522 String host = ""; 523 524 for (int k = 0; k < nl.getLength(); k++){ 526 Node ch = nl.item(k); 527 528 if (ch.getNodeName().matches("(?:[a-z]+\\:)?name")) { name = ch.getFirstChild().getNodeValue(); 532 } 533 534 if (ch.getNodeName().matches("(?:[a-z]+\\:)?listen-port")) { port = ch.getFirstChild().getNodeValue(); 538 } 539 540 if (ch.getNodeName().matches("(?:[a-z]+\\:)?listen-address")) { if (ch.hasChildNodes()){ 544 host = ch.getFirstChild().getNodeValue(); 545 } 546 } 547 } 548 549 if ((name != null) && (!name.equals(""))) { port = port == null || port.equals("") ? "7001" : port; host = host == null || host.equals("") ? "localhost" : host; result.add(new Instance(name, host, port, domains[i])); 556 } 557 } 558 } 559 } catch(FileNotFoundException e) { 560 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 561 } catch (IOException e) { 562 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 563 } catch (ParserConfigurationException e) { 564 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 565 } catch (SAXException e) { 566 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 567 } finally { 568 try { 569 if (inputStream != null) { 570 inputStream.close(); 571 } 572 } catch (IOException e) { 573 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 574 } 575 } 576 } 577 578 return result; 580 } 581 582 586 public void updateInstancesList() { 587 localInstancesCombo.setModel(new InstancesModel(getServerInstances())); 588 updateInstanceInfo(); 589 } 590 591 595 private void updateInstanceInfo() { 596 Instance instance = (Instance) localInstancesCombo.getSelectedItem(); 598 599 if (instance != null) { 600 domainPathField.setText(instance.getDomainPath()); 602 hostField.setText(instance.getHost()); 603 portField.setText(instance.getPort()); 604 } 605 606 } 607 616 public void readSettings(Object object) { 617 this.wizardDescriptor = (WizardDescriptor) object; 618 } 619 620 624 public void storeSettings(Object object) {} 625 626 632 private Vector listeners = new Vector(); 633 634 639 public void removeChangeListener(ChangeListener listener) { 640 if (listeners != null) { 641 synchronized (listeners) { 642 listeners.remove(listener); 643 } 644 } 645 } 646 647 652 public void addChangeListener(ChangeListener listener) { 653 synchronized (listeners) { 654 listeners.add(listener); 655 } 656 } 657 658 661 private void fireChangeEvent() { 662 ChangeEvent event = new ChangeEvent(this); 663 fireChangeEvent(event); 664 } 665 666 671 private void fireChangeEvent(ChangeEvent event) { 672 Vector targetListeners; 673 synchronized (listeners) { 674 targetListeners = (Vector) listeners.clone(); 675 } 676 677 for (int i = 0; i < targetListeners.size(); i++) { 678 ChangeListener listener = (ChangeListener) targetListeners.elementAt(i); 679 listener.stateChanged(event); 680 } 681 } 682 683 691 private class KeyListener extends KeyAdapter { 692 695 public void keyTyped(KeyEvent event) { 696 fireChangeEvent(); 697 } 698 699 702 public void keyReleased(KeyEvent event) { 703 fireChangeEvent(); 704 } 705 } 706 707 714 private class ServerTypeActionListener implements ActionListener { 715 719 public void actionPerformed(ActionEvent e) { 720 if (serverTypeCombo.getSelectedItem().equals(NbBundle.getMessage(ServerPropertiesPanel.class, "SERVER_TYPE_LOCAL"))) { Instance instance = (Instance) localInstancesCombo.getSelectedItem(); 723 724 localInstancesCombo.setEnabled(true); 726 727 domainPathField.setEnabled(true); 729 domainPathField.setEditable(false); 730 731 hostField.setEnabled(true); 733 hostField.setEditable(false); 734 hostField.setText(instance.getHost()); 735 736 portField.setEnabled(true); 738 portField.setEditable(false); 739 portField.setText(instance.getPort()); 740 } else { 741 localInstancesCombo.setEnabled(false); 743 744 domainPathField.setEnabled(false); 746 domainPathField.setEditable(false); 747 748 hostField.setEnabled(true); 750 hostField.setEditable(true); 751 752 portField.setEnabled(true); 754 portField.setEditable(true); 755 } 756 757 isValid(); 758 } 759 } 760 761 767 private class InstanceSelectionListener implements ActionListener { 768 772 public void actionPerformed(ActionEvent e) { 773 updateInstanceInfo(); 774 } 775 } 776 777 784 private static class InstancesModel extends AbstractListModel implements ComboBoxModel { 785 788 private Vector instances; 789 790 793 private int selectedIndex = 0; 794 795 800 public InstancesModel(Vector instances) { 801 this.instances = instances; 803 804 this.selectedIndex = 0; 806 } 807 808 813 public void setSelectedItem(Object item) { 814 selectedIndex = instances.indexOf(item); 817 } 818 819 826 public Object getElementAt(int index) { 827 return instances.elementAt(index); 828 } 829 830 835 public int getSize() { 836 return instances.size(); 837 } 838 839 844 public Object getSelectedItem() { 845 if (instances.size() == 0) { 847 return null; 848 } 849 850 return instances.elementAt(selectedIndex); 852 } 853 854 } 855 856 862 private static class Instance { 863 867 private String name; 868 869 872 private String host; 873 874 877 private String port; 878 879 882 private String domainPath; 883 884 892 public Instance(String name, String host, String port, String domainPath) { 893 this.name = name; 895 this.host = host; 896 this.port = port; 897 this.domainPath = domainPath; 898 } 899 900 905 public String getName() { 906 return this.name; 907 } 908 909 914 public void setName(String name) { 915 this.name = name; 916 } 917 918 923 public String getHost() { 924 return this.host; 925 } 926 927 932 public void setHost(String host) { 933 this.host = host; 934 } 935 936 941 public String getPort() { 942 return this.port; 943 } 944 945 950 public void setPort(String port) { 951 this.port = port; 952 } 953 954 959 public String getDomainPath() { 960 return this.domainPath; 961 } 962 963 968 public void setDomainPath(String domainPath) { 969 this.domainPath = domainPath; 970 } 971 972 976 public String toString() { 977 return name + " [" + host + ":" + port + "]"; } 979 } 980 } 981 | Popular Tags |