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;
016    
017    import com.liferay.portal.kernel.search.BaseSearchEngine;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.StreamUtil;
020    import com.liferay.portal.kernel.util.SystemProperties;
021    
022    import java.io.File;
023    import java.io.FileInputStream;
024    import java.io.FileOutputStream;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class LuceneSearchEngine extends BaseSearchEngine {
030    
031            @Override
032            public synchronized String backup(long companyId, String backupName) {
033                    FileOutputStream fileOutputStream = null;
034    
035                    try {
036                            String fileName = getFileName(backupName);
037    
038                            fileOutputStream = new FileOutputStream(fileName);
039    
040                            LuceneHelperUtil.dumpIndex(companyId, fileOutputStream);
041    
042                            return fileName;
043                    }
044                    catch (Exception e) {
045                            throw new RuntimeException(e);
046                    }
047                    finally {
048                            StreamUtil.cleanUp(fileOutputStream);
049                    }
050            }
051    
052            @Override
053            public void initialize(long companyId) {
054                    super.initialize(companyId);
055    
056                    LuceneHelperUtil.startup(companyId);
057            }
058    
059            @Override
060            public synchronized void removeBackup(long companyId, String backupName) {
061                    String fileName = getFileName(backupName);
062    
063                    FileUtil.delete(fileName);
064            }
065    
066            @Override
067            public void removeCompany(long companyId) {
068                    super.removeCompany(companyId);
069    
070                    LuceneHelperUtil.delete(companyId);
071    
072                    LuceneHelperUtil.shutdown(companyId);
073            }
074    
075            @Override
076            public synchronized void restore(long companyId, String backupName) {
077                    FileInputStream fileInputStream = null;
078    
079                    try {
080                            String fileName = getFileName(backupName);
081    
082                            fileInputStream = new FileInputStream(fileName);
083    
084                            LuceneHelperUtil.loadIndex(companyId, fileInputStream);
085                    }
086                    catch (Exception e) {
087                            throw new RuntimeException(e);
088                    }
089                    finally {
090                            StreamUtil.cleanUp(fileInputStream);
091                    }
092            }
093    
094            protected String getFileName(String backupName) {
095                    return SystemProperties.get(SystemProperties.TMP_DIR) +
096                            File.separator + backupName;
097            }
098    
099    }