KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcKeyGeneratorFactoryRegistry


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc;
13
14 import com.versant.core.util.classhelper.ClassHelper;
15
16 import com.versant.core.common.BindingSupportImpl;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * This manages and creates JdbcKeyGeneratorFactory instances from the names
23  * of the factory classes. It makes sure that a given factory is only
24  * created once.
25  */

26 public class JdbcKeyGeneratorFactoryRegistry {
27
28     private ClassLoader JavaDoc loader;
29     private Map JavaDoc map = new HashMap JavaDoc();
30
31     public JdbcKeyGeneratorFactoryRegistry(ClassLoader JavaDoc loader) {
32         this.loader = loader;
33     }
34
35     public ClassLoader JavaDoc getLoader() {
36         return loader;
37     }
38
39     /**
40      * Get the factory with class name.
41      */

42     public JdbcKeyGeneratorFactory getFactory(String JavaDoc name) {
43         JdbcKeyGeneratorFactory ans = (JdbcKeyGeneratorFactory)map.get(name);
44         if (ans == null) {
45             Class JavaDoc t = null;
46             try {
47                 t = ClassHelper.get().classForName(name, true, loader);
48             } catch (ClassNotFoundException JavaDoc e) {
49                 throw BindingSupportImpl.getInstance().runtime(
50                     "Unable to load JdbcKeyGeneratorFactory class " + name, e);
51             }
52             try {
53                 ans = (JdbcKeyGeneratorFactory)t.newInstance();
54             } catch (Exception JavaDoc x) {
55                 throw BindingSupportImpl.getInstance().runtime(
56                         "Unable to create JdbcKeyGeneratorFactory instance " +
57                         t.getName(), x);
58             }
59             map.put(name, ans);
60         }
61         return ans;
62     }
63
64     /**
65      * Add an alias for a factory.
66      */

67     public void add(String JavaDoc alias, JdbcKeyGeneratorFactory f) {
68         map.put(alias, f);
69     }
70
71 }
72
Popular Tags