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.jdbc.metadata.JdbcColumn;
14: import com.versant.core.jdbc.sql.conv.TypeAsStringConverter;
15: import com.versant.core.jdbc.sql.conv.TypeAsBytesConverter;
16: import com.versant.core.util.classhelper.ClassHelper;
17:
18: import com.versant.core.common.BindingSupportImpl;
19:
20: import java.util.HashMap;
21: import java.util.Map;
22:
23: /**
24: * This manages and creates JdbcConverterFactory instances from the names
25: * of the factory classes. It makes sure that a given factory is only
26: * created once.
27: */
28: public class JdbcConverterFactoryRegistry {
29:
30: private ClassLoader loader;
31: private Map map = new HashMap();
32:
33: /**
34: * Placeholder converter used to indicate that no converter is needed.
35: * It returns null when asked to create a converter.
36: */
37: public static final JdbcConverterFactory NULL_CONVERTER = new JdbcConverterFactory() {
38: public Object createArgsBean() {
39: return null;
40: }
41:
42: public JdbcConverter createJdbcConverter(JdbcColumn col,
43: Object args, JdbcTypeRegistry jdbcTypeRegistry)
44: throws IllegalArgumentException {
45: return null;
46: }
47: };
48:
49: public static final String NULL_CONVERTER_NAME = "{none}";
50: public static final String STRING_CONVERTER_NAME = "STRING";
51: public static final String BYTES_CONVERTER_NAME = "BYTES";
52:
53: public JdbcConverterFactoryRegistry(ClassLoader loader) {
54: this .loader = loader;
55: map.put(NULL_CONVERTER_NAME, NULL_CONVERTER);
56: map.put(STRING_CONVERTER_NAME,
57: new TypeAsStringConverter.Factory());
58: map.put(BYTES_CONVERTER_NAME,
59: new TypeAsBytesConverter.Factory());
60: }
61:
62: public ClassLoader getLoader() {
63: return loader;
64: }
65:
66: /**
67: * Get the factory with class name.
68: */
69: public JdbcConverterFactory getFactory(String name) {
70: JdbcConverterFactory ans = (JdbcConverterFactory) map.get(name);
71: if (ans == null) {
72: Class t = null;
73: try {
74: t = ClassHelper.get().classForName(name, true, loader);
75: } catch (ClassNotFoundException e) {
76: throw BindingSupportImpl.getInstance().runtime(
77: "Unable to load JdbcConverterFactory class "
78: + name, e);
79: }
80: try {
81: ans = (JdbcConverterFactory) t.newInstance();
82: } catch (Exception x) {
83: throw BindingSupportImpl.getInstance().runtime(
84: "Unable to create JdbcConverterFactory instance "
85: + t.getName(), x);
86: }
87: map.put(name, ans);
88: }
89: return ans;
90: }
91: }
|