001    /**
002     * Copyright (c) 2000-2013 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.cluster;
016    
017    import com.liferay.portal.kernel.cluster.Address;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.search.lucene.LuceneHelperUtil;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    /**
027     * @author Shuyang Zhou
028     * @author Tina Tian
029     */
030    public class LuceneClusterUtil {
031    
032            public static void loadIndexesFromCluster(long companyId)
033                    throws SystemException {
034    
035                    LuceneHelperUtil.loadIndexesFromCluster(companyId);
036            }
037    
038            public static void loadIndexesFromCluster(
039                            long[] companyIds, Address bootupAddress)
040                    throws SystemException {
041    
042                    if (bootupAddress == null) {
043                            return;
044                    }
045    
046                    if (_log.isInfoEnabled()) {
047                            _log.info(
048                                    "Start loading Lucene index files from cluster node " +
049                                            bootupAddress);
050                    }
051    
052                    InputStream inputStream = null;
053    
054                    for (long companyId : companyIds) {
055                            try {
056                                    inputStream =
057                                            LuceneHelperUtil.getLoadIndexesInputStreamFromCluster(
058                                                    companyId, bootupAddress);
059    
060                                    LuceneHelperUtil.loadIndex(companyId, inputStream);
061                            }
062                            catch (SystemException se) {
063                                    throw se;
064                            }
065                            catch (IOException ioe) {
066                                    throw new SystemException(ioe);
067                            }
068                            finally {
069                                    if (inputStream != null) {
070                                            try {
071                                                    inputStream.close();
072                                            }
073                                            catch (IOException ioe) {
074                                                    throw new SystemException(ioe);
075                                            }
076                                    }
077                            }
078                    }
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(LuceneClusterUtil.class);
082    
083    }