Implementing LockableResource
This page will cover how to implement the simpler of the two interfaces, LockableResource
LockableResource.java
public interface LockableResource extends Resource { /** * Lock this resource and return a token * * @param timeout - in seconds, or null * @param lockInfo * @return - a result containing the token representing the lock if succesful, * otherwise a failure reason code */ public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException, PreConditionFailedException, LockedException; /** * Renew the lock and return new lock info * * @param token * @return */ public LockResult refreshLock(String token) throws NotAuthorizedException, PreConditionFailedException; /** * If the resource is currently locked, and the tokenId matches the current * one, unlock the resource * * @param tokenId */ public void unlock(String tokenId) throws NotAuthorizedException, PreConditionFailedException; /** * * @return - the current lock, if the resource is locked, or null */ public LockToken getCurrentLock(); }
Your implementation needs to create a lock on the specified resource, and this lock information must be returned on subsequent calls to getCurrentLock etc. Note that some developers new to milton hack the implementation and just return static data or nulls - this will not work in practise. Webdav clients are very particular about the lock state of a resource and they don't like it when things arent what they expect!
Usually you'll want to store locks in a database, but its often acceptable to hold locks in memory provided the information is shared across your cluster (if you have a cluster).
If you're using a single server, and if your resources have a not-null and immutable uniqueId field you can use Milton's SimpleLockManager, which is often a good way to get started.