001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.bean;
016    
017    import java.util.Map;
018    
019    import ognl.ObjectPropertyAccessor;
020    import ognl.OgnlException;
021    
022    import org.apache.tapestry.IBeanProvider;
023    
024    /**
025     *  Adapts a {@link org.apache.tapestry.IBeanProvider} to
026     *  <a href="http://www.ognl.org">OGNL</a> by exposing the named
027     *  beans provided by the provider as read-only properties of
028     *  the provider.
029     * 
030     *  <p>This is registered by {@link org.apache.tapestry.AbstractComponent}.
031     *
032     *  @author Howard Lewis Ship
033     *  @since 2.2
034     *
035     **/
036    
037    public class BeanProviderPropertyAccessor extends ObjectPropertyAccessor
038    {
039        /**
040         *  Checks to see if the name matches the name of a bean inside
041         *  the provider and returns that bean if so.
042         *  Otherwise, invokes the super implementation.
043         * 
044         **/
045        
046        public Object getProperty(Map context, Object target, Object name) throws OgnlException
047        {
048            IBeanProvider provider = (IBeanProvider)target;
049            String beanName = (String)name;
050            
051            if (provider.canProvideBean(beanName))
052                return provider.getBean(beanName);
053            
054            return super.getProperty(context, target, name);
055        }
056    
057        /**
058         *  Returns true if the name matches a bean provided by the provider.
059         *  Otherwise invokes the super implementation.
060         * 
061         **/
062        
063        public boolean hasGetProperty(Map context, Object target, Object oname) throws OgnlException
064        {
065            IBeanProvider provider = (IBeanProvider)target;
066            String beanName = (String)oname;
067    
068            if (provider.canProvideBean(beanName))
069                return true;
070                
071            return super.hasGetProperty(context, target, oname);
072        }
073    
074    }