001    /**
002     * Copyright (c) 2000-present 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.URLUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
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.URL;
029    import java.net.URLConnection;
030    
031    /**
032     * @author Tina Tian
033     */
034    public class URLTemplateResource implements TemplateResource {
035    
036            /**
037             * The empty constructor is required by {@link java.io.Externalizable}. Do
038             * not use this for any other purpose.
039             */
040            public URLTemplateResource() {
041            }
042    
043            public URLTemplateResource(String templateId, URL templateURL) {
044                    if (Validator.isNull(templateId)) {
045                            throw new IllegalArgumentException("Template ID is null");
046                    }
047    
048                    if (templateURL == null) {
049                            throw new IllegalArgumentException("Template URL is null");
050                    }
051    
052                    _templateId = templateId;
053                    _templateURL = templateURL;
054                    _templateURLExternalForm = templateURL.toExternalForm();
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                            _templateURLExternalForm.equals(
071                                    urlTemplateResource._templateURLExternalForm)) {
072    
073                            return true;
074                    }
075    
076                    return false;
077            }
078    
079            @Override
080            public long getLastModified() {
081                    try {
082                            return URLUtil.getLastModifiedTime(_templateURL);
083                    }
084                    catch (IOException ioe) {
085                            _log.error(
086                                    "Unable to get last modified time for template " + _templateId,
087                                    ioe);
088    
089                            return 0;
090                    }
091            }
092    
093            @Override
094            public Reader getReader() throws IOException {
095                    URLConnection urlConnection = _templateURL.openConnection();
096    
097                    return new InputStreamReader(
098                            urlConnection.getInputStream(), TemplateConstants.DEFAUT_ENCODING);
099            }
100    
101            @Override
102            public String getTemplateId() {
103                    return _templateId;
104            }
105    
106            @Override
107            public int hashCode() {
108                    return _templateId.hashCode() * 11 +
109                            _templateURLExternalForm.hashCode();
110            }
111    
112            @Override
113            public void readExternal(ObjectInput objectInput) throws IOException {
114                    _templateId = objectInput.readUTF();
115                    _templateURLExternalForm = objectInput.readUTF();
116    
117                    _templateURL = new URL(_templateURLExternalForm);
118            }
119    
120            @Override
121            public void writeExternal(ObjectOutput objectOutput) throws IOException {
122                    objectOutput.writeUTF(_templateId);
123                    objectOutput.writeUTF(_templateURLExternalForm);
124            }
125    
126            private static final Log _log = LogFactoryUtil.getLog(
127                    URLTemplateResource.class);
128    
129            private String _templateId;
130            private URL _templateURL;
131            private String _templateURLExternalForm;
132    
133    }