1 23 24 package com.sun.enterprise.config.serverbeans.validation.tests; 25 26 import java.util.*; 27 import com.sun.enterprise.util.LocalStringManagerImpl; 28 import com.sun.enterprise.config.serverbeans.validation.StringManagerHelper; 29 import com.sun.enterprise.config.serverbeans.validation.Result; 30 31 import com.sun.enterprise.admin.util.QuotedStringTokenizer; 32 33 public class JvmOptionsTest { 34 35 private static LocalStringManagerImpl smh = 36 StringManagerHelper.getLocalStringsManager(); 37 38 private static final JvmOptionsTest instance = new JvmOptionsTest(); 39 40 private JvmOptionsTest() { 41 } 42 43 public static void validateJvmOptions(String [] jvmOptions, Result result) { 44 try { 45 instance.checkForNullOptions(jvmOptions); 46 Set optionsSet = instance.tokenizeJvmOptions(jvmOptions); 47 instance.checkBeginWithDash(optionsSet); 48 instance.checkQuotes(optionsSet); 49 } 50 catch (Exception e) { 51 result.failed(e.getMessage()); 52 } 53 } 54 55 private void checkForNullOptions(String [] options) 56 throws InvalidJvmOptionsException 57 { 58 if (null == options) { 59 throw new InvalidJvmOptionsException(getNullJvmOptionMsg()); 60 } 61 for (int i = 0; i < options.length; i++) { 62 if (null == options[i]) { 63 throw new InvalidJvmOptionsException(getNullJvmOptionMsg()); 64 } 65 } 66 } 67 68 private void checkBeginWithDash(Set options) 69 throws InvalidJvmOptionsException 70 { 71 List invalidOptions = new ArrayList(); 72 Iterator it = options.iterator(); 73 while (it.hasNext()) { 74 String option = it.next().toString(); 75 if (!option.startsWith("-") && 76 !(option.startsWith("\"-") && option.endsWith("\"")) ) 77 { 78 invalidOptions.add(option); 79 } 80 } 81 if (invalidOptions.size() > 0) { 82 throw new InvalidJvmOptionsException( 83 getInvalidJvmOptionMsg(invalidOptions.toString())); 84 } 85 } 86 87 private void checkQuotes(Set options) throws InvalidJvmOptionsException 88 { 89 List invalidOptions = new ArrayList(); 90 final Iterator it = options.iterator(); 91 while (it.hasNext()) { 92 String option = it.next().toString(); 93 if (!checkQuotes(option)) { 94 invalidOptions.add(option); 95 } 96 } 97 if (invalidOptions.size() > 0) { 98 throw new InvalidJvmOptionsException( 99 getIncorrectQuotesMsg(invalidOptions.toString())); 100 } 101 } 102 103 private boolean checkQuotes(String option) 104 { 105 int length = option.length(); 106 int numQuotes = 0; 107 int index = 0; 108 109 while (index < length && 110 (index = option.indexOf('\"', index)) != -1) 111 { 112 numQuotes++; 113 index++; 114 } 115 return ((numQuotes % 2) == 0); 116 } 117 118 private Set tokenizeJvmOptions(String [] options) { 119 final Set optionsSet = new LinkedHashSet(); 120 final String DELIM = " \t"; 121 for (int i = 0; i < options.length; i++) { 122 QuotedStringTokenizer strTok = new QuotedStringTokenizer( 123 options[i], DELIM); 124 while (strTok.hasMoreTokens()) { 125 optionsSet.add(strTok.nextToken()); 126 } 127 } 128 return Collections.unmodifiableSet(optionsSet); 129 } 130 131 private String getNullJvmOptionMsg() { 132 return smh.getLocalString(getClass().getName() + ".nullJvmOption", 133 "Null Jvm option", new Object [0]); 134 } 135 136 private String getInvalidJvmOptionMsg(String invalidOptions) { 137 return smh.getLocalString(getClass().getName() + ".invalidJvmOption", 138 "{0} - Invalid Jvm option. Option must begin with -", 139 new Object [] {invalidOptions}); 140 } 141 142 private String getIncorrectQuotesMsg(String invalidOptions) { 143 return smh.getLocalString(getClass().getName() + ".incorrectQuotesInJvmOption", 144 "{0} - Invalid Jvm option. Check quotes", 145 new Object [] {invalidOptions}); 146 } 147 148 private static final class InvalidJvmOptionsException extends Exception { 149 InvalidJvmOptionsException(String msg) { 150 super(msg); 151 } 152 } 153 } 154 155 | Popular Tags |