001    /**
002     * Copyright (c) 2000-2010 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.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class FinderPath {
026    
027            public FinderPath(
028                    boolean entityCacheEnabled, boolean finderCacheEnabled,
029                    String className, String methodName, String[] params) {
030    
031                    _entityCacheEnabled = entityCacheEnabled;
032                    _finderCacheEnabled = finderCacheEnabled;
033                    _className = className;
034                    _methodName = methodName;
035                    _params = params;
036    
037                    _initCacheKeyPrefix();
038                    _initLocalCacheKeyPrefix();
039            }
040    
041            public String encodeCacheKey(Object[] args) {
042                    StringBundler sb = new StringBundler(args.length * 2 + 1);
043    
044                    sb.append(_cacheKeyPrefix);
045    
046                    for (Object arg : args) {
047                            sb.append(StringPool.PERIOD);
048                            sb.append(String.valueOf(arg));
049                    }
050    
051                    CacheKeyGenerator cacheKeyGenerator =
052                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
053                                    FinderCache.class.getName());
054    
055                    return cacheKeyGenerator.getCacheKey(sb);
056            }
057    
058            public String encodeLocalCacheKey(Object[] args) {
059                    StringBundler sb = new StringBundler(args.length * 2 + 1);
060    
061                    sb.append(_localCacheKeyPrefix);
062    
063                    for (Object arg : args) {
064                            sb.append(StringPool.PERIOD);
065                            sb.append(String.valueOf(arg));
066                    }
067    
068                    CacheKeyGenerator cacheKeyGenerator =
069                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
070                                    FinderCache.class.getName());
071    
072                    return cacheKeyGenerator.getCacheKey(sb);
073            }
074    
075            public String getClassName() {
076                    return _className;
077            }
078    
079            public String getMethodName() {
080                    return _methodName;
081            }
082    
083            public String[] getParams() {
084                    return _params;
085            }
086    
087            public boolean isEntityCacheEnabled() {
088                    return _entityCacheEnabled;
089            }
090    
091            public boolean isFinderCacheEnabled() {
092                    return _finderCacheEnabled;
093            }
094    
095            private void _initCacheKeyPrefix() {
096                    StringBundler sb = new StringBundler(_params.length * 2 + 3);
097    
098                    sb.append(_methodName);
099                    sb.append(_PARAMS_SEPARATOR);
100    
101                    for (String param : _params) {
102                            sb.append(StringPool.PERIOD);
103                            sb.append(param);
104                    }
105    
106                    sb.append(_ARGS_SEPARATOR);
107    
108                    _cacheKeyPrefix = sb.toString();
109            }
110    
111            private void _initLocalCacheKeyPrefix() {
112                    _localCacheKeyPrefix = _className.concat(StringPool.PERIOD).concat(
113                            _cacheKeyPrefix);
114            }
115    
116            private static final String _ARGS_SEPARATOR = "_A_";
117    
118            private static final String _PARAMS_SEPARATOR = "_P_";
119    
120            private String _cacheKeyPrefix;
121            private String _className;
122            private boolean _entityCacheEnabled;
123            private boolean _finderCacheEnabled;
124            private String _localCacheKeyPrefix;
125            private String _methodName;
126            private String[] _params;
127    
128    }