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.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    }