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.kernel.dao.orm;
16  
17  import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
18  import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  
22  /**
23   * <a href="FinderPath.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Brian Wing Shun Chan
26   */
27  public class FinderPath {
28  
29      public FinderPath(
30          boolean entityCacheEnabled, boolean finderCacheEnabled,
31          String className, String methodName, String[] params) {
32  
33          _entityCacheEnabled = entityCacheEnabled;
34          _finderCacheEnabled = finderCacheEnabled;
35          _className = className;
36          _methodName = methodName;
37          _params = params;
38  
39          _initCacheKeyPrefix();
40          _initLocalCacheKeyPrefix();
41      }
42  
43      public String encodeCacheKey(Object[] args) {
44          StringBundler sb = new StringBundler(args.length * 2 + 1);
45  
46          sb.append(_cacheKeyPrefix);
47  
48          for (Object arg : args) {
49              sb.append(StringPool.PERIOD);
50              sb.append(String.valueOf(arg));
51          }
52  
53          CacheKeyGenerator cacheKeyGenerator =
54              CacheKeyGeneratorUtil.getCacheKeyGenerator(
55                  FinderCache.class.getName());
56  
57          return cacheKeyGenerator.getCacheKey(sb);
58      }
59  
60      public String encodeLocalCacheKey(Object[] args) {
61          StringBundler sb = new StringBundler(args.length * 2 + 1);
62  
63          sb.append(_localCacheKeyPrefix);
64  
65          for (Object arg : args) {
66              sb.append(StringPool.PERIOD);
67              sb.append(String.valueOf(arg));
68          }
69  
70          CacheKeyGenerator cacheKeyGenerator =
71              CacheKeyGeneratorUtil.getCacheKeyGenerator(
72                  FinderCache.class.getName());
73  
74          return cacheKeyGenerator.getCacheKey(sb);
75      }
76  
77      public String getClassName() {
78          return _className;
79      }
80  
81      public String getMethodName() {
82          return _methodName;
83      }
84  
85      public String[] getParams() {
86          return _params;
87      }
88  
89      public boolean isEntityCacheEnabled() {
90          return _entityCacheEnabled;
91      }
92  
93      public boolean isFinderCacheEnabled() {
94          return _finderCacheEnabled;
95      }
96  
97      private void _initCacheKeyPrefix() {
98          StringBundler sb = new StringBundler(_params.length * 2 + 3);
99  
100         sb.append(_methodName);
101         sb.append(_PARAMS_SEPARATOR);
102 
103         for (String param : _params) {
104             sb.append(StringPool.PERIOD);
105             sb.append(param);
106         }
107 
108         sb.append(_ARGS_SEPARATOR);
109 
110         _cacheKeyPrefix = sb.toString();
111     }
112 
113     private void _initLocalCacheKeyPrefix() {
114         _localCacheKeyPrefix = _className.concat(StringPool.PERIOD).concat(
115             _cacheKeyPrefix);
116     }
117 
118     private static final String _ARGS_SEPARATOR = "_A_";
119 
120     private static final String _PARAMS_SEPARATOR = "_P_";
121 
122     private String _cacheKeyPrefix;
123     private String _className;
124     private boolean _entityCacheEnabled;
125     private boolean _finderCacheEnabled;
126     private String _localCacheKeyPrefix;
127     private String _methodName;
128     private String[] _params;
129 
130 }