001    /**
002     * Copyright (c) 2000-2012 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.util.freemarker;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryItem;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.memory.FinalizeAction;
020    import com.liferay.portal.kernel.memory.FinalizeManager;
021    
022    import freemarker.ext.jsp.TaglibFactory;
023    
024    import freemarker.template.TemplateHashModel;
025    import freemarker.template.TemplateModel;
026    import freemarker.template.TemplateModelException;
027    
028    import java.util.Map;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    import javax.servlet.ServletContext;
032    
033    import jodd.util.StringPool;
034    
035    /**
036     * @author Shuyang Zhou
037     */
038    public class FreeMarkerTaglibFactoryUtil implements CacheRegistryItem {
039    
040            public static TemplateHashModel createTaglibFactory(
041                    ServletContext servletContext) {
042    
043                    return new TaglibFactoryCacheWrapper(servletContext);
044            }
045    
046            public String getRegistryName() {
047                    return _registryName;
048            }
049    
050            public void invalidate() {
051                    _templateModels.clear();
052            }
053    
054            private FreeMarkerTaglibFactoryUtil(String contextPath) {
055                    _contextPath = contextPath;
056                    _registryName = FreeMarkerTaglibFactoryUtil.class.getName().concat(
057                            StringPool.AT).concat(_contextPath);
058            }
059    
060            private static FreeMarkerTaglibFactoryUtil getInstance(
061                    ServletContext servletContext) {
062    
063                    if (_instance == null) {
064                            synchronized(FreeMarkerTaglibFactoryUtil.class) {
065                                    if (_instance == null) {
066                                            String contextPath = servletContext.getContextPath();
067    
068                                            // First call within current class loader
069    
070                                            _instance = new FreeMarkerTaglibFactoryUtil(contextPath);
071    
072                                            // Unregister previous one if there is one, this should only
073                                            // happen on plugin redeploy
074    
075                                            CacheRegistryUtil.unregister(_instance._registryName);
076    
077                                            // Register current instance
078    
079                                            CacheRegistryUtil.register(_instance);
080    
081                                            // Save a hard stack copy to prevent Tomcat null out heap
082                                            // reference
083    
084                                            final String name = _instance._registryName;
085    
086                                            // Bind _instance lifecycle to servlet context to prevent
087                                            // memory leak on undeploy
088    
089                                            FinalizeManager.register(
090                                                    servletContext,
091                                                    new FinalizeAction() {
092    
093                                                            public void doFinalize() {
094                                                                    CacheRegistryUtil.unregister(name);
095                                                            }
096    
097                                                    });
098                                    }
099                            }
100                    }
101    
102                    return _instance;
103            }
104    
105            private static volatile FreeMarkerTaglibFactoryUtil _instance;
106    
107            private final String _contextPath;
108            private final String _registryName;
109            private Map<String, TemplateModel> _templateModels =
110                    new ConcurrentHashMap<String, TemplateModel>();
111    
112            private static class TaglibFactoryCacheWrapper
113                    implements TemplateHashModel {
114    
115                    public TaglibFactoryCacheWrapper(ServletContext servletContext) {
116                            FreeMarkerTaglibFactoryUtil freeMarkerTaglibFactoryUtil =
117                                    getInstance(servletContext);
118    
119                            _templateModels = freeMarkerTaglibFactoryUtil._templateModels;
120                            _taglibFactory = new TaglibFactory(servletContext);
121                    }
122    
123                    public TemplateModel get(String uri) throws TemplateModelException {
124                            TemplateModel templateModel = _templateModels.get(uri);
125    
126                            if (templateModel == null) {
127                                    templateModel = _taglibFactory.get(uri);
128    
129                                    _templateModels.put(uri, templateModel);
130                            }
131    
132                            return templateModel;
133                    }
134    
135                    public boolean isEmpty() {
136                            return false;
137                    }
138    
139                    private TaglibFactory _taglibFactory;
140                    private Map<String, TemplateModel> _templateModels;
141    
142            }
143    
144    }