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 */ 017package org.apache.mina.integration.ognl; 018 019import java.util.Map; 020 021import ognl.ObjectPropertyAccessor; 022import ognl.OgnlContext; 023import ognl.OgnlException; 024import ognl.OgnlRuntime; 025import ognl.PropertyAccessor; 026 027/** 028 * An abstract OGNL {@link PropertyAccessor} for MINA constructs. 029 * 030 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 031 */ 032@SuppressWarnings("unchecked") 033public abstract class AbstractPropertyAccessor extends ObjectPropertyAccessor { 034 035 static final Object READ_ONLY_MODE = new Object(); 036 037 static final Object QUERY = new Object(); 038 039 @Override 040 public final boolean hasGetProperty(OgnlContext context, Object target, Object oname) throws OgnlException { 041 if (oname == null) { 042 return false; 043 } 044 045 if (hasGetProperty0(context, target, oname.toString())) { 046 return true; 047 } else { 048 return super.hasGetProperty(context, target, oname); 049 } 050 } 051 052 @Override 053 public final boolean hasSetProperty(OgnlContext context, Object target, Object oname) throws OgnlException { 054 if (context.containsKey(READ_ONLY_MODE)) { 055 // Return true to trigger setPossibleProperty to throw an exception. 056 return true; 057 } 058 059 if (oname == null) { 060 return false; 061 } 062 063 if (hasSetProperty0(context, target, oname.toString())) { 064 return true; 065 } else { 066 return super.hasSetProperty(context, target, oname); 067 } 068 } 069 070 @Override 071 public final Object getPossibleProperty(Map context, Object target, String name) throws OgnlException { 072 Object answer = getProperty0((OgnlContext) context, target, name); 073 if (answer == OgnlRuntime.NotFound) { 074 answer = super.getPossibleProperty(context, target, name); 075 } 076 return answer; 077 } 078 079 @Override 080 public final Object setPossibleProperty(Map context, Object target, String name, Object value) throws OgnlException { 081 if (context.containsKey(READ_ONLY_MODE)) { 082 throw new OgnlException("Expression must be read-only: " + context.get(QUERY)); 083 } 084 085 Object answer = setProperty0((OgnlContext) context, target, name, value); 086 if (answer == OgnlRuntime.NotFound) { 087 answer = super.setPossibleProperty(context, target, name, value); 088 } 089 return answer; 090 } 091 092 protected abstract boolean hasGetProperty0(OgnlContext context, Object target, String name) throws OgnlException; 093 094 protected abstract boolean hasSetProperty0(OgnlContext context, Object target, String name) throws OgnlException; 095 096 protected abstract Object getProperty0(OgnlContext context, Object target, String name) throws OgnlException; 097 098 protected abstract Object setProperty0(OgnlContext context, Object target, String name, Object value) 099 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}