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.portal.velocity;
016    
017    import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import org.apache.velocity.runtime.resource.util.StringResource;
022    import org.apache.velocity.runtime.resource.util.StringResourceRepository;
023    
024    /**
025     * @author Raymond Augé
026     */
027    public class StringResourceRepositoryImpl implements StringResourceRepository {
028    
029            public String getEncoding() {
030                    return _encoding;
031            }
032    
033            public StringResource getStringResource(String key) {
034                    Object resource = _portalCache.get(key);
035    
036                    if ((resource != null) &&
037                            (resource instanceof SerializableStringResource)) {
038    
039                            SerializableStringResource serializableStringResource =
040                                    (SerializableStringResource)resource;
041    
042                            return serializableStringResource.toStringResource();
043                    }
044    
045                    return null;
046            }
047    
048            public void putStringResource(String key, String body) {
049                    _portalCache.put(
050                            key, new SerializableStringResource(body, getEncoding()));
051            }
052    
053            public void putStringResource(String key, String body, String encoding) {
054                    _portalCache.put(key, new SerializableStringResource(body, encoding));
055            }
056    
057            public void removeStringResource(String key) {
058                    _portalCache.remove(key);
059            }
060    
061            public void setEncoding(String encoding) {
062                    _encoding = encoding;
063            }
064    
065            private static final String _CACHE_NAME =
066                    StringResourceRepository.class.getName();
067    
068            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
069                    _CACHE_NAME);
070    
071            private String _encoding = StringPool.UTF8;
072    
073    }