KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > KeyGenerator


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: KeyGenerator.java,v 1.5 2002/09/18 06:54:15 per_nyfelt Exp $
8

9 package org.ozoneDB.core;
10
11 import org.ozoneDB.Setup;
12 import org.ozoneDB.util.LogWriter;
13
14
15 /**
16  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
17  * @version $Revision: 1.5 $Date: 2002/09/18 06:54:15 $
18  */

19 public final class KeyGenerator extends ServerComponent {
20
21     private long idCount = 0;
22
23     private long idBorder = -1;
24
25     private long idRange = 5000;
26
27
28     public KeyGenerator( Env env ) {
29         super( env );
30     }
31
32
33     public void startup() throws Exception JavaDoc {
34         env.logWriter.newEntry( this, "startup...", LogWriter.INFO );
35     }
36
37
38     public void shutdown() throws Exception JavaDoc {
39         env.logWriter.newEntry( this, "shutdown...", LogWriter.INFO );
40     }
41
42
43     public void save() throws Exception JavaDoc {
44     }
45
46
47     public long nextID() {
48         return nextID( 1 );
49     }
50
51
52     public synchronized long nextID( long range ) {
53         if (idCount + range >= idBorder) {
54             try {
55                 idCount = env.state.longProperty( Setup.XOID, -1 );
56                 if (idCount == -1) {
57                     throw new Exception JavaDoc( "Unable to access the " + Setup.XOID + " property." );
58                 }
59                 idBorder = idCount + range + idRange;
60
61                 env.state.setLongProperty( Setup.XOID, idBorder );
62                 setChanged();
63             } catch (Exception JavaDoc e) {
64                 env.fatalError( /*null*/this, "nextID(): " + e.toString(), e );
65             }
66         }
67         long result = idCount;
68         idCount += range;
69
70         return result;
71     }
72
73 }
74
Popular Tags