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.freemarker;
016    
017    import com.liferay.portal.kernel.util.PropsKeys;
018    import com.liferay.portal.util.PropsUtil;
019    
020    import freemarker.cache.CacheStorage;
021    import freemarker.cache.TemplateCache;
022    import freemarker.cache.TemplateLoader;
023    
024    import freemarker.template.Configuration;
025    import freemarker.template.Template;
026    
027    import java.io.IOException;
028    
029    import java.security.AccessController;
030    import java.security.PrivilegedActionException;
031    import java.security.PrivilegedExceptionAction;
032    
033    import java.util.Locale;
034    
035    /**
036     * @author Tina Tian
037     * @author Raymond Aug??
038     */
039    public class LiferayTemplateCache extends TemplateCache {
040    
041            public LiferayTemplateCache(TemplateCache templateCache) {
042                    _templateCache = templateCache;
043            }
044    
045            @Override
046            public void clear() {
047                    _templateCache.clear();
048            }
049    
050            @Override
051            public boolean equals(Object obj) {
052                    return _templateCache.equals(obj);
053            }
054    
055            @Override
056            public CacheStorage getCacheStorage() {
057                    return _templateCache.getCacheStorage();
058            }
059    
060            @Override
061            public long getDelay() {
062                    return _templateCache.getDelay();
063            }
064    
065            @Override
066            public boolean getLocalizedLookup() {
067                    return _templateCache.getLocalizedLookup();
068            }
069    
070            @Override
071            public Template getTemplate(
072                            String templateId, Locale locale, String encoding, boolean parse)
073                    throws IOException {
074    
075                    String[] macroTemplateIds = PropsUtil.getArray(
076                            PropsKeys.FREEMARKER_ENGINE_MACRO_LIBRARY);
077    
078                    for (String macroTemplateId : macroTemplateIds) {
079                            int pos = macroTemplateId.indexOf(" as ");
080    
081                            if (pos != -1) {
082                                    macroTemplateId = macroTemplateId.substring(0, pos);
083                            }
084    
085                            if (templateId.equals(macroTemplateId)) {
086    
087                                    // This template is provided by the portal, so invoke it from an
088                                    // access controller
089    
090                                    try {
091                                            return AccessController.doPrivileged(
092                                                    new TemplatePrivilegedExceptionAction(
093                                                            macroTemplateId, locale, encoding, parse));
094                                    }
095                                    catch (PrivilegedActionException pae) {
096                                            throw (IOException)pae.getException();
097                                    }
098                            }
099                    }
100    
101                    return _templateCache.getTemplate(templateId, locale, encoding, parse);
102            }
103    
104            @Override
105            public TemplateLoader getTemplateLoader() {
106                    return _templateCache.getTemplateLoader();
107            }
108    
109            @Override
110            public int hashCode() {
111                    return _templateCache.hashCode();
112            }
113    
114            @Override
115            public void setConfiguration(Configuration config) {
116                    _templateCache.setConfiguration(config);
117            }
118    
119            @Override
120            public void setDelay(long delay) {
121                    _templateCache.setDelay(delay);
122            }
123    
124            @Override
125            public void setLocalizedLookup(boolean localizedLookup) {
126                    _templateCache.setLocalizedLookup(localizedLookup);
127            }
128    
129            @Override
130            public String toString() {
131                    return _templateCache.toString();
132            }
133    
134            private TemplateCache _templateCache;
135    
136            private class TemplatePrivilegedExceptionAction
137                    implements PrivilegedExceptionAction<Template> {
138    
139                    public TemplatePrivilegedExceptionAction(
140                            String templateId, Locale locale, String encoding, boolean parse) {
141    
142                            _templateId = templateId;
143                            _locale = locale;
144                            _encoding = encoding;
145                            _parse = parse;
146                    }
147    
148                    @Override
149                    public Template run() throws Exception {
150                            return _templateCache.getTemplate(
151                                    _templateId, _locale, _encoding, _parse);
152                    }
153    
154                    private String _encoding;
155                    private Locale _locale;
156                    private boolean _parse;
157                    private String _templateId;
158    
159            }
160    
161    }