    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.io.IOException;
    017import java.io.InputStream;
    018import java.net.MalformedURLException;
    019import java.net.URL;
    020import java.util.Locale;
    021
    022import org.apache.tapestry5.commons.Resource;
    023import org.apache.tapestry5.internal.util.VirtualResource;
    024
    025public class UrlResource extends VirtualResource {
    026
    027    final public static URL PLACEHOLDER_URL;
    028
    029    static {
    030        try
    031        {
    032            PLACEHOLDER_URL = new URL("http://tapestry.apache.org");
    033        }
    034        catch (MalformedURLException e)
    035        {
    036            throw new ExceptionInInitializerError(e);
    037        }
    038    }
    039
    040    final private URL url;
    041
    042    private Boolean exists;
    043
    044    private InputStream inputStream;
    045
    046    public UrlResource()
    047    {
    048        this(PLACEHOLDER_URL);
    049        exists = true;
    050    }
    051
    052    public UrlResource(URL url)
    053    {
    054        super();
    055        this.url = url;
    056    }
    057
    058    @Override
    059    public InputStream openStream() throws IOException
    060    {
    061        if (exists == null)
    062        {
    063            try
    064            {
    065                inputStream = url.openStream();
    066                exists = true;
    067            }
    068            catch (IOException e)
    069            {
    070                exists = false;
    071                throw e;
    072            }
    073        }
    074        return inputStream;
    075    }
    076
    077    @Override
    078    public URL toURL()
    079    {
    080        return url;
    081    }
    082
    083    @Override
    084    public boolean exists()
    085    {
    086        if (exists == null)
    087        {
    088            try
    089            {
    090                openStream();
    091            }
    092            catch (IOException e)
    093            {
    094                // openStream() will always set the exists field.
    095            }
    096        }
    097        return exists;
    098    }
    099
    100    @Override
    101    public Resource forLocale(Locale locale)
    102    {
    103        return this;
    104    }
    105
    106    @Override
    107    public String toString()
    108    {
    109        return "UrlResource [url=" + url + "]";
    110    }
    111
    112}