001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (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.model;
020
021import org.nuxeo.ecm.core.api.Lock;
022
023/**
024 * Manager of locks for a repository.
025 * <p>
026 * The method {@link #close} 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 the owner to check, or {@code null} for no check
066     * @param force {@code true} to just do the remove and not return the previous lock
067     * @return the previous lock (may be {@code null}), with a failed flag if locking failed
068     */
069    Lock removeLock(String id, String owner);
070
071    /**
072     * Checks if a given lock can be removed by the given owner.
073     *
074     * @param oldOwner the existing lock's owner
075     * @param owner the owner (may be {@code null})
076     * @return {@code true} if the lock can be removed
077     */
078    static boolean canLockBeRemoved(String oldOwner, String owner) {
079        return owner == null || owner.equals(oldOwner);
080    }
081
082    /**
083     * Closes the lock manager and releases resources.
084     */
085    void closeLockManager();
086
087    /**
088     * Clears any cache held by the lock manager.
089     */
090    void clearLockManagerCaches();
091
092}