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.tapestry.IRequestCycle;
025    import org.apache.tapestry.Tapestry;
026    import org.apache.tapestry.engine.IEngineService;
027    import org.apache.tapestry.engine.ILink;
028    
029    /**
030     * An implementation of {@link org.apache.tapestry.IAsset}for localizable assets within the JVM's
031     * classpath.
032     * <p>
033     * The localization code here is largely cut-and-paste from {@link ContextAsset}.
034     * 
035     * @author Howard Ship
036     */
037    
038    public class PrivateAsset extends AbstractAsset
039    {
040        private IEngineService _assetService;
041    
042        /**
043         * @deprecated To be removed (someday). Use
044         *             {@link #PrivateAsset(ClasspathResource, IEngineService, Location)}&nbsp;instead.
045         */
046        public PrivateAsset(ClasspathResource resourceLocation, Location location)
047        {
048            this(resourceLocation, null, location);
049        }
050    
051        public PrivateAsset(ClasspathResource resourceLocation, IEngineService assetService,
052                Location location)
053        {
054            super(resourceLocation, location);
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 AssetExternalizerImpl}is located, to copy the resource to a directory
062         * visible to the web server.
063         */
064    
065        public String buildURL(IRequestCycle cycle)
066        {
067            String path = getResourceLocation().getPath();
068    
069            // ClasspathAssetFactory will provide the asset service as a constructor
070            // parameter, but there are a few odd uses of PrivateAsset where this
071            // is not handy.
072    
073            if (_assetService == null)
074                _assetService = cycle.getEngine().getService(Tapestry.ASSET_SERVICE);
075    
076            ILink link = _assetService.getLink(cycle, false, path);
077    
078            return link.getURL();
079        }
080    
081        public InputStream getResourceAsStream(IRequestCycle cycle)
082        {
083            Resource location = getResourceLocation();
084    
085            try
086            {
087                URL url = location.getResourceURL();
088    
089                return url.openStream();
090            }
091            catch (Exception ex)
092            {
093                throw new ApplicationRuntimeException(AssetMessages.noSuchResource(location.getPath()));
094            }
095        }
096    
097    }