Skip to content

Commit aadc5dc

Browse files
committed
Prevent to load the same SPI implementation multiple times
1 parent 96d802f commit aadc5dc

5 files changed

Lines changed: 34 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public static <S> ServiceLoader<S> load(final Class<S> service, ClassLoader load
117117
private final ClassLoader loader;
118118

119119
private Set<S> providers;
120+
private Set<Class<?>> loadedImplementations;
120121

121122
private java.util.ServiceLoader<ServiceLocator> locatorLoader;
122123

@@ -139,6 +140,7 @@ private ServiceLoader(final Class<S> service, final ClassLoader loader)
139140
public void reload()
140141
{
141142
providers = new HashSet<S>();
143+
loadedImplementations = new HashSet<Class<?>>();
142144

143145
for (URL serviceFile : loadServiceFiles())
144146
{
@@ -240,11 +242,19 @@ private void loadClass(final Class<? extends S> serviceClass)
240242
{
241243
return;
242244
}
245+
if (loadedImplementations.contains(serviceClass))
246+
{
247+
return;
248+
}
243249
Collection<? extends S> services = loadEnriched(serviceClass);
244250
if ((services == null) || services.isEmpty())
245251
{
246252
return;
247253
}
254+
for (S service : services)
255+
{
256+
loadedImplementations.add(service.getClass());
257+
}
248258
providers.addAll(services);
249259
}
250260

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.ocpsoft.common.services;
2+
3+
public interface DuplicatedService
4+
{
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.ocpsoft.common.services;
2+
3+
public class DuplicatedServiceImpl implements DuplicatedService
4+
{
5+
6+
}

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

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

18+
import static org.junit.Assert.assertEquals;
19+
1820
import java.util.List;
1921

2022
import junit.framework.Assert;
@@ -91,4 +93,12 @@ public void testEnricherThatProvidesClassDoesNotEnrich() throws Exception
9193
MockServiceEnricher.produceStandard = false;
9294
MockServiceLocator.provide = false;
9395
}
96+
97+
@Test
98+
@SuppressWarnings("unchecked")
99+
public void testDuplicatedServiceFileEntries()
100+
{
101+
List<DuplicatedService> list = Iterators.asList(ServiceLoader.load(DuplicatedService.class));
102+
assertEquals(1, list.size());
103+
}
94104
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.ocpsoft.common.services.DuplicatedServiceImpl
2+
org.ocpsoft.common.services.DuplicatedServiceImpl

0 commit comments

Comments
 (0)