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