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    package org.apache.tapestry.binding;
015    
016    import org.apache.hivemind.ApplicationRuntimeException;
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    import org.apache.tapestry.services.ComponentPropertySource;
022    
023    
024    /**
025     * Simple implementation that allows injection / lookup of meta properties.
026     */
027    public class MetaBinding extends AbstractBinding
028    {
029        private final IComponent _component;
030    
031        private final String _key;
032    
033        private final ComponentPropertySource _propertySource;
034        
035        public MetaBinding(String description, ValueConverter valueConverter, Location location, IComponent component, 
036                ComponentPropertySource propertySource, String key)
037        {
038            super(description, valueConverter, location);
039    
040            Defense.notNull(component, "component");
041            Defense.notNull(propertySource, "propertySource");
042            Defense.notNull(key, "key");
043            
044            _component = component;
045            _key = key;
046            _propertySource = propertySource;
047        }
048    
049        public Object getObject()
050        {
051            try
052            {
053                return _propertySource.getComponentProperty(_component, _key);
054            }
055            catch (Exception ex)
056            {
057                throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
058            }
059        }
060        
061        public Object getComponent()
062        {
063            return _component;
064        }
065        
066        public String toString()
067        {
068            StringBuffer buffer = new StringBuffer("MetaBinding");
069            buffer.append('[');
070            buffer.append(_component.getExtendedId());
071            buffer.append(' ');
072            buffer.append(_key);
073            buffer.append(']');
074    
075            return buffer.toString();
076        }
077    }