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.search.lucene;
016    
017    import com.liferay.portal.kernel.dao.shard.ShardUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.search.Indexer;
021    import com.liferay.portal.kernel.search.SearchEngineUtil;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.Time;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portal.util.comparator.PortletLuceneComparator;
028    
029    import java.util.List;
030    
031    import org.apache.commons.lang.time.StopWatch;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class LuceneIndexer implements Runnable {
037    
038            public LuceneIndexer(long companyId) {
039                    _companyId = companyId;
040            }
041    
042            public void halt() {
043            }
044    
045            public boolean isFinished() {
046                    return _finished;
047            }
048    
049            public void reindex() {
050                    reindex(0);
051            }
052    
053            public void reindex(int delay) {
054                    ShardUtil.pushCompanyService(_companyId);
055    
056                    try {
057                            doReIndex(delay);
058                    }
059                    finally {
060                            ShardUtil.popCompanyService();
061                    }
062            }
063    
064            public void run() {
065                    reindex(PropsValues.INDEX_ON_STARTUP_DELAY);
066            }
067    
068            protected void doReIndex(int delay) {
069                    if (SearchEngineUtil.isIndexReadOnly()) {
070                            return;
071                    }
072    
073                    if (_log.isInfoEnabled()) {
074                            _log.info("Reindexing Lucene started");
075                    }
076    
077                    if (delay < 0) {
078                            delay = 0;
079                    }
080    
081                    try {
082                            if (delay > 0) {
083                                    Thread.sleep(Time.SECOND * delay);
084                            }
085                    }
086                    catch (InterruptedException ie) {
087                    }
088    
089                    StopWatch stopWatch = null;
090    
091                    if (_log.isInfoEnabled()) {
092                            stopWatch = new StopWatch();
093    
094                            stopWatch.start();
095                    }
096    
097                    try {
098                            LuceneHelperUtil.delete(_companyId);
099    
100                            List<Portlet> portlets = PortletLocalServiceUtil.getPortlets(
101                                    _companyId);
102    
103                            portlets = ListUtil.sort(portlets, new PortletLuceneComparator());
104    
105                            for (Portlet portlet : portlets) {
106                                    if (!portlet.isActive()) {
107                                            continue;
108                                    }
109    
110                                    List<Indexer> indexers = portlet.getIndexerInstances();
111    
112                                    if (indexers == null) {
113                                            continue;
114                                    }
115    
116                                    for (Indexer indexer : indexers) {
117                                            reindex(indexer);
118                                    }
119                            }
120    
121                            if (_log.isInfoEnabled()) {
122                                    _log.info(
123                                            "Reindexing Lucene completed in " +
124                                                    (stopWatch.getTime() / Time.SECOND) + " seconds");
125                            }
126                    }
127                    catch (Exception e) {
128                            _log.error("Error encountered while reindexing", e);
129    
130                            if (_log.isInfoEnabled()) {
131                                    _log.info("Reindexing Lucene failed");
132                            }
133                    }
134    
135                    _finished = true;
136            }
137    
138            protected void reindex(Indexer indexer) throws Exception {
139                    StopWatch stopWatch = null;
140    
141                    if (_log.isInfoEnabled()) {
142                            stopWatch = new StopWatch();
143    
144                            stopWatch.start();
145                    }
146    
147                    if (_log.isInfoEnabled()) {
148                            _log.info("Reindexing with " + indexer.getClass() + " started");
149                    }
150    
151                    indexer.reindex(new String[] {String.valueOf(_companyId)});
152    
153                    if (_log.isInfoEnabled()) {
154                            _log.info(
155                                    "Reindexing with " + indexer.getClass() +
156                                            " completed in " + (stopWatch.getTime() / Time.SECOND) +
157                                                    " seconds");
158                    }
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(LuceneIndexer.class);
162    
163            private long _companyId;
164            private boolean _finished;
165    
166    }