KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > dao > drivers > keygen > HighLowKeyGenerator


1 /*
2  * $Id: HighLowKeyGenerator.java,v 1.3 2005/06/07 12:32:31 bel70 Exp $
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Alexey Pavlov <alexnet@users.sourceforge.net>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.dao.drivers.keygen;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * Obtains a key by adding 1 to the highest key in the table.
35  *
36  * @author <a HREF="alexnet@sourceforge.net">A. Pavlov</a>
37  * @version $version$ $Date: 2005/06/07 12:32:31 $
38  */

39 public class HighLowKeyGenerator implements KeyGenerator {
40     /**
41      * Holds a KeyKeeper for each table. The table's name is the key and the
42      * value is a KeyKeeper.
43      */

44     Map JavaDoc keys = Collections.synchronizedMap(new HashMap JavaDoc());
45
46     /**
47      * Returns a generated key.
48      *
49      * @param classMap
50      * provides parameters for retreiving the key
51      * @param db
52      * the source of database connections for the key-values table
53      * @return the generated key
54      * @exception PersistenceException
55      * if an error occurs
56      */

57     public Object JavaDoc generateKey(String JavaDoc[] primaryKeyName, Connection JavaDoc connection)
58             throws SQLException JavaDoc {
59         final String JavaDoc tableName = primaryKeyName[0];
60
61         KeyKeeper keyKeeper;
62         synchronized (keys) {
63             keyKeeper = (KeyKeeper) keys.get(tableName);
64             if (keyKeeper == null) {
65                 keyKeeper = new KeyKeeper(tableName, primaryKeyName);
66                 keys.put(tableName, keyKeeper);
67             }
68         }
69         return keyKeeper.nextKey(connection);
70     }
71
72     /**
73      * Generation of key is before insert.
74      */

75     public boolean isBeforeInsert() {
76         return true;
77     }
78 }
79
Popular Tags