001
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
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
069
070 _instance = new FreeMarkerTaglibFactoryUtil(contextPath);
071
072
073
074
075 CacheRegistryUtil.unregister(_instance._registryName);
076
077
078
079 CacheRegistryUtil.register(_instance);
080
081
082
083
084 final String name = _instance._registryName;
085
086
087
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 }