KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > bean > ExpressionBeanInitializer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.bean;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.tapestry.IBeanProvider;
19 import org.apache.tapestry.IComponent;
20 import org.apache.tapestry.services.ExpressionEvaluator;
21
22 /**
23  * Initializes a helper bean property from an OGNL expression (relative to the bean's
24  * {@link IComponent}).
25  *
26  * @author Howard Lewis Ship
27  * @since 2.2
28  */

29
30 public class ExpressionBeanInitializer extends AbstractBeanInitializer
31 {
32     protected String JavaDoc _expression;
33
34     private final ExpressionEvaluator _evaluator;
35
36     public ExpressionBeanInitializer(ExpressionEvaluator evaluator)
37     {
38         _evaluator = evaluator;
39     }
40
41     public void setBeanProperty(IBeanProvider provider, Object JavaDoc bean)
42     {
43         IComponent component = provider.getComponent();
44
45         try
46         {
47             Object JavaDoc value = _evaluator.read(component, _expression);
48
49             setBeanProperty(bean, value);
50         }
51         catch (Exception JavaDoc ex)
52         {
53             throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
54         }
55     }
56
57     /** @since 3.0 * */
58
59     public String JavaDoc getExpression()
60     {
61         return _expression;
62     }
63
64     /** @since 3.0 * */
65
66     public void setExpression(String JavaDoc expression)
67     {
68         _expression = expression;
69     }
70
71 }
Popular Tags