You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@onami.apache.org by si...@apache.org on 2013/01/15 21:38:02 UTC
svn commit: r1433620 - in /incubator/onami/trunk/spi: ./
src/main/java/org/apache/onami/spi/
src/main/java/org/apache/onami/spi/binder/ src/site/apt/
src/test/java/org/apache/onami/spi/
Author: simonetripodi
Date: Tue Jan 15 20:38:02 2013
New Revision: 1433620
URL: http://svn.apache.org/viewvc?rev=1433620&view=rev
Log:
[ONAMI-51] Services discovered via ServiceLoader must be qualified in the binder in order to address injections
the new code bumps SPI to a major release due to broken backward compatibility with the 99soft provided implementation
Added:
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java (with props)
Removed:
incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/AnnotatedServiceBuilder.java
incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/ServiceBuilder.java
Modified:
incubator/onami/trunk/spi/pom.xml
incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/ServiceLoaderModule.java
incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/FromClassLoaderBuilder.java
incubator/onami/trunk/spi/src/site/apt/services.apt
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/AcmeServiceImpl2.java
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl1.java
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl2.java
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FromSystemPropertiesTestCase.java
incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/ServiceLoaderModuleTestCase.java
Modified: incubator/onami/trunk/spi/pom.xml
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/pom.xml?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/pom.xml (original)
+++ incubator/onami/trunk/spi/pom.xml Tue Jan 15 20:38:02 2013
@@ -27,7 +27,7 @@
</parent>
<artifactId>org.apache.onami.spi</artifactId>
- <version>0.2.0-incubating-SNAPSHOT</version>
+ <version>1.0.0-incubating-SNAPSHOT</version>
<name>Apache Onami-SPI</name>
<description>Apache Onami-SPI is a Java5 compatible small collection of reusable tools to make easier the integration
@@ -73,11 +73,6 @@
</dependency>
<dependency>
- <groupId>com.google.inject.extensions</groupId>
- <artifactId>guice-multibindings</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
Modified: incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/ServiceLoaderModule.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/ServiceLoaderModule.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/ServiceLoaderModule.java (original)
+++ incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/ServiceLoaderModule.java Tue Jan 15 20:38:02 2013
@@ -19,24 +19,24 @@ package org.apache.onami.spi;
* under the License.
*/
-import static com.google.inject.Key.get;
-import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static java.lang.String.format;
import static java.lang.Thread.currentThread;
import static org.apache.onami.spi.ServiceLoader.load;
import java.lang.annotation.Annotation;
import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.inject.Qualifier;
-import org.apache.onami.spi.binder.AnnotatedServiceBuilder;
import org.apache.onami.spi.binder.FromClassLoaderBuilder;
-import org.apache.onami.spi.binder.ServiceBuilder;
import com.google.inject.AbstractModule;
-import com.google.inject.Binder;
-import com.google.inject.Key;
+import com.google.inject.BindingAnnotation;
import com.google.inject.ProvisionException;
-import com.google.inject.multibindings.Multibinder;
+import com.google.inject.binder.AnnotatedBindingBuilder;
+import com.google.inject.binder.LinkedBindingBuilder;
/**
* A Google Guice {@code Module} to simplify the task of
@@ -46,16 +46,95 @@ public abstract class ServiceLoaderModul
extends AbstractModule
{
+ private List<ServiceInfo<?>> services = new LinkedList<ServiceInfo<?>>();
+
+ @Override
+ protected final void configure()
+ {
+ if ( !services.isEmpty() )
+ {
+ throw new IllegalStateException( "Re-entry is not allowed." );
+ }
+
+ configureServices();
+
+ try
+ {
+ for ( ServiceInfo<?> builder : services )
+ {
+ bindService( builder );
+ }
+ }
+ finally
+ {
+ services.clear();
+ }
+ }
+
+ private <S> void bindService( ServiceInfo<S> serviceInfo )
+ {
+ Class<S> serviceType = serviceInfo.getServiceType();
+
+ Iterator<Class<? extends S>> servicesIterator = load( serviceType, serviceInfo.getClassLoader() )
+ .iterator();
+ boolean found = false;
+ while ( servicesIterator.hasNext() )
+ {
+ if ( !found )
+ {
+ found = true;
+ }
+
+ Class<? extends S> serviceImplType = servicesIterator.next();
+
+ bindService( serviceType, serviceImplType );
+ }
+
+ if ( !found )
+ {
+ throw new ProvisionException( format( "No Provider found for Service %s", serviceType.getName() ) );
+ }
+ }
+
+ private <S> void bindService( Class<S> serviceType, Class<? extends S> serviceImplType )
+ {
+ AnnotatedBindingBuilder<S> annotatedBindingBuilder = bind( serviceType );
+ LinkedBindingBuilder<S> linkedBindingBuilder = annotatedBindingBuilder;
+
+ dance: for ( Annotation annotation : serviceImplType.getAnnotations() )
+ {
+ Class<? extends Annotation> annotationType = annotation.annotationType();
+
+ /*
+ * if the serviceImplType is a javax.inject.Qualifier annotation
+ * or
+ * if the serviceImplType is a com.google.inject.BindingAnnotation
+ */
+ if ( annotationType.isAnnotationPresent( Qualifier.class )
+ || annotationType.isAnnotationPresent( BindingAnnotation.class ) )
+ {
+ linkedBindingBuilder = annotatedBindingBuilder.annotatedWith( annotation );
+ break dance;
+ }
+ }
+
+ linkedBindingBuilder.to( serviceImplType );
+ }
+
+ protected abstract void configureServices();
+
/**
* EDSL to bind Services to Providers using the SPI pattern.
*
* @param service The type of the service to be loaded.
* @return the chained EDSL builder.
*/
- protected final <S> FromClassLoaderBuilder bindService( Class<S> service )
+ protected final <S> FromClassLoaderBuilder discover( Class<S> service )
{
checkArgument( service != null, "Impossible to bind null service class!" );
- return new DefaultServiceBuilder<S>( service, binder() );
+ ServiceInfo<S> builder = new ServiceInfo<S>( service );
+ services.add( builder );
+ return builder;
}
/**
@@ -63,94 +142,38 @@ public abstract class ServiceLoaderModul
*
* @param <S> The type of the service to be loaded.
*/
- private static final class DefaultServiceBuilder<S>
+ private static final class ServiceInfo<S>
implements FromClassLoaderBuilder
{
- private final Class<S> service;
-
- private final Binder binder;
+ private final Class<S> serviceType;
private ClassLoader classLoader;
- private Key<S> bindingKey;
-
- public DefaultServiceBuilder( Class<S> service, Binder binder )
+ public ServiceInfo( Class<S> serviceType )
{
- this.service = service;
- this.binder = binder;
-
- bindingKey = get( service );
+ this.serviceType = serviceType;
classLoader = currentThread().getContextClassLoader();
}
/**
* {@inheritDoc}
*/
- public ServiceBuilder annotatedWith( Class<? extends Annotation> annotationType )
- {
- bindingKey = get( service, annotationType );
- return this;
- }
-
- /**
- * {@inheritDoc}
- */
- public ServiceBuilder annotatedWith( Annotation annotation )
- {
- bindingKey = get( service, annotation );
- return this;
- }
-
- /**
- * {@inheritDoc}
- */
- public AnnotatedServiceBuilder fromClassLoader( ClassLoader classLoader )
+ public void fromClassLoader( ClassLoader classLoader )
{
checkArgument( classLoader != null,
- "Impossible to load Service %s with a null ClassLoader", service.getName() );
+ "Impossible to load Service %s with a null ClassLoader", serviceType.getName() );
this.classLoader = classLoader;
- return this;
}
- /**
- * {@inheritDoc}
- */
- public void loadingFirstService()
+ public Class<S> getServiceType()
{
- Iterator<Class<? extends S>> servicesIterator = load( service, classLoader ).iterator();
- if ( !servicesIterator.hasNext() )
- {
- throw new ProvisionException( format( "No Provider found for Service %s", service.getName() ) );
- }
- binder.bind( bindingKey ).to( servicesIterator.next() );
+ return serviceType;
}
- /**
- * {@inheritDoc}
- */
- public void loadingAllServices()
+ public ClassLoader getClassLoader()
{
- Multibinder<S> multiBinder;
-
- if ( bindingKey.getAnnotation() != null )
- {
- multiBinder = newSetBinder( binder, service, bindingKey.getAnnotation() );
- }
- else if ( bindingKey.getAnnotationType() != null )
- {
- multiBinder = newSetBinder( binder, service, bindingKey.getAnnotationType() );
- }
- else
- {
- multiBinder = newSetBinder( binder, service );
- }
-
- Iterator<Class<? extends S>> serviceProviders = load( service, classLoader ).iterator();
- while ( serviceProviders.hasNext() )
- {
- multiBinder.addBinding().to( serviceProviders.next() );
- }
+ return classLoader;
}
}
Modified: incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/FromClassLoaderBuilder.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/FromClassLoaderBuilder.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/FromClassLoaderBuilder.java (original)
+++ incubator/onami/trunk/spi/src/main/java/org/apache/onami/spi/binder/FromClassLoaderBuilder.java Tue Jan 15 20:38:02 2013
@@ -23,7 +23,6 @@ package org.apache.onami.spi.binder;
* Allows specify the {@code ClassLoader} used to load the Service in the binding.
*/
public interface FromClassLoaderBuilder
- extends AnnotatedServiceBuilder
{
/**
@@ -32,6 +31,6 @@ public interface FromClassLoaderBuilder
* @param classLoader the {@code ClassLoader} used to load the Service in the binding.
* @return the chained EDSL builder.
*/
- AnnotatedServiceBuilder fromClassLoader( ClassLoader classLoader );
+ void fromClassLoader( ClassLoader classLoader );
}
Modified: incubator/onami/trunk/spi/src/site/apt/services.apt
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/site/apt/services.apt?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/site/apt/services.apt (original)
+++ incubator/onami/trunk/spi/src/site/apt/services.apt Tue Jan 15 20:38:02 2013
@@ -29,12 +29,12 @@ Binding Services
The <<<org.apache.onami.spi.ServiceLoaderModule>>> provides APIs to bind Services to Provided
discovered via SPI pattern.
- Given an hipotetycal <<<FooService>>>:
+ Given an hypothetical <<<CreditCardProcessor>>>:
+--------------------------------------+
package com.acme;
-public interface FooService
+public interface CreditCardProcessor
{
void doSomething();
@@ -47,11 +47,11 @@ public interface FooService
+--------------------------------------+
package com.acme.first;
-public final class FooServiceImpl1
- implements FooService
+@javax.inject.Named( "Visa" ) // any @javax.inject.Qualifier or @com.google.inject.BindingAnnotation annotated annotation
+public final class VisaCreditCardProcessor
+ implements CreditCardProcessor
{
- @Override
public void doSomething()
{
// ...
@@ -65,11 +65,11 @@ public final class FooServiceImpl1
+--------------------------------------+
package com.acme.second;
-public final class FooServiceImpl2
- implements FooService
+@javax.inject.Named( "MasterCard" ) // any @javax.inject.Qualifier or @com.google.inject.BindingAnnotation annotated annotation
+public final class MasterCardCreditCardProcessor
+ implements CreditCardProcessor
{
- @Override
public void doSomething()
{
// ...
@@ -78,13 +78,13 @@ public final class FooServiceImpl2
}
+--------------------------------------+
- Then define the SPI in <<<META-INF/services/com.acme.FooService>>> file(s):
+ Then define the SPI in <<<META-INF/services/com.acme.CreditCardProcessor>>> file(s):
----------------------------------------
# created by Jack Bauer in less than 24h
-com.acme.first.FooServiceImpl1
-com.acme.second.FooServiceImpl2 # EOL comment
+com.acme.first.VisaCreditCardProcessor
+com.acme.second.MasterCardCreditCardProcessor # EOL comment
# comments and blank lines supported
@@ -92,9 +92,7 @@ com.acme.second.FooServiceImpl2 # EOL
#
----------------------------------------
- Then users can:
-
- * Bind the Service to the first discovered Provider
+ Then users can require Services binding discovering implementation(s) with the service loader:
+--------------------------------------+
import static com.google.inject.Guice.createInjector;
@@ -110,74 +108,163 @@ Injector injector = createInjector( new
{
@Override
- protected void configure()
+ protected void configureServices()
{
- bindService( FooService.class ).loadingFirstService();
+ discover( CreditCardProcessor.class ); //.fromClassLoader( ClassLoader classLoader );
}
} );
+--------------------------------------+
- and the require its injection somewhere else:
+ and the require injections somewhere else:
+--------------------------------------+
public class PowerTool
{
@Inject
- private FooService fooService;
+ @javax.inject.Named( "MasterCard" )
+ private CreditCardProcessor creditCardProcessor;
// setters and algorithms omitted
}
+--------------------------------------+
- * Bind the Service to all discovered Provider
+ if a service is <not> annotated by a qualifier/binding annotation, i.e.
+--------------------------------------+
-import static com.google.inject.Guice.createInjector;
+package com.acme;
-import com.acme.FooService;
-import com.google.inject.Injector;
+public final class DefaultCreditCardProcessor
+ implements CreditCardProcessor
+{
-import org.apache.onami.spi.ServiceLoaderModule;
+ public void doSomething()
+ {
+ // ...
+ }
-...
+}
++--------------------------------------+
-Injector injector = createInjector( new ServiceLoaderModule()
+ Then defined the SPI in <<<META-INF/services/com.acme.CreditCardProcessor>>> file:
+
+----------------------------------------
+# created by Jack Bauer in less than 24h
+
+com.acme.DefaultCreditCardProcessor
+----------------------------------------
+
+ it is enough requiring its injection:
+
++--------------------------------------+
+public class PowerTool
{
- @Override
- protected void configure()
+ @Inject
+ // this is bound to com.acme.DefaultCreditCardProcessor
+ private CreditCardProcessor creditCardProcessor;
+
+ // setters and algorithms omitted
+
+}
++--------------------------------------+
+
+Qualifier annotations
+
+ Services implementations can be annotated with whatever annotation which, at their time, are annotated with a
+ <<<...@javax.inject.Qualifier>>> or <<<...@com.google.inject.BindingAnnotation>>> annotation; i.e. given the following
+ annotations:
+
++--------------------------------------+
+@com.google.inject.BindingAnnotation
+@Retention( RUNTIME )
+@Target( { FIELD, TYPE } )
+public @interface Visa
+{}
++--------------------------------------+
+
+ and
+
++--------------------------------------+
+@com.google.inject.BindingAnnotation
+@Retention( RUNTIME )
+@Target( { FIELD, TYPE } )
+public @interface MasterCard
+{}
++--------------------------------------+
+
+ users can annotate services implementations:
+
++--------------------------------------+
+package com.acme.first;
+
+@Visa
+public final class VisaCreditCardProcessorImpl
+ implements CreditCardProcessor
+{
+
+ public void doSomething()
{
- bindService( FooService.class ).loadingAllService();
+ // ...
}
-} );
+}
+--------------------------------------+
- and the require their injection somewhere else:
+ and
+--------------------------------------+
-public class SuperPowerTool
+package com.acme.second;
+
+@MasterCard
+public final class MasterCardCreditCardProcessorImpl
+ implements CreditCardProcessor
{
+ public void doSomething()
+ {
+ // ...
+ }
+
+}
++--------------------------------------+
+
+ Then define the SPI in <<<META-INF/services/com.acme.CreditCardProcessor>>> file(s):
+
+----------------------------------------
+# created by Jack Bauer in less than 24h
+
+com.acme.first.VisaCreditCardProcessorImpl
+com.acme.second.MasterCardCreditCardProcessorImpl
+----------------------------------------
+
+ Injections can be required by qualifying the injections:
+
++--------------------------------------+
+public class PowerTool
+{
+
+ @Inject
+ @Visa
+ private CreditCardProcessor creditCardProcessor;
+
@Inject
- private Set<FooService> fooService;
+ @MasterCard
+ private CreditCardProcessor creditCardProcessor;
// setters and algorithms omitted
}
+--------------------------------------+
- Eh?!? Just that?!? YES!!! :)
-
Loading Services from SystemProperties
Alternatively, SPIs can be specified via <Java System properties>:
----------------------------------------
-java -Dcom.acme.FooService=com.acme.first.FooServiceImpl1,com.acme.second.FooServiceImpl2
+java -Dcom.acme.CreditCardProcessor=com.acme.first.VisaCreditCardProcessorImpl,com.acme.second.MasterCardCreditCardProcessorImpl
----------------------------------------
Where <<,>> is the separator character between Providers.
Modified: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/AcmeServiceImpl2.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/AcmeServiceImpl2.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/AcmeServiceImpl2.java (original)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/AcmeServiceImpl2.java Tue Jan 15 20:38:02 2013
@@ -1,5 +1,7 @@
package org.apache.onami.spi;
+import javax.inject.Named;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -19,6 +21,7 @@ package org.apache.onami.spi;
* under the License.
*/
+@Named( "second" )
public final class AcmeServiceImpl2
implements AcmeService
{
Added: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java?rev=1433620&view=auto
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java (added)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java Tue Jan 15 20:38:02 2013
@@ -0,0 +1,39 @@
+package org.apache.onami.spi;
+
+/*
+ * 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.
+ */
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import com.google.inject.BindingAnnotation;
+
+@BindingAnnotation
+@Retention( RUNTIME )
+@Target( { FIELD, TYPE } )
+public @interface BarBindingAnnotation
+{
+
+ int value();
+
+}
Propchange: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/BarBindingAnnotation.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl1.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl1.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl1.java (original)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl1.java Tue Jan 15 20:38:02 2013
@@ -19,6 +19,7 @@ package org.apache.onami.spi;
* under the License.
*/
+@BarBindingAnnotation( 1 )
public final class FooServiceImpl1
implements FooService
{
Modified: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl2.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl2.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl2.java (original)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FooServiceImpl2.java Tue Jan 15 20:38:02 2013
@@ -19,6 +19,7 @@ package org.apache.onami.spi;
* under the License.
*/
+@BarBindingAnnotation( 2 )
public final class FooServiceImpl2
implements FooService
{
Modified: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FromSystemPropertiesTestCase.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FromSystemPropertiesTestCase.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FromSystemPropertiesTestCase.java (original)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/FromSystemPropertiesTestCase.java Tue Jan 15 20:38:02 2013
@@ -2,11 +2,7 @@ package org.apache.onami.spi;
import static com.google.inject.Guice.createInjector;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import java.util.Set;
-
-import org.apache.onami.spi.ServiceLoaderModule;
import org.junit.Before;
import org.junit.Test;
@@ -16,11 +12,21 @@ public final class FromSystemPropertiesT
{
@Inject
- private Set<FooService> fooServices;
+ @BarBindingAnnotation( 1 )
+ private FooService fooService1;
+
+ @Inject
+ @BarBindingAnnotation( 2 )
+ private FooService fooService2;
- public void setFooServices( Set<FooService> fooServices )
+ public void setFooService1( FooService fooService1 )
+ {
+ this.fooService1 = fooService1;
+ }
+
+ public void setFooService2( FooService fooService2 )
{
- this.fooServices = fooServices;
+ this.fooService2 = fooService2;
}
@Before
@@ -30,9 +36,9 @@ public final class FromSystemPropertiesT
{
@Override
- protected void configure()
+ protected void configureServices()
{
- bindService( FooService.class ).loadingAllServices();
+ discover( FooService.class );
}
} )
@@ -43,8 +49,8 @@ public final class FromSystemPropertiesT
@Test
public void injectedServicesCaughtFromSystemProperties()
{
- assertFalse( fooServices.isEmpty() );
- assertEquals( 2, fooServices.size() );
+ assertEquals( FooServiceImpl1.class, fooService1.getClass() );
+ assertEquals( FooServiceImpl2.class, fooService2.getClass() );
}
}
Modified: incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/ServiceLoaderModuleTestCase.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/ServiceLoaderModuleTestCase.java?rev=1433620&r1=1433619&r2=1433620&view=diff
==============================================================================
--- incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/ServiceLoaderModuleTestCase.java (original)
+++ incubator/onami/trunk/spi/src/test/java/org/apache/onami/spi/ServiceLoaderModuleTestCase.java Tue Jan 15 20:38:02 2013
@@ -20,10 +20,8 @@ package org.apache.onami.spi;
*/
import static com.google.inject.Guice.createInjector;
-import static com.google.inject.name.Names.named;
import static org.junit.Assert.assertEquals;
-import org.apache.onami.spi.ServiceLoaderModule;
import org.junit.Before;
import org.junit.Test;
@@ -34,13 +32,11 @@ public final class ServiceLoaderModuleTe
{
@Inject
- @Named( "first" )
- private AcmeService acmeService;
+ private AcmeService firstAcmeService;
- public void setAcmeService( AcmeService acmeService )
- {
- this.acmeService = acmeService;
- }
+ @Inject
+ @Named( "second" )
+ private AcmeService secondAcmeService;
@Before
public void setUp()
@@ -49,9 +45,9 @@ public final class ServiceLoaderModuleTe
{
@Override
- protected void configure()
+ protected void configureServices()
{
- bindService( AcmeService.class ).annotatedWith( named( "first" ) ).loadingFirstService();
+ discover( AcmeService.class );
}
} )
@@ -62,7 +58,8 @@ public final class ServiceLoaderModuleTe
@Test
public void singleServiceInjection()
{
- assertEquals( AcmeServiceImpl1.class, acmeService.getClass() );
+ assertEquals( AcmeServiceImpl1.class, firstAcmeService.getClass() );
+ assertEquals( AcmeServiceImpl2.class, secondAcmeService.getClass() );
}
}