This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-1.2.2 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git
commit c06acb322fd2d37c1c60fdff6e733bdcd53b09dc Author: Bertrand Delacretaz <[email protected]> AuthorDate: Thu Mar 23 15:50:05 2017 +0000 SLING-6702 - MetricsServiceFactory + tests. Test are a bit slow for now, starting a full Sling Launchpad should not be needed git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1788272 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 85 ++++++++++++++++++- .../commons/metrics/MetricsServiceFactory.java | 62 ++++++++++++++ .../apache/sling/commons/metrics/package-info.java | 2 +- .../metrics/test/MetricsServiceFactoryIT.java | 96 ++++++++++++++++++++++ 4 files changed, 243 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 2035102..fe07ab1 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,10 @@ https://sling.apache.org/documentation/bundles/metrics.html for details </description> + + <properties> + <org.ops4j.pax.exam.version>4.10.0</org.ops4j.pax.exam.version> + </properties> <scm> <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics</connection> @@ -80,6 +84,38 @@ </execution> </executions> </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + <configuration> + <redirectTestOutputToFile>true</redirectTestOutputToFile> + <systemProperties> + <property> + <name>bundle.filename</name> + <value>${basedir}/target/${project.build.finalName}.jar</value> + </property> + </systemProperties> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.servicemix.tooling</groupId> + <artifactId>depends-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>generate-depends-file</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> @@ -160,5 +196,52 @@ <version>2.12</version> <scope>test</scope> </dependency> - </dependencies> + <dependency> + <groupId>org.ops4j.pax.exam</groupId> + <artifactId>pax-exam</artifactId> + <version>${org.ops4j.pax.exam.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.ops4j.pax.exam</groupId> + <artifactId>pax-exam-cm</artifactId> + <version>${org.ops4j.pax.exam.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.ops4j.pax.exam</groupId> + <artifactId>pax-exam-container-forked</artifactId> + <version>${org.ops4j.pax.exam.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.ops4j.pax.exam</groupId> + <artifactId>pax-exam-junit4</artifactId> + <version>${org.ops4j.pax.exam.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.ops4j.pax.exam</groupId> + <artifactId>pax-exam-link-mvn</artifactId> + <version>${org.ops4j.pax.exam.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>javax.inject</groupId> + <artifactId>javax.inject</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.framework</artifactId> + <version>5.6.2</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.testing.paxexam</artifactId> + <version>0.0.4</version> + <scope>provided</scope> + </dependency> + </dependencies> </project> diff --git a/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java b/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java new file mode 100644 index 0000000..cd1c23f --- /dev/null +++ b/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java @@ -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 ASF 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.commons.metrics; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +/** Utility that provides a MetricsService to any class that + * has been loaded from an OSGi bundle. + * This is meant to make it as easy to access the MetricsService + * as it is to get a Logger, for example. + */ +public class MetricsServiceFactory { + + /** Provide a MetricsService mapped to the Bundle that loaded class c + * @param c a Class loaded by an OSGi bundle + * @return a MetricsService + */ + public static MetricsService getMetricsService(Class<?> c) { + if(c == null) { + throw new IllegalArgumentException("Class parameter is required"); + } + + final Bundle b = FrameworkUtil.getBundle(c); + if(b == null) { + throw new IllegalArgumentException("No BundleContext, Class was not loaded from a Bundle?: " + + c.getClass().getName()); + } + + final BundleContext ctx = b.getBundleContext(); + + // In theory we should unget this reference, but the OSGi framework + // ungets all references held by a bundle when it stops and we cannot + // do much better than that anyway. + final ServiceReference ref = ctx.getServiceReference(MetricsService.class.getName()); + if(ref == null) { + throw new IllegalStateException("MetricsService not found for Bundle " + + b.getSymbolicName()); + } + + return (MetricsService)ctx.getService(ref); + } +} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/commons/metrics/package-info.java b/src/main/java/org/apache/sling/commons/metrics/package-info.java index 3d6a101..0fa4a33 100644 --- a/src/main/java/org/apache/sling/commons/metrics/package-info.java +++ b/src/main/java/org/apache/sling/commons/metrics/package-info.java @@ -22,7 +22,7 @@ * * @version 1.0 */ -@Version("1.1.0") +@Version("1.2.0") package org.apache.sling.commons.metrics; diff --git a/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java b/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java new file mode 100644 index 0000000..1e7cd83 --- /dev/null +++ b/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java @@ -0,0 +1,96 @@ +/* + * 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 ASF 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.commons.metrics.test; + +import javax.inject.Inject; +import org.apache.sling.commons.metrics.MetricsService; +import org.apache.sling.commons.metrics.MetricsServiceFactory; +import static org.apache.sling.testing.paxexam.SlingOptions.slingLaunchpadOakTar; +import org.apache.sling.testing.paxexam.TestSupport; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Configuration; +import static org.ops4j.pax.exam.CoreOptions.composite; +import static org.ops4j.pax.exam.CoreOptions.mavenBundle; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerClass; +import org.osgi.framework.BundleContext; + +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerClass.class) +public class MetricsServiceFactoryIT extends TestSupport { + + @Inject + private BundleContext bundleContext; + + @Configuration + public Option[] configuration() { + return new Option[]{ + baseConfiguration() + }; + } + + @Override + protected Option baseConfiguration() { + return composite( + super.baseConfiguration(), + launchpad(), + testBundle("bundle.filename"), + mavenBundle().groupId("io.dropwizard.metrics").artifactId("metrics-core").versionAsInProject(), + mavenBundle().groupId("org.apache.sling").artifactId("org.apache.sling.testing.paxexam").versionAsInProject(), + mavenBundle().groupId("org.apache.sling").artifactId("org.apache.sling.junit.core").version("1.0.23") + ); + } + + protected Option launchpad() { + final int httpPort = findFreePort(); + final String workingDirectory = workingDirectory(); + return slingLaunchpadOakTar(workingDirectory, httpPort); + } + + @Test + public void nullClass() { + try { + MetricsServiceFactory.getMetricsService(null); + fail("Expecting an Exception"); + } catch(IllegalArgumentException asExpected) { + } + + } + + @Test + public void classNotLoadedFromOsgiBundle() { + try { + MetricsServiceFactory.getMetricsService(String.class); + fail("Expecting an Exception"); + } catch(IllegalArgumentException asExpected) { + } + } + + @Test + public void classFromBundle() { + final MetricsService m = MetricsServiceFactory.getMetricsService(getClass()); + assertNotNull("Expecting a MetricsService", m); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
