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.plugins.repository.maintenance;
017
018import javax.jcr.UnsupportedRepositoryOperationException;
019
020import org.apache.commons.lang3.tuple.Pair;
021import org.apache.jackrabbit.core.RepositoryContext;
022import org.apache.jackrabbit.core.config.ConfigurationException;
023import org.apache.jackrabbit.core.config.RepositoryConfig;
024
025import org.ametys.plugins.repository.provider.AmetysRepository;
026import org.ametys.workspaces.repository.maintenance.AbstractMaintenanceTask;
027
028/**
029 * This manager overrides the standalone manager to do specific in safe-mode (repository is not started)
030 *
031 */
032public class MaintenanceTaskManager extends org.ametys.workspaces.repository.maintenance.MaintenanceTaskManager
033{
034    @Override
035    protected AbstractMaintenanceTask _createTask(MaintenanceTaskType type)
036    {
037        switch (type)
038        {
039            case REMOVE_INCOHERENT_REFERENCES:
040                return new CleanReferenceTask();
041            case RECLAIM_UNUSED_SPACE:
042                return new ReclaimUnusedSpaceTask();
043            default:
044                return super._createTask(type);
045        }
046    }
047    
048    @Override
049    protected Pair<RepositoryConfig, RepositoryContext> _getRepositoryInfo() throws ConfigurationException, UnsupportedRepositoryOperationException
050    {
051        final RepositoryConfig repositoryConfig;
052        final RepositoryContext repositoryContext;
053        
054        if (_repositoryProvider == null)
055        {
056            repositoryConfig = _createRepositoryConfig();
057            repositoryContext = null;
058        }
059        else if (_repositoryProvider.getRepository() instanceof AmetysRepository ametysRepository)
060        {
061            if (!_runningTask.requiresOffline())
062            {
063                repositoryConfig = null;
064                repositoryContext = ametysRepository.getContext();
065            }
066            else
067            {
068                MaintenanceTaskType type = _runningTaskType;
069                _runningTaskType = null;
070                throw new UnsupportedRepositoryOperationException("A maintenance task of type " + type + " can not be run on an online repository");
071            }
072        }
073        else
074        {
075            _runningTaskType = null;
076            throw new UnsupportedRepositoryOperationException("A maintenance task can not be run on an online repository instance of " + _repositoryProvider.getRepository().getClass().getName());
077        }
078        return Pair.of(repositoryConfig, repositoryContext);
079    }
080
081}