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