1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts2.impl;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import com.opensymphony.xwork2.ObjectFactory;
29 import com.opensymphony.xwork2.Result;
30 import com.opensymphony.xwork2.config.ConfigurationException;
31 import com.opensymphony.xwork2.config.entities.InterceptorConfig;
32 import com.opensymphony.xwork2.config.entities.ResultConfig;
33 import com.opensymphony.xwork2.interceptor.Interceptor;
34 import com.opensymphony.xwork2.util.OgnlUtil;
35
36 public class StrutsObjectFactory extends ObjectFactory {
37
38 public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map refParams)
39 throws ConfigurationException {
40 String className = interceptorConfig.getClassName();
41
42 Map<String, String> params = new HashMap<String, String>();
43 Map typeParams = interceptorConfig.getParams();
44 if (typeParams != null && !typeParams.isEmpty())
45 params.putAll(typeParams);
46 if (refParams != null && !refParams.isEmpty())
47 params.putAll(refParams);
48 params.putAll(refParams);
49
50 try {
51
52
53 Object o = buildBean(className, null);
54 OgnlUtil.setProperties(params, o);
55
56 if (o instanceof Interceptor) {
57 Interceptor interceptor = (Interceptor) o;
58 interceptor.init();
59 return interceptor;
60 }
61
62 if (o instanceof org.apache.struts2.spi.Interceptor)
63 return new InterceptorAdapter((org.apache.struts2.spi.Interceptor) o);
64
65 throw new ConfigurationException(
66 "Class [" + className + "] does not implement Interceptor", interceptorConfig);
67 } catch (InstantiationException e) {
68 throw new ConfigurationException(
69 "Unable to instantiate an instance of Interceptor class [" + className + "].",
70 e, interceptorConfig);
71 } catch (IllegalAccessException e) {
72 throw new ConfigurationException(
73 "IllegalAccessException while attempting to instantiate an instance of Interceptor class ["
74 + className + "].",
75 e, interceptorConfig);
76 } catch (Exception e) {
77 throw new ConfigurationException(
78 "Caught Exception while registering Interceptor class " + className,
79 e, interceptorConfig);
80 } catch (NoClassDefFoundError e) {
81 throw new ConfigurationException(
82 "Could not load class " + className
83 + ". Perhaps it exists but certain dependencies are not available?",
84 e, interceptorConfig);
85 }
86 }
87
88 public Result buildResult(ResultConfig resultConfig, Map extraContext) throws Exception {
89 String resultClassName = resultConfig.getClassName();
90 if (resultClassName == null)
91 return null;
92
93 Object result = buildBean(resultClassName, extraContext);
94 OgnlUtil.setProperties(resultConfig.getParams(), result, extraContext);
95
96 if (result instanceof Result)
97 return (Result) result;
98
99 if (result instanceof org.apache.struts2.spi.Result)
100 return new ResultAdapter((org.apache.struts2.spi.Result) result);
101
102 throw new ConfigurationException(result.getClass().getName() + " does not implement Result.");
103 }
104 }