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