Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-4827 Not fully initialized ObjectFactory tries to create beans #153

Merged
merged 1 commit into from
Aug 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ObjectFactory implements Serializable {
private static final Logger LOG = LogManager.getLogger(ObjectFactory.class);

private transient ClassLoader ccl;
private Container container;
private final Container container;

private ActionFactory actionFactory;
private ResultFactory resultFactory;
Expand All @@ -58,15 +58,15 @@ public class ObjectFactory implements Serializable {
private ConverterFactory converterFactory;
private UnknownHandlerFactory unknownHandlerFactory;

@Inject
public ObjectFactory(Container container) {
this.container = container;
}

@Inject(value="objectFactory.classloader", required=false)
public void setClassLoader(ClassLoader cl) {
this.ccl = cl;
}

@Inject
public void setContainer(Container container) {
this.container = container;
}

@Inject
public void setActionFactory(ActionFactory actionFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.opensymphony.xwork2.spring;

import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -72,6 +73,11 @@ public void setEnableAopSupport(String enableAopSupport) {
this.enableAopSupport = BooleanUtils.toBoolean(enableAopSupport);
}

@Inject
public SpringObjectFactory(Container container) {
super(container);
}

/**
* Set the Spring ApplicationContext that should be used to look beans up with.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;

import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -38,6 +41,11 @@ public class SpringProxyableObjectFactory extends SpringObjectFactory {

private List<String> skipBeanNames = new ArrayList<>();

@Inject
public SpringProxyableObjectFactory(Container container) {
super(container);
}

@Override
public Object buildBean(String beanName, Map<String, Object> extraContext) throws Exception {
LOG.debug("Building bean for name {}", beanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void setAutowireStrategy(Integer autowireStrategy) {
LOG.warn("ApplicationContext could not be found. Action classes will not be autowired.");
} else {
setApplicationContext(applicationContext);
factory = new SpringObjectFactory();
factory = new SpringObjectFactory(ActionContext.getContext().getContainer());
factory.setApplicationContext(getApplicationContext());
if (autowireStrategy != null) {
factory.setAutowireStrategy(autowireStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import java.lang.reflect.Proxy;
import java.util.Map;

import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;

/**
* ObjectFactory that returns a FooProxy in the buildBean if the clazz is FooAction
*/
public class ProxyObjectFactory extends ObjectFactory {

@Inject
public ProxyObjectFactory(Container container) {
super(container);
}

/**
* It returns an instance of the bean except if the class is FooAction.
* In this case, it returns a FooProxy of it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ScopedModelDrivenInterceptorTest extends XWorkTestCase {
public void setUp() throws Exception {
super.setUp();
inter = new ScopedModelDrivenInterceptor();
inter.setObjectFactory(new ProxyObjectFactory());
inter.setObjectFactory(new ProxyObjectFactory(container));
}

public void testResolveModel() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public void testConfigurationManager() {

public void testObjectFactoryDestroy() throws Exception {

final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), cm);
Mock mockConfiguration = new Mock(Configuration.class);
Expand All @@ -231,6 +230,7 @@ public void testObjectFactoryDestroy() throws Exception {
String reloadConfigs = container.getInstance(String.class, XWorkConstants.RELOAD_XML_CONFIGURATION);
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(XWorkConstants.RELOAD_XML_CONFIGURATION)),
reloadConfigs);
final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory((Container) mockContainer.proxy());
mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);

mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
Expand Down Expand Up @@ -261,7 +261,7 @@ public void testInterceptorDestroy() throws Exception {
packageConfigs.put("test", packageConfig);

Mock mockContainer = new Mock(Container.class);
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory((Container) mockContainer.proxy()));
String reloadConfigs = container.getInstance(String.class, XWorkConstants.RELOAD_XML_CONFIGURATION);
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(XWorkConstants.RELOAD_XML_CONFIGURATION)),
reloadConfigs);
Expand Down Expand Up @@ -316,6 +316,10 @@ class DispatcherListenerState {
public static class InnerDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
public boolean destroyed = false;

public InnerDestroyableObjectFactory(Container container) {
super(container);
}

public void destroy() {
destroyed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.struts2.cdi;

import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -73,8 +74,9 @@ public void setJndiKey( String jndiKey ) {

Map<Class<?>, InjectionTarget<?>> injectionTargetCache = new ConcurrentHashMap<Class<?>, InjectionTarget<?>>();

public CdiObjectFactory() {
super();
@Inject
public CdiObjectFactory(Container container) {
super(container);
LOG.info("Initializing Struts2 CDI integration...");
this.beanManager = findBeanManager();
if (beanManager != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public void setUp() throws Exception {

@Test
public void testFindBeanManager() throws Exception {
assertNotNull(new CdiObjectFactory().findBeanManager());
assertNotNull(new CdiObjectFactory(null).findBeanManager());
}

@Test
public void testGetBean() throws Exception {
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
FooConsumer fooConsumer = (FooConsumer) cdiObjectFactory.buildBean(FooConsumer.class.getCanonicalName(), null, false);
assertNotNull(fooConsumer);
assertNotNull(fooConsumer.fooService);
}

@Test public void testGetInjectionTarget() throws Exception {
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
final InjectionTarget<?> injectionTarget = cdiObjectFactory.getInjectionTarget(FooConsumer.class);
assertNotNull(injectionTarget);
assertTrue(cdiObjectFactory.injectionTargetCache.containsKey(FooConsumer.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public Container getContainer() {
configuration.addPackageConfig("class-level", classLevelParentPkg);

ActionNameBuilder actionNameBuilder = new SEOActionNameBuilder("true", "-");
ObjectFactory of = new ObjectFactory();
ObjectFactory of = new ObjectFactory(mockContainer);
DefaultInterceptorMapBuilder interceptorBuilder = new DefaultInterceptorMapBuilder();
interceptorBuilder.setConfiguration(configuration);

Expand Down Expand Up @@ -778,8 +778,9 @@ public <T> T getInstance(Class<T> type) {
if (type == FileManagerFactory.class) {
return (T) fileManagerFactory;
}
T obj = type.newInstance();
if (obj instanceof ObjectFactory) {
T obj;
if (type == ObjectFactory.class) {
obj = type.getConstructor(Container.class).newInstance(this);
OgnlReflectionProvider rp = new OgnlReflectionProvider() {

@Override
Expand Down Expand Up @@ -809,6 +810,8 @@ public void setProperty(String name, Object value, Object o, Map<String, Object>

((ObjectFactory) obj).setInterceptorFactory(dif);
((ObjectFactory) obj).setResultFactory(drf);
} else {
obj = type.newInstance();
}
return obj;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class DelegatingObjectFactory extends ObjectFactory implements ObjectFact
private BundleAccessor bundleResourceLoader;
private OsgiConfigurationProvider osgiConfigurationProvider;

@Inject
public DelegatingObjectFactory(Container container) {
super(container);
}

@Inject
public void setDelegateObjectFactory(@Inject Container container,
@Inject("struts.objectFactory.delegate") String delegate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.osgi.framework.ServiceReference;

Expand All @@ -38,6 +39,11 @@ public class SpringOsgiObjectFactory extends ObjectFactory {

private BundleAccessor bundleAccessor;

@Inject
public SpringOsgiObjectFactory(Container container) {
super(container);
}

public Object buildBean(String className, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
if (containsBean(className))
return getBean(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.InterceptorConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.Interceptor;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -80,6 +81,11 @@ public class PlexusObjectFactory extends ObjectFactory {
private PlexusContainer base;
private ReflectionProvider reflectionProvider;

@Inject
public PlexusObjectFactory(Container container) {
super(container);
}

@Inject
public void setReflectionProvider(ReflectionProvider reflectionProvider) {
this.reflectionProvider = reflectionProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public StrutsSpringObjectFactory(
@Inject(StrutsConstants.STRUTS_DEVMODE) String devMode,
@Inject Container container) {

super();
super(container);
boolean useClassCache = BooleanUtils.toBoolean(useClassCacheStr);
LOG.info("Initializing Struts-Spring integration...");

Expand Down