From 6f91d0776a545c911ca4f2875ed9976614711ef9 Mon Sep 17 00:00:00 2001 From: Aleksandr Mashchenko Date: Thu, 27 Jul 2017 22:22:33 +0300 Subject: [PATCH] Inject Container in constructor of the ObjectFactory --- .../java/com/opensymphony/xwork2/ObjectFactory.java | 12 ++++++------ .../xwork2/spring/SpringObjectFactory.java | 6 ++++++ .../xwork2/spring/SpringProxyableObjectFactory.java | 8 ++++++++ .../interceptor/ActionAutowiringInterceptor.java | 2 +- .../com/opensymphony/xwork2/ProxyObjectFactory.java | 8 ++++++++ .../ScopedModelDrivenInterceptorTest.java | 2 +- .../apache/struts2/dispatcher/DispatcherTest.java | 8 ++++++-- .../org/apache/struts2/cdi/CdiObjectFactory.java | 6 ++++-- .../org/apache/struts2/cdi/CdiObjectFactoryTest.java | 6 +++--- .../PackageBasedActionConfigBuilderTest.java | 9 ++++++--- .../apache/struts2/osgi/DelegatingObjectFactory.java | 5 +++++ .../apache/struts2/osgi/SpringOsgiObjectFactory.java | 6 ++++++ .../apache/struts2/plexus/PlexusObjectFactory.java | 6 ++++++ .../struts2/spring/StrutsSpringObjectFactory.java | 2 +- 14 files changed, 67 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java index c5ffbb9d41..652ef94a80 100644 --- a/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java @@ -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; @@ -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) { diff --git a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java index 51a47518a2..fffb037cd8 100644 --- a/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java @@ -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; @@ -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. * diff --git a/core/src/main/java/com/opensymphony/xwork2/spring/SpringProxyableObjectFactory.java b/core/src/main/java/com/opensymphony/xwork2/spring/SpringProxyableObjectFactory.java index ced75167c3..b6ec98625e 100644 --- a/core/src/main/java/com/opensymphony/xwork2/spring/SpringProxyableObjectFactory.java +++ b/core/src/main/java/com/opensymphony/xwork2/spring/SpringProxyableObjectFactory.java @@ -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; @@ -38,6 +41,11 @@ public class SpringProxyableObjectFactory extends SpringObjectFactory { private List skipBeanNames = new ArrayList<>(); + @Inject + public SpringProxyableObjectFactory(Container container) { + super(container); + } + @Override public Object buildBean(String beanName, Map extraContext) throws Exception { LOG.debug("Building bean for name {}", beanName); diff --git a/core/src/main/java/com/opensymphony/xwork2/spring/interceptor/ActionAutowiringInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/spring/interceptor/ActionAutowiringInterceptor.java index 16286782c6..b3de475b3e 100644 --- a/core/src/main/java/com/opensymphony/xwork2/spring/interceptor/ActionAutowiringInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/spring/interceptor/ActionAutowiringInterceptor.java @@ -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); diff --git a/core/src/test/java/com/opensymphony/xwork2/ProxyObjectFactory.java b/core/src/test/java/com/opensymphony/xwork2/ProxyObjectFactory.java index 37821c8adc..93200ae5c2 100644 --- a/core/src/test/java/com/opensymphony/xwork2/ProxyObjectFactory.java +++ b/core/src/test/java/com/opensymphony/xwork2/ProxyObjectFactory.java @@ -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. diff --git a/core/src/test/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java b/core/src/test/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java index 9413f7583d..4f44aa2644 100644 --- a/core/src/test/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java @@ -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 { diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index 4f043cb7ba..b920dcc09c 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -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(), cm); Mock mockConfiguration = new Mock(Configuration.class); @@ -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()); @@ -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); @@ -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; } diff --git a/plugins/cdi/src/main/java/org/apache/struts2/cdi/CdiObjectFactory.java b/plugins/cdi/src/main/java/org/apache/struts2/cdi/CdiObjectFactory.java index 91601b3592..b26581cfe7 100644 --- a/plugins/cdi/src/main/java/org/apache/struts2/cdi/CdiObjectFactory.java +++ b/plugins/cdi/src/main/java/org/apache/struts2/cdi/CdiObjectFactory.java @@ -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; @@ -73,8 +74,9 @@ public void setJndiKey( String jndiKey ) { Map, InjectionTarget> injectionTargetCache = new ConcurrentHashMap, InjectionTarget>(); - public CdiObjectFactory() { - super(); + @Inject + public CdiObjectFactory(Container container) { + super(container); LOG.info("Initializing Struts2 CDI integration..."); this.beanManager = findBeanManager(); if (beanManager != null) { diff --git a/plugins/cdi/src/test/java/org/apache/struts2/cdi/CdiObjectFactoryTest.java b/plugins/cdi/src/test/java/org/apache/struts2/cdi/CdiObjectFactoryTest.java index f35ac55637..74c6f024a3 100644 --- a/plugins/cdi/src/test/java/org/apache/struts2/cdi/CdiObjectFactoryTest.java +++ b/plugins/cdi/src/test/java/org/apache/struts2/cdi/CdiObjectFactoryTest.java @@ -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)); diff --git a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java index dfa1ad6223..f6bbbcc1d2 100644 --- a/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java +++ b/plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java @@ -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); @@ -778,8 +778,9 @@ public T getInstance(Class 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 @@ -809,6 +810,8 @@ public void setProperty(String name, Object value, Object o, Map ((ObjectFactory) obj).setInterceptorFactory(dif); ((ObjectFactory) obj).setResultFactory(drf); + } else { + obj = type.newInstance(); } return obj; } catch (Exception e) { diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/DelegatingObjectFactory.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/DelegatingObjectFactory.java index 079e257f12..ed82d1bf92 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/DelegatingObjectFactory.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/DelegatingObjectFactory.java @@ -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) { diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/SpringOsgiObjectFactory.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/SpringOsgiObjectFactory.java index 3cebb328e4..ac596e22e8 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/SpringOsgiObjectFactory.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/SpringOsgiObjectFactory.java @@ -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; @@ -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 extraContext, boolean injectInternal) throws Exception { if (containsBean(className)) return getBean(className); diff --git a/plugins/plexus/src/main/java/org/apache/struts2/plexus/PlexusObjectFactory.java b/plugins/plexus/src/main/java/org/apache/struts2/plexus/PlexusObjectFactory.java index 506850fab7..7131f579d5 100644 --- a/plugins/plexus/src/main/java/org/apache/struts2/plexus/PlexusObjectFactory.java +++ b/plugins/plexus/src/main/java/org/apache/struts2/plexus/PlexusObjectFactory.java @@ -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; @@ -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; diff --git a/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java b/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java index 83b2853414..cd524dd150 100644 --- a/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java +++ b/plugins/spring/src/main/java/org/apache/struts2/spring/StrutsSpringObjectFactory.java @@ -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...");