001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class FinderPath {
027    
028            public FinderPath(
029                    boolean entityCacheEnabled, boolean finderCacheEnabled,
030                    String className, String methodName, String[] params) {
031    
032                    _entityCacheEnabled = entityCacheEnabled;
033                    _finderCacheEnabled = finderCacheEnabled;
034                    _className = className;
035                    _methodName = methodName;
036                    _params = params;
037    
038                    _initCacheKeyPrefix();
039                    _initLocalCacheKeyPrefix();
040            }
041    
042            public String encodeCacheKey(Object[] args) {
043                    StringBundler sb = new StringBundler(args.length * 2 + 3);
044    
045                    sb.append(ShardUtil.getCurrentShardName());
046                    sb.append(StringPool.PERIOD);
047                    sb.append(_cacheKeyPrefix);
048    
049                    for (Object arg : args) {
050                            sb.append(StringPool.PERIOD);
051                            sb.append(String.valueOf(arg));
052                    }
053    
054                    CacheKeyGenerator cacheKeyGenerator =
055                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
056                                    FinderCache.class.getName());
057    
058                    return cacheKeyGenerator.getCacheKey(sb);
059            }
060    
061            public String encodeLocalCacheKey(Object[] args) {
062                    StringBundler sb = new StringBundler(args.length * 2 + 3);
063    
064                    sb.append(ShardUtil.getCurrentShardName());
065                    sb.append(StringPool.PERIOD);
066                    sb.append(_localCacheKeyPrefix);
067    
068                    for (Object arg : args) {
069                            sb.append(StringPool.PERIOD);
070                            sb.append(String.valueOf(arg));
071                    }
072    
073                    CacheKeyGenerator cacheKeyGenerator =
074                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
075                                    FinderCache.class.getName());
076    
077                    return cacheKeyGenerator.getCacheKey(sb);
078            }
079    
080            public String getClassName() {
081                    return _className;
082            }
083    
084            public String getMethodName() {
085                    return _methodName;
086            }
087    
088            public String[] getParams() {
089                    return _params;
090            }
091    
092            public boolean isEntityCacheEnabled() {
093                    return _entityCacheEnabled;
094            }
095    
096            public boolean isFinderCacheEnabled() {
097                    return _finderCacheEnabled;
098            }
099    
100            private void _initCacheKeyPrefix() {
101                    StringBundler sb = new StringBundler(_params.length * 2 + 3);
102    
103                    sb.append(_methodName);
104                    sb.append(_PARAMS_SEPARATOR);
105    
106                    for (String param : _params) {
107                            sb.append(StringPool.PERIOD);
108                            sb.append(param);
109                    }
110    
111                    sb.append(_ARGS_SEPARATOR);
112    
113                    _cacheKeyPrefix = sb.toString();
114            }
115    
116            private void _initLocalCacheKeyPrefix() {
117                    _localCacheKeyPrefix = _className.concat(StringPool.PERIOD).concat(
118                            _cacheKeyPrefix);
119            }
120    
121            private static final String _ARGS_SEPARATOR = "_A_";
122    
123            private static final String _PARAMS_SEPARATOR = "_P_";
124    
125            private String _cacheKeyPrefix;
126            private String _className;
127            private boolean _entityCacheEnabled;
128            private boolean _finderCacheEnabled;
129            private String _localCacheKeyPrefix;
130            private String _methodName;
131            private String[] _params;
132    
133    }