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.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                    Reader reader = null;
086    
087                    try {
088                            reader = _templateResource.getReader();
089    
090                            char[] buffer = new char[1024];
091    
092                            int result = -1;
093    
094                            UnsyncCharArrayWriter unsyncCharArrayWriter =
095                                    new UnsyncCharArrayWriter();
096    
097                            while ((result = reader.read(buffer)) != -1) {
098                                    unsyncCharArrayWriter.write(buffer, 0, result);
099                            }
100    
101                            templateContent = unsyncCharArrayWriter.toString();
102    
103                            _templateContent.set(templateContent);
104                    }
105                    finally {
106                            if (reader != null) {
107                                    reader.close();
108                            }
109                    }
110    
111                    return new UnsyncStringReader(templateContent);
112            }
113    
114            @Override
115            public String getTemplateId() {
116                    return _templateResource.getTemplateId();
117            }
118    
119            @Override
120            public int hashCode() {
121                    return _templateResource.hashCode();
122            }
123    
124            @Override
125            public void readExternal(ObjectInput objectInput)
126                    throws ClassNotFoundException, IOException {
127    
128                    _lastModified = objectInput.readLong();
129                    _templateResource = (TemplateResource)objectInput.readObject();
130            }
131    
132            @Override
133            public void writeExternal(ObjectOutput objectOutput) throws IOException {
134                    objectOutput.writeLong(_lastModified);
135                    objectOutput.writeObject(_templateResource);
136            }
137    
138            private long _lastModified = System.currentTimeMillis();
139            private AtomicReference<String> _templateContent =
140                    new AtomicReference<String>();
141            private TemplateResource _templateResource;
142    
143    }