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.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    import com.liferay.portal.kernel.util.ContextPathUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import freemarker.ext.jsp.TaglibFactory;
025    
026    import freemarker.template.TemplateHashModel;
027    import freemarker.template.TemplateModel;
028    import freemarker.template.TemplateModelException;
029    
030    import java.util.Map;
031    import java.util.concurrent.ConcurrentHashMap;
032    
033    import javax.servlet.ServletContext;
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            @Override
047            public String getRegistryName() {
048                    return _registryName;
049            }
050    
051            @Override
052            public void invalidate() {
053                    _templateModels.clear();
054            }
055    
056            private static FreeMarkerTaglibFactoryUtil _getInstance(
057                    ServletContext servletContext) {
058    
059                    if (_instance != null) {
060                            return _instance;
061                    }
062    
063                    synchronized(FreeMarkerTaglibFactoryUtil.class) {
064                            if (_instance == null) {
065                                    String contextPath = ContextPathUtil.getContextPath(
066                                            servletContext);
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 memory
087                                    // leak on undeploy
088    
089                                    FinalizeManager.register(
090                                            servletContext,
091                                            new FinalizeAction() {
092    
093                                                    @Override
094                                                    public void doFinalize() {
095                                                            CacheRegistryUtil.unregister(name);
096                                                    }
097    
098                                            });
099                            }
100                    }
101    
102                    return _instance;
103            }
104    
105            private FreeMarkerTaglibFactoryUtil(String contextPath) {
106                    _contextPath = contextPath;
107                    _registryName = FreeMarkerTaglibFactoryUtil.class.getName().concat(
108                            StringPool.AT).concat(_contextPath);
109            }
110    
111            private static volatile FreeMarkerTaglibFactoryUtil _instance;
112    
113            private final String _contextPath;
114            private final String _registryName;
115            private Map<String, TemplateModel> _templateModels =
116                    new ConcurrentHashMap<String, TemplateModel>();
117    
118            private static class TaglibFactoryCacheWrapper
119                    implements TemplateHashModel {
120    
121                    public TaglibFactoryCacheWrapper(ServletContext servletContext) {
122                            FreeMarkerTaglibFactoryUtil freeMarkerTaglibFactoryUtil =
123                                    _getInstance(servletContext);
124    
125                            _templateModels = freeMarkerTaglibFactoryUtil._templateModels;
126                            _taglibFactory = new TaglibFactory(servletContext);
127                    }
128    
129                    @Override
130                    public TemplateModel get(String uri) throws TemplateModelException {
131                            TemplateModel templateModel = _templateModels.get(uri);
132    
133                            if (templateModel == null) {
134                                    templateModel = _taglibFactory.get(uri);
135    
136                                    _templateModels.put(uri, templateModel);
137                            }
138    
139                            return templateModel;
140                    }
141    
142                    @Override
143                    public boolean isEmpty() {
144                            return false;
145                    }
146    
147                    private TaglibFactory _taglibFactory;
148                    private Map<String, TemplateModel> _templateModels;
149    
150            }
151    
152    }