Author: bdelacretaz
Date: Tue May 14 14:40:08 2013
New Revision: 1482347
URL: http://svn.apache.org/r1482347
Log:
SLING-2822 - RuleFilter added
Added:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
(with props)
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
(with props)
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
(with props)
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/Rule.java
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RulesEngine.java
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/impl/RulesEngineImpl.java
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/impl/RulesEngineTest.java
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/Rule.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/Rule.java?rev=1482347&r1=1482346&r2=1482347&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/Rule.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/Rule.java
Tue May 14 14:40:08 2013
@@ -49,16 +49,17 @@ public class Rule {
/** Replace the tags of this rule by supplied ones.
* Tags are lowercased before being set */
- public void setTags(String ...newTags) {
+ public Rule setTags(String ...newTags) {
tags = new HashSet<String>();
for(String tag : newTags) {
tags.add(tag.toLowerCase());
}
+ return this;
}
/** Return this rule's tags */
public Set<String> getTags() {
- return tags;
+ return Collections.unmodifiableSet(tags);
}
/** True if this rule has given tags */
Added:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java?rev=1482347&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
(added)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
Tue May 14 14:40:08 2013
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.api;
+
+/** A filter that accepts or rejects a {@link Rule} */
+public interface RuleFilter {
+ boolean accept(Rule r);
+}
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RuleFilter.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RulesEngine.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RulesEngine.java?rev=1482347&r1=1482346&r2=1482347&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RulesEngine.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/api/RulesEngine.java
Tue May 14 14:40:08 2013
@@ -34,4 +34,7 @@ public interface RulesEngine {
* them in sets (performance, configuration etc.)
*/
List<EvaluationResult> evaluateRules();
+
+ /** Evaluate all rules that the supplied RuleFilter accepts */
+ List<EvaluationResult> evaluateRules(RuleFilter filter);
}
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/impl/RulesEngineImpl.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/impl/RulesEngineImpl.java?rev=1482347&r1=1482346&r2=1482347&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/impl/RulesEngineImpl.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/impl/RulesEngineImpl.java
Tue May 14 14:40:08 2013
@@ -22,10 +22,13 @@ import java.util.List;
import org.apache.sling.hc.api.EvaluationResult;
import org.apache.sling.hc.api.Rule;
+import org.apache.sling.hc.api.RuleFilter;
import org.apache.sling.hc.api.RulesEngine;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class RulesEngineImpl implements RulesEngine {
-
+ private final Logger log = LoggerFactory.getLogger(getClass());
private final List<Rule> rules = new ArrayList<Rule>();
@Override
@@ -40,8 +43,17 @@ public class RulesEngineImpl implements
@Override
public List<EvaluationResult> evaluateRules() {
+ return evaluateRules(null);
+ }
+
+ @Override
+ public List<EvaluationResult> evaluateRules(RuleFilter filter) {
final List<EvaluationResult> result = new
ArrayList<EvaluationResult>();
for(Rule r : rules) {
+ if(filter != null && !filter.accept(r)) {
+ log.debug("Rule {} rejected by {}", r, filter);
+ continue;
+ }
result.add(r.evaluate());
}
return result;
Added:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java?rev=1482347&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
(added)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
Tue May 14 14:40:08 2013
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.util;
+
+import org.apache.sling.hc.api.Rule;
+import org.apache.sling.hc.api.RuleFilter;
+
+/** {@link RulerFilter} that accepts {@link Rule} that have certain tags */
+public class TaggedRuleFilter implements RuleFilter {
+ private String [] tags;
+
+ /** Create a RuleFilter that selects Rules
+ * having all supplied tags (lowercased)
+ */
+ public TaggedRuleFilter(String ...tags) {
+ this.tags = tags;
+ for(int i=0 ; i < tags.length; i++) {
+ tags[i] = tags[i].toLowerCase();
+ }
+ }
+
+ public boolean accept(Rule r) {
+ for(String tag : tags) {
+ if(!r.hasTag(tag)) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/main/java/org/apache/sling/hc/util/TaggedRuleFilter.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Modified:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/impl/RulesEngineTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/impl/RulesEngineTest.java?rev=1482347&r1=1482346&r2=1482347&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/impl/RulesEngineTest.java
(original)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/impl/RulesEngineTest.java
Tue May 14 14:40:08 2013
@@ -26,6 +26,7 @@ import java.util.List;
import org.apache.sling.hc.api.EvaluationResult;
import org.apache.sling.hc.api.Rule;
import org.apache.sling.hc.api.RuleBuilder;
+import org.apache.sling.hc.api.RuleFilter;
import org.apache.sling.hc.api.SystemAttribute;
import org.apache.sling.hc.util.DefaultEvaluator;
import org.junit.Test;
@@ -100,4 +101,32 @@ public class RulesEngineTest {
assertResult(result.get(i++), false, "test_invert_12_-1");
assertResult(result.get(i++), true, "test_invert_12_-12");
}
+
+ @Test
+ public void testRuleFilter() throws IOException {
+ final String rules =
+ "test:constant:5:5\n"
+ + "test:constant:5: > 2\n"
+ + "test:constant:5: < 12\n"
+ + "test:constant:5: between 4 and 6\n"
+ + "test:constant:5: between 12 and 21\n"
+ + "test:constant:5:42\n"
+ + "test:invert:12:-1\n"
+ + "test:invert:12:-12\n"
+ ;
+
+ final TextRulesParser p = new TextRulesParser();
+ p.addBuilder(new TestBuilder());
+
+ final RulesEngineImpl e = new RulesEngineImpl();
+ e.addRules(p.parse(new StringReader(rules)));
+
+ final RuleFilter f = new RuleFilter() {
+ public boolean accept(Rule r) {
+ return r.toString().contains("12");
+ }
+ };
+ final List<EvaluationResult> result = e.evaluateRules(f);
+ assertEquals("Expecting only 4 rules according to RuleFilter", 4,
result.size());
+ }
}
Added:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java?rev=1482347&view=auto
==============================================================================
---
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
(added)
+++
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
Tue May 14 14:40:08 2013
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.util;
+
+import org.apache.sling.hc.api.Rule;
+import org.junit.Test;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class TaggedRuleFilterTest {
+ private Rule notags = new Rule(null, null);
+ private Rule foobar = new Rule(null, null).setTags("foo", "bar");
+ private Rule foo = new Rule(null, null).setTags("foo");
+
+ @Test
+ public void testNoTagsFilter() {
+ final TaggedRuleFilter ntf = new TaggedRuleFilter();
+ assertTrue(ntf.accept(notags));
+ assertTrue(ntf.accept(foobar));
+ assertTrue(ntf.accept(foo));
+ }
+
+ @Test
+ public void testFooTagsFilter() {
+ final TaggedRuleFilter ftf = new TaggedRuleFilter("foo");
+ assertFalse(ftf.accept(notags));
+ assertTrue(ftf.accept(foobar));
+ assertTrue(ftf.accept(foo));
+ }
+
+ @Test
+ public void testFoobarTagsFilter() {
+ final TaggedRuleFilter f = new TaggedRuleFilter("foo", "bar");
+ assertFalse(f.accept(notags));
+ assertTrue(f.accept(foobar));
+ assertFalse(f.accept(foo));
+ }
+
+ @Test
+ public void testFoobarLowercaseTagsFilter() {
+ final TaggedRuleFilter f = new TaggedRuleFilter("Foo", "BAR");
+ assertFalse(f.accept(notags));
+ assertTrue(f.accept(foobar));
+ assertFalse(f.accept(foo));
+ }
+
+}
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/contrib/extensions/healthcheck/hc-core/src/test/java/org/apache/sling/hc/util/TaggedRuleFilterTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL