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
027import org.ametys.core.schedule.progression.ProgressionTrackerFactory;
028import org.ametys.core.schedule.progression.SimpleProgressionTracker;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * ReindexingTask
033 */
034public class ReindexingTask extends AbstractMaintenanceTask
035{
036    private static final String _INDEX_FOLDER_RELATIVE_PATH = "repository" + File.separator + "index";
037    private static final String _WORKSPACES_FOLDER_RELATIVE_PATH = "workspaces";
038
039    private String[] _workspaceFolders;
040
041    @Override
042    protected void initialize() throws RepositoryException
043    {
044        File file = new File(getRepositoryConfig().getHomeDir() + File.separator + _WORKSPACES_FOLDER_RELATIVE_PATH);
045        _workspaceFolders = file.list((File f, String name) -> f.isDirectory());
046        
047        // Initialize the task progress object.
048        _progress = ProgressionTrackerFactory.createContainerProgressionTracker(new I18nizableText("plugin.repositoryapp", "PLUGINS_REPOSITORYAPP_BUTTON_MAINTENANCE_REINDEXING"), _logger);
049        
050        // deleting workspace index folders + repository root index folder = 60 %
051        SimpleProgressionTracker progressTrackerForScan = _progress.addSimpleStep("deletefolders", new I18nizableText("plugin.repositoryapp", "PLUGINS_REPOSITORYAPP_MAINTENANCE_REINDEXING_DELETE_FOLDERS_STEP_LABEL"), 6);
052        progressTrackerForScan.setSize(_workspaceFolders.length + 1);
053        
054        // reindexing = 39 %
055        _progress.addSimpleStep("reindexing", new I18nizableText("plugin.repositoryapp", "PLUGINS_REPOSITORYAPP_BUTTON_MAINTENANCE_REINDEXING"), 3.9);
056        // shuting down = 1 %
057        _progress.addSimpleStep("finishing", new I18nizableText("plugin.repositoryapp", "PLUGINS_REPOSITORYAPP_MAINTENANCE_REINDEXING_SHUTING_DOWN_REPOSITORY_STEP_LABEL"), 0.1);
058    }
059
060    @Override
061    protected void setLogger()
062    {
063        setLogger(LoggerFactory.getLogger(ReindexingTask.class));
064    }
065    
066    @Override
067    protected void apply() throws RepositoryException
068    {
069        // Deleting repository root index folder
070        try
071        {
072            deleteIndexFolder(_INDEX_FOLDER_RELATIVE_PATH);
073            _logger.info("Successfully deleted root repository index folder");
074        }
075        catch (IOException e)
076        {
077            _logger.error(e.getLocalizedMessage(), e);
078        }
079
080        // Deleting workspace index folders
081        for (String folder : _workspaceFolders)
082        {
083            try
084            {
085                deleteIndexFolder(_WORKSPACES_FOLDER_RELATIVE_PATH  + File.separator + folder + File.separator + "index");
086                _logger.info("Successfully deleted index folder of workspace '" + folder + "'");
087            }
088            catch (IOException e)
089            {
090                _logger.error(e.getLocalizedMessage(), e);
091            }
092        }
093
094        // Creates a repository instance to perform the re-indexing process.
095        _logger.info("Starting repository to launch the re-indexing process");
096        RepositoryImpl repository = createRepository();
097        _logger.info("Repository restarted successfully, reindexing process has ended.");
098        ((SimpleProgressionTracker) _progress.getStep("reindexing")).increment();
099
100        // logout
101        repository.shutdown();
102        repository = null;
103        ((SimpleProgressionTracker) _progress.getStep("finishing")).increment();
104    }
105
106    private void deleteIndexFolder(String relativePath) throws IOException
107    {
108        File dir = new File(getRepositoryConfig().getHomeDir() + File.separator + relativePath);
109        FileUtils.deleteDirectory(dir);
110        if (_progress != null)
111        {
112            ((SimpleProgressionTracker) _progress.getStep("deletefolders")).increment();
113        }
114    }
115}