001 package org.apache.ojb.otm.lock.map;
002
003 /* Copyright 2003-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import java.util.Iterator;
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import org.apache.ojb.broker.Identity;
023 import org.apache.ojb.otm.lock.ObjectLock;
024
025 /**
026 *
027 * <javadoc>
028 *
029 * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
030 *
031 */
032 public class InMemoryLockMap implements LockMap
033 {
034
035 private HashMap _locks;
036
037 public InMemoryLockMap ()
038 {
039 _locks = new HashMap();
040 }
041
042
043 /**
044 *
045 * @see org.apache.ojb.otm.lock.map.LockMap#getLock(Identity)
046 *
047 */
048 public ObjectLock getLock(Identity oid)
049 {
050 ObjectLock lock;
051
052 synchronized (_locks)
053 {
054 lock = (ObjectLock) _locks.get(oid);
055 if (lock == null)
056 {
057 lock = new ObjectLock(oid);
058 _locks.put(oid, lock);
059 }
060 }
061 return lock;
062 }
063
064 public void gc()
065 {
066 synchronized (_locks)
067 {
068 for (Iterator it = _locks.entrySet().iterator(); it.hasNext(); )
069 {
070 Map.Entry entry = (Map.Entry) it.next();
071 ObjectLock lock = (ObjectLock) entry.getValue();
072 if (lock.isFree())
073 {
074 it.remove();
075 }
076 }
077 }
078 }
079
080 public String toString()
081 {
082 return _locks.toString();
083 }
084 }