001    /**
002     * Copyright (c) 2000-2010 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 static final String CACHE_NAME =
030                    LiferayResourceCacheUtil.class.getName();
031    
032            public String getEncoding() {
033                    return _encoding;
034            }
035    
036            public StringResource getStringResource(String key) {
037                    Object resource = _portalCache.get(key);
038    
039                    if ((resource != null) &&
040                            (resource instanceof SerializableStringResource)) {
041    
042                            SerializableStringResource serializableStringResource =
043                                    (SerializableStringResource)resource;
044    
045                            return serializableStringResource.toStringResource();
046                    }
047    
048                    return null;
049            }
050    
051            public void putStringResource(String key, String body) {
052                    _portalCache.put(
053                            key , new SerializableStringResource(body, getEncoding()));
054            }
055    
056            public void putStringResource(String key, String body, String encoding) {
057                    _portalCache.put(key , new SerializableStringResource(body, encoding));
058            }
059    
060            public void removeStringResource(String key) {
061                    _portalCache.remove(key);
062            }
063    
064            public void setEncoding(String encoding) {
065                    _encoding = encoding;
066            }
067    
068            private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
069                    CACHE_NAME);
070    
071            private String _encoding = StringPool.UTF8;
072    
073    }