01: package org.apache.ojb.otm.lock.map;
02:
03: /* Copyright 2003-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.util.Iterator;
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: import org.apache.ojb.broker.Identity;
23: import org.apache.ojb.otm.lock.ObjectLock;
24:
25: /**
26: *
27: * <javadoc>
28: *
29: * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
30: *
31: */
32: public class InMemoryLockMap implements LockMap {
33:
34: private HashMap _locks;
35:
36: public InMemoryLockMap() {
37: _locks = new HashMap();
38: }
39:
40: /**
41: *
42: * @see org.apache.ojb.otm.lock.map.LockMap#getLock(Identity)
43: *
44: */
45: public ObjectLock getLock(Identity oid) {
46: ObjectLock lock;
47:
48: synchronized (_locks) {
49: lock = (ObjectLock) _locks.get(oid);
50: if (lock == null) {
51: lock = new ObjectLock(oid);
52: _locks.put(oid, lock);
53: }
54: }
55: return lock;
56: }
57:
58: public void gc() {
59: synchronized (_locks) {
60: for (Iterator it = _locks.entrySet().iterator(); it
61: .hasNext();) {
62: Map.Entry entry = (Map.Entry) it.next();
63: ObjectLock lock = (ObjectLock) entry.getValue();
64: if (lock.isFree()) {
65: it.remove();
66: }
67: }
68: }
69: }
70:
71: public String toString() {
72: return _locks.toString();
73: }
74: }
|