KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > persistence > SeedGenerator


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2000-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43 package org.exolab.jms.persistence;
44
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48
49
50 /**
51  * This class generates seeds
52  *
53  * @version $Revision: 1.3 $ $Date: 2005/06/09 14:39:52 $
54  * @author <a HREF="mailto:tima@intalio.com">Tim Anderson</a>
55  *
56  **/

57 public class SeedGenerator {
58
59     /**
60      * Caches the singleton instance of this class
61      */

62     private static SeedGenerator _instance;
63
64     /**
65      * Monitor used to synchronize the initialization of the
66      * singleton class instance
67      */

68     private static final Object JavaDoc _block = new Object JavaDoc();
69
70
71     /**
72      * Returns the singleton instance.
73      *
74      * Note that initialise() must have been invoked first for this
75      * to return a valid instance.
76      *
77      * @return SeedGenerator the singleton instance
78      */

79     public static SeedGenerator instance() {
80         return _instance;
81     }
82
83     /**
84      * Initialise the seed generator
85      *
86      * @return SeedGenerator the singleton instance
87      */

88     public static SeedGenerator initialise() {
89         if (_instance == null) {
90             synchronized (_block) {
91                 if (_instance == null) {
92                     _instance = new SeedGenerator();
93                 }
94             }
95         }
96
97         return _instance;
98     }
99
100     /**
101      * Return the next seed value for the given name
102      *
103      * @param connection - the connection used
104      * @param name - the name of the seed
105      * @return long - the next seed value
106      * @exception PersistenceException - if the seed cannot be retrieved
107      */

108     public synchronized long next(Connection JavaDoc connection, String JavaDoc name)
109         throws PersistenceException {
110
111         PreparedStatement JavaDoc select = null;
112         PreparedStatement JavaDoc update = null;
113         PreparedStatement JavaDoc insert = null;
114         ResultSet JavaDoc result = null;
115         long value = 0;
116
117         boolean successful = false;
118         try {
119             select = connection.prepareStatement(
120                 "select seed from seeds where name=?");
121             select.setString(1, name);
122
123             result = select.executeQuery();
124             if (result.next()) {
125                 value = result.getLong(1);
126                 value++;
127                 update = connection.prepareStatement(
128                     "update seeds set seed=? where name=?");
129                 update.setLong(1, value);
130                 update.setString(2, name);
131                 update.executeUpdate();
132             } else {
133                 value = 1;
134                 insert = connection.prepareStatement(
135                     "insert into seeds (name, seed) values (?,?)");
136                 insert.setString(1, name);
137                 insert.setLong(2, value);
138                 insert.executeUpdate();
139             }
140         } catch (Exception JavaDoc exception) {
141             throw new PersistenceException("next " + exception.getMessage());
142         } finally {
143             SQLHelper.close(result);
144             SQLHelper.close(select);
145             SQLHelper.close(update);
146             SQLHelper.close(insert);
147         }
148
149         return value;
150     }
151
152     /**
153      * Remove all seeds.
154      *
155      * @param connection - the connection to use
156      * @exception PersistenceException - is thrown if the request does not
157      * complete
158      */

159     public void removeAll(Connection JavaDoc connection)
160         throws PersistenceException {
161
162         PreparedStatement JavaDoc delete = null;
163         try {
164             delete = connection.prepareStatement("delete from seeds");
165             delete.executeUpdate();
166         } catch (Exception JavaDoc error) {
167             throw new PersistenceException("Failed in removeAll with " +
168                 error.toString());
169         } finally {
170             SQLHelper.close(delete);
171         }
172     }
173
174     /**
175      * Issue a close, which in this case invalidates the singleton
176      * TODO: do we really need this
177      */

178     public void close() {
179         _instance = null;
180     }
181
182     /**
183      * Constructor
184      */

185     protected SeedGenerator() {
186     }
187 }
188
Popular Tags