001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.plugin.freeze;
028:
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: /**
034: * Filtering rules for selecting which components to freeze.
035: */
036: public class Rules {
037: private List rules = new ArrayList();
038:
039: static class Rule {
040: boolean deny = false;
041: Class cls;
042:
043: /**
044: * Create a rule from a rule specificaton of the form:
045: * [{allow,deny} ]<classname>
046: * If allow/deny is missing, allow is assumed.
047: */
048: public Rule(String ruleSpec) throws ClassNotFoundException {
049: int pos = ruleSpec.indexOf(' ');
050: String spec = ruleSpec;
051: if (pos >= 0) {
052: String ad = ruleSpec.substring(0, pos);
053: spec = ruleSpec.substring(pos + 1);
054: if (ad.equalsIgnoreCase("allow")) {
055: deny = false;
056: } else if (ad.equalsIgnoreCase("deny")) {
057: deny = true;
058: } else {
059: throw new IllegalArgumentException("Bad ruleSpec");
060: }
061: }
062: cls = Class.forName(spec);
063: }
064:
065: public Rule(Class c, boolean deny) {
066: this .cls = c;
067: this .deny = deny;
068: }
069:
070: public boolean matches(Class c) {
071: return cls.isAssignableFrom(c);
072: }
073:
074: public boolean isAllowRule() {
075: return !deny;
076: }
077:
078: public String toString() {
079: return (deny ? "deny " : "allow ") + cls.getName();
080: }
081: }
082:
083: public void addDenyRule(Class c) {
084: rules.add(new Rule(c, true));
085: }
086:
087: public void addAllowRule(Class c) {
088: rules.add(new Rule(c, false));
089: }
090:
091: public void addRule(String ruleSpec) throws ClassNotFoundException {
092: rules.add(new Rule(ruleSpec));
093: }
094:
095: public boolean allow(Object o) {
096: return allow(o.getClass());
097: }
098:
099: public boolean allow(Class c) {
100: for (int i = 0, n = rules.size(); i < n; i++) {
101: Rule rule = (Rule) rules.get(i);
102: if (rule.matches(c))
103: return rule.isAllowRule();
104: }
105: return true;
106: }
107:
108: Iterator iterator() {
109: return rules.iterator();
110: }
111:
112: public String toString() {
113: StringBuffer buf = new StringBuffer();
114: for (int i = 0, n = rules.size(); i < n; i++) {
115: if (i > 0)
116: buf.append(",");
117: buf.append(rules.get(i));
118: }
119: return buf.toString();
120: }
121:
122: public static void main(String[] args) {
123: Rules rules = new Rules();
124: boolean testMode = false;
125: try {
126: for (int i = 0; i < args.length; i++) {
127: if (testMode) {
128: Class cls = Class.forName(args[i]);
129: System.out.println("rules.allow(" + cls.getName()
130: + ")=" + rules.allow(cls));
131: } else if ("-test".equals(args[i])) {
132: testMode = true;
133: System.out.println("rules=" + rules);
134: } else {
135: rules.addRule(args[i]);
136: }
137: }
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: }
142: }
|