1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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) &&
43              (resource instanceof SerializableStringResource)) {
44  
45              SerializableStringResource serializableStringResource =
46                  (SerializableStringResource)resource;
47  
48              return serializableStringResource.toStringResource();
49          }
50  
51          return null;
52      }
53  
54      public void putStringResource(String key, String body) {
55          _portalCache.put(
56              key , new SerializableStringResource(body, getEncoding()));
57      }
58  
59      public void putStringResource(String key, String body, String encoding) {
60          _portalCache.put(key , new SerializableStringResource(body, encoding));
61      }
62  
63      public void removeStringResource(String key) {
64          _portalCache.remove(key);
65      }
66  
67      public void setEncoding(String encoding) {
68          _encoding = encoding;
69      }
70  
71      private static PortalCache _portalCache = MultiVMPoolUtil.getCache(
72          CACHE_NAME);
73  
74      private String _encoding = StringPool.UTF8;
75  
76  }