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