Skip to content

Commit f306244

Browse files
committed
Implementation of SpringServiceLocator
1 parent b058bcd commit f306244

4 files changed

Lines changed: 78 additions & 8 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.rewrite.spring;
17+
18+
import java.util.Collection;
19+
import java.util.HashSet;
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
import org.springframework.web.context.ContextLoader;
24+
import org.springframework.web.context.WebApplicationContext;
25+
26+
import com.ocpsoft.common.spi.ServiceLocator;
27+
28+
/**
29+
* {@link ServiceLocator} implementation for Spring.
30+
*
31+
* @author Christian Kaltepoth
32+
*/
33+
public class SpringServiceLocator implements ServiceLocator {
34+
35+
@Override
36+
@SuppressWarnings("unchecked")
37+
public <T> Collection<Class<T>> locate(Class<T> clazz) {
38+
39+
Set<Class<T>> result = new HashSet<Class<T>>();
40+
41+
// use the Spring API to obtain the WebApplicationContext
42+
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
43+
44+
// may be null if Spring hasn't started yet
45+
if (context != null) {
46+
47+
// ask spring about SPI implementations
48+
Map<String, T> beans = context.getBeansOfType(clazz);
49+
50+
// add the implementations Class objects to the result set
51+
for (T type : beans.values()) {
52+
result.add((Class<T>) type.getClass());
53+
}
54+
55+
}
56+
57+
return result;
58+
}
59+
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.ocpsoft.rewrite.spring.SpringServiceLocator

‎integration-spring/src/test/java/com/ocpsoft/rewrite/spring/SpringFeaturesConfigProvider.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import javax.servlet.ServletContext;
1919

20+
import org.springframework.stereotype.Component;
21+
2022
import com.ocpsoft.rewrite.bind.El;
2123
import com.ocpsoft.rewrite.config.Configuration;
2224
import com.ocpsoft.rewrite.config.ConfigurationBuilder;
@@ -29,6 +31,7 @@
2931
/**
3032
* @author Christian Kaltepoth
3133
*/
34+
@Component
3235
public class SpringFeaturesConfigProvider extends HttpConfigurationProvider
3336
{
3437

‎integration-spring/src/test/java/com/ocpsoft/rewrite/spring/SpringFeaturesTest.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,30 @@
2626
import org.jboss.shrinkwrap.api.spec.WebArchive;
2727
import org.junit.Test;
2828
import org.junit.runner.RunWith;
29+
import org.springframework.web.context.WebApplicationContext;
2930

3031
import com.ocpsoft.common.spi.ServiceEnricher;
31-
import com.ocpsoft.rewrite.config.ConfigurationProvider;
32+
import com.ocpsoft.common.spi.ServiceLocator;
3233
import com.ocpsoft.rewrite.spi.ExpressionLanguageProvider;
3334
import com.ocpsoft.rewrite.test.HttpAction;
3435
import com.ocpsoft.rewrite.test.RewriteTestBase;
3536

3637
/**
38+
* <p>
39+
* This tests the basic features of the Spring integration module:
40+
* </p>
41+
*
42+
* <ul>
43+
* <li>SpringExpressionLanguageProvider: Binding parameters to Spring beans</li>
44+
* <li>SpringServiceEnricher: {@link SpringExpressionLanguageProvider} requires {@link WebApplicationContext}</li>
45+
* <li>SpringServiceLocator: Automatic discovery of {@link SpringFeaturesConfigProvider}</li>
46+
* </ul>
47+
*
3748
* @author Christian Kaltepoth
3849
*/
3950
@RunWith(Arquillian.class)
4051
public class SpringFeaturesTest extends RewriteTestBase {
52+
4153
@Deployment(testable = false)
4254
public static WebArchive getDeployment() {
4355
return ShrinkWrap
@@ -46,13 +58,7 @@ public static WebArchive getDeployment() {
4658
.setWebXML("jetty-web-spring.xml")
4759
.addAsResource("jetty-log4j.xml", ArchivePaths.create("/log4j.xml"))
4860
.addAsWebInfResource("applicationContext.xml")
49-
.addPackages(true, SpringRoot.class.getPackage())
50-
.addAsResource(new StringAsset(SpringFeaturesConfigProvider.class.getName()),
51-
"/META-INF/services/" + ConfigurationProvider.class.getName())
52-
.addAsResource(new StringAsset(SpringServiceEnricher.class.getName()),
53-
"/META-INF/services/" + ServiceEnricher.class.getName())
54-
.addAsResource(new StringAsset(SpringExpressionLanguageProvider.class.getName()),
55-
"/META-INF/services/" + ExpressionLanguageProvider.class.getName());
61+
.addPackages(true, SpringRoot.class.getPackage());
5662
}
5763

5864
@Test

0 commit comments

Comments
 (0)