01: /**
02: * $RCSfile: $
03: * $Revision: $
04: * $Date: $
05: *
06: * Copyright (C) 2007 Jive Software. All rights reserved.
07: *
08: * This software is published under the terms of the GNU Public License (GPL),
09: * a copy of which is included in this distribution.
10: */package org.jivesoftware.util.lock;
11:
12: import java.util.concurrent.locks.Lock;
13:
14: /**
15: * Manager of {@link Lock Locks} that could be valid when running within a cluster or when in local mode.
16: * By default the LockManager will use a {@link org.jivesoftware.util.lock.LocalLockFactory} but
17: * you can set new factories by sending {@link #setLockFactory(LockFactory)}.
18: *
19: * @author Gaston Dombiak
20: */
21: public class LockManager {
22:
23: private static LockFactory lockFactory;
24:
25: static {
26: setLockFactory(new LocalLockFactory());
27: }
28:
29: /**
30: * Returns the existing lock factory being used for creating new Locks.
31: *
32: * @return the existing lock factory being used for creating new Locks.
33: */
34: public static LockFactory getLockFactory() {
35: return lockFactory;
36: }
37:
38: /**
39: * Sets the lock factory to use for creating new Locks. If <tt>null</tt> then
40: * use {@link LocalLockFactory}.
41: *
42: * @param lockFactory the new lock factory to use for creating new Locks.
43: */
44: public static void setLockFactory(LockFactory lockFactory) {
45: LockManager.lockFactory = lockFactory;
46: }
47:
48: /**
49: * Returns an existing {@link Lock} on the specified key or creates a new one if none was found. This
50: * operation should be thread safe. Successive calls with the same key may or may not return
51: * the same {@link Lock}. However, different threads asking for the same Lock at the same time will
52: * get the same Lock object.
53: *
54: * @param key the object that defines the visibility or scope of the lock.
55: * @return an existing lock on the specified key or creates a new one if none was found.
56: */
57: public static Lock getLock(Object key) {
58: return lockFactory.getLock(key);
59: }
60: }
|