-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spring): support thread-safe Spring Boot WebApplicationContext i…
…nitialization and lookup
- Loading branch information
1 parent
2b89ac0
commit e61a336
Showing
8 changed files
with
171 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,37 +16,41 @@ | |
package org.ocpsoft.rewrite.spring; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.ocpsoft.logging.Logger; | ||
import org.ocpsoft.rewrite.el.spi.BeanNameResolver; | ||
import org.springframework.beans.factory.ListableBeanFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.context.ContextLoader; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
/** | ||
* {@link BeanNameResolver} implementation for Spring. | ||
* | ||
* @author Christian Kaltepoth | ||
* @author <a href="mailto:[email protected]">Lincoln Baxter, III</a> | ||
*/ | ||
public class SpringBeanNameResolver implements BeanNameResolver | ||
{ | ||
|
||
private final Logger log = Logger.getLogger(SpringBeanNameResolver.class); | ||
|
||
@Autowired | ||
private WebApplicationContext applicationContext; | ||
|
||
@Override | ||
public String getBeanName(Class<?> clazz) | ||
{ | ||
|
||
// try to obtain the WebApplicationContext using ContextLoader | ||
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); | ||
if (context == null) { | ||
throw new IllegalStateException("Unable to get current WebApplicationContext"); | ||
if (applicationContext == null) { | ||
applicationContext = ContextLoader.getCurrentWebApplicationContext(); | ||
if (applicationContext == null) { | ||
throw new IllegalStateException("Unable to get current WebApplicationContext"); | ||
} | ||
} | ||
|
||
// obtain a map of bean names | ||
Set<String> beanNames = resolveBeanNames(context, clazz); | ||
Set<String> beanNames = resolveBeanNames(applicationContext, clazz); | ||
|
||
// no beans of that type, nothing we can do | ||
if (beanNames == null || beanNames.size() == 0) { | ||
|
@@ -76,15 +80,14 @@ private Set<String> resolveBeanNames(ListableBeanFactory beanFactory, Class<?> c | |
|
||
final Set<String> result = new HashSet<String>(); | ||
|
||
Map<String, ?> beanMap = beanFactory.getBeansOfType(clazz); | ||
if (beanMap != null) { | ||
for (String name : beanMap.keySet()) { | ||
String[] names = beanFactory.getBeanNamesForType(clazz); | ||
if (names != null) { | ||
for (String name : names) { | ||
if (name != null && !name.startsWith("scopedTarget.")) { | ||
result.add(name); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,31 +21,29 @@ | |
import java.util.Set; | ||
|
||
import org.ocpsoft.common.spi.ServiceLocator; | ||
import org.springframework.web.context.ContextLoader; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
/** | ||
* {@link ServiceLocator} implementation for Spring. | ||
* | ||
* @author Christian Kaltepoth | ||
* @author <a href="mailto:[email protected]">Lincoln Baxter, III</a> | ||
*/ | ||
public class SpringServiceLocator implements ServiceLocator | ||
{ | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> Collection<Class<T>> locate(Class<T> clazz) | ||
{ | ||
Set<Class<T>> result = new LinkedHashSet<Class<T>>(); | ||
|
||
// use the Spring API to obtain the WebApplicationContext | ||
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); | ||
WebApplicationContext applicationContext = SpringServletContextLoader.findCurrentApplicationContext(); | ||
|
||
// may be null if Spring hasn't started yet | ||
if (context != null) { | ||
if (applicationContext != null) { | ||
|
||
// ask spring about SPI implementations | ||
Map<String, T> beans = context.getBeansOfType(clazz); | ||
Map<String, T> beans = applicationContext.getBeansOfType(clazz); | ||
|
||
// add the implementations Class objects to the result set | ||
for (T type : beans.values()) { | ||
|
88 changes: 88 additions & 0 deletions
88
integration-spring/src/main/java/org/ocpsoft/rewrite/spring/SpringServletContextLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2011 <a href="mailto:[email protected]">Lincoln Baxter, III</a> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.ocpsoft.rewrite.spring; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.ocpsoft.rewrite.servlet.spi.ContextListener; | ||
import org.springframework.web.context.ContextLoader; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
import org.springframework.web.context.support.WebApplicationContextUtils; | ||
|
||
import jakarta.servlet.ServletContext; | ||
import jakarta.servlet.ServletContextEvent; | ||
|
||
/** | ||
* Thread-safe {@link ServletContext} loader implementation for Spring. | ||
* | ||
* @author <a href="mailto:[email protected]">Lincoln Baxter, III</a> | ||
*/ | ||
public class SpringServletContextLoader implements ContextListener | ||
{ | ||
private static final Map<ClassLoader, ServletContext> contextMap = new ConcurrentHashMap<>(1); | ||
|
||
@Override | ||
public void contextInitialized(ServletContextEvent event) | ||
{ | ||
ServletContext servletContext = event.getServletContext(); | ||
contextMap.put(Thread.currentThread().getContextClassLoader(), servletContext); | ||
contextMap.put(servletContext.getClassLoader(), servletContext); | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent event) | ||
{ | ||
ServletContext context = event.getServletContext(); | ||
contextMap.entrySet().removeIf(entry -> entry.getValue() == context); | ||
} | ||
|
||
public static ServletContext findCurrentServletContext() | ||
{ | ||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); | ||
|
||
if (requestAttributes instanceof ServletRequestAttributes) { | ||
return ((ServletRequestAttributes) requestAttributes).getRequest().getServletContext(); | ||
} | ||
|
||
return contextMap.get(Thread.currentThread().getContextClassLoader()); | ||
} | ||
|
||
public static WebApplicationContext findCurrentApplicationContext() | ||
{ | ||
ServletContext currentServletContext = findCurrentServletContext(); | ||
|
||
if (currentServletContext != null) { | ||
WebApplicationContext webApplicationContext = WebApplicationContextUtils | ||
.findWebApplicationContext(currentServletContext); | ||
if (webApplicationContext != null) { | ||
return webApplicationContext; | ||
} | ||
} | ||
|
||
return ContextLoader.getCurrentWebApplicationContext(); | ||
} | ||
|
||
@Override | ||
public int priority() | ||
{ | ||
return 0; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...ring/src/main/resources/META-INF/services/org.ocpsoft.rewrite.servlet.spi.ContextListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.ocpsoft.rewrite.spring.SpringServletContextLoader |
1 change: 1 addition & 0 deletions
1
...ring/src/main/resources/META-INF/services/org.ocpsoft.rewrite.servlet.spi.RequestListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.ocpsoft.rewrite.spring.SpringServletContextLoader |