View Javadoc

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;
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  
35      private HashMap _locks;
36  
37      public InMemoryLockMap ()
38      {
39          _locks = new HashMap();
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 it = _locks.entrySet().iterator(); it.hasNext(); )
69              {
70                  Map.Entry entry = (Map.Entry) it.next();
71                  ObjectLock lock = (ObjectLock) entry.getValue();
72                  if (lock.isFree())
73                  {
74                      it.remove();
75                  }
76              }
77          }
78      }
79  
80      public String toString()
81      {
82          return _locks.toString();
83      }
84  }