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.template;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncCharArrayWriter;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.template.TemplateResource;
020    
021    import java.io.IOException;
022    import java.io.ObjectInput;
023    import java.io.ObjectOutput;
024    import java.io.Reader;
025    
026    import java.util.concurrent.atomic.AtomicReference;
027    
028    /**
029     * @author Tina Tian
030     */
031    public class CacheTemplateResource implements TemplateResource {
032    
033            /**
034             * The empty constructor is required by {@link java.io.Externalizable}. Do
035             * not use this for any other purpose.
036             */
037            public CacheTemplateResource() {
038            }
039    
040            public CacheTemplateResource(TemplateResource templateResource) {
041                    if (templateResource == null) {
042                            throw new IllegalArgumentException("Template resource is null");
043                    }
044    
045                    _templateResource = templateResource;
046            }
047    
048            @Override
049            public boolean equals(Object obj) {
050                    if (this == obj) {
051                            return true;
052                    }
053    
054                    if (!(obj instanceof CacheTemplateResource)) {
055                            return false;
056                    }
057    
058                    CacheTemplateResource cacheTemplateResource =
059                            (CacheTemplateResource)obj;
060    
061                    if (_templateResource.equals(cacheTemplateResource._templateResource)) {
062                            return true;
063                    }
064    
065                    return false;
066            }
067    
068            public TemplateResource getInnerTemplateResource() {
069                    return _templateResource;
070            }
071    
072            @Override
073            public long getLastModified() {
074                    return _lastModified;
075            }
076    
077            @Override
078            public Reader getReader() throws IOException {
079                    String templateContent = _templateContent.get();
080    
081                    if (templateContent != null) {
082                            return new UnsyncStringReader(templateContent);
083                    }
084    
085                    try (Reader reader = _templateResource.getReader()) {
086                            char[] buffer = new char[1024];
087    
088                            int result = -1;
089    
090                            UnsyncCharArrayWriter unsyncCharArrayWriter =
091                                    new UnsyncCharArrayWriter();
092    
093                            while ((result = reader.read(buffer)) != -1) {
094                                    unsyncCharArrayWriter.write(buffer, 0, result);
095                            }
096    
097                            templateContent = unsyncCharArrayWriter.toString();
098    
099                            _templateContent.set(templateContent);
100                    }
101    
102                    return new UnsyncStringReader(templateContent);
103            }
104    
105            @Override
106            public String getTemplateId() {
107                    return _templateResource.getTemplateId();
108            }
109    
110            @Override
111            public int hashCode() {
112                    return _templateResource.hashCode();
113            }
114    
115            @Override
116            public void readExternal(ObjectInput objectInput)
117                    throws ClassNotFoundException, IOException {
118    
119                    _lastModified = objectInput.readLong();
120                    _templateResource = (TemplateResource)objectInput.readObject();
121            }
122    
123            @Override
124            public void writeExternal(ObjectOutput objectOutput) throws IOException {
125                    objectOutput.writeLong(_lastModified);
126                    objectOutput.writeObject(_templateResource);
127            }
128    
129            private long _lastModified = System.currentTimeMillis();
130            private final AtomicReference<String> _templateContent =
131                    new AtomicReference<>();
132            private TemplateResource _templateResource;
133    
134    }