1 23 24 27 28 package com.sun.enterprise.admin.util; 29 30 import java.util.*; 31 32 import com.sun.enterprise.util.i18n.StringManager; 33 34 import com.sun.enterprise.admin.util.QuotedStringTokenizer; 35 36 39 final public class JvmOptionsHelper 40 { 41 44 private final JvmOptionsElement head; 45 46 54 public JvmOptionsHelper(String [] options) throws InvalidJvmOptionException 55 { 56 if (null == options) { 57 throw new IllegalArgumentException (); 58 } 59 if(options.length == 0) { 60 head = new JvmOptionsElement(""); 61 } else { 62 head = new JvmOptionsElement(options[0]); 63 } 64 JvmOptionsElement current = head; 65 for (int i = 1; i < options.length; i++) { 66 JvmOptionsElement next = new JvmOptionsElement(options[i]); 67 current.setNext(next); 68 current = next; 69 } 70 } 71 72 85 public String [] addJvmOptions(String [] options) throws InvalidJvmOptionException 86 { 87 if (null == options) 88 { 89 throw new IllegalArgumentException (); 90 } 91 final Set alreadyExist = new HashSet(); 92 JvmOptionsElement last = last(); 93 for (int i = 0; i < options.length; i++) 94 { 95 if (!head.hasOption(options[i])) 96 { 97 JvmOptionsElement x = new JvmOptionsElement(options[i]); 98 last.setNext(x); 99 last = x; 100 } 101 else 102 { 103 alreadyExist.add(options[i]); 104 } 105 } 106 return toStringArray(alreadyExist); 107 } 108 109 112 public JvmOptionsElement last() 113 { 114 JvmOptionsElement current = head; 115 while (current.hasNext()) 116 { 117 current = current.next(); 118 } 119 return current; 120 } 121 122 130 public String [] deleteJvmOptions(String [] options) 131 { 132 if (null == options) 133 { 134 throw new IllegalArgumentException (); 135 } 136 final Set donotExist = new HashSet(); 139 for (int i = 0; i < options.length; i++) 140 { 141 if (!head.deleteJvmOption(options[i])) 142 { 143 donotExist.add(options[i]); 144 } 145 } 146 return toStringArray(donotExist); 147 } 148 149 152 public String [] getJvmOptionsAsStoredInXml() 153 { 154 Set s = new LinkedHashSet(); 155 JvmOptionsElement current = head; 156 while (!JvmOptionsElement.isLast(current)) 157 { 158 String options = current.getJvmOptionsAsStoredInXml(); 159 if ((options != null) && (options.length() > 0)) 160 { 161 s.add(options); 162 } 163 current = current.next(); 164 } 165 return toStringArray(s); 166 } 167 168 171 public String [] getJvmOptions() 172 { 173 Set s = new LinkedHashSet(); 174 JvmOptionsElement current = head; 175 while (!JvmOptionsElement.isLast(current)) 176 { 177 ArrayList options = current.getJvmOptions(); 178 if ((options != null) && (options.size() > 0)) 179 { 180 s.addAll(options); 181 } 182 current = current.next(); 183 } 184 return toStringArray(s); 185 } 186 187 public static String [] toStringArray(Collection c) 188 { 189 final String [] s = new String [c.size()]; 190 final Iterator it = c.iterator(); 191 int i = 0; 192 while (it.hasNext()) 193 { 194 s[i] = (String )it.next(); 195 i++; 196 } 197 return s; 198 } 199 200 private static String [] tokenize(String [] options) 201 { 202 Set s = new LinkedHashSet(); 203 for (int i = 0; i < options.length; i++) 205 { 206 QuotedStringTokenizer strTok = new QuotedStringTokenizer( 207 options[i], " \t"); 208 while (strTok.hasMoreTokens()) 209 { 210 s.add(strTok.nextToken()); 211 } 212 } 213 return toStringArray(s); 215 } 216 } 217 218 223 class JvmOptionsElement 224 { 225 private static final StringManager strMgr = 226 StringManager.getManager(JvmOptionsElement.class); 227 228 231 private static final JvmOptionsElement DEFAULT = 232 new JvmOptionsElement() { 233 boolean hasOption(String option) { return false; } 234 boolean deleteJvmOption(String option) { return false; } 235 String getJvmOptionsAsStoredInXml() { return ""; } 236 ArrayList getJvmOptions() { return new ArrayList(); } 237 boolean hasNext() { return false; } 238 void setNext() { throw new UnsupportedOperationException (); } 239 }; 240 241 private final Set jvmOptions = new LinkedHashSet(); 242 243 private JvmOptionsElement next; 244 245 static boolean isLast(JvmOptionsElement e) 246 { 247 return (e == DEFAULT); 248 } 249 250 253 private JvmOptionsElement() 254 { 255 } 256 257 265 JvmOptionsElement(String options) throws InvalidJvmOptionException 266 { 267 if (null == options) 268 { 269 throw new IllegalArgumentException (); 270 } 271 QuotedStringTokenizer strTok = new QuotedStringTokenizer(options, " \t"); 273 while (strTok.hasMoreTokens()) 275 { 276 String option = strTok.nextToken(); 277 checkValidOption(option); 278 jvmOptions.add(option); 279 } 280 next = DEFAULT; 281 } 282 283 287 void setNext(JvmOptionsElement element) 288 { 289 if (null == element) 290 { 291 throw new IllegalArgumentException (); 292 } 293 this.next = element; 294 } 295 296 boolean hasNext() 297 { 298 return (DEFAULT != next); 299 } 300 301 JvmOptionsElement next() { return next; } 302 303 boolean hasOption(String option) 304 { 305 boolean exists = jvmOptions.contains(option); 306 if (!exists) { exists = next.hasOption(option); } 307 return exists; 308 } 309 310 317 boolean deleteJvmOption(String option) 318 { 319 boolean b1 = jvmOptions.remove(option); 320 boolean b2 = next().deleteJvmOption(option); 321 return (b1 || b2); 322 } 323 324 326 String getJvmOptionsAsStoredInXml() 327 { 328 if (jvmOptions.size() == 0) { return ""; } 329 final StringBuffer sb = new StringBuffer (); 330 final Iterator it = jvmOptions.iterator(); 331 while (it.hasNext()) 332 { 333 sb.append(it.next()); 334 if (it.hasNext()) { sb.append(SEP); } 335 } 336 return sb.toString(); 337 } 338 339 341 ArrayList getJvmOptions() 342 { 343 final ArrayList arr = new ArrayList(); 344 if (jvmOptions.size() != 0) 345 { 346 final Iterator it = jvmOptions.iterator(); 347 while (it.hasNext()) 348 { 349 String nextOption = (String )it.next(); 350 if(nextOption.length()>2 && nextOption.startsWith("\"") && nextOption.endsWith("\"")) 351 { 352 nextOption = nextOption.substring(1, nextOption.length()-1); 353 } 354 arr.add(nextOption); 355 } 356 } 357 return arr; 358 } 359 360 static final char SEP = ' '; 361 362 public String toString() 363 { 364 return getJvmOptionsAsStoredInXml(); 365 } 366 367 public boolean equals(Object o) 368 { 369 if (this == o) { return true; } 370 boolean isEqual = false; 371 if (o instanceof JvmOptionsElement) 372 { 373 JvmOptionsElement that = (JvmOptionsElement)o; 374 return this.jvmOptions.containsAll(that.jvmOptions); 375 } 376 return isEqual; 377 } 378 379 private void checkValidOption(String option) 380 throws InvalidJvmOptionException 381 { 382 if ((null == option) || option.equals("")) 383 { 384 throw new InvalidJvmOptionException(strMgr.getString( 385 "jvmOptions.invalid_option", option)); 386 } 387 if (!option.startsWith("-") && 388 !(option.startsWith("\"-") && option.endsWith("\""))) 389 { 390 throw new InvalidJvmOptionException(strMgr.getString( 391 "jvmOptions.no_dash", option)); 392 } 393 checkQuotes(option); 395 } 397 398 void checkQuotes(String option) throws InvalidJvmOptionException 400 { 401 int length = option.length(); 402 int numQuotes = 0; 403 int index = 0; 404 405 while (index < length && 406 (index = option.indexOf('\"', index)) != -1) 407 { 408 numQuotes++; 409 index++; 410 } 411 if ((numQuotes % 2) != 0) 412 { 413 throw new InvalidJvmOptionException(strMgr.getString( 414 "jvmOptions.incorrect_quotes", option)); 415 } 416 } 417 } 419 | Popular Tags |