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.asset;
016    
017    import java.io.InputStream;
018    import java.net.URL;
019    
020    import org.apache.hivemind.ApplicationRuntimeException;
021    import org.apache.hivemind.Location;
022    import org.apache.hivemind.Resource;
023    import org.apache.hivemind.util.ClasspathResource;
024    import org.apache.hivemind.util.Defense;
025    import org.apache.tapestry.engine.IEngineService;
026    import org.apache.tapestry.engine.ILink;
027    
028    /**
029     * An implementation of {@link org.apache.tapestry.IAsset}for localizable assets within the JVM's
030     * classpath.
031     * <p>
032     * The localization code here is largely cut-and-paste from {@link ContextAsset}.
033     * 
034     * @author Howard Ship
035     */
036    
037    public class PrivateAsset extends AbstractAsset
038    {
039        private IEngineService _assetService;
040    
041        /**
042         * @deprecated To be removed (someday). Use
043         *             {@link #PrivateAsset(ClasspathResource, IEngineService, Location)}&nbsp;instead.
044         */
045        public PrivateAsset(ClasspathResource resourceLocation, Location location)
046        {
047            this(resourceLocation, null, location);
048        }
049    
050        public PrivateAsset(ClasspathResource resourceLocation, IEngineService assetService,
051                Location location)
052        {
053            super(resourceLocation, location);
054    
055            Defense.notNull(assetService, "assetService");
056    
057            _assetService = assetService;
058        }
059    
060        /**
061         * Gets the localized version of the resource. Build the URL for the resource. If possible, the
062         * application's {@link AssetExternalizerImpl}is located, to copy the resource to a directory
063         * visible to the web server.
064         */
065    
066        public String buildURL()
067        {
068            String path = getResourceLocation().getPath();
069    
070            ILink link = _assetService.getLink(false, path);
071    
072            return link.getURL();
073        }
074    
075        public InputStream getResourceAsStream()
076        {
077            Resource location = getResourceLocation();
078    
079            try
080            {
081                URL url = location.getResourceURL();
082    
083                return url.openStream();
084            }
085            catch (Exception ex)
086            {
087                throw new ApplicationRuntimeException(AssetMessages.noSuchResource(location.getPath()));
088            }
089        }
090    
091    }