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