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            }
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                                    else {
097                                            urlConnection = url.openConnection();
098                                    }
099                            }
100    
101                            return urlConnection.getLastModified();
102                    }
103                    catch (IOException ioe) {
104                            _log.error(
105                                    "Unable to get last modified time for template " + _templateId,
106                                    ioe);
107    
108                            return 0;
109                    }
110                    finally {
111                            if (urlConnection != null) {
112                                    try {
113                                            urlConnection.getInputStream().close();
114                                    }
115                                    catch (IOException ioe) {
116                                    }
117                            }
118                    }
119            }
120    
121            @Override
122            public Reader getReader() throws IOException {
123                    URLConnection urlConnection = _templateURL.openConnection();
124    
125                    return new InputStreamReader(
126                            urlConnection.getInputStream(), TemplateConstants.DEFAUT_ENCODING);
127            }
128    
129            @Override
130            public String getTemplateId() {
131                    return _templateId;
132            }
133    
134            @Override
135            public int hashCode() {
136                    return _templateId.hashCode() * 11 + _templateURL.hashCode();
137            }
138    
139            @Override
140            public void readExternal(ObjectInput objectInput) throws IOException {
141                    _templateId = objectInput.readUTF();
142                    _templateURL = new URL(objectInput.readUTF());
143            }
144    
145            @Override
146            public void writeExternal(ObjectOutput objectOutput) throws IOException {
147                    objectOutput.writeUTF(_templateId);
148                    objectOutput.writeUTF(_templateURL.toExternalForm());
149            }
150    
151            private static Log _log = LogFactoryUtil.getLog(URLTemplateResource.class);
152    
153            private String _templateId;
154            private URL _templateURL;
155    
156    }