Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
- Fix for issue 222
Browse files Browse the repository at this point in the history
  https://jersey.dev.java.net/issues/show_bug.cgi?id=222
  The Spring integration now correctly supports configurations with
  hierarchical application contexts.
  • Loading branch information
PaulSandoz committed Mar 16, 2009
1 parent 2e9a624 commit 285f913
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
14 changes: 11 additions & 3 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
release 1.0.3 (2009-03/04)
- Added ConnectionListenerFilter
ConnectionListenerFilter provides informations about proceeding requests and
responses.
- Fix for issue 222
https://jersey.dev.java.net/issues/show_bug.cgi?id=222
The Spring integration now correctly supports configurations with
hierarchical application contexts.
- Fix for issue 39
https://jersey.dev.java.net/issues/show_bug.cgi?id=39
Listening to and getting feedback from the client making requests and
receiving responses is now possible using the filter:
com.sun.jersey.api.client.filter.ConnectionListenerFilter
Such a filter can be utilized to obtain progress for the sending
and recieving of large documents.
- Fixed issue 181
https://jersey.dev.java.net/issues/show_bug.cgi?id=181
NotFoundException construction now includes an optional URI that was not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ClassUtils;
Expand All @@ -60,7 +63,6 @@
import com.sun.jersey.spi.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.config.BeanDefinition;

/**
* The Spring-based {@link IoCComponentProviderFactory}.
Expand All @@ -73,7 +75,7 @@
public class SpringComponentProviderFactory implements IoCComponentProviderFactory {

private static final Logger LOGGER = Logger.getLogger(SpringComponentProviderFactory.class.getName());

private final ConfigurableApplicationContext springContext;

public SpringComponentProviderFactory(ResourceConfig rc, ConfigurableApplicationContext springContext) {
Expand All @@ -82,16 +84,16 @@ public SpringComponentProviderFactory(ResourceConfig rc, ConfigurableApplication
}

private void register(ResourceConfig rc, ConfigurableApplicationContext springContext) {
String[] names = springContext.getBeanDefinitionNames();
String[] names = BeanFactoryUtils.beanNamesIncludingAncestors(springContext);
for (String name : names) {
Class<?> type = ClassUtils.getUserClass( springContext.getType(name) );
Class<?> type = ClassUtils.getUserClass(springContext.getType(name));
if (ResourceConfig.isProviderClass(type)) {
LOGGER.info("Registering Spring bean, " + name +
LOGGER.info("Registering Spring bean, " + name +
", of type " + type.getName() +
" as a provider class");
rc.getClasses().add(type);
} else if (ResourceConfig.isRootResourceClass(type)) {
LOGGER.info("Registering Spring bean, " + name +
LOGGER.info("Registering Spring bean, " + name +
", of type " + type.getName() +
" as a root resource class");
rc.getClasses().add(type);
Expand All @@ -104,7 +106,7 @@ public IoCComponentProvider getComponentProvider(Class c) {
}

public IoCComponentProvider getComponentProvider(ComponentContext cc, Class c) {
final Autowire autowire = (Autowire)c.getAnnotation(Autowire.class);
final Autowire autowire = (Autowire) c.getAnnotation(Autowire.class);
if (autowire != null) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("Creating resource class " +
Expand All @@ -115,23 +117,50 @@ public IoCComponentProvider getComponentProvider(ComponentContext cc, Class c) {
}
return new SpringInstantiatedComponentProvider(c, autowire);
}

final String beanName = getBeanName(cc, c, springContext);
if (beanName == null) {
return null;
}

final String scope = springContext.getBeanFactory().
getBeanDefinition(beanName).getScope();
final String scope = findBeanDefinition(beanName).getScope();
return new SpringManagedComponentProvider(getComponentScope(scope), beanName, c);
}

/**
* Fine the bean definition from a given context or from any of the parent
* contexts.
*
* @param beanName the bean name.
* @return the bean definition.
* @throws NoSuchBeanDefinitionException if the bean definition could not
* be found.
*/
private BeanDefinition findBeanDefinition(String beanName) {
ConfigurableApplicationContext current = springContext;
BeanDefinition beanDef = null;
do {
try {
return current.getBeanFactory().getBeanDefinition(beanName);
} catch (NoSuchBeanDefinitionException e) {
final ApplicationContext parent = current.getParent();
if (parent != null && parent instanceof ConfigurableApplicationContext) {
current = (ConfigurableApplicationContext) parent;
} else {
throw e;
}
}
} while (beanDef == null && current != null);
return beanDef;
}

private ComponentScope getComponentScope(String scope) {
ComponentScope cs = scopeMap.get(scope);
return (cs != null) ? cs : ComponentScope.Undefined;
}

private final Map<String, ComponentScope> scopeMap = createScopeMap();

private final Map<String, ComponentScope> scopeMap = createScopeMap();

private Map<String, ComponentScope> createScopeMap() {
Map<String, ComponentScope> m = new HashMap<String, ComponentScope>();
m.put(BeanDefinition.SCOPE_SINGLETON, ComponentScope.Singleton);
Expand All @@ -141,6 +170,7 @@ private Map<String, ComponentScope> createScopeMap() {
}

private class SpringInstantiatedComponentProvider implements IoCInstantiatedComponentProvider {

private final Class c;
private final Autowire a;

Expand All @@ -153,17 +183,18 @@ public Object getInstance() {
return springContext.getBeanFactory().createBean(c,
a.mode().getSpringCode(), a.dependencyCheck());
}

public Object getInjectableInstance(Object o) {
return SpringComponentProviderFactory.getInjectableInstance(o);
}
}

private class SpringManagedComponentProvider implements IoCManagedComponentProvider {

private final ComponentScope scope;
private final String beanName;
private final Class c;

SpringManagedComponentProvider(ComponentScope scope, String beanName, Class c) {
this.scope = scope;
this.beanName = beanName;
Expand All @@ -185,7 +216,7 @@ public Object getInstance() {

private static Object getInjectableInstance(Object o) {
if (AopUtils.isAopProxy(o)) {
final Advised aopResource = (Advised)o;
final Advised aopResource = (Advised) o;
try {
return aopResource.getTargetSource().getTarget();
} catch (Exception e) {
Expand All @@ -210,7 +241,7 @@ private static String getBeanName(ComponentContext cc, Class<?> c, ApplicationCo
}
}

final String names[] = springContext.getBeanNamesForType(c);
final String names[] = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(springContext, c);

if (names.length == 0) {
return null;
Expand Down Expand Up @@ -280,4 +311,4 @@ private static <I> String toCSV(Collection<I> items, String separator, String de
}
return sb.toString();
}
}
}

0 comments on commit 285f913

Please sign in to comment.