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.binding;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.util.Defense;
019    import org.apache.tapestry.IComponent;
020    import org.apache.tapestry.coerce.ValueConverter;
021    
022    /**
023     * Binding whose value is a named bean provided by a component.
024     * 
025     * @author Howard M. Lewis Ship
026     * @since 4.0
027     */
028    public class BeanBinding extends AbstractBinding
029    {
030        private final IComponent _component;
031    
032        private final String _beanName;
033    
034        public BeanBinding(String description, ValueConverter valueConverter, Location location,
035                IComponent component, String beanName)
036        {
037            super(description, valueConverter, location);
038    
039            Defense.notNull(component, "component");
040            Defense.notNull(beanName, "beanName");
041    
042            _component = component;
043            _beanName = beanName;
044        }
045    
046        /**
047         * Beans obtained via the binding should *not* be cached by the component because beans may be
048         * discarded inside the {@link org.apache.tapestry.bean.BeanProvider} based on the life cycle of
049         * the bean.  The BeanProvider caches beans as well.
050         */
051    
052        public boolean isInvariant()
053        {
054            return false;
055        }
056    
057        public Object getComponent()
058        {
059            return _component;
060        }
061    
062        public Object getObject()
063        {
064            return _component.getBeans().getBean(_beanName);
065        }
066    
067    }