diff options
Diffstat (limited to 'policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java')
-rw-r--r-- | policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java | 227 |
1 files changed, 25 insertions, 202 deletions
diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java b/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java index 0e73eac1..bbf7d229 100644 --- a/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java +++ b/policy-core/src/main/java/org/onap/policy/drools/core/lock/PolicyResourceLockManager.java @@ -20,213 +20,36 @@ package org.onap.policy.drools.core.lock; -import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; -import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureApi.OperResult; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.onap.policy.common.capabilities.Lockable; +import org.onap.policy.common.capabilities.Startable; /** - * Manager of resource locks. Checks for API implementers. + * Manager of resource locks. */ -public class PolicyResourceLockManager extends SimpleLockManager { - - private static Logger logger = LoggerFactory.getLogger(PolicyResourceLockManager.class); - - /** - * Used by junit tests. - */ - protected PolicyResourceLockManager() { - super(); - } - - /** - * Get instance. - * - * @return the manager singleton - */ - public static PolicyResourceLockManager getInstance() { - return Singleton.instance; - } - - @Override - public boolean lock(String resourceId, String owner, int holdSec) { - if (resourceId == null) { - throw makeNullArgException(MSG_NULL_RESOURCE_ID); - } - - if (owner == null) { - throw makeNullArgException(MSG_NULL_OWNER); - } - - - return doBoolIntercept(impl -> impl.beforeLock(resourceId, owner, holdSec), () -> { - - // implementer didn't do the work - defer to the superclass - boolean locked = super.lock(resourceId, owner, holdSec); - - doIntercept(false, impl -> impl.afterLock(resourceId, owner, locked)); - - return locked; - }); - } - - @Override - public boolean refresh(String resourceId, String owner, int holdSec) { - if (resourceId == null) { - throw makeNullArgException(MSG_NULL_RESOURCE_ID); - } - - if (owner == null) { - throw makeNullArgException(MSG_NULL_OWNER); - } - - - return doBoolIntercept(impl -> impl.beforeRefresh(resourceId, owner, holdSec), () -> { - - // implementer didn't do the work - defer to the superclass - boolean refreshed = super.refresh(resourceId, owner, holdSec); - - doIntercept(false, impl -> impl.afterRefresh(resourceId, owner, refreshed)); - - return refreshed; - }); - } - - @Override - public boolean unlock(String resourceId, String owner) { - if (resourceId == null) { - throw makeNullArgException(MSG_NULL_RESOURCE_ID); - } - - if (owner == null) { - throw makeNullArgException(MSG_NULL_OWNER); - } - - - return doBoolIntercept(impl -> impl.beforeUnlock(resourceId, owner), () -> { - - // implementer didn't do the work - defer to the superclass - boolean unlocked = super.unlock(resourceId, owner); - - doIntercept(false, impl -> impl.afterUnlock(resourceId, owner, unlocked)); - - return unlocked; - }); - } - - /** - * Is locked. - * - * @throws IllegalArgumentException if the resourceId is {@code null} - */ - @Override - public boolean isLocked(String resourceId) { - if (resourceId == null) { - throw makeNullArgException(MSG_NULL_RESOURCE_ID); - } - - - return doBoolIntercept(impl -> impl.beforeIsLocked(resourceId), () -> - - // implementer didn't do the work - defer to the superclass - super.isLocked(resourceId) - ); - } +public interface PolicyResourceLockManager extends Startable, Lockable { /** - * Is locked by. + * Requests a lock on a resource. Typically, the lock is not immediately granted, + * though a "lock" object is always returned. Once the lock has been granted (or + * denied), the callback will be invoked to indicate the result. * - * @throws IllegalArgumentException if the resourceId or owner is {@code null} - */ - @Override - public boolean isLockedBy(String resourceId, String owner) { - if (resourceId == null) { - throw makeNullArgException(MSG_NULL_RESOURCE_ID); - } - - if (owner == null) { - throw makeNullArgException(MSG_NULL_OWNER); - } - - return doBoolIntercept(impl -> impl.beforeIsLockedBy(resourceId, owner), () -> - - // implementer didn't do the work - defer to the superclass - super.isLockedBy(resourceId, owner) - ); - } - - /** - * Applies a function to each implementer of the lock feature. Returns as soon as one - * of them returns a result other than <b>OPER_UNHANDLED</b>. If they all return - * <b>OPER_UNHANDLED</b>, then it returns the result of applying the default function. + * <p/> + * Notes: + * <dl> + * <li>The callback may be invoked <i>before</i> this method returns</li> + * <li>The implementation need not honor waitForLock={@code true}</li> + * </dl> * - * @param interceptFunc intercept function - * @param defaultFunc default function - * @return {@code true} if success, {@code false} otherwise - */ - private boolean doBoolIntercept(Function<PolicyResourceLockFeatureApi, OperResult> interceptFunc, - Supplier<Boolean> defaultFunc) { - - OperResult result = doIntercept(OperResult.OPER_UNHANDLED, interceptFunc); - if (result != OperResult.OPER_UNHANDLED) { - return (result == OperResult.OPER_ACCEPTED); - } - - return defaultFunc.get(); - } - - /** - * Applies a function to each implementer of the lock feature. Returns as soon as one - * of them returns a non-null value. - * - * @param continueValue if the implementer returns this value, then it continues to - * check addition implementers - * @param func function to be applied to the implementers - * @return first non-null value returned by an implementer, <i>continueValue</i> if - * they all returned <i>continueValue</i> - */ - private <T> T doIntercept(T continueValue, Function<PolicyResourceLockFeatureApi, T> func) { - - for (PolicyResourceLockFeatureApi impl : getImplementers()) { - try { - T result = func.apply(impl); - if (result != continueValue) { - return result; - } - - } catch (RuntimeException e) { - logger.warn("lock feature {} threw an exception", impl, e); - } - } - - return continueValue; - } - - // these may be overridden by junit tests - - /** - * Get implementers. - * - * @return the list of feature implementers - */ - protected List<PolicyResourceLockFeatureApi> getImplementers() { - return PolicyResourceLockFeatureApiConstants.getImpl().getList(); - } - - /** - * Initialization-on-demand holder idiom. - */ - private static class Singleton { - - private static final PolicyResourceLockManager instance = new PolicyResourceLockManager(); - - /** - * Not invoked. - */ - private Singleton() { - super(); - } - } + * @param resourceId identifier of the resource to be locked + * @param ownerKey information identifying the owner requesting the lock + * @param holdSec amount of time, in seconds, for which the lock should be held once + * it has been granted, after which it will automatically be released + * @param callback callback to be invoked once the lock is granted, or subsequently + * lost; must not be {@code null} + * @param waitForLock {@code true} to wait for the lock, if it is currently locked, + * {@code false} otherwise + * @return a new lock + */ + public Lock createLock(String resourceId, String ownerKey, int holdSec, LockCallback callback, + boolean waitForLock); } |