001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.Serializable;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Shuyang Zhou
029     */
030    public class FinderPath {
031    
032            public FinderPath(
033                    boolean entityCacheEnabled, boolean finderCacheEnabled,
034                    Class<?> resultClass, String cacheName, String methodName,
035                    String[] params) {
036    
037                    this(
038                            entityCacheEnabled, finderCacheEnabled, resultClass, cacheName,
039                            methodName, params, -1);
040            }
041    
042            public FinderPath(
043                    boolean entityCacheEnabled, boolean finderCacheEnabled,
044                    Class<?> resultClass, String cacheName, String methodName,
045                    String[] params, long columnBitmask) {
046    
047                    _entityCacheEnabled = entityCacheEnabled;
048                    _finderCacheEnabled = finderCacheEnabled;
049                    _resultClass = resultClass;
050                    _cacheName = cacheName;
051                    _methodName = methodName;
052                    _params = params;
053                    _columnBitmask = columnBitmask;
054    
055                    _initCacheKeyPrefix();
056                    _initLocalCacheKeyPrefix();
057            }
058    
059            public Serializable encodeCacheKey(Object[] args) {
060                    StringBundler sb = new StringBundler(args.length * 2 + 3);
061    
062                    sb.append(ShardUtil.getCurrentShardName());
063                    sb.append(StringPool.PERIOD);
064                    sb.append(_cacheKeyPrefix);
065    
066                    for (Object arg : args) {
067                            sb.append(StringPool.PERIOD);
068                            sb.append(StringUtil.toHexString(arg));
069                    }
070    
071                    CacheKeyGenerator cacheKeyGenerator =
072                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
073                                    FinderCache.class.getName());
074    
075                    return cacheKeyGenerator.getCacheKey(sb);
076            }
077    
078            public Serializable encodeLocalCacheKey(Object[] args) {
079                    StringBundler sb = new StringBundler(args.length * 2 + 3);
080    
081                    sb.append(ShardUtil.getCurrentShardName());
082                    sb.append(StringPool.PERIOD);
083                    sb.append(_localCacheKeyPrefix);
084    
085                    for (Object arg : args) {
086                            sb.append(StringPool.PERIOD);
087                            sb.append(StringUtil.toHexString(arg));
088                    }
089    
090                    CacheKeyGenerator cacheKeyGenerator =
091                            CacheKeyGeneratorUtil.getCacheKeyGenerator(
092                                    FinderCache.class.getName());
093    
094                    return cacheKeyGenerator.getCacheKey(sb);
095            }
096    
097            public String getCacheName() {
098                    return _cacheName;
099            }
100    
101            public long getColumnBitmask() {
102                    return _columnBitmask;
103            }
104    
105            public String getMethodName() {
106                    return _methodName;
107            }
108    
109            public String[] getParams() {
110                    return _params;
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 void _initCacheKeyPrefix() {
126                    StringBundler sb = new StringBundler(_params.length * 2 + 3);
127    
128                    sb.append(_methodName);
129                    sb.append(_PARAMS_SEPARATOR);
130    
131                    for (String param : _params) {
132                            sb.append(StringPool.PERIOD);
133                            sb.append(param);
134                    }
135    
136                    sb.append(_ARGS_SEPARATOR);
137    
138                    _cacheKeyPrefix = sb.toString();
139            }
140    
141            private void _initLocalCacheKeyPrefix() {
142                    _localCacheKeyPrefix = _cacheName.concat(StringPool.PERIOD).concat(
143                            _cacheKeyPrefix);
144            }
145    
146            private static final String _ARGS_SEPARATOR = "_A_";
147    
148            private static final String _PARAMS_SEPARATOR = "_P_";
149    
150            private String _cacheKeyPrefix;
151            private String _cacheName;
152            private long _columnBitmask;
153            private boolean _entityCacheEnabled;
154            private boolean _finderCacheEnabled;
155            private String _localCacheKeyPrefix;
156            private String _methodName;
157            private String[] _params;
158            private Class<?> _resultClass;
159    
160    }