001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.dao.orm;
016    
017    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
018    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
019    import com.liferay.portal.kernel.dao.shard.ShardUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.Serializable;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class FinderPath {
030    
031            public FinderPath(
032                    boolean entityCacheEnabled, boolean finderCacheEnabled,
033                    Class<?> resultClass, String className, String methodName,
034                    String[] params) {
035    
036                    _entityCacheEnabled = entityCacheEnabled;
037                    _finderCacheEnabled = finderCacheEnabled;
038                    _resultClass = resultClass;
039                    _className = className;
040                    _methodName = methodName;
041                    _params = params;
042    
043                    _initCacheKeyPrefix();
044                    _initLocalCacheKeyPrefix();
045            }
046    
047            public Serializable encodeCacheKey(Object[] args) {
048                    StringBundler sb = new StringBundler(args.length * 2 + 3);
049    
050                    sb.append(ShardUtil.getCurrentShardName());
051                    sb.append(StringPool.PERIOD);
052                    sb.append(_cacheKeyPrefix);
053    
054                    for (Object arg : args) {
055                            sb.append(StringPool.PERIOD);
056                            sb.append(StringUtil.toHexString(arg));
057                    }
058    
059                    CacheKeyGenerator cacheKeyGenerator =
060                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
061                                    FinderCache.class.getName());
062    
063                    return cacheKeyGenerator.getCacheKey(sb);
064            }
065    
066            public Serializable encodeLocalCacheKey(Object[] args) {
067                    StringBundler sb = new StringBundler(args.length * 2 + 3);
068    
069                    sb.append(ShardUtil.getCurrentShardName());
070                    sb.append(StringPool.PERIOD);
071                    sb.append(_localCacheKeyPrefix);
072    
073                    for (Object arg : args) {
074                            sb.append(StringPool.PERIOD);
075                            sb.append(StringUtil.toHexString(arg));
076                    }
077    
078                    CacheKeyGenerator cacheKeyGenerator =
079                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
080                                    FinderCache.class.getName());
081    
082                    return cacheKeyGenerator.getCacheKey(sb);
083            }
084    
085            public String getClassName() {
086                    return _className;
087            }
088    
089            public String getMethodName() {
090                    return _methodName;
091            }
092    
093            public String[] getParams() {
094                    return _params;
095            }
096    
097            public Class<?> getResultClass() {
098                    return _resultClass;
099            }
100    
101            public boolean isEntityCacheEnabled() {
102                    return _entityCacheEnabled;
103            }
104    
105            public boolean isFinderCacheEnabled() {
106                    return _finderCacheEnabled;
107            }
108    
109            private void _initCacheKeyPrefix() {
110                    StringBundler sb = new StringBundler(_params.length * 2 + 3);
111    
112                    sb.append(_methodName);
113                    sb.append(_PARAMS_SEPARATOR);
114    
115                    for (String param : _params) {
116                            sb.append(StringPool.PERIOD);
117                            sb.append(param);
118                    }
119    
120                    sb.append(_ARGS_SEPARATOR);
121    
122                    _cacheKeyPrefix = sb.toString();
123            }
124    
125            private void _initLocalCacheKeyPrefix() {
126                    _localCacheKeyPrefix = _className.concat(StringPool.PERIOD).concat(
127                            _cacheKeyPrefix);
128            }
129    
130            private static final String _ARGS_SEPARATOR = "_A_";
131    
132            private static final String _PARAMS_SEPARATOR = "_P_";
133    
134            private String _cacheKeyPrefix;
135            private String _className;
136            private boolean _entityCacheEnabled;
137            private boolean _finderCacheEnabled;
138            private String _localCacheKeyPrefix;
139            private String _methodName;
140            private String[] _params;
141            private Class<?> _resultClass;
142    
143    }