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  package org.apache.mina.integration.ognl;
    18
    19  import java.util.Map;
    20
    21  import ognl.ObjectPropertyAccessor;
    22  import ognl.OgnlContext;
    23  import ognl.OgnlException;
    24  import ognl.OgnlRuntime;
    25  import ognl.PropertyAccessor;
    26
    27  /**
    28   * An abstract OGNL {@link PropertyAccessor} for MINA constructs.
    29   *
    30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    31   */
    32  @SuppressWarnings("unchecked")
    33  public abstract class AbstractPropertyAccessor extends ObjectPropertyAccessor {
    34
    35      static final Object READ_ONLY_MODE = new Object();
    36
    37      static final Object QUERY = new Object();
    38
    39      @Override
    40      public final boolean hasGetProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
    41          if (oname == null) {
    42              return false;
    43          }
    44
    45          if (hasGetProperty0(context, target, oname.toString())) {
    46              return true;
    47          } else {
    48              return super.hasGetProperty(context, target, oname);
    49          }
    50      }
    51
    52      @Override
    53      public final boolean hasSetProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
    54          if (context.containsKey(READ_ONLY_MODE)) {
    55              // Return true to trigger setPossibleProperty to throw an exception.
    56              return true;
    57          }
    58
    59          if (oname == null) {
    60              return false;
    61          }
    62
    63          if (hasSetProperty0(context, target, oname.toString())) {
    64              return true;
    65          } else {
    66              return super.hasSetProperty(context, target, oname);
    67          }
    68      }
    69
    70      @Override
    71      public final Object getPossibleProperty(Map context, Object target, String name) throws OgnlException {
    72          Object answer = getProperty0((OgnlContext) context, target, name);
    73          if (answer == OgnlRuntime.NotFound) {
    74              answer = super.getPossibleProperty(context, target, name);
    75          }
    76          return answer;
    77      }
    78
    79      @Override
    80      public final Object setPossibleProperty(Map context, Object target, String name, Object value) throws OgnlException {
    81          if (context.containsKey(READ_ONLY_MODE)) {
    82              throw new OgnlException("Expression must be read-only: " + context.get(QUERY));
    83          }
    84
    85          Object answer = setProperty0((OgnlContext) context, target, name, value);
    86          if (answer == OgnlRuntime.NotFound) {
    87              answer = super.setPossibleProperty(context, target, name, value);
    88          }
    89          return answer;
    90      }
    91
    92      protected abstract boolean hasGetProperty0(OgnlContext context, Object target, String name) throws OgnlException;
    93
    94      protected abstract boolean hasSetProperty0(OgnlContext context, Object target, String name) throws OgnlException;
    95
    96      protected abstract Object getProperty0(OgnlContext context, Object target, String name) throws OgnlException;
    97
    98      protected abstract Object setProperty0(OgnlContext context, Object target, String name, Object value)
    99              throws OgnlException;
    100
    101     // The following methods uses the four method above, so there's no need
    102     // to override them.
    103
    104     @Override
    105     public final Object getProperty(Map context, Object target, Object oname) throws OgnlException {
    106         return super.getProperty(context, target, oname);
    107     }
    108
    109     @Override
    110     public final boolean hasGetProperty(Map context, Object target, Object oname) throws OgnlException {
    111         return super.hasGetProperty(context, target, oname);
    112     }
    113
    114     @Override
    115     public final boolean hasSetProperty(Map context, Object target, Object oname) throws OgnlException {
    116         return super.hasSetProperty(context, target, oname);
    117     }
    118
    119     @Override
    120     public final void setProperty(Map context, Object target, Object oname, Object value) throws OgnlException {
    121         super.setProperty(context, target, oname, value);
    122     }
    123 }