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