001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.workspaces.repository.maintenance; 017 018import java.io.File; 019import java.io.IOException; 020 021import javax.jcr.RepositoryException; 022 023import org.apache.commons.io.FileUtils; 024import org.apache.jackrabbit.core.RepositoryImpl; 025import org.slf4j.LoggerFactory; 026 027/** 028 * ReindexingTask 029 */ 030public class ReindexingTask extends AbstractMaintenanceTask 031{ 032 private static final String _INDEX_FOLDER_RELATIVE_PATH = "repository" + File.separator + "index"; 033 private static final String _WORKSPACES_FOLDER_RELATIVE_PATH = "workspaces"; 034 035 private String[] _workspaceFolders; 036 037 @Override 038 protected void initialize() throws RepositoryException 039 { 040 File file = new File(getRepositoryConfig().getHomeDir() + File.separator + _WORKSPACES_FOLDER_RELATIVE_PATH); 041 _workspaceFolders = file.list((File f, String name) -> f.isDirectory()); 042 043 // Initialize the task progress object. 044 // deleting workspace index folders + repository root index folder = 60% 045 // reindexing = 40% 046 _progress = new TaskProgress(5f / 3 * (_workspaceFolders.length + 1)); 047 } 048 049 @Override 050 protected void setLogger() 051 { 052 setLogger(LoggerFactory.getLogger(ReindexingTask.class)); 053 } 054 055 @Override 056 protected void apply() throws RepositoryException 057 { 058 // Deleting repository root index folder 059 try 060 { 061 deleteIndexFolder(_INDEX_FOLDER_RELATIVE_PATH); 062 _logger.info("Successfully deleted root repository index folder"); 063 } 064 catch (IOException e) 065 { 066 _logger.error(e.getLocalizedMessage(), e); 067 } 068 069 // Deleting workspace index folders 070 for (String folder : _workspaceFolders) 071 { 072 try 073 { 074 deleteIndexFolder(_WORKSPACES_FOLDER_RELATIVE_PATH + File.separator + folder + File.separator + "index"); 075 _logger.info("Successfully deleted index folder of workspace '" + folder + "'"); 076 } 077 catch (IOException e) 078 { 079 _logger.error(e.getLocalizedMessage(), e); 080 } 081 } 082 083 // Creates a repository instance to perform the re-indexing process. 084 _logger.info("Starting repository to launch the re-indexing process"); 085 RepositoryImpl repository = createRepository(); 086 _logger.info("Repository restarted successfully, reindexing process has ended."); 087 setProgressTo(90); 088 089 // logout 090 _logger.info("Shuting down repository"); 091 repository.shutdown(); 092 repository = null; 093 setProgressTo(100); 094 } 095 096 private void deleteIndexFolder(String relativePath) throws IOException 097 { 098 File dir = new File(getRepositoryConfig().getHomeDir() + File.separator + relativePath); 099 FileUtils.deleteDirectory(dir); 100 if (_progress != null) 101 { 102 _progress.progress(); 103 } 104 } 105 106 private void setProgressTo(int percentage) 107 { 108 if (_progress != null) 109 { 110 _progress.progressTotalPercentage(percentage); 111 } 112 } 113}