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    
020    import java.io.Serializable;
021    
022    /**
023     * a persistent entry for locks. All locks that are hold from
024     * transaction on objects are represented by a LockENtry and made
025     * persistent to the database.
026     * @author Thomas Mahler
027     */
028    public class LockEntry implements Serializable
029    {
030            static final long serialVersionUID = 8060850552557793930L;    /**
031         * marks a Read Lock.
032         */
033        public static int LOCK_READ = 0;
034    
035        /**
036         * marks a Write Lock.
037         */
038        public static int LOCK_WRITE = 1;
039    
040        /**
041         * the unique OID of the object to be locked.
042         */
043        private String oidString;
044    
045        /**
046         * the GUID of the transaction that holds the lock
047         */
048        private String transactionId;
049    
050        /**
051         * the timestamp marking the time of acquisition of this lock
052         */
053        private long timestamp;
054    
055        /**
056         * the isolationlevel for this lock.
057         */
058        private int isolationLevel;
059    
060        /**
061         * marks if this is a read or a write lock.
062         * LOCK_READ = 0;
063         * LOCK_WRITE = 1;
064         */
065        private int lockType;
066    
067        /**
068         * Multiargument constructor for fast loading of LockEntries by OJB.
069         */
070        public LockEntry(String oidString,
071                         String transactionId,
072                         long timestamp,
073                         int isolationLevel,
074                         int lockType)
075        {
076            this.oidString = oidString;
077            this.transactionId = transactionId;
078            this.timestamp = timestamp;
079            this.isolationLevel = isolationLevel;
080            this.lockType = lockType;
081    
082        }
083    
084        /**
085         * build a LockEntry from an OID and a Transaction ID
086         */
087        public LockEntry(String oidString, String transactionId)
088        {
089            this.oidString = oidString;
090            this.transactionId = transactionId;
091        }
092    
093        /**
094         * default constructor
095         */
096        public LockEntry()
097        {
098        }
099    
100        /**
101         * returns the OID STring of the locked object.
102         */
103        public String getOidString()
104        {
105            return oidString;
106        }
107    
108        /**
109         * returns the GUID string of the locking transaction.
110         */
111        public String getTransactionId()
112        {
113            return transactionId;
114        }
115    
116        /**
117         * returns the timestamp of the acqusition of the lock.
118         */
119        public long getTimestamp()
120        {
121            return timestamp;
122        }
123    
124        /**
125         * returns the isolation level of this lock
126         */
127        public int getIsolationLevel()
128        {
129            return isolationLevel;
130        }
131    
132        /**
133         * returns the locktype of this lock.
134         * @return LOCK_READ if lock is a readlock,
135         * LOCK_WRITE if lock is a Write lock.
136         */
137        public int getLockType()
138        {
139            return lockType;
140        }
141    
142        /**
143         * sets the locktype of this lockentry.
144         * @param locktype LOCK_READ for read, LOCK_WRITE for write lock.
145         */
146        public void setLockType(int locktype)
147        {
148            this.lockType = locktype;
149        }
150    
151        /**
152         * returns true if this lock is owned by transaction tx, else false.
153         */
154        public boolean isOwnedBy(TransactionImpl tx)
155        {
156            return this.getTransactionId().equals(tx.getGUID());
157        }
158    
159    
160        /**
161         * Sets the isolationLevel.
162         * @param isolationLevel The isolationLevel to set
163         */
164        public void setIsolationLevel(int isolationLevel)
165        {
166            this.isolationLevel = isolationLevel;
167        }
168    
169        /**
170         * Sets the oidString.
171         * @param oidString The oidString to set
172         */
173        public void setOidString(String oidString)
174        {
175            this.oidString = oidString;
176        }
177    
178        /**
179         * Sets the timestamp.
180         * @param timestamp The timestamp to set
181         */
182        public void setTimestamp(long timestamp)
183        {
184            this.timestamp = timestamp;
185        }
186    
187        /**
188         * Sets the transactionId.
189         * @param transactionId The transactionId to set
190         */
191        public void setTransactionId(String transactionId)
192        {
193            this.transactionId = transactionId;
194        }
195    
196    }