View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  
18  package org.apache.commons.betwixt.expression;
19  
20  import org.apache.commons.beanutils.DynaBean;
21  
22  /**
23   * An Expression that gets a property value from a DynaBean.
24   * 
25   * @see org.apache.commons.beanutils.DynaBean
26   * 
27   * @author Michael Becke
28   * @since 0.5
29   */
30  public class DynaBeanExpression implements Expression {
31  
32      /** The name of the DynaBean property to get */
33      private String propertyName;
34  
35      /**
36       * Crates a new DynaBeanExpression.
37       */
38      public DynaBeanExpression() {
39          super();
40      }
41  
42      /**
43       * Crates a new DynaBeanExpression.
44       * 
45       * @param propertyName the name of the DynaBean property to use
46       */
47      public DynaBeanExpression(String propertyName) {
48          super();
49          setPropertyName(propertyName);
50      }
51  
52      /**
53       * Returns the value of a DynaBean property from the bean stored in 
54       * the Context.  Returns <code>null</code> if no DynaBean is stored 
55       * in the Context or if the propertyName has not been set.
56       * 
57       * @param context the content containing the DynaBean
58       * 
59       * @return the DynaBean property value or <code>null</code>
60       */
61      public Object evaluate(Context context) {
62          
63          if (context.getBean() instanceof DynaBean && propertyName != null) {
64              return ((DynaBean)context.getBean()).get(propertyName);
65          } else {
66              return null;
67          }
68      }
69  
70      /**
71       * Do nothing.
72       * @see Expression#update
73       */
74      public void update(Context context, String newValue) {
75          // do nothing
76      }
77  
78      /**
79       * Gets the name of the property to get from the DynaBean.
80       * @return the name of the property that this expression reads
81       */
82      public String getPropertyName() {
83          return propertyName;
84      }
85  
86      /**
87       * Sets the name of the property to get from the DynaBean.
88       * @param propertyName the property that this expression reads, not null
89       */
90      public void setPropertyName(String propertyName) {
91          if (propertyName == null) {
92              throw new IllegalArgumentException("propertyName is null");
93          }
94          this.propertyName = propertyName;
95      }
96  
97  }