KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > sequence > lib > SpeedoSequence


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.sequence.lib;
29
30 import javax.jdo.JDODataStoreException;
31 import javax.jdo.datastore.Sequence;
32
33 import org.objectweb.jorm.facility.naming.generator.LongGen;
34 import org.objectweb.jorm.facility.naming.generator.LongGenIncrAccessor;
35 import org.objectweb.speedo.pm.api.ProxyManager;
36 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
37 import org.objectweb.speedo.sequence.api.SequenceManager;
38 import org.objectweb.speedo.sequence.api.SerializableSequence;
39
40 /**
41  * This class corresponds to the description of the tag sequence
42  * in the XML file.
43  * A sequence has :
44  * - a name
45  * - a datastore name (not mandatory)
46  * - a factory class (not mandatory)
47  * - a strategy
48  * - a package name
49  * @author Y.Bersihand
50  */

51 public class SpeedoSequence implements Sequence, SerializableSequence {
52     
53     public final static byte NON_TRANSACTIONAL = 1;
54     public final static byte CONTIGUOUS = 2;
55     public final static byte TRANSACTIONAL = 3;
56     
57     public final static String JavaDoc NON_TRANSACTIONAL_STRING = "nontransactional";
58     public final static String JavaDoc CONTIGUOUS_STRING = "contiguous";
59     public final static String JavaDoc TRANSACTIONAL_STRING = "transactional";
60     
61     /**
62      * The name of the sequence
63      */

64     public String JavaDoc name;
65
66     /**
67      * The name of the sequence in the datastore
68      * Can be null or ""
69      */

70     public String JavaDoc datastoreName;
71     
72     /**
73      * The name of the factory class
74      * Can be null or ""
75      */

76     public String JavaDoc factoryClass;
77     
78     /**
79      * The strategy of the sequence (NON_TRANSACTIONAL | CONTIGUOUS | NON_CONTIGUOUS)
80      */

81     public byte strategy;
82     
83     /**
84      * The name of the package in which the sequence has been defined
85      */

86     public String JavaDoc packageName;
87     
88     public Integer JavaDoc increment = new Integer JavaDoc(1);
89     public Integer JavaDoc start;
90     public Integer JavaDoc cache;
91     
92     //the sequence manager
93
private SequenceManager sequenceManager = null;
94     
95     //the long gen associated to this sequence
96
private LongGen longGen = null;
97     
98     //used for the allocate method to avoid I/O
99
private int countDown = 0;
100     private long currentId = 0;
101     
102     public LongGen getLongGen() {
103         return longGen;
104     }
105     public void setLongGen(LongGen longGen) {
106         this.longGen = longGen;
107         //set the increment of the longGen
108
this.longGen.setIncrement(this.increment.intValue());
109     }
110     
111     /**
112      * Transforms a String into a Byte. The String must corresponds to local variables.
113      * It returns the byte associated with the variable.
114      * @param s String to transform.
115      * @return the byte associated to the String.
116      */

117     public static byte strategyToByte(String JavaDoc s) {
118         if (s.equalsIgnoreCase(TRANSACTIONAL_STRING))
119             return TRANSACTIONAL;
120         else if (s.equalsIgnoreCase(CONTIGUOUS_STRING))
121             return CONTIGUOUS;
122         else
123             return NON_TRANSACTIONAL;
124     }
125     
126     /**
127      * Transforms a byte into a String.
128      * @param b the byte to transform.
129      * @return the String associated to the byte.
130      */

131     public static String JavaDoc strategyToString(byte b) {
132        if (b == TRANSACTIONAL)
133            return TRANSACTIONAL_STRING;
134        else if (b == CONTIGUOUS)
135            return CONTIGUOUS_STRING;
136        else
137             return NON_TRANSACTIONAL_STRING;
138     }
139     
140     public SequenceManager getSequenceManager() {
141         return sequenceManager;
142     }
143     public void setSequenceManager(SequenceManager sequenceManager) {
144         this.sequenceManager = sequenceManager;
145     }
146     
147     // IMPLEMENTATION OF THE Sequence INTERFACE //
148
//-------------------------------------------//
149

150     /**
151      * Provides a hint to the implementation that
152      * the application will need additional sequence
153      * value objects in short order.
154      */

155     public void allocate(int additional) {
156         if (longGen != null) {
157             try {
158                 currentId = longGen.allocateIds(additional) - (additional * increment.intValue());
159                 countDown += additional;
160             } catch (Exception JavaDoc e) {
161                 return;
162             }
163         }
164     }
165     
166     /**
167      * Returns the current sequence value object if it is available.
168      */

169     public Object JavaDoc current() {
170         if (longGen != null) {
171             if (checkStrategy()) {
172                 if (countDown != 0) {
173                     return new Long JavaDoc(currentId);
174                 } else {
175                     try {
176                         return new Long JavaDoc(longGen.current());
177                     } catch (Exception JavaDoc e) {
178                         throw new JDODataStoreException("Problem on current() with sequence " + name
179                                 + ": " + e.getMessage());
180                     }
181                 }
182             }
183         }
184         throw new JDODataStoreException("Problem on current() with sequence " + name
185                 + ": id generator not ready.");
186     }
187     
188     /**
189      * Returns the fully qualified name of the Sequence.
190      */

191     public String JavaDoc getName() {
192         return (packageName == null)?(""):(packageName + ".") + name;
193     }
194     
195     /**
196      * Returns the next sequence value object.
197      */

198     public Object JavaDoc next() {
199         if (longGen != null) {
200             if (checkStrategy()) {
201                 // if count down is not equal to zero, use the internal counter
202
if (countDown != 0) {
203                     long res;
204                     countDown --;
205                     res = currentId;
206                     currentId += increment.intValue();
207                     return new Long JavaDoc(res);
208                 } else {
209                     //else, make a call to the datastore
210
try {
211                         return new Long JavaDoc(longGen.genId());
212                     } catch (Exception JavaDoc e) {
213                         throw new JDODataStoreException("Problem on next() with sequence " + name
214                                 + ": " + e.getMessage());
215                     }
216                 }
217                 
218             }
219         }
220         throw new JDODataStoreException("Problem on next() with sequence " + name
221                 + ": id generator not ready.");
222     }
223     
224     public long nextValue() {
225         return ((Long JavaDoc) next()).longValue();
226     }
227
228     public long currentValue() {
229         return ((Long JavaDoc) current()).longValue();
230     }
231     
232     /**
233      * @return true if the operation is allowed according to the strategy
234      * else, false.
235      */

236     private boolean checkStrategy() {
237         if (strategy != NON_TRANSACTIONAL) {
238             //get the pmf
239
ProxyManagerFactory pmf = sequenceManager.getPMF();
240             //get the pm
241
ProxyManager pm = pmf.lookup();
242             //check that a transaction is active
243
return (pm != null && pm.currentTransaction().isActive());
244         }
245         return true;
246     }
247 }
248
Popular Tags