001 /*
002 * 11/14/2002 - 09:40:11
003 *
004 * $RCSfile: KeyGeneratorFactory.java,v $ - JDBF Object Relational mapping system
005 * Copyright (C) 2002 JDBF Development Team
006 *
007 * http://jdbf.sourceforge.net
008 *
009 * This program is free software; you can redistribute it and/or
010 * modify it under the terms of the GNU Lesser General Public License
011 * as published by the Free Software Foundation; either version 2
012 * of the License, or (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017 * GNU Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public License
020 * along with this program; if not, write to the Free Software
021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022 */
023
024 /*
025 $Id: KeyGeneratorFactory.java,v 1.3 2004/05/20 22:40:02 gmartone Exp $
026 */
027 package org.jdbf.engine.keygen;
028 import java.util.ArrayList;
029 /**
030 * <code>KeyGeneratorFactory</code> holds a list of key
031 * generators.<br>
032 *
033 * @author Giovanni Martone<br>
034 * @version $Revision: 1.3 $<br>
035 * last changed by $Author: gmartone $
036 *
037 */
038 public class KeyGeneratorFactory {
039
040 /**
041 * Creates an empty object
042 *
043 */
044 private KeyGeneratorFactory() {}
045 /**
046 * List of <code>SqlInterfaceEntry</code>
047 * holding the instantiated interfaces
048 */
049 static ArrayList generators = new ArrayList();
050
051 /**
052 * Single entry of KeyGenerator
053 */
054 static class KeyGeneratorEntry {
055
056 /**
057 * Logic name of KeyGenerator
058 */
059 String name;
060
061 /**
062 * KeyGenerator object
063 */
064 KeyGenerator keyGenerator;
065
066 /**
067 * Creates a KeyGeneratorEntry given name and keyGenerator
068 *
069 * @param name
070 * @param keyGenerator
071 */
072 KeyGeneratorEntry(String name, KeyGenerator keyGenerator) {
073 this.name = name;
074 this.keyGenerator = keyGenerator;
075 }
076 }
077
078 /**
079 * Interface that creates the KeyGenerator object
080 */
081 static interface KeyGeneratorCreator {
082 KeyGenerator create();
083 }
084 /** A list of the available key generators */
085 private static KeyGeneratorCreator[] keyGenerators =
086
087 new KeyGeneratorCreator [] {
088 new KeyGeneratorCreator() {
089
090 public KeyGenerator create() {
091 return new MaxKeyGenerator();
092 }
093
094 public String toString() {
095 return "max";
096 }
097 },
098 new KeyGeneratorCreator() {
099
100 public KeyGenerator create() {
101 return new HighLowKeyGenerator();
102 }
103
104 public String toString() {
105 return "highlow";
106 }
107 },
108 new KeyGeneratorCreator() {
109
110 public KeyGenerator create() {
111 return new SequenceKeyGenerator();
112 }
113
114 public String toString() {
115 return "sequence";
116 }
117 },
118 new KeyGeneratorCreator() {
119
120 public KeyGenerator create() {
121 return new IdentityKeyGenerator();
122 }
123
124 public String toString() {
125 return "identity";
126 }
127 }
128 };
129 /**
130 * Adds a <code>SqlInterface</code> to the list of
131 * instantiated interfaces.
132 *
133 * @param name the short name of the <code>SqlInterface</code>
134 * @return <code>KeyGenerator</code> added to the list
135 * @throws KeyGenerationException if error occurs
136 *
137 */
138 private static KeyGenerator addKeyGenerator(String name)
139 throws KeyGenerationException{
140 KeyGenerator generator = null;
141 for (int i=0; i<keyGenerators.length; i++) {
142 if (name.equals(keyGenerators[i].toString())) {
143 generator = keyGenerators[i].create();
144 generators.add(new KeyGeneratorEntry
145 (keyGenerators[i].toString(), generator));
146 return generator;
147 }
148 }
149 return null;
150 }
151
152 /**
153 * Return an <code>KeyGenerator</code> for the given
154 * class name.
155 *
156 * @param name the short name of the <code>SqlInterface</code>
157 * @return <code>KeyGenerator</code> for the given
158 * class name.
159 * @throws KeyGenerationException if error occurs
160 *
161 */
162 private static KeyGenerator getKeyGeneratorIntern(String name)
163 throws KeyGenerationException{
164 KeyGeneratorEntry entry = null;
165 int size = generators.size();
166 for (int i=0; i<size; i++) {
167 entry = (KeyGeneratorEntry)generators.get(i);
168 if (entry.name.equals(name))
169 return entry.keyGenerator;
170 }
171 return null;
172 }
173
174 /**
175 * Return the KeyGenerator specified in name
176 *
177 * @param name of the key generator
178 * @return <code>KeyGenerator</code> indexed by the given name
179 * @throws KeyGenerationException if error occurs
180 *
181 */
182 public synchronized static KeyGenerator getKeyGenerator(String name)
183 throws KeyGenerationException{
184
185 String low = name.toLowerCase();
186 KeyGenerator generator = getKeyGeneratorIntern(low);
187 if (generator != null)
188 return generator;
189 generator = addKeyGenerator(low);
190 if (generator != null)
191 return generator;
192 throw new KeyGenerationException("mapping.keyGenNotFound", name);
193 }
194 }
195 /*
196 $Log: KeyGeneratorFactory.java,v $
197 Revision 1.3 2004/05/20 22:40:02 gmartone
198 Changed for task 99073 (Coverage Javadocs)
199 Revision 1.2 2004/04/29 22:38:19 gmartone
200 Task 66484 (Logging System)
201
202 */
|