001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.logging;
005:
006: import com.tc.logging.TCLogger;
007:
008: import java.util.Arrays;
009: import java.util.Collections;
010: import java.util.HashMap;
011: import java.util.HashSet;
012: import java.util.Map;
013: import java.util.Set;
014:
015: /**
016: * Generic comma separated options parser
017: */
018: public class Options {
019: public static final String ALL = "ALL";
020: public static final String NONE = "NONE";
021:
022: private final Map options = new HashMap();
023:
024: public Options(String input, String[] keys, TCLogger logger,
025: Map defaults) {
026: if (keys == null) {
027: throw new NullPointerException("keys is null");
028: }
029: if (logger == null) {
030: throw new NullPointerException("logger is null");
031: }
032: checkKeys(keys);
033:
034: if (defaults == null) {
035: defaults = Collections.EMPTY_MAP;
036: }
037:
038: if (input != null) {
039: input = input.replaceAll("\\s", "");
040: if (input.length() > 0) {
041: Set valid = new HashSet(Arrays.asList(keys));
042: String[] configValues = input.split(",");
043:
044: for (int i = 0; i < configValues.length; i++) {
045: String value = configValues[i];
046: if (value.length() > 0) {
047: boolean negate = value.startsWith("-");
048: if (negate) {
049: value = value.substring(1);
050: }
051:
052: if (value.length() == 0) {
053: continue;
054: }
055:
056: if (valid.contains(value)) {
057: options.put(value, negate ? Boolean.FALSE
058: : Boolean.TRUE);
059: } else if (ALL.equals(value)) {
060: enableAll(keys);
061: } else if (NONE.equals(value)) {
062: disableAll(keys);
063: } else {
064: logger
065: .warn("["
066: + value
067: + "] is not a valid option, ignoring it");
068: }
069: }
070: }
071: }
072: }
073:
074: defaultMissingOptions(keys, defaults);
075: }
076:
077: private void disableAll(String[] keys) {
078: for (int i = 0; i < keys.length; i++) {
079: options.put(keys[i], Boolean.FALSE);
080: }
081: }
082:
083: private void checkKeys(String[] keys) {
084: for (int i = 0; i < keys.length; i++) {
085: String key = keys[i];
086: if (ALL.equals(key) || NONE.equals(key)) {
087: throw new IllegalArgumentException("Illegal key " + key
088: + " at position " + i);
089: }
090: }
091: }
092:
093: public boolean getOption(String opt) {
094: return ((Boolean) options.get(opt)).booleanValue();
095: }
096:
097: private void enableAll(String[] keys) {
098: for (int i = 0; i < keys.length; i++) {
099: options.put(keys[i], Boolean.TRUE);
100: }
101: }
102:
103: private void defaultMissingOptions(String[] keys, Map defaults) {
104: for (int i = 0; i < keys.length; i++) {
105: String key = keys[i];
106: if (defaults.containsKey(key) && !options.containsKey(key)) {
107: Object value = defaults.get(key);
108: if (value instanceof Boolean) {
109: options.put(key, value);
110: } else {
111: throw new IllegalArgumentException(
112: "Invalid default value in map for " + key
113: + ": " + value);
114: }
115: }
116:
117: // finally default option to false if no default present, and not explicitly set
118: if (!options.containsKey(key)) {
119: options.put(key, Boolean.FALSE);
120: }
121: }
122: }
123:
124: }
|