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.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang3.StringUtils;
023import org.apache.solr.client.solrj.SolrServerException;
024import org.quartz.JobDataMap;
025import org.quartz.JobExecutionContext;
026
027import org.ametys.cms.content.indexing.solr.SolrIndexer;
028import org.ametys.core.schedule.Schedulable;
029import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
030import org.ametys.plugins.core.schedule.Scheduler;
031
032/**
033 * A {@link Schedulable} job for reloading Solr ACL caches for given workspaces (only for segments not already in cache).
034 */
035public class ReloadSolrAclCachesForCoresSchedulable extends AbstractStaticSchedulable
036{
037    /** Parameter for workspace */
038    public static final String WORKSPACE_KEY = "workspaces";
039    
040    private static final String __JOBDATAMAP_WORKSPACE_KEY = Scheduler.PARAM_VALUES_PREFIX + WORKSPACE_KEY;
041    
042    /** The Solr indexer */
043    protected SolrIndexer _solrIndexer;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE);
050    }
051    
052    @Override
053    public void execute(JobExecutionContext context) throws Exception
054    {
055        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
056        String workspaceNames = jobDataMap.getString(__JOBDATAMAP_WORKSPACE_KEY);
057        for (String workspaceName : StringUtils.split(workspaceNames, ","))
058        {
059            try
060            {
061                _solrIndexer.reloadAclCache(workspaceName, true);
062            }
063            catch (IOException | SolrServerException e)
064            {
065                // Don't re-throw the exception
066                getLogger().warn(String.format("Error reloading Solr ACL cache for workspace '%s'. Maybe Solr server is not started up.", workspaceName), e);
067            }
068        }
069    }
070}