View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.plugins.s2.el;
18  
19  import static org.seasar.framework.message.MessageFormatter.getMessage;
20  
21  import java.beans.FeatureDescriptor;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.el.ELContext;
27  import javax.el.ELException;
28  import javax.el.ELResolver;
29  import javax.el.PropertyNotFoundException;
30  import javax.el.PropertyNotWritableException;
31  
32  import org.seasar.cubby.spi.beans.Attribute;
33  import org.seasar.cubby.spi.beans.BeanDesc;
34  import org.seasar.cubby.spi.beans.BeanDescFactory;
35  
36  /**
37   * S2Container の public フィールドをプロパティとして認識させるための {@link ELResolver} です。
38   * 
39   * @author baba
40   */
41  public class S2BeanELResolver extends ELResolver {
42  
43  	/** 読み込み専用。 */
44  	private final boolean readOnly;
45  
46  	/**
47  	 * インスタンスを生成します。
48  	 */
49  	public S2BeanELResolver() {
50  		this(false);
51  	}
52  
53  	/**
54  	 * インスタンスを生成します。
55  	 * 
56  	 * @param readOnly
57  	 *            読み込み専用とする場合は <code>true</code>、そうでない場合は <code>false</code>
58  	 */
59  	public S2BeanELResolver(final boolean readOnly) {
60  		super();
61  		this.readOnly = readOnly;
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	@Override
68  	public Object getValue(final ELContext context, final Object base,
69  			final Object property) throws NullPointerException,
70  			PropertyNotFoundException, ELException {
71  		if (context == null) {
72  			throw new NullPointerException();
73  		}
74  		if (base == null || property == null) {
75  			return null;
76  		}
77  
78  		final String propertyName = property.toString();
79  		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
80  		if (!beanDesc.hasPropertyAttribute(propertyName)) {
81  			return null;
82  		}
83  
84  		try {
85  			final Attribute attributeDesc = beanDesc
86  					.getPropertyAttribute(propertyName);
87  			final Object value = attributeDesc.getValue(base);
88  			context.setPropertyResolved(true);
89  			return value;
90  		} catch (final Exception e) {
91  			return null;
92  		}
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 */
98  	@Override
99  	public Class<?> getType(final ELContext context, final Object base,
100 			final Object property) {
101 		if (context == null) {
102 			throw new NullPointerException();
103 		}
104 
105 		if (base == null || property == null) {
106 			return null;
107 		}
108 
109 		final String propertyName = property.toString();
110 		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
111 		if (!beanDesc.hasPropertyAttribute(propertyName)) {
112 			return null;
113 		}
114 
115 		try {
116 			final Attribute attributeDesc = beanDesc
117 					.getPropertyAttribute(propertyName);
118 			final Class<?> propertyType = attributeDesc.getType();
119 			context.setPropertyResolved(true);
120 			return propertyType;
121 		} catch (final Exception e) {
122 			return null;
123 		}
124 	}
125 
126 	/**
127 	 * {@inheritDoc}
128 	 */
129 	@Override
130 	public void setValue(final ELContext context, final Object base,
131 			final Object property, final Object value) {
132 		if (context == null) {
133 			throw new NullPointerException();
134 		}
135 		if (base == null || property == null) {
136 			return;
137 		}
138 
139 		final String propertyName = property.toString();
140 		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
141 		if (!beanDesc.hasPropertyAttribute(propertyName)) {
142 			return;
143 		}
144 
145 		if (this.readOnly) {
146 			throw new PropertyNotWritableException(getMessage("ECUB0001",
147 					new Object[] { base.getClass().getName() }));
148 		}
149 
150 		try {
151 			final Attribute attributeDesc = beanDesc
152 					.getPropertyAttribute(propertyName);
153 			attributeDesc.setValue(base, value);
154 			context.setPropertyResolved(true);
155 		} catch (final Exception e) {
156 		}
157 	}
158 
159 	/**
160 	 * {@inheritDoc}
161 	 */
162 	@Override
163 	public boolean isReadOnly(final ELContext context, final Object base,
164 			final Object property) {
165 		if (context == null) {
166 			throw new NullPointerException();
167 		}
168 		if (base == null || property == null) {
169 			return false;
170 		}
171 
172 		final String propertyName = property.toString();
173 		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
174 		if (!beanDesc.hasPropertyAttribute(propertyName)) {
175 			return true;
176 		}
177 
178 		if (this.readOnly) {
179 			return true;
180 		}
181 
182 		try {
183 			final Attribute attributeDesc = beanDesc
184 					.getPropertyAttribute(propertyName);
185 			final boolean readOnly = !attributeDesc.isWritable();
186 			context.setPropertyResolved(true);
187 			return readOnly;
188 		} catch (final Exception e) {
189 			return false;
190 		}
191 	}
192 
193 	/**
194 	 * {@inheritDoc}
195 	 */
196 	@Override
197 	public Iterator<FeatureDescriptor> getFeatureDescriptors(
198 			final ELContext context, final Object base) {
199 		if (context == null) {
200 			throw new NullPointerException();
201 		}
202 
203 		if (base == null) {
204 			return null;
205 		}
206 
207 		final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
208 		try {
209 			final List<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
210 			for (final Attribute attributeDesc : beanDesc
211 					.findtPropertyAttributes()) {
212 				final String propertyName = attributeDesc.getName();
213 				final FeatureDescriptor descriptor = new FeatureDescriptor();
214 				descriptor.setDisplayName(propertyName);
215 				descriptor.setExpert(false);
216 				descriptor.setHidden(false);
217 				descriptor.setName(propertyName);
218 				descriptor.setPreferred(true);
219 				descriptor.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
220 				descriptor.setValue(TYPE, attributeDesc.getType());
221 				descriptors.add(descriptor);
222 			}
223 
224 			return descriptors.iterator();
225 		} catch (final Exception e) {
226 			// do nothing
227 		}
228 		return null;
229 	}
230 
231 	/**
232 	 * {@inheritDoc}
233 	 */
234 	@Override
235 	public Class<?> getCommonPropertyType(final ELContext context,
236 			final Object base) {
237 		if (context == null) {
238 			throw new NullPointerException();
239 		}
240 
241 		if (base != null) {
242 			return Object.class;
243 		}
244 
245 		return null;
246 	}
247 
248 }