1 23 24 29 package com.sun.enterprise.cli.framework; 30 31 import java.net.URL ; 33 import java.net.URLEncoder ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Vector ; 37 import java.util.Enumeration ; 38 import java.util.Properties ; 39 import java.util.StringTokenizer ; 40 import java.io.IOException ; 41 import java.io.Serializable ; 42 import java.io.ObjectOutputStream ; 43 import java.io.FileOutputStream ; 44 import java.io.ObjectInputStream ; 45 import java.io.InputStream ; 46 import java.io.FileInputStream ; 47 import java.io.File ; 48 import java.io.FileNotFoundException ; 49 import java.io.FilenameFilter ; 50 import java.io.UnsupportedEncodingException ; 51 52 import javax.xml.parsers.DocumentBuilder ; 54 import javax.xml.parsers.DocumentBuilderFactory ; 55 import javax.xml.parsers.ParserConfigurationException ; 56 57 import org.xml.sax.SAXException ; 58 import org.xml.sax.EntityResolver ; 59 import org.xml.sax.InputSource ; 60 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 61 62 import org.w3c.dom.Document ; 63 import org.w3c.dom.Node ; 64 import org.w3c.dom.NamedNodeMap ; 65 66 70 public class CLIDescriptorsReader 71 { 72 73 public static int DONT_SERIALIZE = 0; 74 public static int SERIALIZE_COMMANDS_TO_FILES = 1; 75 public static int SERIALIZE_COMMANDS_TO_FILE = 2; 76 public static String SERIALIZED_DESCRIPTOR_FILE = ".CLIDescriptors"; 77 public static String SERIALIZED_COMMANDS_DEFAULT_DIR = "cli"; 78 public static String ENVIRONMENT_PREFIX = "environment-prefix"; 79 public static String ENVIRONMENT_FILENAME = "environment-filename"; 80 private static CLIDescriptorsReader cliDescriptorsReader; 82 83 private String serializeDir = SERIALIZED_COMMANDS_DEFAULT_DIR; 85 86 private int serializeDescriptors = DONT_SERIALIZE; 89 90 private ValidCommandsList commandsList = null; 92 93 private HashMap validOptions = null; 95 96 private HashMap replaceOptions = null; 98 99 private Vector descriptors = null; 101 102 private Vector properties = null; 104 105 private String defaultCommand = null; 107 108 private String helpClass = null; 110 111 112 113 private static String DESCRIPTOR_FILE_NAME = "CLIDescriptor.xml"; 114 117 protected CLIDescriptorsReader() 118 { 119 initialize(); 120 } 121 122 123 127 protected CLIDescriptorsReader(URL descriptor) 128 { 129 setDescriptor(descriptor); 130 initialize(); 131 } 132 133 134 138 protected CLIDescriptorsReader(Vector descriptors) 139 { 140 this.descriptors = descriptors; 141 initialize(); 142 } 143 144 145 148 private void initialize() 149 { 150 commandsList = new ValidCommandsList(); 151 validOptions = new HashMap (); 152 replaceOptions = new HashMap (); 153 properties = new Vector (); 154 } 155 156 157 160 public static CLIDescriptorsReader getInstance() 161 { 162 if (cliDescriptorsReader == null) 163 { 164 cliDescriptorsReader= new CLIDescriptorsReader(); 165 } 166 return cliDescriptorsReader; 167 } 168 169 170 176 public ValidCommand getCommand(String commandName) 177 throws CommandValidationException 178 { 179 ValidCommand command = null; 180 if ((commandsList == null) || (commandsList.size() == 0)) 181 { 182 command = getCommandFromFileOrDescriptor(commandName); 183 } 184 else 185 { 186 command = commandsList.getValidCommand(commandName); 187 } 188 return command; 189 } 190 191 192 196 public String getDefaultCommand() 197 { 198 return defaultCommand; 199 } 200 201 202 206 public String getHelpClass() 207 { 208 return helpClass; 209 } 210 211 217 public ValidCommandsList getCommandsList() throws CommandValidationException 218 { 219 if ((commandsList == null) || (commandsList.size() == 0)) 220 { 221 loadAllCommandsFromFilesOrDescriptors(); 222 } 223 return commandsList; 224 } 225 226 227 231 public Iterator getProperties() throws CommandValidationException 232 { 233 return properties.iterator(); 234 } 235 236 237 242 private ValidCommand getCommandFromFileOrDescriptor(String commandName) 243 throws CommandValidationException 244 { 245 ValidCommand command = null; 246 247 248 249 250 if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES) 251 { 252 command = getSerializedCommand(commandName); 253 if (command != null) 254 { 255 return command; 256 } 257 } 258 259 int saveSerializedDescriptor = getSerializeDescriptorsProperty(); 262 setSerializeDescriptorsProperty(DONT_SERIALIZE); 263 loadAllCommandsFromFilesOrDescriptors(); 264 setSerializeDescriptorsProperty(saveSerializedDescriptor); 266 if (commandName != null) 267 return commandsList.getValidCommand(commandName); 268 else if (defaultCommand != null) 269 return commandsList.getValidCommand(defaultCommand); 270 else 271 throw new CommandValidationException( 272 LocalStringsManagerFactory.getFrameworkLocalStringsManager(). 273 getString("CommandNotSpecified", null)); 274 275 } 276 277 278 284 private ValidCommand getSerializedCommand(String commandName) 285 throws CommandValidationException 286 { 287 288 ValidCommand command = null; 289 String encodedCommandName = null; 290 try{ 291 encodedCommandName = URLEncoder.encode(commandName,"UTF-8"); 292 }catch(UnsupportedEncodingException ue){ 293 encodedCommandName = commandName; 294 } 295 final InputStream in = CLIDescriptorsReader.class.getClassLoader().getResourceAsStream(serializeDir + "/." + encodedCommandName); 296 297 if (in!=null) 298 { 299 command = getSerializedCommand(in); 300 } 301 else { 302 return null; 303 } 304 305 312 313 return command; 314 } 315 316 317 322 private ValidCommand getSerializedCommand(InputStream commandFile) 323 throws CommandValidationException 324 { 325 try 326 { 327 ValidCommand command = null; 328 ObjectInputStream in = new ObjectInputStream (commandFile); 329 330 333 334 defaultCommand = (String ) in.readObject(); 335 helpClass = (String ) in.readObject(); 336 properties = (Vector ) in.readObject(); 337 338 command = (ValidCommand) in.readObject(); 339 CLILogger.getInstance().printDebugMessage("++++++++++++++++++++++++++++ Command loaded from file and it is " + command); 340 return command; 341 } 342 catch (Exception e) 343 { 344 throw new CommandValidationException(e); 345 } 346 } 347 348 349 352 private void loadAllCommandsFromFilesOrDescriptors() 353 throws CommandValidationException 354 { 355 if (serializeDescriptors == DONT_SERIALIZE) 356 { 357 readDescriptors(); 358 } 359 else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES) 360 { 361 loadCommandsFromMultipleFiles(); 362 } 363 else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILE) 364 { 365 loadCommandsFromSingleFile(); 366 } 367 } 368 369 370 373 private void loadCommandsFromMultipleFiles() 374 throws CommandValidationException 375 { 376 File commandsDir = new File (serializeDir); 377 378 if ((commandsDir != null) && (commandsDir.exists()) && 379 (getSerializedCommandsFileList(commandsDir).length > 0)) 380 { 381 getDescriptors(); 382 if (descriptors == null) 383 { 384 LocalStringsManager lsm = 385 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 386 throw new CommandValidationException(lsm.getString("NoDescriptorsDefined")); 387 } 388 boolean isSerializedCommandsLatest = true; 389 for (int i = 0; (i < descriptors.size()) && 390 isSerializedCommandsLatest; i++) 391 { 392 URL descriptorURL = (URL ) descriptors.get(i); 393 File descriptorFile = getDescriptorFile(descriptorURL); 394 if ((descriptorFile != null) && (descriptorFile.exists())) 395 { 396 if (descriptorFile.lastModified() > commandsDir.lastModified()) 397 { 398 isSerializedCommandsLatest = false; 399 break; 400 } 401 } 402 else 403 { 404 } 406 } 407 if (isSerializedCommandsLatest) 408 { 409 if (!loadSerializedCommands()) 410 { 411 readDescriptors(); 412 } 413 } 414 else 415 { 416 readDescriptors(); 417 } 418 } 419 else 420 { 421 readDescriptors(); 422 } 423 } 424 425 426 429 private void loadCommandsFromSingleFile() 430 throws CommandValidationException 431 { 432 File serializedDescriptorFile = getSerializedDescriptorFile(); 433 if (serializedDescriptorFile.exists()) 434 CLILogger.getInstance().printDebugMessage("Yes the file exists"); 435 if (!((serializedDescriptorFile != null) && 436 (serializedDescriptorFile.exists()))) 437 { 438 readDescriptors(); 439 return; 440 } 441 442 getDescriptors(); 443 if (descriptors == null) 444 { 445 LocalStringsManager lsm = 446 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 447 throw new CommandValidationException(lsm.getString("NoDescriptorsDefined")); 448 } 449 boolean isSerializedDescriptorLatest = true; 450 for (int i = 0; (i < descriptors.size()) && 451 isSerializedDescriptorLatest; i++) 452 { 453 URL descriptorURL = (URL ) descriptors.get(i); 454 File descriptorFile = getDescriptorFile(descriptorURL); 455 if ((descriptorFile != null) && (descriptorFile.exists())) 456 { 457 if (descriptorFile.lastModified() > 458 serializedDescriptorFile.lastModified()) 459 { 460 isSerializedDescriptorLatest = false; 461 break; 462 } 463 } 464 else 465 { 466 } 468 } 469 if (isSerializedDescriptorLatest) 470 { 471 loadSerializedDescriptorFile(); 472 } 473 else 474 { 475 readDescriptors(); 476 } 477 } 478 479 480 484 private void readDescriptors() throws CommandValidationException 485 { 486 commandsList.removeAllCommands(); 487 try 491 { 492 DOMParser parser = new DOMParser(); 494 parser.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", true); 495 parser.setFeature( "http://xml.org/sax/features/validation", true ); 496 parser.setFeature( "http://xml.org/sax/features/namespaces", true ); 497 parser.setFeature( "http://apache.org/xml/features/validation/schema", true ); 498 parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false); 499 parser.setEntityResolver(new CLIEntityResolver()); 500 501 if (descriptors == null) 505 { 506 getDescriptors(); 507 if (descriptors == null) 508 { 509 LocalStringsManager lsm = 510 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 511 throw new CommandValidationException(lsm.getString("NoDescriptorsDefined")); 512 } 513 } 514 for (int i = 0; i < descriptors.size(); i++) 515 { 516 URL descriptorURL = (URL ) descriptors.get(i); 517 parser.parse(new InputSource (descriptorURL.toString())); 520 Document document = parser.getDocument(); 521 generateOptionsAndCommands(document); 522 } 523 replaceOptionsInCommandsList(replaceOptions); 527 528 if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILES) 529 { 530 saveCommandsAsMultipleFiles(); 531 } 532 else if (serializeDescriptors == SERIALIZE_COMMANDS_TO_FILE) 533 { 534 saveCommandsAsSingleFile(); 535 } 536 537 } 538 catch (SAXException sxe) 539 { 540 Exception x = sxe; 542 if (sxe.getException() != null) 543 x = sxe.getException(); 544 throw new CommandValidationException(x); 545 } 546 catch (IOException ioe) 547 { 548 throw new CommandValidationException(ioe); 550 } 552 } 553 554 555 558 private String [] getSerializedCommandsFileList(File commandsDir) 559 { 560 String [] commandFiles = commandsDir.list(new FilenameFilter () { 561 public boolean accept(File parentDir, String name) { 562 return (name.startsWith(".")); 563 }}); 564 return commandFiles; 565 } 566 567 572 private File getSerializedDescriptorFile() 573 { 574 File file = null; 575 try 576 { 577 file = new File (serializeDir + "/" + SERIALIZED_DESCRIPTOR_FILE); 578 } 579 catch (NullPointerException npe) 580 { 581 } 582 583 return file; 584 } 585 586 587 592 private File getDescriptorFile(URL descriptor) 593 { 594 File file = null; 595 try 596 { 597 file = new File (descriptor.getFile()); 598 if (!file.exists()) 599 file = null; 600 } 601 catch (NullPointerException e) 602 { 603 604 } 605 return file; 606 } 607 608 609 614 private boolean loadSerializedCommands() 615 throws CommandValidationException 616 { 617 File commandsDir = new File (serializeDir); 618 619 if (!((commandsDir != null) && (commandsDir.exists()) && 620 (getSerializedCommandsFileList(commandsDir).length > 0))) 621 { 622 return false; 623 } 624 625 String [] commandFiles = getSerializedCommandsFileList(commandsDir); 626 627 boolean loaded = true; 628 for (int i = 0; (i < commandFiles.length) && loaded; i++) 629 { 630 final InputStream in = CLIDescriptorsReader.class.getClassLoader().getResourceAsStream(serializeDir + "/." + commandFiles[i] ); 632 633 try 634 { 635 ValidCommand command = getSerializedCommand(in); 636 if (command != null) 638 { 639 commandsList.addCommand(command); 640 } 641 else 642 { 643 loaded = false; 644 } 645 } 646 catch (CommandValidationException e) 647 { 648 loaded = false; 649 break; 650 } 651 } 652 return loaded; 653 } 654 655 656 660 private void loadSerializedDescriptorFile() 661 { 662 try 663 { 664 File fileName = getSerializedDescriptorFile(); 665 ObjectInputStream in = new ObjectInputStream (new FileInputStream (fileName)); 668 669 670 defaultCommand = (String ) in.readObject(); 671 helpClass = (String ) in.readObject(); 672 properties = (Vector ) in.readObject(); 673 674 commandsList = (ValidCommandsList) in.readObject(); 675 CLILogger.getInstance().printDebugMessage("++++++++++++++++++++++++++++ Commands loaded from file"); 676 } 677 catch(FileNotFoundException fnfe) 678 { 679 680 } 681 catch(IOException ioe) 682 { 683 684 } 685 catch(ClassNotFoundException cnfe) 686 { 687 688 } 689 } 690 691 692 695 private Vector getDescriptors() throws CommandValidationException 696 { 697 if (descriptors != null) 698 return descriptors; 699 700 try 701 { 702 String descriptor_file_name = System.getProperty("DESCRIPTOR_FILE"); 703 if(descriptor_file_name == null) 704 descriptor_file_name = DESCRIPTOR_FILE_NAME; 705 Enumeration urls = CLIDescriptorsReader.class.getClassLoader().getResources( 706 descriptor_file_name); 707 if ((urls == null) || (!urls.hasMoreElements())) 708 { 709 return descriptors; 710 } 711 712 while (urls.hasMoreElements()) 713 { 714 URL url = (URL ) urls.nextElement(); 715 setDescriptor(url); 716 } 717 } 718 catch(IOException ioe) 719 { 720 LocalStringsManager lsm = 721 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 722 CLILogger.getInstance().printMessage(lsm.getString("CouldNotLoadDescriptor")); 723 } 724 return descriptors; 725 } 726 727 728 732 public void setDescriptor(URL descriptor) 733 { 734 if (descriptor != null) 735 { 736 if (descriptors == null) 737 { 738 descriptors = new Vector (); 739 } 740 descriptors.add(descriptor); 741 java.util.Collections.reverse(descriptors); 743 } 744 } 745 746 747 751 public void setDescriptors(Vector descriptors) 752 { 753 this.descriptors = descriptors; 754 } 755 756 757 761 private void generateOptionsAndCommands(Document document) 762 { 763 if (document != null) 764 { 765 for (Node nextKid = document.getDocumentElement().getFirstChild(); 766 nextKid != null; nextKid = nextKid.getNextSibling()) 767 { 768 String nodeName = nextKid.getNodeName(); 769 if (nodeName.equalsIgnoreCase("CommandProperties")) 770 { 771 Properties props = new Properties (); 772 for (Node grandKid = nextKid.getFirstChild(); 773 grandKid != null; grandKid = grandKid.getNextSibling()) 774 { 775 String grandKidNodeName = grandKid.getNodeName(); 776 if (grandKidNodeName.equalsIgnoreCase("CommandProperty")) 777 { 778 NamedNodeMap nodeMap = grandKid.getAttributes(); 779 String nameAttr = 780 nodeMap.getNamedItem("name").getNodeValue(); 781 String valueAttr = 782 nodeMap.getNamedItem("value").getNodeValue(); 783 props.setProperty(nameAttr, valueAttr); 784 } 785 } 786 final NamedNodeMap commandPropertiesAttribute = nextKid.getAttributes(); 787 if (commandPropertiesAttribute != null && 788 commandPropertiesAttribute.getNamedItem("defaultcommand") != null ) 789 defaultCommand = commandPropertiesAttribute.getNamedItem( 790 "defaultcommand").getNodeValue(); 791 if (commandPropertiesAttribute != null && 792 commandPropertiesAttribute.getNamedItem("helpclass") != null ) 793 helpClass = commandPropertiesAttribute.getNamedItem( 794 "helpclass").getNodeValue(); 795 796 properties.add(props); 797 } 798 else if (nodeName.equalsIgnoreCase("Options")) 799 { 800 for (Node grandKid = nextKid.getFirstChild(); 801 grandKid != null; grandKid = grandKid.getNextSibling()) 802 { 803 String grandKidNodeName = grandKid.getNodeName(); 804 if (grandKidNodeName.equalsIgnoreCase("Option")) 805 { 806 ValidOption option = generateOption(document, grandKid); 807 validOptions.put(option.getName(), option); 808 } 809 else if (grandKidNodeName.equalsIgnoreCase("ReplaceOption")) 810 { 811 ValidOption option = generateOption(document, grandKid); 812 replaceOptions.put(option.getName(), option); 813 } 814 815 } 816 } 817 else if (nodeName.equalsIgnoreCase("Commands")) 820 { 821 for (Node grandKid = nextKid.getFirstChild(); 822 grandKid != null; grandKid = grandKid.getNextSibling()) 823 { 824 String grandKidNodeName = grandKid.getNodeName(); 825 if (grandKidNodeName.equalsIgnoreCase("Command")) 826 { 827 ValidCommand command = generateCommand(document, grandKid); 828 commandsList.addCommand(command); 829 } 830 } 831 } 832 } 833 } 834 } 835 836 837 844 private ValidOption generateOption(Document document, Node grandKid) 845 { 846 ValidOption option = new ValidOption(); 847 NamedNodeMap nodeMap = grandKid.getAttributes(); 848 String nameAttr = nodeMap.getNamedItem("name").getNodeValue(); 849 String typeAttr = nodeMap.getNamedItem("type").getNodeValue(); 850 boolean valueReqdAttr = Boolean.valueOf(nodeMap.getNamedItem("value-required").getNodeValue()).booleanValue(); 851 Node defaultAttrNode = nodeMap.getNamedItem("default"); 852 String defaultAttr = null; 853 if (defaultAttrNode != null) 854 defaultAttr = defaultAttrNode.getNodeValue(); 855 option.setName(nameAttr); 856 option.setType(typeAttr); 857 option.setRequired(valueReqdAttr?ValidOption.REQUIRED:ValidOption.OPTIONAL); 858 option.setDefaultValue(defaultAttr); 859 for (Node nextGrandKid = grandKid.getFirstChild(); nextGrandKid != null; 860 nextGrandKid = nextGrandKid.getNextSibling()) 861 { 862 String grandKidName = nextGrandKid.getNodeName(); 863 if (grandKidName.equalsIgnoreCase("shortoption")) 864 { 865 String shortOption = nextGrandKid.getFirstChild().getNodeValue(); 866 shortOption = shortOption.trim(); 867 if (shortOption != null) 868 { 869 option.setShortName(shortOption); 870 } 871 } 872 } 873 return option; 874 } 875 876 877 884 private ValidCommand generateCommand(Document document, Node grandKid) 885 { 886 ValidCommand command = new ValidCommand(); 887 888 NamedNodeMap nodeMap = grandKid.getAttributes(); 889 String nameAttr = nodeMap.getNamedItem("name").getNodeValue(); 890 String classNameAttr = nodeMap.getNamedItem("classname").getNodeValue(); 891 String numberOfOperandsAttr = nodeMap.getNamedItem("numberofoperands").getNodeValue(); 892 893 String defaultOperandAttr = null; 894 if (nodeMap.getNamedItem("defaultoperand") != null) 895 defaultOperandAttr = nodeMap.getNamedItem("defaultoperand").getNodeValue(); 896 897 Node usageTextNode = nodeMap.getNamedItem("usage-text"); 898 String usageTextAttr = null; 899 if (usageTextNode != null) 900 usageTextAttr = usageTextNode.getNodeValue(); 901 command.setName(nameAttr); 902 command.setNumberOfOperands(numberOfOperandsAttr); 903 command.setDefaultOperand(defaultOperandAttr); 904 command.setClassName(classNameAttr); 905 command.setUsageText(usageTextAttr); 906 907 for (Node nextGrandKid = grandKid.getFirstChild(); nextGrandKid != null; 908 nextGrandKid = nextGrandKid.getNextSibling()) 909 { 910 String grandKidName = nextGrandKid.getNodeName(); 911 if (grandKidName.equalsIgnoreCase("ValidOption")) 912 { 913 final NamedNodeMap validOptionNodeMap = 914 nextGrandKid.getAttributes(); 915 final String validOption = 916 validOptionNodeMap.getNamedItem("name").getNodeValue(); 917 String deprecatedOption = null; 918 if (validOptionNodeMap.getNamedItem("deprecatedoption") != null) 919 { 920 deprecatedOption = validOptionNodeMap.getNamedItem("deprecatedoption").getNodeValue(); 921 } 922 String defaultValue = null; 923 if (validOptionNodeMap.getNamedItem("defaultvalue") != null) 924 { 925 defaultValue = validOptionNodeMap.getNamedItem("defaultvalue").getNodeValue(); 926 } 927 928 final ValidOption option = findOption(validOption); 929 if (option == null) 930 { 931 CLILogger.getInstance().printDebugMessage("Valid option (" + 932 validOption + 933 ")is not found in xml : " + 934 command.getName()); 935 return command; 936 } 937 ValidOption newOption = new ValidOption(option); 938 if (deprecatedOption != null) 939 newOption.setDeprecatedOption(deprecatedOption); 940 if (defaultValue != null) 941 newOption.setDefaultValue(defaultValue); 942 943 command.addValidOption(newOption); 944 } 945 else if (grandKidName.equalsIgnoreCase("RequiredOption")) 946 { 947 final NamedNodeMap reqdOptionNodeMap = 948 nextGrandKid.getAttributes(); 949 final String reqdOption = 950 reqdOptionNodeMap.getNamedItem("name").getNodeValue(); 951 String deprecatedOption = null; 952 if (reqdOptionNodeMap.getNamedItem("deprecatedoption") != null) 953 { 954 deprecatedOption = 955 reqdOptionNodeMap.getNamedItem("deprecatedoption").getNodeValue(); 956 } 957 final ValidOption option = findOption(reqdOption); 958 if (option == null) 959 { 960 CLILogger.getInstance().printDebugMessage("Required option (" + 961 reqdOption + 962 ")is not found in xml : " + 963 command.getName()); 964 return command; 965 } 966 if (deprecatedOption != null) 967 option.setDeprecatedOption(deprecatedOption); 968 command.addRequiredOption(new ValidOption(option)); 969 } 970 else if (grandKidName.equalsIgnoreCase("DeprecatedOption")) 971 { 972 final String deprecatedOption = nextGrandKid.getFirstChild().getNodeValue(); 973 final ValidOption option = findOption(deprecatedOption); 974 if (option == null) 975 { 976 CLILogger.getInstance().printDebugMessage("Deprecated option (" + 977 deprecatedOption + 978 ")is not found in xml : " + 979 command.getName()); 980 return command; 981 } 982 command.addDeprecatedOption(option); 983 } 984 else if (grandKidName.equalsIgnoreCase("properties")) 985 { 986 for (Node nextGreatGrandKid = nextGrandKid.getFirstChild(); 987 nextGreatGrandKid != null; 988 nextGreatGrandKid = nextGreatGrandKid.getNextSibling()) 989 { 990 String greatGrandKidName = nextGreatGrandKid.getNodeName(); 991 if (greatGrandKidName.equalsIgnoreCase("property")) 992 { 993 nodeMap = nextGreatGrandKid.getAttributes(); 994 String propertyNameAttr = 995 nodeMap.getNamedItem("name").getNodeValue(); 996 997 Vector values = new Vector (); 998 for (Node nextGreatGreatGrandKid = nextGreatGrandKid.getFirstChild(); 999 nextGreatGreatGrandKid != null; 1000 nextGreatGreatGrandKid = nextGreatGreatGrandKid.getNextSibling()) 1001 { 1002 String greatGreatGrandKidName = nextGreatGreatGrandKid.getNodeName(); 1003 if (greatGreatGrandKidName.equalsIgnoreCase("value")) 1004 { 1005 String value = null; 1006 1007 if (nextGreatGreatGrandKid.getFirstChild() != null) 1008 value = nextGreatGreatGrandKid.getFirstChild().getNodeValue(); 1009 values.add(value); 1010 } 1011 1012 } 1013 command.setProperty(propertyNameAttr, values); 1014 } 1015 } 1016 } 1017 } 1018 1019 return command; 1020 } 1021 1022 1023 1028 private ValidOption findOption(String optionStr) 1029 { 1030 ValidOption option = null; 1031 option = (ValidOption) validOptions.get(optionStr); 1032 return option; 1033 } 1034 1035 1036 1040 private void saveCommandsAsMultipleFiles() throws CommandValidationException 1041 { 1042 Iterator commands = commandsList.getCommands(); 1043 while (commands.hasNext()) 1044 { 1045 try 1046 { 1047 ValidCommand command = (ValidCommand) commands.next(); 1048 File file = new File (serializeDir); 1049 if (!file.exists()) 1050 { 1051 file.mkdir(); 1052 } 1053 String encodedCommandName = null; 1054 try{ 1055 encodedCommandName = URLEncoder.encode(command.getName(),"UTF-8"); 1056 }catch(UnsupportedEncodingException ue){ 1057 encodedCommandName = command.getName(); 1058 } 1059 ObjectOutputStream os = new ObjectOutputStream ( 1060 new FileOutputStream (serializeDir + 1061 "/."+ encodedCommandName)); 1062 1063 os.writeObject(defaultCommand); 1064 os.writeObject(helpClass); 1065 os.writeObject(properties); 1066 1067 os.writeObject(command); 1068 } 1069 catch(Exception e) 1070 { 1071 LocalStringsManager lsm = 1073 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 1074 CLILogger.getInstance().printWarning(lsm.getString( 1075 "CouldNotWriteCommandToFile", 1076 new Object [] {e.getMessage()} )); 1077 } 1078 } 1079 } 1080 1081 1084 private void saveCommandsAsSingleFile() throws CommandValidationException 1085 { 1086 try 1087 { 1088 ObjectOutputStream os = new ObjectOutputStream ( 1089 new FileOutputStream (serializeDir + 1090 "/"+ SERIALIZED_DESCRIPTOR_FILE)); 1091 os.writeObject(defaultCommand); 1092 os.writeObject(helpClass); 1093 os.writeObject(properties); 1094 1095 os.writeObject(commandsList); 1096 } 1097 catch(Exception e) 1098 { 1099 LocalStringsManager lsm = 1101 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 1102 CLILogger.getInstance().printWarning(lsm.getString( 1103 "CouldNotWriteComponentToFile", 1104 new Object [] {e.getMessage()} )); 1105 1106 } 1107 } 1108 1109 1110 1113 private int getSerializeDescriptorsProperty() 1114 { 1115 return serializeDescriptors; 1116 } 1117 1118 1127 public void setSerializeDescriptorsProperty(int serializeDescriptors) 1128 { 1129 if ((serializeDescriptors <= 2) && (serializeDescriptors >=0)) 1130 this.serializeDescriptors = serializeDescriptors; 1131 } 1132 1133 1138 public String getSerializeDir() 1139 { 1140 return serializeDir; 1141 } 1142 1143 1144 1147 private void setSerializeDir(String serializeDir) 1148 throws CommandValidationException 1149 { 1150 try 1151 { 1152 File file = new File (serializeDir); 1153 if (file.exists()) 1154 { 1155 this.serializeDir = serializeDir; 1156 } 1157 else 1158 { 1159 LocalStringsManager lsm = 1160 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 1161 CLILogger.getInstance().printWarning(lsm.getString( 1162 "InvalidFilePath", 1163 new Object [] {serializeDir})); 1164 } 1165 } 1166 catch (NullPointerException npe) 1167 { 1168 LocalStringsManager lsm = 1169 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 1170 throw new CommandValidationException(lsm.getString("CouldNoSetSerializeDirectory"), 1171 npe); 1172 } 1173 } 1174 1175 1176 private void replaceOptionsInCommandsList(HashMap replaceOptions) 1177 { 1178 Iterator replaceOptionNames = replaceOptions.keySet().iterator(); 1179 while (replaceOptionNames.hasNext()) 1180 { 1181 final String replaceOptionName = (String ) replaceOptionNames.next(); 1182 Iterator commands = commandsList.getCommands(); 1183 while (commands.hasNext()) 1184 { 1185 ValidCommand command = (ValidCommand) commands.next(); 1186 if ( command.hasValidOption(replaceOptionName) || 1187 command.hasRequiredOption(replaceOptionName) || 1188 command.hasDeprecatedOption(replaceOptionName) ) 1189 { 1190 command.replaceAllOptions((ValidOption)replaceOptions.get(replaceOptionName)); 1191 } 1192 } 1193 } 1194 } 1195 1196 1197 public String getEnvironmentPrefix(){ 1198 if(properties != null){ 1199 for(int i = 0 ; i < properties.size(); i++){ 1200 Properties props = (Properties )properties.get(i); 1201 String prefix = props.getProperty(ENVIRONMENT_PREFIX); 1202 if(prefix != null) 1203 { 1204 return prefix; 1205 } 1206 } 1207 } 1208 return null; 1209 } 1210 1211 1212 public String getEnvironmentFileName(){ 1213 if(properties != null){ 1214 for(int i = 0 ; i < properties.size(); i++){ 1215 Properties props = (Properties )properties.get(i); 1216 String file = props.getProperty(ENVIRONMENT_FILENAME); 1217 if(file != null) 1218 { 1219 return file; 1220 } 1221 } 1222 } 1223 return ".cliprefs"; 1224 } 1225 1226 1227 1230 public static void main(String [] args) 1231 { 1232 try 1233 { 1234 final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance(); 1236 1237 cliDescriptorsReader.setSerializeDescriptorsProperty(CLIDescriptorsReader.SERIALIZE_COMMANDS_TO_FILES); 1239 1241 Iterator commands = cliDescriptorsReader.getCommandsList().getCommands(); 1242 1243 1255 1256 } 1257 catch(CommandValidationException cve) 1258 { 1259 System.out.println(cve.getLocalizedMessage()); 1260 } 1261 } 1262} 1263 1264 1265class CLIEntityResolver implements EntityResolver { 1266 1267 1268 public CLIEntityResolver() { 1269 } 1270 1271 public InputSource resolveEntity(String publicId, String systemId) 1272 throws org.xml.sax.SAXException , java.io.IOException { 1273 if (systemId.endsWith("CLISpecification.xsd")) { 1274 InputStream in = this.getClass().getClassLoader().getResourceAsStream( 1276 "com/sun/enterprise/cli/framework/CLISpecification.xsd"); 1277 return new InputSource (in); 1278 } else { 1279 return null; 1281 } 1282 } 1283 1284} 1285 1286 | Popular Tags |