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.search.buffer;
016    
017    import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocal;
018    import com.liferay.portal.kernel.search.Indexer;
019    import com.liferay.portal.kernel.util.ClassUtil;
020    import com.liferay.portal.kernel.util.HashUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.ClassedModel;
024    
025    import java.lang.reflect.Method;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class IndexerRequest {
031    
032            public IndexerRequest(
033                    Method method, ClassedModel classedModel, Indexer indexer) {
034    
035                    _method = method;
036                    _classedModel = classedModel;
037                    _indexer = indexer;
038    
039                    _forceSync = ProxyModeThreadLocal.isForceSync();
040                    _modelClassName = classedModel.getModelClassName();
041                    _modelPrimaryKey = (Long)_classedModel.getPrimaryKeyObj();
042            }
043    
044            public IndexerRequest(
045                    Method method, Indexer indexer, String modelClassName,
046                    Long modelPrimaryKey) {
047    
048                    _method = method;
049                    _indexer = indexer;
050                    _modelClassName = modelClassName;
051                    _modelPrimaryKey = modelPrimaryKey;
052    
053                    _classedModel = null;
054                    _forceSync = ProxyModeThreadLocal.isForceSync();
055            }
056    
057            @Override
058            public boolean equals(Object object) {
059                    if (this == object) {
060                            return true;
061                    }
062    
063                    if (!(object instanceof IndexerRequest)) {
064                            return false;
065                    }
066    
067                    IndexerRequest indexerRequest = (IndexerRequest)object;
068    
069                    if (Validator.equals(_indexer, indexerRequest._indexer) &&
070                            (Validator.equals(_method, indexerRequest._method) ||
071                             (Validator.equals(
072                                     _method.getName(), indexerRequest._method.getName()) &&
073                              Validator.equals(
074                                      _modelPrimaryKey, indexerRequest._modelPrimaryKey))) &&
075                            Validator.equals(_modelClassName, indexerRequest._modelClassName)) {
076    
077                            return true;
078                    }
079    
080                    return false;
081            }
082    
083            public void execute() throws Exception {
084                    boolean previousForceSync = ProxyModeThreadLocal.isForceSync();
085    
086                    try {
087                            ProxyModeThreadLocal.setForceSync(_forceSync);
088    
089                            if (_method.getParameterTypes().length == 1) {
090                                    _method.invoke(_indexer, _classedModel);
091                            }
092                            else {
093                                    _method.invoke(_indexer, _modelClassName, _modelPrimaryKey);
094                            }
095                    }
096                    finally {
097                            ProxyModeThreadLocal.setForceSync(previousForceSync);
098                    }
099            }
100    
101            public String getSearchEngineId() {
102                    return _indexer.getSearchEngineId();
103            }
104    
105            @Override
106            public int hashCode() {
107                    int hashCode = HashUtil.hash(0, _method.getName());
108    
109                    hashCode = HashUtil.hash(hashCode, _modelClassName);
110                    hashCode = HashUtil.hash(hashCode, _modelPrimaryKey);
111    
112                    return hashCode;
113            }
114    
115            @Override
116            public String toString() {
117                    StringBundler sb = new StringBundler(13);
118    
119                    sb.append("{classModel=");
120                    sb.append(_classedModel);
121                    sb.append(", forceSync=");
122                    sb.append(_forceSync);
123                    sb.append(", indexer=");
124                    sb.append(ClassUtil.getClassName(_indexer));
125                    sb.append(", method=");
126                    sb.append(_method);
127                    sb.append(", modelClassName=");
128                    sb.append(_modelClassName);
129                    sb.append(", modelPrimaryKey=");
130                    sb.append(_modelPrimaryKey);
131                    sb.append("}");
132    
133                    return sb.toString();
134            }
135    
136            private final ClassedModel _classedModel;
137            private final boolean _forceSync;
138            private final Indexer _indexer;
139            private final Method _method;
140            private final String _modelClassName;
141            private final Long _modelPrimaryKey;
142    
143    }