KickJava   Java API By Example, From Geeks To Geeks.

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


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 public final class IdentifierManager extends AbstractManager
27 {
28     public final static String JavaDoc XID = "myoodb.xid";
29     public final static long BLOCK_SIZE = 100;
30
31     private long m_objectIdentifierBlock;
32     private long m_objectIdentifierBegin;
33     private long m_objectIdentifierEnd;
34
35     private long m_connectionIdentifier;
36     private long m_transactionIdentifier;
37
38     public IdentifierManager()
39     {
40         reset();
41     }
42
43     private void store()
44     {
45         try
46         {
47             MyOodbManager.getTheManager().getIdentifierProperties().setLongProperty(XID, m_objectIdentifierBlock);
48             MyOodbManager.getTheManager().storeIdentifierProperties();
49         }
50         catch (Exception JavaDoc e)
51         {
52             MyOodbManager.getTheManager().fatalError(this, "Identifier Manager unable to store identifier: " + e, e);
53         }
54     }
55
56     public void startup() throws Exception JavaDoc
57     {
58     }
59
60     public void shutdown() throws Exception JavaDoc
61     {
62     }
63
64     public void reset()
65     {
66         m_objectIdentifierBlock = -1;
67         m_objectIdentifierBegin = 0;
68         m_objectIdentifierEnd = 0;
69
70         m_connectionIdentifier = 0;
71         m_transactionIdentifier = 0;
72     }
73
74     public synchronized long getNextObjectId()
75     {
76         if (m_objectIdentifierBlock == -1)
77         {
78             m_objectIdentifierBlock = MyOodbManager.getTheManager().getIdentifierProperties().getLongProperty(XID, -1);
79
80             if (m_objectIdentifierBlock == -1)
81             {
82                 MyOodbManager.getTheManager().fatalError(this, "Identifier Manager unable to access the " + XID + " property.", null);
83             }
84             else if (m_objectIdentifierBlock != 0)
85             {
86                 m_objectIdentifierEnd = m_objectIdentifierBlock;
87             }
88
89             m_objectIdentifierBlock += BLOCK_SIZE;
90
91             store();
92         }
93
94         if (m_objectIdentifierBegin >= BLOCK_SIZE)
95         {
96             m_objectIdentifierBegin = 0;
97             m_objectIdentifierEnd = m_objectIdentifierBlock;
98
99             m_objectIdentifierBlock += BLOCK_SIZE;
100
101             store();
102         }
103
104         return ++m_objectIdentifierBegin + m_objectIdentifierEnd;
105     }
106
107     public synchronized long getNextConnectionId()
108     {
109         return ++m_connectionIdentifier;
110     }
111
112     public synchronized long getNextTransactionId()
113     {
114         return ++m_transactionIdentifier;
115     }
116 }
117
Popular Tags