001/*
002 *  Copyright 2017 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.cms.indexing;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.commons.lang3.StringUtils;
028import org.slf4j.LoggerFactory;
029
030import org.ametys.plugins.core.impl.schedule.StaticRunnable;
031import org.ametys.plugins.repository.provider.AbstractRepository;
032import org.ametys.plugins.repository.provider.JackrabbitRepository;
033
034/**
035 * A {@link Runnable} for reloading Solr ACL caches for given workspaces (only for segments not already in cache).
036 */
037public class ReloadSolrAclCachesForCoresRunnable extends StaticRunnable
038{
039    private JackrabbitRepository _repository;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE);
046    }
047    
048    @Override
049    protected void _configureParameterValues(Configuration paramConfigs) throws ConfigurationException
050    {
051        // Do nothing
052    }
053    
054    @Override
055    public Map<String, Object> getParameterValues()
056    {
057        Map<String, Object> params = new HashMap<>();
058        try
059        {
060            String[] workspaces = _repository.getWorkspaces();
061            params.put(ReloadSolrAclCachesForCoresSchedulable.WORKSPACE_KEY, StringUtils.join(workspaces, ','));
062        }
063        catch (RepositoryException e)
064        {
065            LoggerFactory.getLogger(ReloadSolrAclCachesForCoresRunnable.class).error("", e);
066        }
067        
068        return params;
069    }
070}