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.WorkspaceSelector;
032
033/**
034 * A {@link Runnable} for reloading Solr ACL caches for given workspaces (only for segments not already in cache).
035 */
036public class ReloadSolrAclCachesForCoresRunnable extends StaticRunnable
037{
038    private WorkspaceSelector _workspaceSelector;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _workspaceSelector = (WorkspaceSelector) manager.lookup(WorkspaceSelector.ROLE);
044        super.service(manager);
045    }
046    
047    @Override
048    protected void _configureParameterValues(Configuration paramConfigs) throws ConfigurationException
049    {
050        // Do nothing
051    }
052    
053    @Override
054    public Map<String, Object> getParameterValues()
055    {
056        Map<String, Object> params = new HashMap<>();
057        try
058        {
059            String[] workspaces = _workspaceSelector.getWorkspaces();
060            params.put(ReloadSolrAclCachesForCoresSchedulable.WORKSPACE_KEY, StringUtils.join(workspaces, ','));
061        }
062        catch (RepositoryException e)
063        {
064            LoggerFactory.getLogger(ReloadSolrAclCachesForCoresRunnable.class).error("", e);
065        }
066        
067        return params;
068    }
069}