001    /**
002     * Copyright (c) 2000-present 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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
020    import com.liferay.portal.kernel.cache.key.CacheKeyGeneratorUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.model.BaseModel;
025    
026    import java.io.Serializable;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Shuyang Zhou
031     */
032    @ProviderType
033    public class FinderPath {
034    
035            public FinderPath(
036                    boolean entityCacheEnabled, boolean finderCacheEnabled,
037                    Class<?> resultClass, String cacheName, String methodName,
038                    String[] params) {
039    
040                    this(
041                            entityCacheEnabled, finderCacheEnabled, resultClass, cacheName,
042                            methodName, params, -1);
043            }
044    
045            public FinderPath(
046                    boolean entityCacheEnabled, boolean finderCacheEnabled,
047                    Class<?> resultClass, String cacheName, String methodName,
048                    String[] params, long columnBitmask) {
049    
050                    _entityCacheEnabled = entityCacheEnabled;
051                    _finderCacheEnabled = finderCacheEnabled;
052                    _resultClass = resultClass;
053                    _cacheName = cacheName;
054                    _columnBitmask = columnBitmask;
055    
056                    if (BaseModel.class.isAssignableFrom(_resultClass)) {
057                            _cacheKeyGeneratorCacheName =
058                                    FinderCache.class.getName() + "#BaseModel";
059                    }
060                    else {
061                            _cacheKeyGeneratorCacheName = FinderCache.class.getName();
062                    }
063    
064                    CacheKeyGenerator cacheKeyGenerator =
065                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
066                                    _cacheKeyGeneratorCacheName);
067    
068                    if (cacheKeyGenerator.isCallingGetCacheKeyThreadSafe()) {
069                            _cacheKeyGenerator = cacheKeyGenerator;
070                    }
071                    else {
072                            _cacheKeyGenerator = null;
073                    }
074    
075                    _initCacheKeyPrefix(methodName, params);
076                    _initLocalCacheKeyPrefix();
077            }
078    
079            public Serializable encodeCacheKey(Object[] arguments) {
080                    StringBundler sb = new StringBundler(arguments.length * 2 + 1);
081    
082                    sb.append(_cacheKeyPrefix);
083    
084                    for (Object arg : arguments) {
085                            sb.append(StringPool.PERIOD);
086                            sb.append(StringUtil.toHexString(arg));
087                    }
088    
089                    return _getCacheKey(sb);
090            }
091    
092            public Serializable encodeLocalCacheKey(Object[] arguments) {
093                    StringBundler sb = new StringBundler(arguments.length * 2 + 1);
094    
095                    sb.append(_localCacheKeyPrefix);
096    
097                    for (Object arg : arguments) {
098                            sb.append(StringPool.PERIOD);
099                            sb.append(StringUtil.toHexString(arg));
100                    }
101    
102                    return _getCacheKey(sb);
103            }
104    
105            public String getCacheName() {
106                    return _cacheName;
107            }
108    
109            public long getColumnBitmask() {
110                    return _columnBitmask;
111            }
112    
113            public Class<?> getResultClass() {
114                    return _resultClass;
115            }
116    
117            public boolean isEntityCacheEnabled() {
118                    return _entityCacheEnabled;
119            }
120    
121            public boolean isFinderCacheEnabled() {
122                    return _finderCacheEnabled;
123            }
124    
125            private Serializable _getCacheKey(StringBundler sb) {
126                    CacheKeyGenerator cacheKeyGenerator = _cacheKeyGenerator;
127    
128                    if (cacheKeyGenerator == null) {
129                            cacheKeyGenerator = CacheKeyGeneratorUtil.getCacheKeyGenerator(
130                                    _cacheKeyGeneratorCacheName);
131                    }
132    
133                    return cacheKeyGenerator.getCacheKey(sb);
134            }
135    
136            private void _initCacheKeyPrefix(String methodName, String[] params) {
137                    StringBundler sb = new StringBundler(params.length * 2 + 3);
138    
139                    sb.append(methodName);
140                    sb.append(_PARAMS_SEPARATOR);
141    
142                    for (String param : params) {
143                            sb.append(StringPool.PERIOD);
144                            sb.append(param);
145                    }
146    
147                    sb.append(_ARGS_SEPARATOR);
148    
149                    _cacheKeyPrefix = sb.toString();
150            }
151    
152            private void _initLocalCacheKeyPrefix() {
153                    _localCacheKeyPrefix = _cacheName.concat(StringPool.PERIOD).concat(
154                            _cacheKeyPrefix);
155            }
156    
157            private static final String _ARGS_SEPARATOR = "_A_";
158    
159            private static final String _PARAMS_SEPARATOR = "_P_";
160    
161            private final CacheKeyGenerator _cacheKeyGenerator;
162            private final String _cacheKeyGeneratorCacheName;
163            private String _cacheKeyPrefix;
164            private final String _cacheName;
165            private final long _columnBitmask;
166            private final boolean _entityCacheEnabled;
167            private final boolean _finderCacheEnabled;
168            private String _localCacheKeyPrefix;
169            private final Class<?> _resultClass;
170    
171    }