001
014
015 package com.liferay.portal.kernel.template;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.Validator;
020
021 import java.io.File;
022 import java.io.IOException;
023 import java.io.InputStreamReader;
024 import java.io.ObjectInput;
025 import java.io.ObjectOutput;
026 import java.io.Reader;
027
028 import java.net.JarURLConnection;
029 import java.net.URL;
030 import java.net.URLConnection;
031
032
035 public class URLTemplateResource implements TemplateResource {
036
037
041 public URLTemplateResource() {
042 }
043
044 public URLTemplateResource(String templateId, URL templateURL) {
045 if (Validator.isNull(templateId)) {
046 throw new IllegalArgumentException("Template ID is null");
047 }
048
049 if (templateURL == null) {
050 throw new IllegalArgumentException("Template URL is null");
051 }
052
053 _templateId = templateId;
054 _templateURL = templateURL;
055 }
056
057 @Override
058 public boolean equals(Object obj) {
059 if (this == obj) {
060 return true;
061 }
062
063 if (!(obj instanceof URLTemplateResource)) {
064 return false;
065 }
066
067 URLTemplateResource urlTemplateResource = (URLTemplateResource)obj;
068
069 if (_templateId.equals(urlTemplateResource._templateId) &&
070 _templateURL.equals(urlTemplateResource._templateURL)) {
071
072 return true;
073 }
074
075 return false;
076 }
077
078 @Override
079 public long getLastModified() {
080 URLConnection urlConnection = null;
081
082 try {
083 urlConnection = _templateURL.openConnection();
084
085 if (urlConnection instanceof JarURLConnection) {
086 JarURLConnection jarURLConnection =
087 (JarURLConnection)urlConnection;
088
089 URL url = jarURLConnection.getJarFileURL();
090
091 String protocol = url.getProtocol();
092
093 if (protocol.equals("file")) {
094 return new File(url.getFile()).lastModified();
095 }
096
097 urlConnection = url.openConnection();
098 }
099
100 return urlConnection.getLastModified();
101 }
102 catch (IOException ioe) {
103 _log.error(
104 "Unable to get last modified time for template " + _templateId,
105 ioe);
106
107 return 0;
108 }
109 finally {
110 if (urlConnection != null) {
111 try {
112 urlConnection.getInputStream().close();
113 }
114 catch (IOException ioe) {
115 }
116 }
117 }
118 }
119
120 @Override
121 public Reader getReader() throws IOException {
122 URLConnection urlConnection = _templateURL.openConnection();
123
124 return new InputStreamReader(
125 urlConnection.getInputStream(), TemplateConstants.DEFAUT_ENCODING);
126 }
127
128 @Override
129 public String getTemplateId() {
130 return _templateId;
131 }
132
133 @Override
134 public int hashCode() {
135 return _templateId.hashCode() * 11 + _templateURL.hashCode();
136 }
137
138 @Override
139 public void readExternal(ObjectInput objectInput) throws IOException {
140 _templateId = objectInput.readUTF();
141 _templateURL = new URL(objectInput.readUTF());
142 }
143
144 @Override
145 public void writeExternal(ObjectOutput objectOutput) throws IOException {
146 objectOutput.writeUTF(_templateId);
147 objectOutput.writeUTF(_templateURL.toExternalForm());
148 }
149
150 private static Log _log = LogFactoryUtil.getLog(URLTemplateResource.class);
151
152 private String _templateId;
153 private URL _templateURL;
154
155 }