1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.freemarker;
16  
17  import java.io.IOException;
18  import java.io.InputStream;
19  
20  import java.net.URL;
21  import java.net.URLConnection;
22  
23  /**
24   * <a href="URLTemplateSource.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Mika Koivisto
27   */
28  public class URLTemplateSource {
29  
30      public URLTemplateSource(URL url) throws IOException {
31          _url = url;
32          _urlConnection = url.openConnection();
33      }
34  
35      public boolean equals(Object obj) {
36          if (obj instanceof URLTemplateSource) {
37              URLTemplateSource urlTemplateSource = (URLTemplateSource)obj;
38  
39              if (_url.equals(urlTemplateSource._url)) {
40                  return true;
41              }
42          }
43  
44          return false;
45      }
46  
47      public int hashCode() {
48          return _url.hashCode();
49      }
50  
51      public String toString() {
52          return _url.toString();
53      }
54  
55      protected void closeStream() throws IOException {
56          try {
57              if (_inputStream != null) {
58                  _inputStream.close();
59              }
60              else {
61                  _urlConnection.getInputStream().close();
62              }
63          }
64          finally {
65              _inputStream = null;
66              _urlConnection = null;
67          }
68      }
69  
70      protected InputStream getInputStream() throws IOException {
71          _inputStream = _urlConnection.getInputStream();
72  
73          return _inputStream;
74      }
75  
76      protected long getLastModified() {
77          return _urlConnection.getLastModified();
78      }
79  
80      private InputStream _inputStream;
81      private URL _url;
82      private URLConnection _urlConnection;
83  
84  }