01: /* DelegatingVariableResolver.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Thu Jun 1 13:53:53 2006, Created by andrewho
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.zkplus.spring;
18:
19: import java.util.Map;
20: import java.util.HashMap;
21: import javax.servlet.ServletContext;
22:
23: import org.zkoss.zk.ui.Executions;
24: import org.zkoss.xel.VariableResolver;
25:
26: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27: import org.springframework.context.ApplicationContext;
28: import org.springframework.web.context.support.WebApplicationContextUtils;
29:
30: /**
31: * DelegatingVariableResolver, a spring bean variable resolver.
32: *
33: * <p>It defines a variable called <code>springContext</code> to represent
34: * the instance of <code>org.springframework.context.ApplicationContext</code>.
35: * It also looks variables for beans defined in <code>springContext</code>.
36: *
37: * <p>Usage:<br>
38: * <code><?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?></code>
39: *
40: * @author andrewho
41: */
42: public class DelegatingVariableResolver implements VariableResolver {
43: protected ApplicationContext _ctx;
44: protected final Map _vars = new HashMap();
45:
46: /**
47: * Get the spring application context.
48: */
49: protected ApplicationContext getApplicationContext() {
50: if (_ctx != null)
51: return _ctx;
52:
53: _ctx = SpringUtil.getApplicationContext();
54: _vars.put("springContext", _ctx);
55: return _ctx;
56: }
57:
58: /**
59: * Get the spring bean by the specified name.
60: */
61: public Object resolveVariable(String name) {
62: Object o = _vars.get(name);
63: if (o == null) {
64: try {
65: o = getApplicationContext().getBean(name);
66: } catch (NoSuchBeanDefinitionException ex) {
67: o = null;
68: }
69: if (o != null)
70: _vars.put(name, o);
71: }
72: return o;
73: }
74: }
|