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.BindingException;
020 import org.apache.tapestry.IAsset;
021 import org.apache.tapestry.IComponent;
022 import org.apache.tapestry.coerce.ValueConverter;
023
024 /**
025 * A binding that is connected to an asset provided by a component.
026 *
027 * @author Howard M. Lewis Ship
028 * @since 4.0
029 */
030 public class AssetBinding extends AbstractBinding
031 {
032 private final IComponent _component;
033
034 public AssetBinding(String description, ValueConverter valueConverter, Location location,
035 IComponent component, String assetName)
036 {
037 super(assetName, valueConverter, location);
038
039 Defense.notNull(component, "component");
040 Defense.notNull(assetName, "assetName");
041
042 _component = component;
043 }
044
045 public Object getObject()
046 {
047 IAsset result = _component.getAsset(_description);
048
049 if (result == null)
050 throw new BindingException(BindingMessages.missingAsset(_component, _description),
051 _component, getLocation(), this, null);
052
053 return result;
054 }
055
056 public Object getComponent()
057 {
058 return _component;
059 }
060 }