01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc;
12:
13: import com.versant.core.util.classhelper.ClassHelper;
14:
15: import com.versant.core.common.BindingSupportImpl;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: /**
21: * This manages and creates JdbcKeyGeneratorFactory instances from the names
22: * of the factory classes. It makes sure that a given factory is only
23: * created once.
24: */
25: public class JdbcKeyGeneratorFactoryRegistry {
26:
27: private ClassLoader loader;
28: private Map map = new HashMap();
29:
30: public JdbcKeyGeneratorFactoryRegistry(ClassLoader loader) {
31: this .loader = loader;
32: }
33:
34: public ClassLoader getLoader() {
35: return loader;
36: }
37:
38: /**
39: * Get the factory with class name.
40: */
41: public JdbcKeyGeneratorFactory getFactory(String name) {
42: JdbcKeyGeneratorFactory ans = (JdbcKeyGeneratorFactory) map
43: .get(name);
44: if (ans == null) {
45: Class t = null;
46: try {
47: t = ClassHelper.get().classForName(name, true, loader);
48: } catch (ClassNotFoundException e) {
49: throw BindingSupportImpl.getInstance().runtime(
50: "Unable to load JdbcKeyGeneratorFactory class "
51: + name, e);
52: }
53: try {
54: ans = (JdbcKeyGeneratorFactory) t.newInstance();
55: } catch (Exception x) {
56: throw BindingSupportImpl.getInstance().runtime(
57: "Unable to create JdbcKeyGeneratorFactory instance "
58: + t.getName(), x);
59: }
60: map.put(name, ans);
61: }
62: return ans;
63: }
64:
65: /**
66: * Add an alias for a factory.
67: */
68: public void add(String alias, JdbcKeyGeneratorFactory f) {
69: map.put(alias, f);
70: }
71:
72: }
|