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 public long getLastModified() {
079 URLConnection urlConnection = null;
080
081 try {
082 urlConnection = _templateURL.openConnection();
083
084 if (urlConnection instanceof JarURLConnection) {
085 JarURLConnection jarURLConnection =
086 (JarURLConnection)urlConnection;
087
088 URL url = jarURLConnection.getJarFileURL();
089
090 String protocol = url.getProtocol();
091
092 if (protocol.equals("file")) {
093 return new File(url.getFile()).lastModified();
094 }
095 else {
096 urlConnection = url.openConnection();
097 }
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 public Reader getReader() throws IOException {
121 URLConnection urlConnection = _templateURL.openConnection();
122
123 return new InputStreamReader(
124 urlConnection.getInputStream(), DEFAUT_ENCODING);
125 }
126
127 public String getTemplateId() {
128 return _templateId;
129 }
130
131 @Override
132 public int hashCode() {
133 return _templateId.hashCode() * 11 + _templateURL.hashCode();
134 }
135
136 public void readExternal(ObjectInput objectInput) throws IOException {
137 _templateId = objectInput.readUTF();
138 _templateURL = new URL(objectInput.readUTF());
139 }
140
141 public void writeExternal(ObjectOutput objectOutput) throws IOException {
142 objectOutput.writeUTF(_templateId);
143 objectOutput.writeUTF(_templateURL.toExternalForm());
144 }
145
146 private static Log _log = LogFactoryUtil.getLog(URLTemplateResource.class);
147
148 private String _templateId;
149 private URL _templateURL;
150
151 }