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 /** The JackRabbit RepositoryImpl */ 036 protected RepositoryImpl _repository; 037 038 private String[] _workspaceFolders; 039 040 @Override 041 protected void initialize() throws RepositoryException 042 { 043 File file = new File(_repositoryConfig.getHomeDir() + File.separator + _WORKSPACES_FOLDER_RELATIVE_PATH); 044 _workspaceFolders = file.list((File f, String name) -> f.isDirectory()); 045 046 // Initialize the task progress object. 047 // deleting workspace index folders + repository root index folder = 60% 048 // reindexing = 40% 049 _progress = new TaskProgress(5f / 3 * (_workspaceFolders.length + 1)); 050 } 051 052 @Override 053 protected void setLogger() 054 { 055 setLogger(LoggerFactory.getLogger(ReindexingTask.class)); 056 } 057 058 @Override 059 protected void apply() throws RepositoryException 060 { 061 // Deleting repository root index folder 062 try 063 { 064 deleteIndexFolder(_INDEX_FOLDER_RELATIVE_PATH); 065 _logger.info("Successfully deleted root repository index folder"); 066 } 067 catch (IOException e) 068 { 069 _logger.error(e.getLocalizedMessage(), e); 070 } 071 072 // Deleting workspace index folders 073 for (String folder : _workspaceFolders) 074 { 075 try 076 { 077 deleteIndexFolder(_WORKSPACES_FOLDER_RELATIVE_PATH + File.separator + folder + File.separator + "index"); 078 _logger.info("Successfully deleted index folder of workspace '" + folder + "'"); 079 } 080 catch (IOException e) 081 { 082 _logger.error(e.getLocalizedMessage(), e); 083 } 084 } 085 086 // Creates a repository instance to perform the re-indexing process. 087 _logger.info("Starting repository to launch the re-indexing process"); 088 _repository = RepositoryImpl.create(_repositoryConfig); 089 _logger.info("Repository restarted successfully, reindexing process has ended."); 090 setProgressTo(90); 091 092 // logout 093 _logger.info("Shuting down repository"); 094 _repository.shutdown(); 095 _repository = null; 096 setProgressTo(100); 097 } 098 099 @Override 100 protected void close() 101 { 102 // shutdown properly if an exception has been thrown 103 if (_repository != null) 104 { 105 _repository.shutdown(); 106 } 107 } 108 109 private void deleteIndexFolder(String relativePath) throws IOException 110 { 111 File dir = new File(_repositoryConfig.getHomeDir() + File.separator + relativePath); 112 FileUtils.deleteDirectory(dir); 113 if (_progress != null) 114 { 115 _progress.progress(); 116 } 117 } 118 119 private void setProgressTo(int percentage) 120 { 121 if (_progress != null) 122 { 123 _progress.progressTotalPercentage(percentage); 124 } 125 } 126}