001/*
002 *  Copyright 2025 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 */
016
017package org.ametys.web.cache.scheduler;
018
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.quartz.JobDataMap;
022import org.quartz.JobExecutionContext;
023
024import org.ametys.core.schedule.Schedulable;
025import org.ametys.core.schedule.progression.ContainerProgressionTracker;
026import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
027import org.ametys.plugins.core.schedule.Scheduler;
028import org.ametys.web.cache.FOCommHelper;
029
030/**
031 * A {@link Schedulable} job which invalidates the cache of a site.
032 */
033public class InvalidateRemoteFOCacheSchedulable extends AbstractStaticSchedulable
034{
035    /** The key for the id of the object to unlock */
036    public static final String SITENAME_KEY = "siteName";
037    
038    private static final String __JOBDATAMAP_SITENAME_KEY = Scheduler.PARAM_VALUES_PREFIX + SITENAME_KEY;
039    
040    /** The cache helper */
041    protected FOCommHelper _foCommHelper;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _foCommHelper = (FOCommHelper) manager.lookup(FOCommHelper.ROLE);
048    }
049    
050    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
051    {
052        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
053        String siteName = (String) jobDataMap.get(__JOBDATAMAP_SITENAME_KEY);
054        _foCommHelper.invalidateFOCacheImmediately(siteName);
055    }
056}