Skip to content

Commit 6f91d07

Browse files
committed
Inject Container in constructor of the ObjectFactory
1 parent f5ade7d commit 6f91d07

File tree

14 files changed

+67
-19
lines changed

14 files changed

+67
-19
lines changed

core/src/main/java/com/opensymphony/xwork2/ObjectFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class ObjectFactory implements Serializable {
4949
private static final Logger LOG = LogManager.getLogger(ObjectFactory.class);
5050

5151
private transient ClassLoader ccl;
52-
private Container container;
52+
private final Container container;
5353

5454
private ActionFactory actionFactory;
5555
private ResultFactory resultFactory;
@@ -58,15 +58,15 @@ public class ObjectFactory implements Serializable {
5858
private ConverterFactory converterFactory;
5959
private UnknownHandlerFactory unknownHandlerFactory;
6060

61+
@Inject
62+
public ObjectFactory(Container container) {
63+
this.container = container;
64+
}
65+
6166
@Inject(value="objectFactory.classloader", required=false)
6267
public void setClassLoader(ClassLoader cl) {
6368
this.ccl = cl;
6469
}
65-
66-
@Inject
67-
public void setContainer(Container container) {
68-
this.container = container;
69-
}
7070

7171
@Inject
7272
public void setActionFactory(ActionFactory actionFactory) {

core/src/main/java/com/opensymphony/xwork2/spring/SpringObjectFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.opensymphony.xwork2.spring;
1717

1818
import com.opensymphony.xwork2.ObjectFactory;
19+
import com.opensymphony.xwork2.inject.Container;
1920
import com.opensymphony.xwork2.inject.Inject;
2021
import org.apache.commons.lang3.BooleanUtils;
2122
import org.apache.logging.log4j.LogManager;
@@ -72,6 +73,11 @@ public void setEnableAopSupport(String enableAopSupport) {
7273
this.enableAopSupport = BooleanUtils.toBoolean(enableAopSupport);
7374
}
7475

76+
@Inject
77+
public SpringObjectFactory(Container container) {
78+
super(container);
79+
}
80+
7581
/**
7682
* Set the Spring ApplicationContext that should be used to look beans up with.
7783
*

core/src/main/java/com/opensymphony/xwork2/spring/SpringProxyableObjectFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.springframework.beans.factory.support.RootBeanDefinition;
2424
import org.springframework.context.ApplicationContext;
2525

26+
import com.opensymphony.xwork2.inject.Container;
27+
import com.opensymphony.xwork2.inject.Inject;
28+
2629
import java.util.ArrayList;
2730
import java.util.List;
2831
import java.util.Map;
@@ -38,6 +41,11 @@ public class SpringProxyableObjectFactory extends SpringObjectFactory {
3841

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

44+
@Inject
45+
public SpringProxyableObjectFactory(Container container) {
46+
super(container);
47+
}
48+
4149
@Override
4250
public Object buildBean(String beanName, Map<String, Object> extraContext) throws Exception {
4351
LOG.debug("Building bean for name {}", beanName);

core/src/main/java/com/opensymphony/xwork2/spring/interceptor/ActionAutowiringInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void setAutowireStrategy(Integer autowireStrategy) {
106106
LOG.warn("ApplicationContext could not be found. Action classes will not be autowired.");
107107
} else {
108108
setApplicationContext(applicationContext);
109-
factory = new SpringObjectFactory();
109+
factory = new SpringObjectFactory(ActionContext.getContext().getContainer());
110110
factory.setApplicationContext(getApplicationContext());
111111
if (autowireStrategy != null) {
112112
factory.setAutowireStrategy(autowireStrategy);

core/src/test/java/com/opensymphony/xwork2/ProxyObjectFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
import java.lang.reflect.Proxy;
66
import java.util.Map;
77

8+
import com.opensymphony.xwork2.inject.Container;
9+
import com.opensymphony.xwork2.inject.Inject;
10+
811
/**
912
* ObjectFactory that returns a FooProxy in the buildBean if the clazz is FooAction
1013
*/
1114
public class ProxyObjectFactory extends ObjectFactory {
1215

16+
@Inject
17+
public ProxyObjectFactory(Container container) {
18+
super(container);
19+
}
20+
1321
/**
1422
* It returns an instance of the bean except if the class is FooAction.
1523
* In this case, it returns a FooProxy of it.

core/src/test/java/com/opensymphony/xwork2/interceptor/ScopedModelDrivenInterceptorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ScopedModelDrivenInterceptorTest extends XWorkTestCase {
3636
public void setUp() throws Exception {
3737
super.setUp();
3838
inter = new ScopedModelDrivenInterceptor();
39-
inter.setObjectFactory(new ProxyObjectFactory());
39+
inter.setObjectFactory(new ProxyObjectFactory(container));
4040
}
4141

4242
public void testResolveModel() throws Exception {

core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ public void testConfigurationManager() {
221221

222222
public void testObjectFactoryDestroy() throws Exception {
223223

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

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

263263
Mock mockContainer = new Mock(Container.class);
264-
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
264+
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory((Container) mockContainer.proxy()));
265265
String reloadConfigs = container.getInstance(String.class, XWorkConstants.RELOAD_XML_CONFIGURATION);
266266
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(XWorkConstants.RELOAD_XML_CONFIGURATION)),
267267
reloadConfigs);
@@ -316,6 +316,10 @@ class DispatcherListenerState {
316316
public static class InnerDestroyableObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
317317
public boolean destroyed = false;
318318

319+
public InnerDestroyableObjectFactory(Container container) {
320+
super(container);
321+
}
322+
319323
public void destroy() {
320324
destroyed = true;
321325
}

plugins/cdi/src/main/java/org/apache/struts2/cdi/CdiObjectFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.struts2.cdi;
2121

2222
import com.opensymphony.xwork2.ObjectFactory;
23+
import com.opensymphony.xwork2.inject.Container;
2324
import com.opensymphony.xwork2.inject.Inject;
2425
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Logger;
@@ -73,8 +74,9 @@ public void setJndiKey( String jndiKey ) {
7374

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

76-
public CdiObjectFactory() {
77-
super();
77+
@Inject
78+
public CdiObjectFactory(Container container) {
79+
super(container);
7880
LOG.info("Initializing Struts2 CDI integration...");
7981
this.beanManager = findBeanManager();
8082
if (beanManager != null) {

plugins/cdi/src/test/java/org/apache/struts2/cdi/CdiObjectFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public void setUp() throws Exception {
2727

2828
@Test
2929
public void testFindBeanManager() throws Exception {
30-
assertNotNull(new CdiObjectFactory().findBeanManager());
30+
assertNotNull(new CdiObjectFactory(null).findBeanManager());
3131
}
3232

3333
@Test
3434
public void testGetBean() throws Exception {
35-
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
35+
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
3636
FooConsumer fooConsumer = (FooConsumer) cdiObjectFactory.buildBean(FooConsumer.class.getCanonicalName(), null, false);
3737
assertNotNull(fooConsumer);
3838
assertNotNull(fooConsumer.fooService);
3939
}
4040

4141
@Test public void testGetInjectionTarget() throws Exception {
42-
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory();
42+
final CdiObjectFactory cdiObjectFactory = new CdiObjectFactory(null);
4343
final InjectionTarget<?> injectionTarget = cdiObjectFactory.getInjectionTarget(FooConsumer.class);
4444
assertNotNull(injectionTarget);
4545
assertTrue(cdiObjectFactory.injectionTargetCache.containsKey(FooConsumer.class));

plugins/convention/src/test/java/org/apache/struts2/convention/PackageBasedActionConfigBuilderTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public Container getContainer() {
338338
configuration.addPackageConfig("class-level", classLevelParentPkg);
339339

340340
ActionNameBuilder actionNameBuilder = new SEOActionNameBuilder("true", "-");
341-
ObjectFactory of = new ObjectFactory();
341+
ObjectFactory of = new ObjectFactory(mockContainer);
342342
DefaultInterceptorMapBuilder interceptorBuilder = new DefaultInterceptorMapBuilder();
343343
interceptorBuilder.setConfiguration(configuration);
344344

@@ -778,8 +778,9 @@ public <T> T getInstance(Class<T> type) {
778778
if (type == FileManagerFactory.class) {
779779
return (T) fileManagerFactory;
780780
}
781-
T obj = type.newInstance();
782-
if (obj instanceof ObjectFactory) {
781+
T obj;
782+
if (type == ObjectFactory.class) {
783+
obj = type.getConstructor(Container.class).newInstance(this);
783784
OgnlReflectionProvider rp = new OgnlReflectionProvider() {
784785

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

810811
((ObjectFactory) obj).setInterceptorFactory(dif);
811812
((ObjectFactory) obj).setResultFactory(drf);
813+
} else {
814+
obj = type.newInstance();
812815
}
813816
return obj;
814817
} catch (Exception e) {

0 commit comments

Comments
 (0)