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    
015    package org.apache.tapestry.asset;
016    
017    import org.apache.hivemind.Location;
018    import org.apache.hivemind.Resource;
019    import org.apache.tapestry.IAsset;
020    import org.apache.tapestry.spec.IComponentSpecification;
021    
022    import java.net.URL;
023    import java.net.URLConnection;
024    import java.util.Locale;
025    
026    /**
027     * Default asset factory used when the asset path contains a prefix that is not recognized. It is
028     * assumed that the prefix is, in fact, the scheme of an external URL.
029     * 
030     * @author Howard M. Lewis Ship
031     * @since 4.0
032     */
033    public class DefaultAssetFactory implements AssetFactory
034    {
035    
036        public boolean assetExists(IComponentSpecification spec, Resource baseResource, String path, Locale locale)
037        {
038            try {
039                URL url = new URL(path);
040    
041                URLConnection conn = url.openConnection();
042    
043                return true;
044            } catch (Throwable t) {
045    
046                return false;
047            }
048        }
049    
050        public IAsset createAsset(Resource baseResource, IComponentSpecification spec, String path, Locale locale, Location location)
051        {
052            return new ExternalAsset(path, location);
053        }
054    
055        public IAsset createAsset(Resource resource, Location location)
056        {
057            return new ExternalAsset(resource.getPath(), location);
058        }
059    
060        public IAsset createAbsoluteAsset(String path, Locale locale, Location location)
061        {
062            return new ExternalAsset(path, location);
063        }
064    
065    }