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.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                    LuceneHelperUtil.loadIndexesFromCluster(companyId);
034            }
035    
036            public static void loadIndexesFromCluster(
037                    long[] companyIds, Address bootupAddress) {
038    
039                    if (bootupAddress == null) {
040                            return;
041                    }
042    
043                    if (_log.isInfoEnabled()) {
044                            _log.info(
045                                    "Start loading Lucene index files from cluster node " +
046                                            bootupAddress);
047                    }
048    
049                    InputStream inputStream = null;
050    
051                    for (long companyId : companyIds) {
052                            try {
053                                    inputStream =
054                                            LuceneHelperUtil.getLoadIndexesInputStreamFromCluster(
055                                                    companyId, bootupAddress);
056    
057                                    LuceneHelperUtil.loadIndex(companyId, inputStream);
058                            }
059                            catch (SystemException se) {
060                                    throw se;
061                            }
062                            catch (IOException ioe) {
063                                    throw new SystemException(ioe);
064                            }
065                            finally {
066                                    if (inputStream != null) {
067                                            try {
068                                                    inputStream.close();
069                                            }
070                                            catch (IOException ioe) {
071                                                    throw new SystemException(ioe);
072                                            }
073                                    }
074                            }
075                    }
076            }
077    
078            private static final Log _log = LogFactoryUtil.getLog(
079                    LuceneClusterUtil.class);
080    
081    }