001    package org.apache.ojb.odmg.locking;
002    
003    /* Copyright 2002-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 org.apache.ojb.odmg.TransactionImpl;
019    import org.apache.ojb.broker.util.configuration.Configuration;
020    import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
021    
022    import java.util.Collection;
023    
024    /**
025     * The base class of all LockingStrategies. It provides the basic
026     * infrastructure to read and write locks to the persistent storage.
027     * @deprecated
028     * @author Thomas Mahler
029     */
030    public abstract class AbstractLockStrategy implements LockStrategy
031    {
032        /**
033         * the timeout for lock entries
034         */
035        public static long DEFAULT_LOCK_TIMEOUT = 30000;
036    
037        /**
038         * the map holding all locks
039         */
040        private static LockMap lockMap = null;
041    
042    
043        public AbstractLockStrategy()
044        {
045            synchronized (AbstractLockStrategy.class)
046            {
047                if (lockMap == null)
048                {
049                    lockMap = LockMapFactory.getLockMap();
050                    Configuration conf = OjbConfigurator.getInstance().getConfigurationFor(null);
051                    DEFAULT_LOCK_TIMEOUT = conf.getInteger("LockTimeout", 60000);
052                }
053            }
054        }
055    
056    
057        /**
058         * returns the LockEntry for the Writer of object obj.
059         * If now writer exists, null is returned.
060         */
061        protected LockEntry getWriter(Object obj)
062        {
063            return lockMap.getWriter(obj);
064        }
065    
066        /**
067         * returns a collection of Reader LockEntries for object obj.
068         * If now LockEntries could be found an empty Vector is returned.
069         */
070        protected Collection getReaders(Object obj)
071        {
072            return lockMap.getReaders(obj);
073        }
074    
075        /**
076         * Add a reader lock entry for transaction tx on object obj
077         * to the persistent storage.
078         */
079        protected boolean addReader(TransactionImpl tx, Object obj)
080        {
081            return lockMap.addReader(tx, obj);
082        }
083    
084        /**
085         * remove a reader lock entry for transaction tx on object obj
086         * from the persistent storage.
087         */
088        protected void removeReader(TransactionImpl tx, Object obj)
089        {
090            lockMap.removeReader(tx, obj);
091        }
092    
093        /**
094         * remove a writer lock entry for transaction tx on object obj
095         * from the persistent storage.
096         */
097        protected void removeWriter(LockEntry writer)
098        {
099            lockMap.removeWriter(writer);
100        }
101    
102    
103        /**
104         * upgrade a reader lock entry for transaction tx on object obj
105         * and write it to the persistent storage.
106         */
107        protected boolean upgradeLock(LockEntry reader)
108        {
109            return lockMap.upgradeLock(reader);
110        }
111    
112        /**
113         * generate a writer lock entry for transaction tx on object obj
114         * and write it to the persistent storage.
115         */
116        protected boolean setWriter(TransactionImpl tx, Object obj)
117        {
118            return lockMap.setWriter(tx, obj);
119        }
120    
121        /**
122         * check if there is a reader lock entry for transaction tx on object obj
123         * in the persistent storage.
124         */
125        protected boolean hasReadLock(TransactionImpl tx, Object obj)
126        {
127            return lockMap.hasReadLock(tx, obj);
128        }
129    
130    }