KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > Lock


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import org.myoodb.exception.*;
27
28 public final class Lock implements AbstractLock
29 {
30     private long m_lockId;
31     private int m_level = ACCESS_NONE;
32
33     public Lock()
34     {
35         reset();
36     }
37
38     public int tryAcquire(AbstractTransaction tx, int level)
39     {
40         synchronized (this)
41         {
42             int prevLevel = getLockLevel(tx);
43
44             if ((m_lockId == -1) || (isAcquiredBy(tx) == true))
45             {
46                 m_lockId = tx.getTransactionIdentifier();
47                 m_level = level > m_level ? level : m_level;
48                 return prevLevel;
49             }
50             else
51             {
52                 return NOT_ACQUIRED;
53             }
54         }
55     }
56
57     public void release(AbstractTransaction tx)
58     {
59         /* TODO: make work with nested transactions
60         if (isAcquiredBy(tx) == false)
61         {
62             throw new PermissionException("Invalid release (Lock not owned): " + m_lockId);
63         }
64         */

65
66         reset();
67     }
68
69     public boolean isAcquiredBy(AbstractTransaction tx)
70     {
71         return (m_lockId != -1) ? (m_lockId == tx.getTransactionIdentifier()) : false;
72     }
73
74     public long getLockIdentifier()
75     {
76         return m_lockId;
77     }
78
79     public int getLockLevel(AbstractTransaction tx)
80     {
81         if (tx != null)
82         {
83             return isAcquiredBy(tx) ? m_level : ACCESS_NONE;
84         }
85         else
86         {
87             return m_level;
88         }
89     }
90
91     public int getLockLevel()
92     {
93         return m_level;
94     }
95
96     public void reset()
97     {
98         synchronized (this)
99         {
100             m_lockId = -1;
101             m_level = ACCESS_NONE;
102         }
103     }
104 }
105
Popular Tags