KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: KeyGeneratorFactory.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.SQLException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.jresearch.gossip.configuration.Configurator;
31
32 /**
33  * KeyGeneratorFactory holds a list of key generators.
34  *
35  * @author <a HREF="alexnet@sourceforge.net">A. Pavlov</a>
36  * @version $version$ $Date: 2005/06/07 12:32:31 $
37  */

38 public class KeyGeneratorFactory {
39     private KeyGeneratorFactory() {
40     }
41
42     /**
43      * List of <code>SqlInterfaceEntry</code> holding the instantiated
44      * interfaces
45      */

46     static ArrayList JavaDoc generators = new ArrayList JavaDoc();
47
48     static Configurator conf = Configurator.getInstance();
49
50     static class KeyGeneratorEntry {
51         String JavaDoc name;
52
53         KeyGenerator keyGenerator;
54
55         KeyGeneratorEntry(String JavaDoc name, KeyGenerator keyGenerator) {
56             this.name = name;
57             this.keyGenerator = keyGenerator;
58         }
59     }
60
61     static interface KeyGeneratorCreator {
62         KeyGenerator create();
63     }
64
65     /** A list of the available key generators */
66     private static KeyGeneratorCreator[] keyGenerators = new KeyGeneratorCreator[] {
67             new KeyGeneratorCreator() {
68                 public KeyGenerator create() {
69                     return new HighLowKeyGenerator();
70                 }
71
72                 public String JavaDoc toString() {
73                     return "highlow";
74                 }
75             }, new KeyGeneratorCreator() {
76                 public KeyGenerator create() {
77                     return new SequenceKeyGenerator();
78                 }
79
80                 public String JavaDoc toString() {
81                     return "sequence";
82                 }
83             } };
84
85     /**
86      * Adds a <code>SqlInterface</code> to the list of instantiated
87      * interfaces.
88      *
89      * @param the
90      * short name of the <code>SqlInterface</code>
91      * @return the <code>SqlInterface</code> added to the list
92      */

93     private static KeyGenerator addKeyGenerator(String JavaDoc name)
94             throws SQLException JavaDoc {
95         KeyGenerator generator = null;
96         for (int i = 0; i < keyGenerators.length; i++) {
97             if (name.equals(keyGenerators[i].toString())) {
98                 generator = keyGenerators[i].create();
99                 generators.add(new KeyGeneratorEntry(keyGenerators[i]
100                         .toString(), generator));
101                 return generator;
102             }
103         }
104         return null;
105     }
106
107     /**
108      * Return an <code>KeyGenerator</code> for the given class name.
109      *
110      * @param the
111      * short name of the <code>SqlInterface</code>
112      * @return a <code>KeyGenerator</code> for the given class name.
113      */

114     private static KeyGenerator getKeyGeneratorIntern(String JavaDoc name)
115             throws SQLException JavaDoc {
116         KeyGeneratorEntry entry = null;
117         int size = generators.size();
118         for (int i = 0; i < size; i++) {
119             entry = (KeyGeneratorEntry) generators.get(i);
120             if (entry.name.equals(name))
121                 return entry.keyGenerator;
122         }
123         return null;
124     }
125
126     /**
127      * @param name
128      * of the key generator
129      * @return a <code>KeyGenerator</code> indexed by the given name
130      */

131     public synchronized static KeyGenerator getKeyGenerator(String JavaDoc name)
132             throws SQLException JavaDoc {
133         String JavaDoc low = name.toLowerCase();
134         KeyGenerator generator = getKeyGeneratorIntern(low);
135         if (generator != null)
136             return generator;
137
138         generator = addKeyGenerator(low);
139         if (generator != null)
140             return generator;
141
142         throw new SQLException JavaDoc("mapping.keyGenNotFound", name);
143
144     }
145
146 }
147
Popular Tags