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.lock;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.quartz.JobDataMap;
021import org.quartz.JobExecutionContext;
022
023import org.ametys.core.schedule.Schedulable;
024import org.ametys.core.schedule.progression.ContainerProgressionTracker;
025import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
026import org.ametys.plugins.core.schedule.Scheduler;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029
030/**
031 * A {@link Schedulable} job which unlock an object.
032 */
033public class UnlockSchedulable extends AbstractStaticSchedulable
034{
035    /** The key for the id of the object to unlock */
036    public static final String OBJECT_ID_KEY = "objectId";
037    
038    private static final String __JOBDATAMAP_OBJECT_ID_KEY = Scheduler.PARAM_VALUES_PREFIX + OBJECT_ID_KEY;
039    
040    /** The Ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    @Override
051    public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception
052    {
053        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
054        String objectId = (String) jobDataMap.get(__JOBDATAMAP_OBJECT_ID_KEY);
055        
056        getLogger().info("Trying to unlock the object '{}'", objectId);
057        try
058        {
059            LockableAmetysObject object = _resolver.resolveById(objectId);
060            
061            if (object.isLocked())
062            {
063                object.unlock();
064                getLogger().info("Successfully unlocked the object '{}'", objectId);
065            }
066            else
067            {
068                getLogger().warn("The object of ID '{}' was scheduled to be unlocked but it already is.", objectId);
069            }
070        }
071        catch (UnknownAmetysObjectException e)
072        {
073            // The object doesn't exist anymore
074            if (getLogger().isWarnEnabled())
075            {
076                getLogger().warn("The object of ID '" + objectId + "' was scheduled to be unlocked but it doesn't exist anymore.", e);
077            }
078        }
079    }
080}