KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > lock > map > InMemoryLockMap


1 package org.apache.ojb.otm.lock.map;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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 JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
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
35     private HashMap JavaDoc _locks;
36
37     public InMemoryLockMap ()
38     {
39         _locks = new HashMap JavaDoc();
40     }
41
42
43     /**
44      *
45      * @see org.apache.ojb.otm.lock.map.LockMap#getLock(Identity)
46      *
47      */

48     public ObjectLock getLock(Identity oid)
49     {
50         ObjectLock lock;
51
52         synchronized (_locks)
53         {
54             lock = (ObjectLock) _locks.get(oid);
55             if (lock == null)
56             {
57                 lock = new ObjectLock(oid);
58                 _locks.put(oid, lock);
59             }
60         }
61         return lock;
62     }
63
64     public void gc()
65     {
66         synchronized (_locks)
67         {
68             for (Iterator JavaDoc it = _locks.entrySet().iterator(); it.hasNext(); )
69             {
70                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
71                 ObjectLock lock = (ObjectLock) entry.getValue();
72                 if (lock.isFree())
73                 {
74                     it.remove();
75                 }
76             }
77         }
78     }
79
80     public String JavaDoc toString()
81     {
82         return _locks.toString();
83     }
84 }
85
Popular Tags