Skip to content

Commit f2d82b2

Browse files
committed
Added ServiceLocator SPI
1 parent a41475d commit f2d82b2

7 files changed

Lines changed: 183 additions & 24 deletions

File tree

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.lang.reflect.InvocationTargetException;
2424
import java.net.URL;
2525
import java.util.ArrayList;
26+
import java.util.Collection;
2627
import java.util.Enumeration;
2728
import java.util.HashSet;
2829
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Set;
3132

3233
import com.ocpsoft.common.spi.ServiceEnricher;
34+
import com.ocpsoft.common.spi.ServiceLocator;
3335

3436
/**
3537
* This class handles looking up service providers on the class path. It implements the <a
@@ -109,34 +111,14 @@ public static <S> ServiceLoader<S> load(final Class<S> service, ClassLoader load
109111
return new ServiceLoader<S>(service, loader);
110112
}
111113

112-
/**
113-
* Creates a new service loader for the given service type, using the extension class loader.
114-
*
115-
* This convenience method simply locates the extension class loader, call it extClassLoader, and then returns
116-
*
117-
* <code>ServiceLoader.load(service, extClassLoader)</code>
118-
*
119-
* If the extension class loader cannot be found then the system class loader is used; if there is no system class
120-
* loader then the bootstrap class loader is used.
121-
*
122-
* This method is intended for use when only installed providers are desired. The resulting service will only find
123-
* and load providers that have been installed into the current Java virtual machine; providers on the application's
124-
* class path will be ignored.
125-
*
126-
* @param service The interface or abstract class representing the service
127-
* @return A new service loader
128-
*/
129-
public static <S> ServiceLoader<S> loadInstalled(final Class<S> service)
130-
{
131-
throw new UnsupportedOperationException("Not implemented");
132-
}
133-
134114
private final String serviceFile;
135115
private final Class<S> expectedType;
136116
private final ClassLoader loader;
137117

138118
private Set<S> providers;
139119

120+
private java.util.ServiceLoader<ServiceLocator> locatorLoader;
121+
140122
private ServiceLoader(final Class<S> service, final ClassLoader loader)
141123
{
142124
this.loader = loader;
@@ -161,6 +143,22 @@ public void reload()
161143
{
162144
loadServiceFile(serviceFile);
163145
}
146+
147+
if (locatorLoader == null)
148+
locatorLoader = java.util.ServiceLoader
149+
.load(ServiceLocator.class);
150+
151+
for (ServiceLocator locator : locatorLoader)
152+
{
153+
Collection<Class<S>> serviceTypes = locator.locate(expectedType);
154+
if ((serviceTypes != null) && !serviceTypes.isEmpty())
155+
{
156+
for (Class<S> type : serviceTypes) {
157+
loadClass(type); // TODO lb3 test this now
158+
}
159+
}
160+
}
161+
164162
}
165163

166164
private List<URL> loadServiceFiles()
@@ -232,6 +230,11 @@ private String trim(String line)
232230
private void loadService(final String serviceClassName)
233231
{
234232
Class<? extends S> serviceClass = loadClass(serviceClassName);
233+
loadClass(serviceClass);
234+
}
235+
236+
private void loadClass(final Class<? extends S> serviceClass)
237+
{
235238
if (serviceClass == null)
236239
{
237240
return;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.List;
25+
26+
import com.ocpsoft.common.pattern.Weighted;
27+
28+
/**
29+
* Used to provide provide pre-enriched services to the {@link ServiceLoader} as an alternate method of
30+
*
31+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
32+
*
33+
*/
34+
public interface ServiceProvider extends Weighted
35+
{
36+
public <T> List<T> getServices(Class<T> type);
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ocpsoft.common.spi;
17+
18+
import java.util.Collection;
19+
20+
import com.ocpsoft.common.services.ServiceLoader;
21+
22+
/**
23+
* Locates additional service implementation classes to be instantiated by the {@link ServiceLoader}
24+
*
25+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
26+
*/
27+
public interface ServiceLocator
28+
{
29+
/**
30+
* Locate additional class types of which can be produced by this {@link ServiceLocator}. If no services could be
31+
* located, this method must return an empty collection.
32+
*/
33+
<T> Collection<Class<T>> locate(Class<T> type);
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ocpsoft.common.services;
17+
18+
/**
19+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
20+
*
21+
*/
22+
public class DummyServiceImpl2 implements DummyService
23+
{
24+
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.ServiceLocator;
28+
29+
/**
30+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
31+
*
32+
*/
33+
public class MockServiceLocator implements ServiceLocator
34+
{
35+
public static boolean provide = false;
36+
37+
@Override
38+
public <T> Collection<Class<T>> locate(final Class<T> type)
39+
{
40+
if (provide)
41+
{
42+
Collection result = new ArrayList();
43+
result.add(DummyServiceImpl2.class);
44+
return result;
45+
}
46+
return null;
47+
}
48+
49+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.junit.Test;
2323

24-
import com.ocpsoft.common.services.ServiceLoader;
2524
import com.ocpsoft.common.util.Iterators;
2625

2726
/**
@@ -36,6 +35,17 @@ public void test()
3635
{
3736
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
3837
List<DummyService> list = Iterators.asList(services);
39-
Assert.assertFalse(list.isEmpty());
38+
Assert.assertEquals(1, list.size());
39+
}
40+
41+
@Test
42+
@SuppressWarnings("unchecked")
43+
public void testWithProviderExtension()
44+
{
45+
MockServiceLocator.provide = true;
46+
ServiceLoader<DummyService> services = ServiceLoader.load(DummyService.class);
47+
List<DummyService> list = Iterators.asList(services);
48+
Assert.assertEquals(2, list.size());
49+
MockServiceLocator.provide = false;
4050
}
4151
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.ocpsoft.common.services.MockServiceLocator

0 commit comments

Comments
 (0)