Author: bdelacretaz
Date: Mon Aug 5 15:18:38 2013
New Revision: 1510554
URL: http://svn.apache.org/r1510554
Log:
SLING-2987 - HealthCheckSelector added
Added:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/HealthCheckSelector.java
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java
Removed:
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/AllRulesTest.java
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/OsgiHealthCheckFacadeTest.java
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/OsgiRulesTest.java
Modified:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java
sling/branches/SLING-2987-healthcheck-redesign/it/pom.xml
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/U.java
Added:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/HealthCheckSelector.java
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/HealthCheckSelector.java?rev=1510554&view=auto
==============================================================================
---
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/HealthCheckSelector.java
(added)
+++
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/api/HealthCheckSelector.java
Mon Aug 5 15:18:38 2013
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+import java.util.List;
+
+/** Select from available {@link HealthCheck} services */
+public interface HealthCheckSelector {
+
+ /** Retrieve {@link HealthCheck} services that have
+ * specific tags (declared using the {@link Constants.HC_TAGS}
+ * service property)
+ *
+ * @param tags If not supplied, all available services are
+ * returned. A tag can be prefixed by a minus sign to omit
+ * services having that tag. Supplied tags are combined with
+ * AND to select within available services
+ */
+ public List<HealthCheck> getTaggedHealthCheck(String ... tags);
+}
Modified:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java?rev=1510554&r1=1510553&r2=1510554&view=diff
==============================================================================
---
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java
(original)
+++
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java
Mon Aug 5 15:18:38 2013
@@ -84,7 +84,7 @@ public class HealthCheckMBean implements
}
public HealthCheckMBean(HealthCheck hc) {
- String name = hc.getInfo().get(Constants.HC_NAME);
+ String name = hc.getInfo() == null ? null :
hc.getInfo().get(Constants.HC_NAME);
if(name == null) {
name = hc.toString();
}
Added:
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java?rev=1510554&view=auto
==============================================================================
---
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
(added)
+++
sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
Mon Aug 5 15:18:38 2013
@@ -0,0 +1,88 @@
+/*
+ * 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.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.hc.api.Constants;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.HealthCheckSelector;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class HealthCheckSelectorImpl implements HealthCheckSelector {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+ private BundleContext bundleContext;
+ public static final String OMIT_PREFIX = "-";
+
+ @Activate
+ protected void activate(ComponentContext ctx) {
+ bundleContext = ctx.getBundleContext();
+ }
+
+ @Override
+ public List<HealthCheck> getTaggedHealthCheck(String... tags) {
+
+ // Build service filter
+ final StringBuilder filterBuilder = new StringBuilder();
+
filterBuilder.append("(&(objectClass=").append(HealthCheck.class.getName()).append(")");
+ if(tags.length > 0) {
+ final int prefixLen = OMIT_PREFIX.length();
+ for(String tag : tags) {
+ tag = tag.trim();
+ if(tag.startsWith(OMIT_PREFIX)) {
+
filterBuilder.append("(!(").append(Constants.HC_TAGS).append("=").append(tag.substring(prefixLen)).append("))");
+ } else {
+
filterBuilder.append("(").append(Constants.HC_TAGS).append("=").append(tag).append(")");
+ }
+ }
+ }
+ filterBuilder.append(")");
+
+ final List<HealthCheck> result = new ArrayList<HealthCheck>();
+ try {
+ final String filterString = filterBuilder.length() == 0 ? null :
filterBuilder.toString();
+ bundleContext.createFilter(filterString); // check syntax early
+ final ServiceReference [] refs =
bundleContext.getServiceReferences(HealthCheck.class.getName(), filterString);
+ if(refs == null) {
+ log.info("Found no HealthCheck services with filter [{}]",
filterString);
+ } else {
+ log.info("Found {} HealthCheck services with filter [{}]",
refs.length, filterString);
+ for(ServiceReference ref : refs) {
+ final HealthCheck hc =
(HealthCheck)bundleContext.getService(ref);
+ log.debug("Selected HealthCheck service {}", hc);
+ result.add(hc);
+ }
+ }
+ } catch(InvalidSyntaxException ise) {
+ throw new IllegalStateException("Invalid OSGi filter syntax in '"
+ filterBuilder + "'", ise);
+ }
+ return result;
+ }
+}
Modified: sling/branches/SLING-2987-healthcheck-redesign/it/pom.xml
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/it/pom.xml?rev=1510554&r1=1510553&r2=1510554&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/it/pom.xml (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/it/pom.xml Mon Aug 5
15:18:38 2013
@@ -25,6 +25,7 @@
<exam.version>3.0.3</exam.version>
<url.version>1.5.2</url.version>
<org.ops4j.pax.logging.DefaultServiceLog.level>INFO</org.ops4j.pax.logging.DefaultServiceLog.level>
+ <felix.shell>false</felix.shell>
</properties>
<build>
@@ -36,6 +37,7 @@
<systemProperties>
<org.ops4j.pax.logging.DefaultServiceLog.level>${org.ops4j.pax.logging.DefaultServiceLog.level}</org.ops4j.pax.logging.DefaultServiceLog.level>
<sling.hc.core.version>${project.version}</sling.hc.core.version>
+ <felix.shell>${felix.shell}</felix.shell>
</systemProperties>
</configuration>
</plugin>
@@ -58,12 +60,6 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.hc.rules</artifactId>
- <version>${project.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.2</version>
Added:
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java?rev=1510554&view=auto
==============================================================================
---
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java
(added)
+++
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java
Mon Aug 5 15:18:38 2013
@@ -0,0 +1,204 @@
+/*
+ * 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.it.core;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Inject;
+
+import org.apache.sling.hc.api.Constants;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.HealthCheckSelector;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@RunWith(PaxExam.class)
+public class HealthCheckSelectorTest {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Inject
+ private HealthCheckSelector selector;
+
+ @Inject
+ private BundleContext bundleContext;
+
+ private List<TestHealthCheck> testServices = new
ArrayList<TestHealthCheck>();
+ private static int instanceCounter = 0;
+
+ class TestHealthCheck implements HealthCheck {
+
+ private final int id;
+ private final ServiceRegistration<?> reg;
+ final String [] tags;
+
+ TestHealthCheck(String ... tags) {
+ id = instanceCounter++;
+ this.tags = tags;
+ final Dictionary<String, Object> props = new Hashtable<String,
Object>();
+ if(tags != null) {
+ props.put(Constants.HC_TAGS, tags);
+ }
+ props.put(Constants.HC_TAGS, tags);
+ reg = bundleContext.registerService(HealthCheck.class.getName(),
this, props);
+ log.info("Registered {} with {}={}", new Object[] { this,
Constants.HC_TAGS, props.get(Constants.HC_TAGS)});
+ }
+
+ @Override
+ public String toString() {
+ return Arrays.asList(tags).toString();
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ return other instanceof TestHealthCheck &&
((TestHealthCheck)other).id == id;
+ }
+
+ @Override
+ public int hashCode() {
+ return id;
+ }
+
+ @Override
+ public Result execute(ResultLog log) {
+ return null;
+ }
+
+ @Override
+ public Map<String, String> getInfo() {
+ return null;
+ }
+
+ void unregister() {
+ reg.unregister();
+ }
+ }
+
+ @Configuration
+ public Option[] config() {
+ return U.config();
+ }
+
+ @Before
+ public void setup() {
+ testServices.add(new TestHealthCheck("foo"));
+ testServices.add(new TestHealthCheck("bar"));
+ testServices.add(new TestHealthCheck("foo", "bar"));
+ testServices.add(new TestHealthCheck("other", "thing"));
+ }
+
+ @After
+ public void cleanup() {
+ for(TestHealthCheck tc : testServices) {
+ tc.unregister();
+ }
+ }
+
+ /** @param included true or false, in the same order as testServices */
+ private void assertServices(List<HealthCheck> s, boolean ... included) {
+ final Iterator<TestHealthCheck> it = testServices.iterator();
+ for(boolean inc : included) {
+ final TestHealthCheck thc = it.next();
+ if(inc) {
+ assertTrue("Expecting list of services to include " + thc,
s.contains(thc));
+ } else {
+ assertFalse("Not expecting list of services to include " +
thc, s.contains(thc));
+ }
+ }
+ }
+
+ @Test
+ public void testSelectorService() {
+ assertNotNull("Expecting HealthCheckSelector service to be provided",
selector);
+ }
+
+ @Test
+ public void testAllServices() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck();
+ assertServices(s, true, true, true, true);
+ }
+
+ @Test
+ public void testFooTag() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("foo");
+ assertServices(s, true, false, true, false);
+ }
+
+ @Test
+ public void testBarTag() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("bar");
+ assertServices(s, false, true, true, false);
+ }
+
+ @Test
+ public void testFooAndBar() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("foo",
"bar");
+ assertServices(s, false, false, true, false);
+ }
+
+ @Test
+ public void testFooMinusBar() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("foo",
"-bar");
+ assertServices(s, true, false, false, false);
+ }
+
+ @Test
+ public void testOther() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("other");
+ assertServices(s, false, false, false, true);
+ }
+
+ @Test
+ public void testMinusOther() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("-other");
+ assertServices(s, true, true, true, false);
+ }
+
+ @Test
+ public void testMinusOtherFoo() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("-other",
"-foo");
+ assertServices(s, false, true, false, false);
+ }
+
+ @Test
+ public void testNoResults() {
+ final List<HealthCheck> s = selector.getTaggedHealthCheck("NOT A TAG");
+ assertTrue("Expecting no services", s.isEmpty());
+ }
+}
Modified:
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/U.java
URL:
http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/U.java?rev=1510554&r1=1510553&r2=1510554&view=diff
==============================================================================
---
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/U.java
(original)
+++
sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/U.java
Mon Aug 5 15:18:38 2013
@@ -17,66 +17,40 @@
*/
package org.apache.sling.hc.it.core;
-import static org.junit.Assert.assertEquals;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;
-import static org.ops4j.pax.exam.CoreOptions.when;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+import static org.ops4j.pax.exam.CoreOptions.when;
-import java.io.IOException;
-import java.io.StringReader;
-import java.util.List;
-
-import org.apache.sling.hc.api.EvaluationResult;
-import org.apache.sling.hc.api.HealthCheckFacade;
-import org.apache.sling.hc.api.RulesEngine;
import org.ops4j.pax.exam.Option;
/** Test utilities */
public class U {
- static Option[] config(boolean includeRules) {
+ static Option[] config() {
final String coreVersion = System.getProperty("sling.hc.core.version");
- String localRepo = System.getProperty("maven.repo.local", "");
+ final String localRepo = System.getProperty("maven.repo.local", "");
+ final boolean felixShell =
"true".equals(System.getProperty("felix.shell", "false"));
- if(includeRules) {
- return options(
- when(localRepo.length() > 0).useOptions(
-
systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
- ),
- junitBundles(),
- provision(
- mavenBundle("org.apache.sling",
"org.apache.sling.hc.core", coreVersion),
- mavenBundle("org.apache.sling",
"org.apache.sling.hc.rules", coreVersion)
- )
- );
- } else {
- return options(
- when(localRepo.length() > 0).useOptions(
-
systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
- ),
- junitBundles(),
+ return options(
+ when(localRepo.length() > 0).useOptions(
+
systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo)
+ ),
+ junitBundles(),
+ when(felixShell).useOptions(
provision(
- mavenBundle("org.apache.sling",
"org.apache.sling.hc.core", coreVersion)
+ mavenBundle("org.apache.felix",
"org.apache.felix.gogo.shell", "0.10.0"),
+ mavenBundle("org.apache.felix",
"org.apache.felix.gogo.runtime", "0.10.0"),
+ mavenBundle("org.apache.felix",
"org.apache.felix.gogo.command", "0.12.0")
)
- );
- }
- }
-
- static List<EvaluationResult> evaluateRules(HealthCheckFacade facade,
String [] rules) throws IOException {
- final RulesEngine e = facade.getNewRulesEngine();
- final StringBuilder b = new StringBuilder();
- for(String line : rules) {
- b.append(line).append("\n");
- }
- e.addRules(facade.parseSimpleTextRules(new
StringReader(b.toString())));
- return e.evaluateRules();
- }
-
- static void assertResult(EvaluationResult rr, boolean expectOk, String
ruleString) {
- assertEquals("Expecting " + rr.getRule() + " result to match",
expectOk, !rr.anythingToReport());
- assertEquals("Expecting " + rr.getRule() + " string to match",
ruleString, rr.getRule().toString());
+ ),
+ provision(
+ mavenBundle("org.apache.felix", "org.apache.felix.scr",
"1.6.2"),
+ mavenBundle("org.apache.sling",
"org.apache.sling.hc.core", coreVersion),
+ mavenBundle("org.apache.sling",
"org.apache.sling.commons.osgi", "2.2.0")
+ )
+ );
}
}