001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.mina.integration.ognl;
018:
019: import java.util.Map;
020:
021: import ognl.ObjectPropertyAccessor;
022: import ognl.OgnlContext;
023: import ognl.OgnlException;
024: import ognl.OgnlRuntime;
025: import ognl.PropertyAccessor;
026:
027: /**
028: * An abstract OGNL {@link PropertyAccessor} for MINA constructs.
029: *
030: * @author The Apache MINA Project (dev@mina.apache.org)
031: * @version $Rev: 601229 $, $Date: 2007-12-05 00:13:18 -0700 (Wed, 05 Dec 2007) $
032: */
033: @SuppressWarnings("unchecked")
034: public abstract class AbstractPropertyAccessor extends
035: ObjectPropertyAccessor {
036:
037: static final Object READ_ONLY_MODE = new Object();
038: static final Object QUERY = new Object();
039:
040: @Override
041: public final boolean hasGetProperty(OgnlContext context,
042: Object target, Object oname) throws OgnlException {
043: if (oname == null) {
044: return false;
045: }
046:
047: if (hasGetProperty0(context, target, oname.toString())) {
048: return true;
049: } else {
050: return super .hasGetProperty(context, target, oname);
051: }
052: }
053:
054: @Override
055: public final boolean hasSetProperty(OgnlContext context,
056: Object target, Object oname) throws OgnlException {
057: if (context.containsKey(READ_ONLY_MODE)) {
058: // Return true to trigger setPossibleProperty to throw an exception.
059: return true;
060: }
061:
062: if (oname == null) {
063: return false;
064: }
065:
066: if (hasSetProperty0(context, target, oname.toString())) {
067: return true;
068: } else {
069: return super .hasSetProperty(context, target, oname);
070: }
071: }
072:
073: @Override
074: public final Object getPossibleProperty(Map context, Object target,
075: String name) throws OgnlException {
076: Object answer = getProperty0((OgnlContext) context, target,
077: name);
078: if (answer == OgnlRuntime.NotFound) {
079: answer = super .getPossibleProperty(context, target, name);
080: }
081: return answer;
082: }
083:
084: @Override
085: public final Object setPossibleProperty(Map context, Object target,
086: String name, Object value) throws OgnlException {
087: if (context.containsKey(READ_ONLY_MODE)) {
088: throw new OgnlException("Expression must be read-only: "
089: + context.get(QUERY));
090: }
091:
092: Object answer = setProperty0((OgnlContext) context, target,
093: name, value);
094: if (answer == OgnlRuntime.NotFound) {
095: answer = super .setPossibleProperty(context, target, name,
096: value);
097: }
098: return answer;
099: }
100:
101: protected abstract boolean hasGetProperty0(OgnlContext context,
102: Object target, String name) throws OgnlException;
103:
104: protected abstract boolean hasSetProperty0(OgnlContext context,
105: Object target, String name) throws OgnlException;
106:
107: protected abstract Object getProperty0(OgnlContext context,
108: Object target, String name) throws OgnlException;
109:
110: protected abstract Object setProperty0(OgnlContext context,
111: Object target, String name, Object value)
112: throws OgnlException;
113:
114: // The following methods uses the four method above, so there's no need
115: // to override them.
116:
117: @Override
118: public final Object getProperty(Map context, Object target,
119: Object oname) throws OgnlException {
120: return super .getProperty(context, target, oname);
121: }
122:
123: @Override
124: public final boolean hasGetProperty(Map context, Object target,
125: Object oname) throws OgnlException {
126: return super .hasGetProperty(context, target, oname);
127: }
128:
129: @Override
130: public final boolean hasSetProperty(Map context, Object target,
131: Object oname) throws OgnlException {
132: return super .hasSetProperty(context, target, oname);
133: }
134:
135: @Override
136: public final void setProperty(Map context, Object target,
137: Object oname, Object value) throws OgnlException {
138: super.setProperty(context, target, oname, value);
139: }
140: }
|