Skip to content

Commit 8e507ea

Browse files
committed
Merge pull request ocpsoft#9 from chkal/spring-service-locator
ServiceLocator for Spring
2 parents 6b8ccb4 + 9527b30 commit 8e507ea

4 files changed

Lines changed: 77 additions & 11 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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,30 @@
2222
import org.jboss.arquillian.junit.Arquillian;
2323
import org.jboss.shrinkwrap.api.ArchivePaths;
2424
import org.jboss.shrinkwrap.api.ShrinkWrap;
25-
import org.jboss.shrinkwrap.api.asset.StringAsset;
2625
import org.jboss.shrinkwrap.api.spec.WebArchive;
2726
import org.junit.Test;
2827
import org.junit.runner.RunWith;
28+
import org.springframework.web.context.WebApplicationContext;
2929

30-
import com.ocpsoft.common.spi.ServiceEnricher;
31-
import com.ocpsoft.rewrite.config.ConfigurationProvider;
32-
import com.ocpsoft.rewrite.spi.ExpressionLanguageProvider;
3330
import com.ocpsoft.rewrite.test.HttpAction;
3431
import com.ocpsoft.rewrite.test.RewriteTestBase;
3532

3633
/**
34+
* <p>
35+
* This tests the basic features of the Spring integration module:
36+
* </p>
37+
*
38+
* <ul>
39+
* <li>SpringExpressionLanguageProvider: Binding parameters to Spring beans</li>
40+
* <li>SpringServiceEnricher: {@link SpringExpressionLanguageProvider} requires {@link WebApplicationContext}</li>
41+
* <li>SpringServiceLocator: Automatic discovery of {@link SpringFeaturesConfigProvider}</li>
42+
* </ul>
43+
*
3744
* @author Christian Kaltepoth
3845
*/
3946
@RunWith(Arquillian.class)
4047
public class SpringFeaturesTest extends RewriteTestBase {
48+
4149
@Deployment(testable = false)
4250
public static WebArchive getDeployment() {
4351
return ShrinkWrap
@@ -46,13 +54,7 @@ public static WebArchive getDeployment() {
4654
.setWebXML("jetty-web-spring.xml")
4755
.addAsResource("jetty-log4j.xml", ArchivePaths.create("/log4j.xml"))
4856
.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());
57+
.addPackages(true, SpringRoot.class.getPackage());
5658
}
5759

5860
@Test

0 commit comments

Comments
 (0)