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.deploy.sandbox.SandboxHandler;
018    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
019    import com.liferay.portal.kernel.cache.PortalCache;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
021    import com.liferay.portal.kernel.template.StringTemplateResource;
022    import com.liferay.portal.kernel.template.Template;
023    import com.liferay.portal.kernel.template.TemplateConstants;
024    import com.liferay.portal.kernel.template.TemplateException;
025    import com.liferay.portal.kernel.template.TemplateResource;
026    import com.liferay.portal.kernel.template.TemplateResourceLoader;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    
030    import java.io.Serializable;
031    import java.io.Writer;
032    
033    import java.util.HashMap;
034    import java.util.Map;
035    import java.util.Set;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Tina Tian
041     */
042    public abstract class AbstractTemplate implements Template {
043    
044            public AbstractTemplate(
045                    TemplateResource templateResource,
046                    TemplateResource errorTemplateResource, Map<String, Object> context,
047                    TemplateContextHelper templateContextHelper, String templateManagerName,
048                    long interval) {
049    
050                    if (templateResource == null) {
051                            throw new IllegalArgumentException("Template resource is null");
052                    }
053    
054                    if (templateContextHelper == null) {
055                            throw new IllegalArgumentException(
056                                    "Template context helper is null");
057                    }
058    
059                    if (templateManagerName == null) {
060                            throw new IllegalArgumentException("Template manager name is null");
061                    }
062    
063                    this.templateResource = templateResource;
064                    this.errorTemplateResource = errorTemplateResource;
065    
066                    this.context = new HashMap<String, Object>();
067    
068                    if (context != null) {
069                            for (Map.Entry<String, Object> entry : context.entrySet()) {
070                                    put(entry.getKey(), entry.getValue());
071                            }
072                    }
073    
074                    _templateContextHelper = templateContextHelper;
075    
076                    if (interval != 0) {
077                            _cacheTemplateResource(templateManagerName);
078                    }
079            }
080    
081            @Override
082            public Object get(String key) {
083                    if (key == null) {
084                            return null;
085                    }
086    
087                    return context.get(key);
088            }
089    
090            @Override
091            public String[] getKeys() {
092                    Set<String> keys = context.keySet();
093    
094                    return keys.toArray(new String[keys.size()]);
095            }
096    
097            @Override
098            public void prepare(HttpServletRequest request) {
099                    _templateContextHelper.prepare(this, request);
100            }
101    
102            @Override
103            public void processTemplate(Writer writer) throws TemplateException {
104                    if (errorTemplateResource == null) {
105                            try {
106                                    processTemplate(templateResource, writer);
107    
108                                    return;
109                            }
110                            catch (Exception e) {
111                                    throw new TemplateException(
112                                            "Unable to process template " +
113                                                    templateResource.getTemplateId(),
114                                            e);
115                            }
116                    }
117    
118                    Writer oldWriter = (Writer)get(TemplateConstants.WRITER);
119    
120                    try {
121                            UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
122    
123                            put(TemplateConstants.WRITER, unsyncStringWriter);
124    
125                            processTemplate(templateResource, unsyncStringWriter);
126    
127                            StringBundler sb = unsyncStringWriter.getStringBundler();
128    
129                            sb.writeTo(writer);
130                    }
131                    catch (Exception e) {
132                            put(TemplateConstants.WRITER, writer);
133    
134                            handleException(e, writer);
135                    }
136                    finally {
137                            put(TemplateConstants.WRITER, oldWriter);
138                    }
139            }
140    
141            @Override
142            public void put(String key, Object value) {
143                    if ((key == null) || (value == null)) {
144                            return;
145                    }
146    
147                    context.put(key, value);
148            }
149    
150            protected String getTemplateResourceUUID(
151                    TemplateResource templateResource) {
152    
153                    return TemplateConstants.TEMPLATE_RESOURCE_UUID_PREFIX.concat(
154                            StringPool.POUND).concat(templateResource.getTemplateId());
155            }
156    
157            protected abstract void handleException(Exception exception, Writer writer)
158                    throws TemplateException;
159    
160            protected abstract void processTemplate(
161                            TemplateResource templateResource, Writer writer)
162                    throws Exception;
163    
164            protected Map<String, Object> context;
165            protected TemplateResource errorTemplateResource;
166            protected TemplateResource templateResource;
167    
168            private void _cacheTemplateResource(String templateManagerName) {
169                    String templateId = templateResource.getTemplateId();
170    
171                    if (templateManagerName.equals(TemplateConstants.LANG_TYPE_VM) &&
172                            templateId.contains(SandboxHandler.SANDBOX_MARKER)) {
173    
174                            return;
175                    }
176    
177                    if (!(templateResource instanceof CacheTemplateResource) &&
178                            !(templateResource instanceof StringTemplateResource)) {
179    
180                            templateResource = new CacheTemplateResource(templateResource);
181                    }
182    
183                    String cacheName = TemplateResourceLoader.class.getName();
184    
185                    cacheName = cacheName.concat(StringPool.PERIOD).concat(
186                            templateManagerName);
187    
188                    PortalCache<String, Serializable> portalCache =
189                            MultiVMPoolUtil.getCache(cacheName);
190    
191                    Object object = portalCache.get(templateResource.getTemplateId());
192    
193                    if ((object == null) || !templateResource.equals(object)) {
194                            portalCache.put(templateResource.getTemplateId(), templateResource);
195                    }
196    
197                    if (errorTemplateResource == null) {
198                            return;
199                    }
200    
201                    String errorTemplateId = errorTemplateResource.getTemplateId();
202    
203                    if (templateManagerName.equals(TemplateConstants.LANG_TYPE_VM) &&
204                            errorTemplateId.contains(SandboxHandler.SANDBOX_MARKER)) {
205    
206                            return;
207                    }
208    
209                    if (!(errorTemplateResource instanceof CacheTemplateResource) &&
210                            !(errorTemplateResource instanceof StringTemplateResource)) {
211    
212                            errorTemplateResource = new CacheTemplateResource(
213                                    errorTemplateResource);
214                    }
215    
216                    object = portalCache.get(errorTemplateResource.getTemplateId());
217    
218                    if ((object == null) || !errorTemplateResource.equals(object)) {
219                            portalCache.put(
220                                    errorTemplateResource.getTemplateId(), errorTemplateResource);
221                    }
222            }
223    
224            private TemplateContextHelper _templateContextHelper;
225    
226    }