001 /*
002 * $Id: BeanProviderAccessor.java 1187867 2011-10-23 11:35:10Z simonetripodi $
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020 /**
021 *
022 */
023 package org.apache.commons.ognl.test.objects;
024
025 import org.apache.commons.ognl.ObjectPropertyAccessor;
026 import org.apache.commons.ognl.OgnlContext;
027 import org.apache.commons.ognl.OgnlException;
028 import org.apache.commons.ognl.OgnlRuntime;
029 import org.apache.commons.ognl.PropertyAccessor;
030 import org.apache.commons.ognl.enhance.ExpressionCompiler;
031 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
032
033 import java.util.Map;
034
035 /**
036 * Implementation of provider that works with {@link BeanProvider} instances.
037 */
038 public class BeanProviderAccessor
039 extends ObjectPropertyAccessor
040 implements PropertyAccessor
041 {
042 @Override
043 public Object getProperty( Map<String, Object> context, Object target, Object name )
044 throws OgnlException
045 {
046 BeanProvider provider = (BeanProvider) target;
047 String beanName = (String) name;
048
049 return provider.getBean( beanName );
050 }
051
052 /**
053 * Returns true if the name matches a bean provided by the provider. Otherwise invokes the super implementation.
054 **/
055
056 @Override
057 public boolean hasGetProperty( Map<String, Object> context, Object target, Object oname )
058 throws OgnlException
059 {
060 BeanProvider provider = (BeanProvider) target;
061 String beanName = ( (String) oname ).replaceAll( "\"", "" );
062
063 return provider.getBean( beanName ) != null;
064 }
065
066 @Override
067 public String getSourceAccessor( OgnlContext context, Object target, Object name )
068 {
069 BeanProvider provider = (BeanProvider) target;
070 String beanName = ( (String) name ).replaceAll( "\"", "" );
071
072 if ( provider.getBean( beanName ) != null )
073 {
074 context.setCurrentAccessor( BeanProvider.class );
075 context.setCurrentType( provider.getBean( beanName ).getClass() );
076
077 ExpressionCompiler.addCastString( context,
078 "(("
079 + OgnlRuntime.getCompiler( context ).getInterfaceClass( provider.getBean( beanName ).getClass() ).getName()
080 + ")" );
081
082 return ".getBean(\"" + beanName + "\"))";
083 }
084
085 return super.getSourceAccessor( context, target, name );
086 }
087
088 @Override
089 public String getSourceSetter( OgnlContext context, Object target, Object name )
090 {
091 throw new UnsupportedCompilationException( "Can't set beans on BeanProvider." );
092 }
093 }