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