1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.osgi;
23
24 import java.util.Map;
25
26 import com.opensymphony.xwork2.ObjectFactory;
27 import com.opensymphony.xwork2.config.PackageProvider;
28 import com.opensymphony.xwork2.inject.Container;
29 import com.opensymphony.xwork2.inject.Inject;
30 import org.apache.struts2.util.ObjectFactoryDestroyable;
31
32 public class DelegatingObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {
33 private ObjectFactory delegateObjectFactory;
34 private BundleAccessor bundleResourceLoader;
35 private OsgiConfigurationProvider osgiConfigurationProvider;
36
37 @Inject
38 public void setDelegateObjectFactory(@Inject Container container,
39 @Inject("struts.objectFactory.delegate") String delegate) {
40 if (delegate == null) {
41 delegate = "struts";
42 }
43 delegateObjectFactory = container.getInstance(ObjectFactory.class, delegate);
44 }
45
46 @Inject
47 public void setBundleResourceLoader(BundleAccessor rl) {
48 this.bundleResourceLoader = rl;
49 }
50
51
52 public boolean isNoArgConstructorRequired() {
53 return delegateObjectFactory.isNoArgConstructorRequired();
54 }
55
56 public Object buildBean(Class clazz, Map extraContext) throws Exception {
57 return delegateObjectFactory.buildBean(clazz, extraContext);
58 }
59
60 public Object buildBean(String className, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
61 try {
62 return delegateObjectFactory.buildBean(className, extraContext, injectInternal);
63 } catch (Exception e) {
64 Object object = bundleResourceLoader.loadClass(className).newInstance();
65 if (injectInternal)
66 injectInternalBeans(object);
67 return object;
68 }
69 }
70
71 @Override
72 public Class getClassInstance(String className) throws ClassNotFoundException {
73 try {
74 return delegateObjectFactory.getClassInstance(className);
75 }
76 catch (Exception e) {
77 return bundleResourceLoader.loadClass(className);
78 }
79 }
80
81 public void destroy() {
82 if (osgiConfigurationProvider != null) {
83 osgiConfigurationProvider.destroy();
84 }
85 }
86
87 @Inject("osgi")
88 public void setOsgiConfigurationProvider(PackageProvider osgiConfigurationProvider) {
89 this.osgiConfigurationProvider = (OsgiConfigurationProvider) osgiConfigurationProvider;
90 }
91 }