001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.freemarker;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import freemarker.cache.TemplateLoader;
020    
021    import java.io.Reader;
022    import java.io.StringReader;
023    
024    import java.util.Map;
025    import java.util.concurrent.ConcurrentHashMap;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class StringTemplateLoader implements TemplateLoader {
031    
032            @Override
033            public void closeTemplateSource(Object templateSource) {
034            }
035    
036            @Override
037            public Object findTemplateSource(String name) {
038                    return _templates.get(name);
039            }
040    
041            @Override
042            public long getLastModified(Object templateSource) {
043                    StringTemplateSource stringTemplateSource =
044                            (StringTemplateSource)templateSource;
045    
046                    return stringTemplateSource._lastModified;
047            }
048    
049            @Override
050            public Reader getReader(Object templateSource, String encoding) {
051                    StringTemplateSource stringTemplateSource =
052                            (StringTemplateSource)templateSource;
053    
054                    return new StringReader(stringTemplateSource._templateSource);
055            }
056    
057            public void putTemplate(String name, String templateSource) {
058                    putTemplate(name, templateSource, System.currentTimeMillis());
059            }
060    
061            public void putTemplate(
062                    String name, String templateSource, long lastModified) {
063    
064                    _templates.put(
065                            name, new StringTemplateSource(name, templateSource, lastModified));
066            }
067    
068            public void removeTemplate(String name) {
069                    _templates.remove(name);
070            }
071    
072            private Map<String, StringTemplateSource> _templates =
073                    new ConcurrentHashMap<String, StringTemplateSource>();
074    
075            private class StringTemplateSource {
076    
077                    public StringTemplateSource(
078                            String name, String templateSource, long lastModified) {
079    
080                            if (name == null) {
081                                    throw new IllegalArgumentException("Name is null");
082                            }
083    
084                            if (templateSource == null) {
085                                    throw new IllegalArgumentException("Template source is null");
086                            }
087    
088                            if (lastModified < -1) {
089                                    throw new IllegalArgumentException(
090                                            "Last modified is less than -1");
091                            }
092    
093                            _name = name;
094                            _templateSource = templateSource;
095                            _lastModified = lastModified;
096                    }
097    
098                    @Override
099                    public boolean equals(Object obj) {
100                            if (this == obj) {
101                                    return true;
102                            }
103    
104                            if (!(obj instanceof StringTemplateSource)) {
105                                    return false;
106                            }
107    
108                            StringTemplateSource stringTemplateSource =
109                                    (StringTemplateSource)obj;
110    
111                            if (Validator.equals(_name, stringTemplateSource._name)) {
112                                    return true;
113                            }
114    
115                            return false;
116                    }
117    
118                    @Override
119                    public int hashCode() {
120                            return _name.hashCode();
121                    }
122    
123                    private long _lastModified;
124                    private String _name;
125                    private String _templateSource;
126    
127            }
128    
129    }