001// Copyright 2014 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.
014package org.apache.tapestry5.internal.services;
015
016import java.net.MalformedURLException;
017import java.net.URL;
018import java.util.Map;
019
020import org.apache.tapestry5.Asset;
021import org.apache.tapestry5.internal.AssetConstants;
022import org.apache.tapestry5.ioc.Resource;
023import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
024import org.apache.tapestry5.services.AssetFactory;
025
026public class ExternalUrlAssetFactory implements AssetFactory {
027    
028    final private String protocol;
029    
030    final private UrlResource rootResource;
031    
032    final private Map<URL, Asset> cache = CollectionFactory.newMap();
033
034    public ExternalUrlAssetFactory(String protocol)
035    {
036        super();
037        this.protocol = protocol;
038//        try
039//        {
040            if (protocol.equals(AssetConstants.PROTOCOL_RELATIVE)) {
041                protocol = "http";
042            }
043            this.rootResource = new UrlResource();
044//        }
045//        catch (MalformedURLException e)
046//        {
047//            throw new RuntimeException(e);
048//        }
049    }
050
051    @Override
052    public Resource getRootResource()
053    {
054        return rootResource;
055    }
056
057    @Override
058    public Asset createAsset(Resource resource)
059    {
060        final URL url = resource.toURL();
061        Asset asset = cache.get(url);
062        if (asset == null)
063        {
064            asset = new UrlAsset(url.toExternalForm(), resource);
065            cache.put(url, asset);
066        }
067        return asset;
068    }
069    
070}