KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > generator > LongGenIncr


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.facility.naming.generator;
25
26 import org.objectweb.jorm.api.PAccessor;
27 import org.objectweb.jorm.api.PBinding;
28 import org.objectweb.jorm.api.PClassMapping;
29 import org.objectweb.jorm.api.PException;
30 import org.objectweb.jorm.naming.api.PName;
31
32 /**
33  * Implements a long generator (LongGen) that produces new long ID merely by
34  * incrementing a persistent long variable.
35  * @author P. Dechamboux
36  */

37 public abstract class LongGenIncr
38         implements PLongGen, PAccessor, LongGenIncrAccessor, PBinding {
39     private LongGenIncrMgr mgr;
40     /**
41      * The next long ID to be delivered by next call to genId. This is
42      * also a persistent variable.
43      */

44     private long nextId = 0;
45
46     // IMPLEMENTATION OF METHODS FROM THE LongGen INTERFACE
47

48     /**
49      * Initializes this long generator. Verifies if it already exists and if
50      * not, creates it.
51      * @param name The name identifier associated with this long generator.
52      * @param pcm The associated PClassMapping.
53      * @param mgr The manager associated with this generator.
54      */

55     public void init(String JavaDoc name, PClassMapping pcm, LongGenMgr mgr)
56             throws PException {
57         this.mgr = (LongGenIncrMgr) mgr;
58         // Initializes the PBinding
59
init(pcm);
60         // Verifies if it already exists
61
PName lgpn = pcm.getPBinder().decodeString(name);
62         Object JavaDoc conn = mgr.getPMapper().getConnection();
63         try {
64             this.bind(lgpn);
65             if (exist(conn)) {
66                 read(conn, this);
67             } else {
68                 // Creates a new one and initializes it.
69
export(conn, name);
70                 write(conn, this);
71             }
72         } catch (PException pe) {
73             mgr.getPMapper().closeConnection(conn);
74             throw pe;
75         }
76         mgr.getPMapper().closeConnection(conn);
77     }
78
79     /**
80      * Generates a new long identifier.
81      * @return The new identifier.
82      */

83     public synchronized long genId() throws PException {
84         Object JavaDoc conn = mgr.getPMapper().getConnection();
85         try {
86             return genId(conn);
87         } finally {
88             mgr.getPMapper().closeConnection(conn);
89         }
90     }
91
92     public long genId(Object JavaDoc conn) throws PException {
93         nextId++;
94         write(conn, this);
95         return nextId - 1;
96     }
97
98     // IMPLEMENTATION OF METHODS FROM THE PAccessor INTERFACE
99

100     /**
101      * It returns the memory instance within which the persistent variables are
102      * defined.
103      * @return The actual object containing memory persistent variables.
104      */

105     public Object JavaDoc getMemoryInstance() {
106         return this;
107     }
108
109     // IMPLEMENTATION OF METHODS FROM THE LongGenAccessor INTERFACE
110

111     /**
112      * nextId getter.
113      */

114     public long paGetNextId() {
115         return nextId;
116     }
117
118     /**
119      * nextId setter.
120      */

121     public void paSetNextId(long val) {
122         nextId = val;
123     }
124 }
125
Popular Tags