1 23 24 package com.sun.enterprise.cli.commands; 25 26 import com.sun.enterprise.cli.framework.*; 27 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier; 28 import javax.management.Attribute ; 29 import javax.management.AttributeList ; 30 import javax.management.remote.JMXServiceURL ; 31 import javax.management.remote.JMXConnector ; 32 import javax.management.remote.JMXConnectorFactory ; 33 import javax.management.MBeanServerConnection ; 34 import javax.management.ObjectName ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.InstanceNotFoundException ; 37 import javax.management.IntrospectionException ; 38 import javax.management.ReflectionException ; 39 40 import com.sun.appserv.management.util.misc.ExceptionUtil; 41 42 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 43 44 import com.sun.enterprise.admin.servermgmt.RepositoryConfig; 45 import com.sun.enterprise.admin.servermgmt.RepositoryManager; 46 import com.sun.enterprise.admin.servermgmt.RepositoryException; 47 import com.sun.enterprise.util.SystemPropertyConstants; 48 import com.sun.enterprise.util.ASenvPropertyReader; 49 50 import com.sun.appserv.management.client.prefs.LoginInfo; 51 import com.sun.appserv.management.client.prefs.LoginInfoStore; 52 import com.sun.appserv.management.client.prefs.LoginInfoStoreFactory; 53 54 55 import java.util.Vector ; 56 import java.util.HashMap ; 57 import java.util.Map ; 58 import java.util.logging.Level ; 59 import java.net.MalformedURLException ; 60 import java.io.IOException ; 61 import java.util.StringTokenizer ; 62 import java.util.Properties ; 63 import java.util.Enumeration ; 64 import java.io.File ; 65 import java.util.List ; 66 import java.util.Iterator ; 67 68 69 74 public abstract class S1ASCommand extends Command 75 { 76 77 public final static String JMX_PROTOCOL = "jmxmp"; 78 public final static String TERSE = "terse"; 79 public final static String INTERACTIVE = "interactive"; 80 public final static String PASSWORDFILE = "passwordfile"; 81 public final static String ECHO = "echo"; 82 public final static String HOST = "host"; 83 public final static String PORT = "port"; 84 public final static String USER = "user"; 85 public final static String PASSWORD = "password"; 86 public final static String SECURE = "secure"; 87 public final static String MAPPED_PASSWORD = "mappedpassword"; 88 public final static String OBJECT_NAME = "objectname"; 89 public final static String ARGUMENTS = "arguments"; 90 public final static String OPERATION = "operation"; 91 public final static String PARAMS = "params"; 92 public final static String PARAM_TYPES = "paramtypes"; 93 public final static String RETURN_TYPE = "returntype"; 94 public final static String DISPLAY_TYPE = "displaytype"; 95 public final static String PROPERTY = "property."; 96 public final static String DOMAIN = "domain"; 97 98 public final static String ATTRIBUTE_LIST_CLASS = AttributeList .class.getName(); 99 public final static String PROPERTIES_CLASS = Properties .class.getName(); 100 public final static String BOOLEAN_CLASS = Boolean .class.getName(); 101 public final static String PRIMITIVE_BOOLEAN_CLASS = boolean.class.getName(); 102 public final static String STRING_ARRAY = (new String []{}).getClass().getName(); 103 public final static String MAP_CLASS = Map .class.getName(); 104 public final static String LONG_CLASS = Long .class.getName(); 105 public final static String PRIMITIVE_LONG_CLASS = long.class.getName(); 106 public final static String INTEGER_CLASS = Integer .class.getName(); 107 public final static String PRIMITIVE_INTEGER_CLASS = int.class.getName(); 108 public final static String OBJECT_CLASS = Object .class.getName(); 109 110 public final static String PARAM_DELIMITER = ","; 111 public final static String PARAM_VALUE_DELIMITER = "="; 112 public final static String PROPERTY_DELIMITER = ":"; 113 114 protected final static String ASADMINPREFS = ".asadminprefs"; 115 116 117 protected final static String SHORTHAND_DELIMETER = ":"; 118 protected final static char ESCAPE_CHAR = '\\'; 119 protected static final String SET_DELIMETER = "="; 120 121 protected final static String ENV_PREFIX = "AS_ADMIN_"; 122 protected static String NOT_DEPRECATED_PASSWORDFILE_OPTIONS = 123 "password|adminpassword|userpassword|masterpassword|aliaspassword|mappedpassword"; 124 protected final static String COMMENT_PREFIX = "#"; 125 126 127 public final static String LIST_JMS_RESOURCES = "list-jms-resources"; 128 129 private String userValue = null; 132 private boolean warningDisplayed = false; 133 134 135 public S1ASCommand() 136 { 137 final ASenvPropertyReader reader = new ASenvPropertyReader( 139 System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY)); 140 reader.setSystemProperties(); 141 } 142 143 144 151 public boolean validateOptions() throws CommandValidationException 152 { 153 setLoggerLevel(); 154 155 if (getBooleanOption(ECHO)) 157 { 158 CLILogger.getInstance().printMessage(this.toString()); 159 } 160 HashMap options = getOptions(); 163 Iterator optionNames = options.keySet().iterator(); 164 while (optionNames.hasNext()) 165 { 166 final String optionKey = (String ) optionNames.next(); 167 if (optionKey.matches(NOT_DEPRECATED_PASSWORDFILE_OPTIONS)) 168 validatePasswordOption(optionKey); 169 } 170 return true; 171 } 172 173 174 private void validatePasswordOption(String passwordOptionName) 175 throws CommandValidationException 176 { 177 if (getCLOption(passwordOptionName) != null) 180 throw new CommandValidationException(getLocalizedString("PasswordsNotAllowedOnCommandLine", 181 new Object []{passwordOptionName})); 182 if (getENVOption(passwordOptionName) != null) 183 CLILogger.getInstance().printWarning(getLocalizedString("PasswordsNotAllowedInEnvironment", 184 new Object []{passwordOptionName})); 185 } 186 191 protected void setLoggerLevel() 192 { 193 final boolean terse = getBooleanOption(TERSE); 194 195 if (terse) 196 { 197 CLILogger.getInstance().setOutputLevel(java.util.logging.Level.INFO); 199 } 200 else 201 { 202 CLILogger.getInstance().setOutputLevel(java.util.logging.Level.FINE); 204 } 205 } 206 207 208 220 protected MBeanServerConnection getMBeanServerConnection(String protocol, 221 String host, int port, 222 String user, String password) 223 throws CommandException 224 { 225 final JMXServiceURL url = getJMXServiceURL(protocol, host, port); 228 try 229 { 230 final JMXConnector jmxc = JMXConnectorFactory.connect(url); 231 232 CLILogger.getInstance().printDebugMessage("Get an MBeanSErverConnection"); 234 return jmxc.getMBeanServerConnection(); 235 } 236 catch(IOException ioe) 237 { 238 throw new CommandException(ioe); 239 } 240 } 241 242 243 249 private JMXServiceURL getJMXServiceURL(String protocol, String host, int port) 250 throws CommandException 251 { 252 try 253 { 254 CLILogger.getInstance().printDebugMessage( 255 "Create a JMXMP connector client and " + 256 "connect it to the JMXMP connector server"); 257 258 final JMXServiceURL url = new JMXServiceURL (protocol, host, port); 259 CLILogger.getInstance().printDebugMessage("url = " + url.toString()); 260 return url; 261 } 262 catch(MalformedURLException mue) 263 { 264 throw new CommandException(mue); 265 } 266 } 267 268 269 281 protected MBeanServerConnection getMBeanServerConnection(String host, int port, 282 String user, String password) 283 throws CommandException, CommandValidationException 284 { 285 try 286 { 287 String jmxProtocol = null; 288 289 if (getBooleanOption(SECURE)) { 290 jmxProtocol = DefaultConfiguration.S1_HTTPS_PROTOCOL; 291 } else { 292 jmxProtocol = DefaultConfiguration.S1_HTTP_PROTOCOL; 293 } 294 295 final JMXServiceURL url = new JMXServiceURL (jmxProtocol, host, port); 296 final JMXConnector conn = JMXConnectorFactory.connect(url, 297 initEnvironment(user, password)); 298 return conn.getMBeanServerConnection(); 299 } 300 catch (Exception e) 301 { 302 throw new CommandException(e); 303 } 304 } 305 306 318 protected ServerConnectionIdentifier createServerConnectionIdentifier( 319 String host, int port, String user, String password) 320 throws CommandException, CommandValidationException 321 { 322 ServerConnectionIdentifier conn = 323 new ServerConnectionIdentifier(); conn.setHostName(host); 325 conn.setHostPort(port); 326 conn.setUserName(user); 327 conn.setPassword(password); 328 329 if (getBooleanOption(SECURE)) { 330 conn.setSecure(true); 331 } else { 332 conn.setSecure(false); 333 } 334 335 return conn; 336 } 337 338 339 343 private Map initEnvironment(String user, String password) 344 throws CommandValidationException, CommandException 345 { 346 final Map env = new HashMap (); 347 final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol"; 348 349 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, 350 this.getClass().getClassLoader()); 351 env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, PKGS); 352 env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, user); 353 env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, password); 354 env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME, 355 DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME); 356 return ( env ); 357 } 358 359 360 364 protected String getHost() 365 { 366 return getOption(HOST); 367 } 368 369 370 374 protected int getPort() throws CommandValidationException 375 { 376 String port = getOption(PORT); 377 378 try 379 { 380 int portInt = 0; 381 portInt = Integer.parseInt(port); 382 return portInt; 383 } 384 catch(Exception e) 385 { 386 throw new CommandValidationException(getLocalizedString("InvalidPortNumber", 387 new Object [] {port}), e); 388 } 389 } 390 391 392 402 protected String getUser() throws CommandValidationException 403 { 404 if (getOption(USER) == null && userValue == null) 405 { 406 userValue = getUserFromASADMINPASS(); 408 if (userValue != null) 409 return userValue; 410 411 userValue= getValuesFromASADMINPREFS(USER); 413 if (userValue != null) 414 { 415 CLILogger.getInstance().printDebugMessage( 416 "user value read from " + ASADMINPREFS); 417 return userValue; 418 } 419 else if (getBooleanOption(INTERACTIVE)) 420 { 421 try { 423 InputsAndOutputs.getInstance().getUserOutput().print( 424 getLocalizedString("AdminUserPrompt")); 425 return InputsAndOutputs.getInstance().getUserInput().getLine(); 426 } 427 catch (IOException ioe) 428 { 429 throw new CommandValidationException( 430 getLocalizedString("CannotReadOption", 431 new Object []{"user"})); 432 } 433 } 434 } 435 else if (getOption(USER) != null) 436 return getOption(USER); 437 438 else if (userValue != null) 439 return userValue; 440 441 throw new CommandValidationException(getLocalizedString("OptionIsRequired", 443 new Object [] {USER})); 444 } 445 446 447 453 protected String getUserFromASADMINPASS() 454 { 455 String userValue = null; 456 int port; 457 String host = getHost(); 458 try 459 { 460 port = getPort(); 462 } 463 catch(CommandValidationException cve) 464 { 465 return userValue; 469 } 470 471 try 473 { 474 final LoginInfoStore store = LoginInfoStoreFactory.getStore(null); 475 if (store.exists(host, port)) 476 { 477 LoginInfo login = store.read(host, port); 478 userValue = login.getUser(); 479 if (userValue != null) 480 { 481 CLILogger.getInstance().printDebugMessage( 482 "user value read from " + store.getName()); 483 } 484 } 485 } 486 catch(final Exception e) { 487 final Object [] params = new String [] {host, "" + port}; 488 final String msg = getLocalizedString("LoginInfoCouldNotBeRead", params); 489 CLILogger.getInstance().printWarning(msg); 490 CLILogger.getInstance().printExceptionStackTrace(e); 491 } 492 return userValue; 493 } 494 495 496 502 protected String getValuesFromASADMINPREFS(String nameOfValue) 503 { 504 String returnVal = null; 505 java.io.BufferedReader reader = null; 506 try 507 { 508 File file = checkForFileExistence(System.getProperty("user.home"), ASADMINPREFS); 509 reader = new java.io.BufferedReader (new java.io.FileReader (file)); 510 String line = reader.readLine(); 511 while (!(line == null) && returnVal==null) 512 { 513 if (line.regionMatches(true, 0, ENV_PREFIX, 0, ENV_PREFIX.length())) 515 { 516 final String [] nameAndValue = splitNameValuePairs(line); 518 if (nameAndValue[0].equalsIgnoreCase(nameOfValue)) 519 returnVal = nameAndValue[1]; 520 521 } 522 line = reader.readLine(); 523 } 524 reader.close(); 525 return returnVal; 526 } 527 catch (Exception e) 528 { 529 return null; 530 } 531 } 532 533 534 540 private String [] splitNameValuePairs(String nameValuePairs) 541 { 542 final String tempStr = nameValuePairs.substring(ENV_PREFIX.length()); 544 int idx = tempStr.indexOf(PARAM_VALUE_DELIMITER); 545 if (idx>0 && idx<tempStr.length()-1 ) { 546 String name=tempStr.substring(0,idx); 547 String value=tempStr.substring(idx+1); 548 String [] nameAndValue = new String [] {name,value}; 549 return nameAndValue; 550 } 551 return new String [] {"",""}; 552 } 553 554 555 560 protected String getPassword() throws CommandValidationException, CommandException 561 { 562 return getPassword(PASSWORD, "AdminPasswordPrompt", "AdminPasswordConfirmationPrompt", 565 true, true, false, false, null, null, true, false, false, true); 566 } 567 568 569 protected boolean isPasswordValid(String passwd) { 570 if (passwd.length() < 8) { 571 return false; 572 } else { 573 return true; 574 } 575 } 576 577 protected String getPassword(String optionName, 578 boolean allowedOnCommandLine, 579 boolean readPrefsFile, 580 boolean readPasswordOptionFromPrefs, 581 boolean readMasterPasswordFile, RepositoryManager mgr, RepositoryConfig config, 582 boolean promptUser, boolean confirm, boolean validate, boolean displayWarning) 583 throws CommandValidationException, CommandException 584 { 585 return getPassword(optionName, "InteractiveOptionPrompt", "InteractiveOptionConfirmationPrompt", 586 allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, 587 readMasterPasswordFile, mgr, config, promptUser, confirm, validate, displayWarning); 588 } 589 590 613 protected String getPassword(String optionName, 614 String promptMsg, 615 String confirmationPromptMsg, 616 boolean allowedOnCommandLine, 617 boolean readPrefsFile, 618 boolean readPasswordOptionFromPrefs, 619 boolean readMasterPasswordFile, RepositoryManager mgr, RepositoryConfig config, 620 boolean promptUser, boolean confirm, boolean validate, boolean displayWarning) 621 throws CommandValidationException, CommandException 622 { 623 String passwordVal; 624 if (getOption(PASSWORDFILE) != null) { 626 loadPasswordFileOptions(optionName); 627 } 628 passwordVal = getCLOption(optionName); 631 632 if (passwordVal == null) 633 { 634 if (readPrefsFile) 636 { 637 if (getOption(PORT) != null) 640 { 641 String host = getHost(); 642 int port = getPort(); 643 try 644 { 645 final LoginInfoStore store = LoginInfoStoreFactory.getStore(null); 646 if (store.exists(host, port)) 647 { 648 LoginInfo login = store.read(host, port); 649 passwordVal = login.getPassword(); 650 if (passwordVal != null) 651 { 652 CLILogger.getInstance().printDebugMessage( 653 "password value read from " + store.getName()); 654 } 655 } 656 } 657 catch(final Exception e) 658 { 659 final Object [] params = new String [] {host, "" + port}; 660 final String msg = getLocalizedString("LoginInfoCouldNotBeRead", params); 661 CLILogger.getInstance().printWarning(msg); 662 CLILogger.getInstance().printExceptionStackTrace(e); 663 } 664 } 665 if (passwordVal == null) 667 { 668 passwordVal = getValuesFromASADMINPREFS(optionName); 669 if (passwordVal != null) 670 { 671 CLILogger.getInstance().printDebugMessage( 672 "password value read from " + ASADMINPREFS); 673 } 674 } 675 } 676 if (passwordVal == null) 677 { 678 if (readPrefsFile && readPasswordOptionFromPrefs) 680 { 681 passwordVal = getValuesFromASADMINPREFS(PASSWORD); 682 } 683 if (passwordVal == null) 684 { 685 if (readMasterPasswordFile && mgr != null && config != null) 687 { 688 try 689 { 690 passwordVal = mgr.readMasterPasswordFile(config); 691 } catch (RepositoryException ex) 692 { 693 throw new CommandException(ex); 694 } 695 } 696 if (passwordVal == null) 697 { 698 if (promptUser) 701 { 702 if (confirm) 703 { 704 passwordVal = getInteractiveOptionWithConfirmation(optionName, 705 getLocalizedString(promptMsg, new Object [] {optionName}), 706 getLocalizedString(confirmationPromptMsg, new Object [] {optionName}), 707 validate); 708 } else 709 { 710 passwordVal = getInteractiveOption(optionName, 711 getLocalizedString(promptMsg, new Object [] {optionName})); 712 } 713 } 714 } 715 } 716 } 717 } 718 if (validate && passwordVal != null && !isPasswordValid(passwordVal)) 720 { 721 throw new CommandValidationException(getLocalizedString("PasswordLimit", 722 new Object []{optionName})); 723 } 724 return passwordVal; 725 } 726 727 738 protected String getInteractiveOptionWithConfirmation(String optionName, String prompt, 739 String confirmationPrompt, boolean validatePassword) 740 throws CommandValidationException 741 { 742 String optionValue = getInteractiveOption(optionName, prompt); 743 if (validatePassword && !isPasswordValid(optionValue)) { 744 throw new CommandValidationException(getLocalizedString("PasswordLimit", 745 new Object []{optionName})); 746 } 747 String optionValueAgain = getInteractiveOption(optionName, confirmationPrompt); 748 if (!optionValue.equals(optionValueAgain)) { 749 throw new CommandValidationException(getLocalizedString("OptionsDoNotMatch", 750 new Object [] {optionName})); 751 } 752 return optionValue; 753 } 754 755 764 protected String getInteractiveOption(String optionName, String prompt) 765 throws CommandValidationException 766 { 767 String optionValue; 769 if (getBooleanOption(INTERACTIVE)) 773 { 774 try 775 { 776 InputsAndOutputs.getInstance().getUserOutput().print(prompt); 777 optionValue = new CliUtil().getPassword(); 778 } 779 catch (java.lang.NoClassDefFoundError e) 780 { 781 optionValue = readInput(); 782 } 783 catch (java.lang.UnsatisfiedLinkError e) 784 { 785 optionValue = readInput(); 786 } 787 catch (Exception e) 788 { 789 throw new CommandValidationException(e); 790 } 791 } 792 else 794 throw new CommandValidationException(getLocalizedString("OptionIsRequired", 795 new Object [] {optionName})); 796 return optionValue; 797 } 798 799 800 803 protected String readInput() 804 { 805 try { 806 return InputsAndOutputs.getInstance().getUserInput().getLine(); 807 } 808 catch (IOException ioe) 809 { 810 return null; 811 } 812 } 813 814 815 816 817 821 private void loadPasswordFileOptions(String optionName) throws CommandException 822 { 823 try 824 { 825 final String passwordFileName = getOption(PASSWORDFILE); 826 File file = checkForFileExistence(null, passwordFileName); 827 java.io.BufferedReader reader = new java.io.BufferedReader ( 828 new java.io.FileReader (file)); 829 String line = reader.readLine(); 830 boolean displayWarning = false; 831 832 while (!(line == null)) 833 { 834 if (line.length() > 0) { 836 displayWarning = checkPasswordFileOptions(line, optionName) || displayWarning; 837 } 838 839 line = reader.readLine(); 840 } 841 reader.close(); 842 if (displayWarning && !warningDisplayed) 843 { 844 CLILogger.getInstance().printWarning(getLocalizedString( 845 "DeprecatedOptionsFromPasswordfile")); 846 warningDisplayed = true; 847 } 848 } 849 catch (CommandException ce) 850 { 851 throw ce; 852 } 853 catch (Exception e) 854 { 855 throw new CommandException(e); 856 } 857 } 858 859 860 868 private boolean checkPasswordFileOptions(String line, String optionName) 869 { 870 if (line.regionMatches(true, 0, ENV_PREFIX, 0, ENV_PREFIX.length())) 872 { 873 final String [] envNameAndValue = splitNameValuePairs(line); 874 875 if (optionName == null || optionName.equalsIgnoreCase(envNameAndValue[0])) { 876 if (getOption(optionName) == null && 877 envNameAndValue.length > 1) 878 { 879 setOption(optionName, envNameAndValue[1]); 881 } 882 if (optionName.matches(NOT_DEPRECATED_PASSWORDFILE_OPTIONS)) 883 return false; 884 else 885 return true; 886 } else { 887 return false; 888 } 889 } 890 else if (line.regionMatches(true, 0, COMMENT_PREFIX, 0, COMMENT_PREFIX.length())) 891 { 892 return false; 893 } 894 return true; 895 } 896 897 898 904 protected File checkForFileExistence(String parent, String fileName) 905 throws CommandException 906 { 907 if (fileName == null) return null; 908 File file = null; 909 if (parent == null) 910 file = new File (fileName); 911 else 912 file = new File (parent, fileName); 913 if ( file.canRead() == false ) 914 { 915 throw new CommandException(getLocalizedString("FileDoesNotExist", 916 new Object [] {fileName})); 917 } 918 return file; 919 } 920 921 922 926 protected String getObjectName() throws CommandException 927 { 928 final String objectName = (String )((Vector )getProperty(OBJECT_NAME)).get(0); 929 final String objectNameStr = replacePattern(objectName); 930 CLILogger.getInstance().printDebugMessage("Object Name = [" + 931 objectNameStr + "]"); 932 return objectNameStr; 933 } 934 935 936 940 protected Object [] getParamsInfo()throws CommandException, CommandValidationException 941 { 942 final Vector params = (Vector ) getProperty(PARAMS); 943 final String [] typesInfo = getTypesInfo(); 944 Object [] paramsInfo = params==null?null:new Object [params.size()]; 945 946 if(typesInfo != null && paramsInfo != null && paramsInfo.length != typesInfo.length) 947 { 948 throw new CommandException(getLocalizedString("BadDescriptorXML")); 949 } 950 for (int i = 0; params != null && i < params.size(); i++) 951 { 952 String paramStr = (String ) params.get(i); 953 954 CLILogger.getInstance().printDebugMessage("Types Info ["+i+"] = " + typesInfo[i]); 955 if (typesInfo[i].equals(ATTRIBUTE_LIST_CLASS)) 958 { 959 paramsInfo[i] = getAttributeList(paramStr); 960 } 961 else if (typesInfo[i].equals(PROPERTIES_CLASS)) 962 { 963 paramsInfo[i] = createPropertiesParam((String )params.get(i)); 964 } 965 else if (typesInfo[i].equals(BOOLEAN_CLASS) || 966 typesInfo[i].equals(PRIMITIVE_BOOLEAN_CLASS)) 967 { 968 if (createBooleanVal((String )params.get(i)) != null) 969 paramsInfo[i] = createBooleanVal((String )params.get(i)); 970 } 971 else if (typesInfo[i].equals(LONG_CLASS) || 972 typesInfo[i].equals(PRIMITIVE_LONG_CLASS)) 973 { 974 if (createLongVal((String )params.get(i)) != null) 975 paramsInfo[i] = createLongVal((String )params.get(i)); 976 } 977 else if (typesInfo[i].equals(INTEGER_CLASS) || 978 typesInfo[i].equals(PRIMITIVE_INTEGER_CLASS)) 979 { 980 if (createIntegerVal((String )params.get(i)) != null) 981 paramsInfo[i] = createIntegerVal((String )params.get(i)); 982 } 983 else if (typesInfo[i].equals(STRING_ARRAY)) 984 { 985 CLILogger.getInstance().printDebugMessage("Creating String Array"); 986 paramsInfo[i] = createStringArrayParam((String )params.get(i)); 987 } 988 else if (typesInfo[i].equals(MAP_CLASS)) 989 { 990 CLILogger.getInstance().printDebugMessage("Creating Map Class"); 991 paramsInfo[i] = createMapParam(paramStr); 992 } 993 else if (typesInfo[i].equals(OBJECT_CLASS)) 994 { 995 CLILogger.getInstance().printDebugMessage("Creating Object Class"); 996 paramsInfo[i] = (Object ) replacePattern(paramStr); 997 CLILogger.getInstance().printDebugMessage("param value = " + paramsInfo[i]); 998 } 999 else 1000 { 1001 CLILogger.getInstance().printDebugMessage((String ) params.get(i)); 1002 paramsInfo[i] = replacePattern((String )params.get(i)); 1003 1004 CLILogger.getInstance().printDebugMessage("ParamsInfo = " + paramsInfo[i]); 1005 } 1006 } 1007 return paramsInfo; 1008 } 1009 1010 1011 1015 protected String [] getTypesInfo() 1016 { 1017 Vector typesList = (Vector ) getProperty(PARAM_TYPES); 1018 String types[] = typesList==null?null:new String [typesList.size()]; 1019 CLILogger.getInstance().printDebugMessage("Types = "); 1020 for (int i = 0; typesList != null && i < typesList.size(); i++) 1021 { 1022 types[i] = (String ) typesList.get(i); 1023 CLILogger.getInstance().printDebugMessage(types[i]+","); 1024 } 1025 CLILogger.getInstance().printDebugMessage(""); 1026 return types; 1027 } 1028 1029 1030 1034 protected String getOperationName() throws CommandException 1035 { 1036 String operationName = (String ) ((Vector ) getProperty(OPERATION)).get(0); 1037 String operationNameStr = replacePattern(operationName); 1038 CLILogger.getInstance().printDebugMessage("OperationName = " + operationNameStr); 1039 return operationNameStr; 1040 } 1041 1042 1043 1047 protected String getReturnType() 1048 { 1049 String returnType = (String ) ((Vector ) getProperty(RETURN_TYPE)).get(0); 1050 CLILogger.getInstance().printDebugMessage("ReturnType = " + returnType); 1051 return returnType; 1052 } 1053 1054 1055 1059 private AttributeList getAttributeList(String paramStr) 1060 throws CommandException 1061 { 1062 AttributeList attrList = new AttributeList (); 1063 StringTokenizer paramsTokenizer = new StringTokenizer (paramStr, PARAM_DELIMITER); 1064 int size = paramsTokenizer.countTokens(); 1065 for (int i = 0; i < size; i++) 1066 { 1067 final String nameValue = paramsTokenizer.nextToken(); 1068 final String nameValueStr = replacePattern(nameValue); 1069 if (nameValueStr == null) continue; 1070 final int index = getDelimeterIndex(nameValueStr, PARAM_VALUE_DELIMITER, 0); 1071 final String attrName = nameValueStr.substring(0, index); 1072 final String attrValue = nameValueStr.substring(index+1); 1073 CLILogger.getInstance().printDebugMessage("**Attr Name = " + attrName + 1074 ", Value = " + attrValue); 1075 attrList.add( new Attribute (attrName, attrValue) ); 1076 } 1077 return attrList; 1078 } 1079 1080 1081 1085 protected Properties createPropertiesParam(String propertyVal) 1086 throws CommandException, CommandValidationException 1087 { 1088 final String propertyStr = replacePattern(propertyVal); 1089 if (propertyStr == null) return null; 1090 Properties properties = new Properties (); 1091 final CLITokenizer propertyTok = new CLITokenizer(propertyStr, PROPERTY_DELIMITER); 1092 while (propertyTok.hasMoreTokens()) { 1093 final String nameAndvalue = propertyTok.nextToken(); 1094 final CLITokenizer nameTok = new CLITokenizer(nameAndvalue, PARAM_VALUE_DELIMITER); 1095 if (nameTok.countTokens() == 2) 1096 { 1097 properties.setProperty(nameTok.nextTokenWithoutEscapeAndQuoteChars(), 1098 nameTok.nextTokenWithoutEscapeAndQuoteChars()); 1099 } 1100 else 1101 { 1102 throw new CommandValidationException(getLocalizedString("InvalidPropertySyntax")); 1103 } 1104 } 1105 return properties; 1106 } 1107 1108 1109 1113 private Map createMapParam(final String paramVal) 1114 throws CommandException, CommandValidationException 1115 { 1116 if (paramVal == null) return null; 1117 Map mapParam = new HashMap (); 1118 try { 1119 StringTokenizer paramsTokenizer = new StringTokenizer (paramVal, 1120 PARAM_DELIMITER); 1121 int size = paramsTokenizer.countTokens(); 1122 for (int ii = 0; ii < size; ii++) 1123 { 1124 final String nameValue = paramsTokenizer.nextToken(); 1125 final String nameValueStr = replacePattern(nameValue); 1126 1127 if (nameValueStr == null) continue; 1128 1129 if (nameValueStr.startsWith(PROPERTY)) { 1130 String propertyStr = nameValueStr.substring(PROPERTY.length()+1); 1134 if (propertyStr.length()>0) { 1136 final CLITokenizer propertyTok = new CLITokenizer(propertyStr, PROPERTY_DELIMITER); 1137 while (propertyTok.hasMoreTokens()) { 1138 final String nameAndvalue = propertyTok.nextToken(); 1139 final CLITokenizer nameTok = new CLITokenizer(nameAndvalue, PARAM_VALUE_DELIMITER); 1140 if (nameTok.countTokens() == 2) 1141 { 1142 mapParam.put(PROPERTY+nameTok.nextTokenWithoutEscapeAndQuoteChars(), 1145 nameTok.nextTokenWithoutEscapeAndQuoteChars()); 1146 } 1147 else 1148 { 1149 throw new CommandValidationException(getLocalizedString("InvalidPropertySyntax")); 1150 } 1151 } 1152 1153 } 1154 1155 } else { 1156 1157 final int index = getDelimeterIndex(nameValueStr, 1158 PARAM_VALUE_DELIMITER, 0); 1159 if (index > 0) { 1160 final String mapKey = nameValueStr.substring(0, index); 1161 final String mapValue = nameValueStr.substring(index+1); 1162 if (mapValue.length() > 0) 1164 { 1165 CLILogger.getInstance().printDebugMessage("**Map Key = " + 1166 mapKey + 1167 ", Value = " + 1168 mapValue); 1169 mapParam.put( mapKey, mapValue); 1170 } 1171 1172 } 1173 } 1174 1175 } 1176 } 1177 catch (Exception e) { 1178 throw new CommandException(e); 1179 } 1180 return mapParam; 1181 } 1182 1183 1184 1189 private Boolean createBooleanVal(String booleanVal) 1190 throws CommandException 1191 { 1192 String booleanStr = replacePattern(booleanVal); 1193 return Boolean.valueOf(booleanStr); 1194 } 1195 1196 1197 1202 private Long createLongVal(String longVal) 1203 throws CommandException 1204 { 1205 String longStr = replacePattern(longVal); 1206 return Long.valueOf(longStr); 1207 } 1208 1209 1210 1215 private Integer createIntegerVal(String integerVal) 1216 throws CommandException 1217 { 1218 String integerStr = replacePattern(integerVal); 1219 return Integer.valueOf(integerStr); 1220 } 1221 1222 1223 1227 protected String [] createStringArrayParam(String str) 1228 throws CommandException 1229 { 1230 final String strArrayVal = replacePattern(str); 1231 CLILogger.getInstance().printDebugMessage("strArrayVal value = \""+ strArrayVal + "\""); 1232 if ((strArrayVal == null) || (strArrayVal.equals("")) || (strArrayVal.equals("null"))) 1233 return null; 1234 1235 final CLITokenizer cliTokenizer = new CLITokenizer(strArrayVal, PROPERTY_DELIMITER); 1236 String [] strArray = new String [cliTokenizer.countTokens()]; 1237 int ii=0; 1238 while (cliTokenizer.hasMoreTokens()) 1239 { 1240 strArray[ii++] = cliTokenizer.nextTokenWithoutEscapeAndQuoteChars(); 1241 } 1242 return strArray; 1243 } 1244 1245 1246 public void displayExceptionMessage(Exception e) throws CommandException 1247 { 1248 Throwable rootException = ExceptionUtil.getRootCause(e); 1250 1251 if (rootException.getLocalizedMessage() != null) 1252 CLILogger.getInstance().printDetailMessage(rootException.getLocalizedMessage()); 1253 throw new CommandException(getLocalizedString("CommandUnSuccessful", 1254 new Object [] {name} ), e); 1255 } 1256 1257 1258 1259 1263 protected void handleReturnValue(Object returnval) 1264 { 1265 if (returnval == null) 1266 return; 1267 Class cl = returnval.getClass(); 1268 if (cl.isArray()) 1269 { 1270 if (cl == new ObjectName [0].getClass()) 1271 { 1272 final ObjectName [] objs = (ObjectName [])returnval; 1273 if (objs.length == 0) 1274 { 1275 CLILogger.getInstance().printDetailMessage( 1276 getLocalizedString("NoElementsToList")); 1277 return; 1278 } 1279 1280 final String displayType = (String )((Vector )getProperty(DISPLAY_TYPE)).get(0); 1281 for (int ii=0; ii<objs.length; ii++) 1282 { 1283 ObjectName objectName = (ObjectName )objs[ii]; 1284 CLILogger.getInstance().printDebugMessage("ObjectName = " + objectName); 1285 CLILogger.getInstance().printMessage(objectName.getKeyProperty(displayType)); 1286 } 1287 } 1288 else 1289 { 1290 final Object [] objs = (Object [])returnval; 1291 if (objs.length == 0) 1292 { 1293 CLILogger.getInstance().printDetailMessage( 1294 getLocalizedString("NoElementsToList")); 1295 return; 1296 } 1297 1298 for (int ii=0; ii<objs.length; ii++) 1299 { 1300 CLILogger.getInstance().printMessage( 1301 objs[ii].toString()); 1302 } 1303 } 1304 } 1305 else if (cl.getName().equals(List .class.getName()) || returnval instanceof List ) 1306 { 1307 final List returnList = (List )returnval; 1308 if (returnList.size() > 0) 1309 { 1310 for (int i = 0; i<returnList.size(); i++) 1311 { 1312 CLILogger.getInstance().printMessage( 1313 returnList.get(i).toString()); 1314 } 1315 } 1316 else 1317 CLILogger.getInstance().printDebugMessage( 1318 "Return list is empty"); 1319 } 1320 } 1321} 1322 | Popular Tags |