001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
033     * @author Tina Tian
034     */
035    public class URLTemplateResource implements TemplateResource {
036    
037            /**
038             * The empty constructor is required by {@link java.io.Externalizable}. Do
039             * not use this for any other purpose.
040             */
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                    _templateURLExternalForm = templateURL.toExternalForm();
056            }
057    
058            @Override
059            public boolean equals(Object obj) {
060                    if (this == obj) {
061                            return true;
062                    }
063    
064                    if (!(obj instanceof URLTemplateResource)) {
065                            return false;
066                    }
067    
068                    URLTemplateResource urlTemplateResource = (URLTemplateResource)obj;
069    
070                    if (_templateId.equals(urlTemplateResource._templateId) &&
071                            _templateURLExternalForm.equals(
072                                    urlTemplateResource._templateURLExternalForm)) {
073    
074                            return true;
075                    }
076    
077                    return false;
078            }
079    
080            @Override
081            public long getLastModified() {
082                    URLConnection urlConnection = null;
083    
084                    try {
085                            urlConnection = _templateURL.openConnection();
086    
087                            if (urlConnection instanceof JarURLConnection) {
088                                    JarURLConnection jarURLConnection =
089                                            (JarURLConnection)urlConnection;
090    
091                                    URL url = jarURLConnection.getJarFileURL();
092    
093                                    String protocol = url.getProtocol();
094    
095                                    if (protocol.equals("file")) {
096                                            return new File(url.getFile()).lastModified();
097                                    }
098    
099                                    urlConnection = url.openConnection();
100                            }
101    
102                            return urlConnection.getLastModified();
103                    }
104                    catch (IOException ioe) {
105                            _log.error(
106                                    "Unable to get last modified time for template " + _templateId,
107                                    ioe);
108    
109                            return 0;
110                    }
111                    finally {
112                            if (urlConnection != null) {
113                                    try {
114                                            urlConnection.getInputStream().close();
115                                    }
116                                    catch (IOException ioe) {
117                                    }
118                            }
119                    }
120            }
121    
122            @Override
123            public Reader getReader() throws IOException {
124                    URLConnection urlConnection = _templateURL.openConnection();
125    
126                    return new InputStreamReader(
127                            urlConnection.getInputStream(), TemplateConstants.DEFAUT_ENCODING);
128            }
129    
130            @Override
131            public String getTemplateId() {
132                    return _templateId;
133            }
134    
135            @Override
136            public int hashCode() {
137                    return _templateId.hashCode() * 11 +
138                            _templateURLExternalForm.hashCode();
139            }
140    
141            @Override
142            public void readExternal(ObjectInput objectInput) throws IOException {
143                    _templateId = objectInput.readUTF();
144                    _templateURLExternalForm = objectInput.readUTF();
145    
146                    _templateURL = new URL(_templateURLExternalForm);
147            }
148    
149            @Override
150            public void writeExternal(ObjectOutput objectOutput) throws IOException {
151                    objectOutput.writeUTF(_templateId);
152                    objectOutput.writeUTF(_templateURLExternalForm);
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(URLTemplateResource.class);
156    
157            private String _templateId;
158            private URL _templateURL;
159            private String _templateURLExternalForm;
160    
161    }