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 The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev: 601229 $, $Date: 2007-12-05 08:13:18 +0100 (mer, 05 déc 2007) $
32   */
33  @SuppressWarnings("unchecked")
34  public abstract class AbstractPropertyAccessor extends ObjectPropertyAccessor {
35  
36      static final Object READ_ONLY_MODE = new Object();
37      static final Object QUERY = new Object();
38      
39      @Override
40      public final boolean hasGetProperty(OgnlContext context, Object target,
41              Object oname) throws OgnlException {
42          if (oname == null) {
43              return false;
44          }
45          
46          if (hasGetProperty0(context, target, oname.toString())) {
47              return true;
48          } else {
49              return super.hasGetProperty(context, target, oname);
50          }
51      }
52  
53      @Override
54      public final boolean hasSetProperty(OgnlContext context, Object target,
55              Object oname) throws OgnlException {
56          if (context.containsKey(READ_ONLY_MODE)) {
57              // Return true to trigger setPossibleProperty to throw an exception.
58              return true;
59          }
60          
61          if (oname == null) {
62              return false;
63          }
64          
65          if (hasSetProperty0(context, target, oname.toString())) {
66              return true;
67          } else {
68              return super.hasSetProperty(context, target, oname);
69          }
70      }
71  
72      @Override
73      public final Object getPossibleProperty(Map context, Object target, String name)
74              throws OgnlException {
75          Object answer = getProperty0((OgnlContext) context, target, name);
76          if (answer == OgnlRuntime.NotFound) {
77              answer = super.getPossibleProperty(context, target, name);
78          }
79          return answer;
80      }
81  
82      @Override
83      public final Object setPossibleProperty(Map context, Object target, String name,
84              Object value) throws OgnlException {
85          if (context.containsKey(READ_ONLY_MODE)) {
86              throw new OgnlException("Expression must be read-only: " + context.get(QUERY));
87          }
88          
89          Object answer = setProperty0((OgnlContext) context, target, name, value);
90          if (answer == OgnlRuntime.NotFound) {
91              answer = super.setPossibleProperty(context, target, name, value);
92          }
93          return answer;
94      }
95      
96      protected abstract boolean hasGetProperty0(
97              OgnlContext context, Object target, String name) throws OgnlException;
98  
99      protected abstract boolean hasSetProperty0(
100             OgnlContext context, Object target, String name) throws OgnlException;
101 
102     protected abstract Object getProperty0(
103             OgnlContext context, Object target, String name) throws OgnlException;
104 
105     protected abstract Object setProperty0(
106             OgnlContext context, Object target, String name, Object value) throws OgnlException;
107 
108 
109     // The following methods uses the four method above, so there's no need
110     // to override them.
111     
112     @Override
113     public final Object getProperty(Map context, Object target, Object oname)
114             throws OgnlException {
115         return super.getProperty(context, target, oname);
116     }
117 
118     @Override
119     public final boolean hasGetProperty(Map context, Object target, Object oname)
120             throws OgnlException {
121         return super.hasGetProperty(context, target, oname);
122     }
123 
124     @Override
125     public final boolean hasSetProperty(Map context, Object target, Object oname)
126             throws OgnlException {
127         return super.hasSetProperty(context, target, oname);
128     }
129 
130     @Override
131     public final void setProperty(Map context, Object target, Object oname,
132             Object value) throws OgnlException {
133         super.setProperty(context, target, oname, value);
134     }
135 }