001/* 002 * (C) Copyright 2006-2019 Nuxeo (http://nuxeo.com/) and others. 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 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.api.lock; 020 021import org.nuxeo.ecm.core.api.Lock; 022 023/** 024 * Manager of locks for a repository. 025 * <p> 026 * The method {@link #closeLockManager} must be called when done with the lock manager. 027 * 028 * @since 6.0 029 */ 030public interface LockManager { 031 032 /** 033 * Gets the lock on a document. 034 * <p> 035 * If the document does not exist, {@code null} is returned. 036 * 037 * @param id the document id 038 * @return the existing lock, or {@code null} when there is no lock 039 */ 040 Lock getLock(String id); 041 042 /** 043 * Sets a lock on a document. 044 * <p> 045 * If the document is already locked, returns its existing lock status (there is no re-locking, {@link #removeLock} 046 * must be called first). 047 * 048 * @param id the document id 049 * @param lock the lock to set 050 * @return {@code null} if locking succeeded, or the existing lock if locking failed 051 */ 052 Lock setLock(String id, Lock lock); 053 054 /** 055 * Removes the lock from a document. 056 * <p> 057 * The previous lock is returned. 058 * <p> 059 * If {@code owner} is {@code null} then the lock is unconditionally removed. 060 * <p> 061 * If {@code owner} is not {@code null}, it must match the existing lock owner for the lock to be removed. If it 062 * doesn't match, the returned lock will return {@code true} for {@link Lock#getFailed}. 063 * 064 * @param id the document id 065 * @param owner the owner to check, or {@code null} for no check 066 * @return the previous lock (may be {@code null}), with a failed flag if locking failed 067 */ 068 Lock removeLock(String id, String owner); 069 070 /** 071 * Checks if a given lock can be removed by the given owner. 072 * 073 * @param oldOwner the existing lock's owner 074 * @param owner the owner (may be {@code null}) 075 * @return {@code true} if the lock can be removed 076 */ 077 static boolean canLockBeRemoved(String oldOwner, String owner) { 078 return owner == null || owner.equals(oldOwner); 079 } 080 081 /** 082 * Closes the lock manager and releases resources. 083 */ 084 default void closeLockManager() { 085 // nothing 086 } 087 088 /** 089 * Clears any cache held by the lock manager. 090 */ 091 default void clearLockManagerCaches() { 092 // nothing 093 } 094 095}