001// Copyright 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
015package org.apache.tapestry.enhance;
016
017import org.apache.hivemind.ApplicationRuntimeException;
018import org.apache.hivemind.Locatable;
019import org.apache.hivemind.Location;
020import org.apache.hivemind.Resource;
021import org.apache.hivemind.util.Defense;
022import org.apache.tapestry.IScript;
023import org.apache.tapestry.engine.IScriptSource;
024
025/**
026 * @author Howard M. Lewis Ship
027 * @since 4.0
028 */
029public class DeferredScriptImpl implements DeferredScript, Locatable
030{
031    final Resource _scriptResource;
032
033    final IScriptSource _scriptSource;
034
035    final Location _location;
036
037    public DeferredScriptImpl(Resource resource, IScriptSource source, Location location)
038    {
039        Defense.notNull(resource, "resource");
040        Defense.notNull(source, "source");
041
042        _scriptResource = resource;
043        _scriptSource = source;
044        _location = location;
045    }
046
047    public IScript getScript()
048    {
049        // No real reason to cache the script here, because a) they are rarely used
050        // and b) the IScriptSource caches.
051
052        try
053        {
054            return _scriptSource.getScript(_scriptResource);
055        }
056        catch (Exception ex)
057        {
058            // Decorate the thrown exception with the correct location.
059
060            throw new ApplicationRuntimeException(ex.getMessage(), _location, ex);
061        }
062    }
063
064    public Location getLocation()
065    {
066        return _location;
067    }
068
069    public String toString()
070    {
071        return "DeferredScriptImpl[" + _scriptResource + "]";
072    }
073}