001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.freemarker;
016    
017    import java.io.IOException;
018    import java.io.InputStream;
019    
020    import java.net.URL;
021    import java.net.URLConnection;
022    
023    /**
024     * @author Mika Koivisto
025     */
026    public class URLTemplateSource {
027    
028            public URLTemplateSource(URL url) throws IOException {
029                    _url = url;
030                    _urlConnection = url.openConnection();
031            }
032    
033            @Override
034            public boolean equals(Object obj) {
035                    if (obj instanceof URLTemplateSource) {
036                            URLTemplateSource urlTemplateSource = (URLTemplateSource)obj;
037    
038                            if (_url.equals(urlTemplateSource._url)) {
039                                    return true;
040                            }
041                    }
042    
043                    return false;
044            }
045    
046            @Override
047            public int hashCode() {
048                    return _url.hashCode();
049            }
050    
051            @Override
052            public String toString() {
053                    return _url.toString();
054            }
055    
056            protected void closeStream() throws IOException {
057                    try {
058                            if (_inputStream != null) {
059                                    _inputStream.close();
060                            }
061                            else {
062                                    _urlConnection.getInputStream().close();
063                            }
064                    }
065                    finally {
066                            _inputStream = null;
067                            _urlConnection = null;
068                    }
069            }
070    
071            protected InputStream getInputStream() throws IOException {
072                    _inputStream = _urlConnection.getInputStream();
073    
074                    return _inputStream;
075            }
076    
077            protected long getLastModified() {
078                    return _urlConnection.getLastModified();
079            }
080    
081            private InputStream _inputStream;
082            private URL _url;
083            private URLConnection _urlConnection;
084    
085    }