1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.integration.velocity;
17
18 import net.sf.morph.Defaults;
19 import net.sf.morph.reflect.BeanReflector;
20 import net.sf.morph.util.Assert;
21
22 import org.apache.velocity.context.AbstractContext;
23 import org.apache.velocity.context.Context;
24
25 /**
26 * Single parent bean Velocity context implemented with a Reflector.
27 * @author Matt Benson
28 * @since Morph 1.1
29 */
30 public class ReflectorVelocityContext extends AbstractContext {
31 private Object bean;
32 private BeanReflector reflector;
33
34 /**
35 * Create a new ReflectorVelocityContext which must subsequently have its <code>bean</code> property set.
36 */
37 public ReflectorVelocityContext() {
38 super();
39 }
40
41 /**
42 * Create a new ReflectorVelocityContext.
43 * @param bean
44 */
45 public ReflectorVelocityContext(Object bean) {
46 setBean(bean);
47 }
48
49 /**
50 * Create a new ReflectorVelocityContext.
51 * @param bean
52 * @param inner
53 */
54 public ReflectorVelocityContext(Object bean, Context inner) {
55 super(inner);
56 setBean(bean);
57 }
58
59 /**
60 * Create a new ReflectorVelocityContext.
61 * @param bean
62 * @param reflector
63 */
64 public ReflectorVelocityContext(Object bean, BeanReflector reflector) {
65 setBean(bean);
66 setReflector(reflector);
67 }
68
69 /**
70 * Create a new ReflectorVelocityContext.
71 * @param bean
72 * @param reflector
73 * @param inner
74 */
75 public ReflectorVelocityContext(Object bean, BeanReflector reflector, Context inner) {
76 super(inner);
77 setBean(bean);
78 setReflector(reflector);
79 }
80
81 /**
82 * {@inheritDoc}
83 */
84 public boolean internalContainsKey(Object arg0) {
85 return arg0 instanceof String
86 && getReflector().isReadable(getBean(), (String) arg0);
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 public Object internalGet(String arg0) {
93 return internalContainsKey(arg0) ? getReflector().get(getBean(), arg0) : null;
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 public Object[] internalGetKeys() {
100 return getReflector().getPropertyNames(getBean());
101 }
102
103 /**
104 * {@inheritDoc}
105 */
106 public Object internalPut(String arg0, Object arg1) {
107 Object result = get(arg0);
108 getReflector().set(getBean(), arg0, arg1);
109 return result;
110 }
111
112 /**
113 * Set to null and take our lumps.
114 */
115 public Object internalRemove(Object arg0) {
116 return put((String) arg0, null);
117 }
118
119 /**
120 * Get the bean.
121 * @return Object
122 */
123 public Object getBean() {
124 return bean;
125 }
126
127 /**
128 * Set the bean.
129 * @param bean the Object to set
130 */
131 public void setBean(Object bean) {
132 Assert.notNull(bean, "bean");
133 this.bean = bean;
134 }
135
136 /**
137 * Get the reflector.
138 * @return BeanReflector
139 */
140 public synchronized BeanReflector getReflector() {
141 if (reflector == null) {
142 setReflector(Defaults.createBeanReflector());
143 }
144 return reflector;
145 }
146
147 /**
148 * Set the reflector.
149 * @param reflector the BeanReflector to set
150 */
151 public synchronized void setReflector(BeanReflector reflector) {
152 this.reflector = reflector;
153 }
154
155 }