001    /*
002     *  Copyright 2007 Tacos.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     * 
008     *       http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     *  under the License.
016     */
017    package net.sf.tacos.binding;
018    
019    import net.sf.tacos.asset.AbsoluteAsset;
020    import org.apache.hivemind.Location;
021    import org.apache.hivemind.util.Defense;
022    import org.apache.tapestry.BindingException;
023    import org.apache.tapestry.IAsset;
024    import org.apache.tapestry.IComponent;
025    import org.apache.tapestry.binding.AbstractBinding;
026    import org.apache.tapestry.coerce.ValueConverter;
027    import org.apache.tapestry.services.AbsoluteURLBuilder;
028    
029    /**
030     * A binding that is connected to an asset provided by a component.
031     * 
032     * @author Andreas Andreou
033     * @since 4.1
034     */
035    public class AbsoluteAssetBinding extends AbstractBinding
036    {
037        private final IComponent component;
038        private AbsoluteURLBuilder absoluteURLBuilder;
039    
040        public AbsoluteAssetBinding(String description, ValueConverter valueConverter, 
041                AbsoluteURLBuilder absoluteURLBuilder, Location location, IComponent component, String assetName)
042        {
043            super(assetName, valueConverter, location);
044    
045            Defense.notNull(component, "component");
046            Defense.notNull(assetName, "assetName");
047    
048            this.component = component;
049            this.absoluteURLBuilder = absoluteURLBuilder;
050        }
051    
052        public Object getObject()
053        {
054            IAsset asset = component.getAsset(_description);
055    
056            if (asset == null)
057                throw new BindingException("Component " + component.getExtendedId() + 
058                        " does not contain an asset named ''" + _description + "''.",
059                        component, getLocation(), this, null);
060    
061            return new AbsoluteAsset(asset, absoluteURLBuilder);
062        }
063    
064        public Object getComponent()
065        {
066            return component;
067        }
068    }
069