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