Skip to content

Commit ef93560

Browse files
committed
ServiceEnricher may now produce multiple instances of a service for a given type
1 parent f2d82b2 commit ef93560

7 files changed

Lines changed: 127 additions & 21 deletions

File tree

‎api/src/main/java/com/ocpsoft/common/services/ServiceLoader.java‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ private void loadClass(final Class<? extends S> serviceClass)
239239
{
240240
return;
241241
}
242-
S serviceInstance = loadEnriched(serviceClass);
243-
if (serviceInstance == null)
242+
Collection<? extends S> services = loadEnriched(serviceClass);
243+
if ((services == null) || services.isEmpty())
244244
{
245245
return;
246246
}
247-
providers.add(serviceInstance);
247+
providers.addAll(services);
248248
}
249249

250250
private Class<? extends S> loadClass(final String serviceClassName)
@@ -276,11 +276,11 @@ private Class<? extends S> loadClass(final String serviceClassName)
276276
*
277277
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
278278
*/
279-
public static <T> T loadEnriched(final Class<T> serviceClass)
279+
public static <T> Collection<T> loadEnriched(final Class<T> serviceClass)
280280
{
281281
try
282282
{
283-
T service = null;
283+
Collection<T> services = new ArrayList<T>();
284284
ServiceEnricher origin = null;
285285

286286
if (!NonEnriching.class.isAssignableFrom(serviceClass))
@@ -293,20 +293,20 @@ public static <T> T loadEnriched(final Class<T> serviceClass)
293293

294294
for (ServiceEnricher enricher : enricherLoader)
295295
{
296-
service = enricher.produce(serviceClass);
297-
if (service != null)
296+
services = enricher.produce(serviceClass);
297+
if (services != null)
298298
{
299299
origin = enricher;
300300
break;
301301
}
302302
}
303303
}
304304

305-
if (service == null)
305+
if (services.isEmpty())
306306
{
307307
Constructor<? extends T> constructor = serviceClass.getDeclaredConstructor();
308308
constructor.setAccessible(true);
309-
service = constructor.newInstance();
309+
services.add(constructor.newInstance());
310310
}
311311

312312
if (!NonEnriching.class.isAssignableFrom(serviceClass))
@@ -315,12 +315,14 @@ public static <T> T loadEnriched(final Class<T> serviceClass)
315315
{
316316
if (!enricher.equals(origin))
317317
{
318-
service = enricher.enrich(service);
318+
for (T service : services) {
319+
enricher.enrich(service);
320+
}
319321
}
320322
}
321323
}
322324

323-
return service;
325+
return services;
324326
}
325327
catch (NoClassDefFoundError e)
326328
{

‎api/src/main/java/com/ocpsoft/common/spi/ServiceEnricher.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.ocpsoft.common.spi;
1717

18+
import java.util.Collection;
19+
1820
import com.ocpsoft.common.services.ServiceLoader;
1921

2022
/**
@@ -25,13 +27,17 @@
2527
public interface ServiceEnricher
2628
{
2729
/**
28-
* Produce an enriched service of the given type. If no enriching took place, this method must return null.
30+
* Produce a {@link Collection} of enriched services of the given type. If no instances were produced, this method
31+
* must return an empty {@link Collection}. Only one {@link ServiceEnricher} may produce a service at any given time.
32+
* If more than one {@link ServiceEnricher} may produce the same service, only the first will be used.
2933
*/
30-
<T> T produce(Class<T> type);
34+
<T> Collection<T> produce(Class<T> type);
3135

3236
/**
3337
* Enrich an instantiated instance of the given service type. If no enriching took place, this method must return the
34-
* original service without modification.
38+
* original service without modification. This method is called only when the service was <strong>not</strong>
39+
* produced by this {@link ServiceEnricher}; in that case, enrichment should have already taken place at the time of
40+
* production.
3541
*/
36-
<T> T enrich(T service);
42+
<T> void enrich(T service);
3743
}

‎api/src/test/java/com/ocpsoft/common/services/DummyServiceImpl2.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@
1717

1818
/**
1919
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
20-
*
20+
*
2121
*/
2222
public class DummyServiceImpl2 implements DummyService
2323
{
24+
private final String name;
25+
26+
public DummyServiceImpl2()
27+
{
28+
this.name = "default";
29+
}
30+
31+
public DummyServiceImpl2(final String name)
32+
{
33+
this.name = name;
34+
}
2435

36+
public String getName()
37+
{
38+
return name;
39+
}
2540
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2011, Red Hat, Inc., and individual contributors
4+
* by the @authors tag. See the copyright.txt in the distribution for a
5+
* full listing of individual contributors.
6+
*
7+
* This is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation; either version 2.1 of
10+
* the License, or (at your option) any later version.
11+
*
12+
* This software is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this software; if not, write to the Free
19+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21+
*/
22+
package com.ocpsoft.common.services;
23+
24+
import java.util.ArrayList;
25+
import java.util.Collection;
26+
27+
import com.ocpsoft.common.spi.ServiceEnricher;
28+
29+
/**
30+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
31+
*
32+
*/
33+
public class MockServiceEnricher implements ServiceEnricher
34+
{
35+
public static boolean enrich = false;
36+
37+
@Override
38+
@SuppressWarnings("unchecked")
39+
public <T> Collection<T> produce(final Class<T> type)
40+
{
41+
Collection<T> result = new ArrayList<T>();
42+
/*
43+
* If we are asked for an instance of #2, provide two instances of #2 with different names.
44+
*/
45+
if (enrich && DummyServiceImpl2.class.equals(type))
46+
{
47+
result.add((T) new DummyServiceImpl2("first"));
48+
result.add((T) new DummyServiceImpl2("second"));
49+
}
50+
return result;
51+
52+
}
53+
54+
@Override
55+
public <T> void enrich(final T service)
56+
{}
57+
58+
}

‎api/src/test/java/com/ocpsoft/common/services/MockServiceLocator.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public class MockServiceLocator implements ServiceLocator
3535
public static boolean provide = false;
3636

3737
@Override
38+
@SuppressWarnings({ "rawtypes", "unchecked" })
3839
public <T> Collection<Class<T>> locate(final Class<T> type)
3940
{
41+
Collection result = new ArrayList();
4042
if (provide)
4143
{
42-
Collection result = new ArrayList();
4344
result.add(DummyServiceImpl2.class);
44-
return result;
4545
}
46-
return null;
46+
return result;
4747
}
4848

4949
}

‎api/src/test/java/com/ocpsoft/common/services/ServiceLoaderTest.java‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ServiceLoaderTest
3131
{
3232
@Test
3333
@SuppressWarnings("unchecked")
34-
public void test()
34+
public void testBasicBehavior()
3535
{
3636
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
3737
List<DummyService> list = Iterators.asList(services);
@@ -40,12 +40,36 @@ public void test()
4040

4141
@Test
4242
@SuppressWarnings("unchecked")
43-
public void testWithProviderExtension()
43+
public void testWithLocatorExtension()
4444
{
4545
MockServiceLocator.provide = true;
4646
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
4747
List<DummyService> list = Iterators.asList(services);
4848
Assert.assertEquals(2, list.size());
4949
MockServiceLocator.provide = false;
5050
}
51+
52+
@Test
53+
@SuppressWarnings("unchecked")
54+
public void testWithEnricherProviderDoesNotProduceUnlessTypeMatches() throws Exception
55+
{
56+
MockServiceEnricher.enrich = true;
57+
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
58+
List<DummyService> list = Iterators.asList(services);
59+
Assert.assertEquals(1, list.size());
60+
MockServiceEnricher.enrich = false;
61+
}
62+
63+
@Test
64+
@SuppressWarnings("unchecked")
65+
public void testWithEnricherAndLocatorProducesTwoSeparateNamedServiceInstances() throws Exception
66+
{
67+
MockServiceEnricher.enrich = true;
68+
MockServiceLocator.provide = true;
69+
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
70+
List<DummyService> list = Iterators.asList(services);
71+
Assert.assertEquals(3, list.size());
72+
MockServiceEnricher.enrich = false;
73+
MockServiceLocator.provide = false;
74+
}
5175
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.ocpsoft.common.services.MockServiceEnricher

0 commit comments

Comments
 (0)