1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.velocity;
16  
17  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18  import com.liferay.portal.kernel.cache.PortalCache;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import org.apache.velocity.runtime.resource.util.StringResource;
22  import org.apache.velocity.runtime.resource.util.StringResourceRepository;
23  
24  /**
25   * <a href="StringResourceRepositoryImpl.java.html"><b><i>View Source</i></b>
26   * </a>
27   *
28   * @author Raymond Augé
29   */
30  public class StringResourceRepositoryImpl implements StringResourceRepository {
31  
32      public static final String CACHE_NAME =
33          LiferayResourceCacheUtil.class.getName();
34  
35      public String getEncoding() {
36          return _encoding;
37      }
38  
39      public StringResource getStringResource(String key) {
40          Object resource = _portalCache.get(key);
41  
42          if ((resource != null) && (resource instanceof StringResource)) {
43              return (StringResource)resource;
44          }
45  
46          return null;
47      }
48  
49      public void putStringResource(String key, String body) {
50          _portalCache.put(key , new StringResource(body, getEncoding()));
51      }
52  
53      public void putStringResource(String key, String body, String encoding) {
54          _portalCache.put(key , new StringResource(body, encoding));
55      }
56  
57      public void removeStringResource(String key) {
58          _portalCache.remove(key);
59      }
60  
61      public void setEncoding(String encoding) {
62          _encoding = encoding;
63      }
64  
65      private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
66          CACHE_NAME);
67  
68      private String _encoding = StringPool.UTF8;
69  
70  }