001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public boolean equals(Object obj) {
034                    if (obj instanceof URLTemplateSource) {
035                            URLTemplateSource urlTemplateSource = (URLTemplateSource)obj;
036    
037                            if (_url.equals(urlTemplateSource._url)) {
038                                    return true;
039                            }
040                    }
041    
042                    return false;
043            }
044    
045            public int hashCode() {
046                    return _url.hashCode();
047            }
048    
049            public String toString() {
050                    return _url.toString();
051            }
052    
053            protected void closeStream() throws IOException {
054                    try {
055                            if (_inputStream != null) {
056                                    _inputStream.close();
057                            }
058                            else {
059                                    _urlConnection.getInputStream().close();
060                            }
061                    }
062                    finally {
063                            _inputStream = null;
064                            _urlConnection = null;
065                    }
066            }
067    
068            protected InputStream getInputStream() throws IOException {
069                    _inputStream = _urlConnection.getInputStream();
070    
071                    return _inputStream;
072            }
073    
074            protected long getLastModified() {
075                    return _urlConnection.getLastModified();
076            }
077    
078            private InputStream _inputStream;
079            private URL _url;
080            private URLConnection _urlConnection;
081    
082    }