Author: bdelacretaz Date: Thu Mar 23 15:50:05 2017 New Revision: 1788272 URL: http://svn.apache.org/viewvc?rev=1788272&view=rev Log: SLING-6702 - MetricsServiceFactory + tests. Test are a bit slow for now, starting a full Sling Launchpad should not be needed
Added: sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java - copied, changed from r1788244, sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/Counter.java sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/ sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java Modified: sling/trunk/bundles/commons/metrics/pom.xml sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/package-info.java Modified: sling/trunk/bundles/commons/metrics/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/pom.xml?rev=1788272&r1=1788271&r2=1788272&view=diff ============================================================================== --- sling/trunk/bundles/commons/metrics/pom.xml (original) +++ sling/trunk/bundles/commons/metrics/pom.xml Thu Mar 23 15:50:05 2017 @@ -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> Copied: sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java (from r1788244, sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/Counter.java) URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java?p2=sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java&p1=sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/Counter.java&r1=1788244&r2=1788272&rev=1788272&view=diff ============================================================================== --- sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/Counter.java (original) +++ sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/MetricsServiceFactory.java Thu Mar 23 15:50:05 2017 @@ -19,35 +19,44 @@ package org.apache.sling.commons.metrics; - -import org.osgi.annotation.versioning.ProviderType; - -/** - * An incrementing and decrementing counter metric. +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. */ -@ProviderType -public interface Counter extends Counting, Metric{ - /** - * Increment the counter by one. - */ - void increment(); - - /** - * Decrement the counter by one. - */ - void decrement(); - - /** - * Increment the counter by {@code n}. - * - * @param n the amount by which the counter will be increased - */ - void increment(long n); - - /** - * Decrement the counter by {@code n}. - * - * @param n the amount by which the counter will be decreased +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 */ - void decrement(long n); -} + 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 Modified: sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/package-info.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/package-info.java?rev=1788272&r1=1788271&r2=1788272&view=diff ============================================================================== --- sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/package-info.java (original) +++ sling/trunk/bundles/commons/metrics/src/main/java/org/apache/sling/commons/metrics/package-info.java Thu Mar 23 15:50:05 2017 @@ -22,7 +22,7 @@ * * @version 1.0 */ -@Version("1.1.0") +@Version("1.2.0") package org.apache.sling.commons.metrics; Added: sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java?rev=1788272&view=auto ============================================================================== --- sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java (added) +++ sling/trunk/bundles/commons/metrics/src/test/java/org/apache/sling/commons/metrics/test/MetricsServiceFactoryIT.java Thu Mar 23 15:50:05 2017 @@ -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); + } + +}