View Javadoc

    1   /*
    2    * $Id: DynaBeanPropertyAccessor.java 478625 2006-11-23 17:31:52Z wsmoak $
    3    * Copyright 2004 The Apache Software Foundation.
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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
    19  package org.apache.struts2.s1;
    20
    21  import java.io.Serializable;
    22  import org.apache.commons.beanutils.DynaBean;
    23  import org.apache.commons.beanutils.DynaClass;
    24  import ognl.OgnlException;
    25  import ognl.PropertyAccessor;
    26  import java.util.Map;
    27
    28  /***
    29   * Provides access to DynaBean properties in OGNL
    30   */
    31  public class DynaBeanPropertyAccessor implements PropertyAccessor {
    32
    33      public Object getProperty(Map context, Object target, Object name) throws OgnlException {
    34
    35          if (target instanceof DynaBean && name != null) {
    36              DynaBean bean = (DynaBean)target;
    37              DynaClass cls = bean.getDynaClass();
    38              String key = name.toString();
    39              if (cls.getDynaProperty(key) != null) {
    40                  return bean.get(key);
    41              }
    42          }
    43          return null;
    44      }
    45
    46      public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
    47
    48          if (target instanceof DynaBean && name != null) {
    49              DynaBean bean = (DynaBean)target;
    50              String key = name.toString();
    51              bean.set(key, value);
    52          }
    53      }
    54
    55  }