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  public abstract class AbstractPropertyAccessor extends ObjectPropertyAccessor {
33  
34      static final Object READ_ONLY_MODE = new Object();
35  
36      static final Object QUERY = new Object();
37  
38      @Override
39      public final boolean hasGetProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
40          if (oname == null) {
41              return false;
42          }
43  
44          if (hasGetProperty0(context, target, oname.toString())) {
45              return true;
46          } else {
47              return super.hasGetProperty(context, target, oname);
48          }
49      }
50  
51      @Override
52      public final boolean hasSetProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
53          if (context.containsKey(READ_ONLY_MODE)) {
54              // Return true to trigger setPossibleProperty to throw an exception.
55              return true;
56          }
57  
58          if (oname == null) {
59              return false;
60          }
61  
62          if (hasSetProperty0(context, target, oname.toString())) {
63              return true;
64          } else {
65              return super.hasSetProperty(context, target, oname);
66          }
67      }
68  
69      @Override
70      public final Object getPossibleProperty(Map context, Object target, String name) throws OgnlException {
71          Object answer = getProperty0((OgnlContext) context, target, name);
72          if (answer == OgnlRuntime.NotFound) {
73              answer = super.getPossibleProperty(context, target, name);
74          }
75          return answer;
76      }
77  
78      @Override
79      public final Object setPossibleProperty(Map context, Object target, String name, Object value) throws OgnlException {
80          if (context.containsKey(READ_ONLY_MODE)) {
81              throw new OgnlException("Expression must be read-only: " + context.get(QUERY));
82          }
83  
84          Object answer = setProperty0((OgnlContext) context, target, name, value);
85          if (answer == OgnlRuntime.NotFound) {
86              answer = super.setPossibleProperty(context, target, name, value);
87          }
88          return answer;
89      }
90  
91      protected abstract boolean hasGetProperty0(OgnlContext context, Object target, String name) throws OgnlException;
92  
93      protected abstract boolean hasSetProperty0(OgnlContext context, Object target, String name) throws OgnlException;
94  
95      protected abstract Object getProperty0(OgnlContext context, Object target, String name) throws OgnlException;
96  
97      protected abstract Object setProperty0(OgnlContext context, Object target, String name, Object value)
98              throws OgnlException;
99  }