KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdbc.metadata.JdbcColumn;
15 import com.versant.core.jdbc.sql.conv.TypeAsStringConverter;
16 import com.versant.core.jdbc.sql.conv.TypeAsBytesConverter;
17 import com.versant.core.util.classhelper.ClassHelper;
18
19 import com.versant.core.common.BindingSupportImpl;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * This manages and creates JdbcConverterFactory instances from the names
26  * of the factory classes. It makes sure that a given factory is only
27  * created once.
28  */

29 public class JdbcConverterFactoryRegistry {
30
31     private ClassLoader JavaDoc loader;
32     private Map JavaDoc map = new HashMap JavaDoc();
33
34     /**
35      * Placeholder converter used to indicate that no converter is needed.
36      * It returns null when asked to create a converter.
37      */

38     public static final JdbcConverterFactory NULL_CONVERTER = new JdbcConverterFactory() {
39         public Object JavaDoc createArgsBean() {
40             return null;
41         }
42         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
43                 JdbcTypeRegistry jdbcTypeRegistry) throws IllegalArgumentException JavaDoc {
44             return null;
45         }
46     };
47
48     public static final String JavaDoc NULL_CONVERTER_NAME = "{none}";
49     public static final String JavaDoc STRING_CONVERTER_NAME = "STRING";
50     public static final String JavaDoc BYTES_CONVERTER_NAME = "BYTES";
51
52     public JdbcConverterFactoryRegistry(ClassLoader JavaDoc loader) {
53         this.loader = loader;
54         map.put(NULL_CONVERTER_NAME, NULL_CONVERTER);
55         map.put(STRING_CONVERTER_NAME, new TypeAsStringConverter.Factory());
56         map.put(BYTES_CONVERTER_NAME, new TypeAsBytesConverter.Factory());
57     }
58
59     public ClassLoader JavaDoc getLoader() {
60         return loader;
61     }
62
63     /**
64      * Get the factory with class name.
65      */

66     public JdbcConverterFactory getFactory(String JavaDoc name) {
67         JdbcConverterFactory ans = (JdbcConverterFactory)map.get(name);
68         if (ans == null) {
69             Class JavaDoc t = null;
70             try {
71                 t = ClassHelper.get().classForName(name, true, loader);
72             } catch (ClassNotFoundException JavaDoc e) {
73                 throw BindingSupportImpl.getInstance().runtime(
74                     "Unable to load JdbcConverterFactory class " + name, e);
75             }
76             try {
77                 ans = (JdbcConverterFactory)t.newInstance();
78             } catch (Exception JavaDoc x) {
79                 throw BindingSupportImpl.getInstance().runtime(
80                         "Unable to create JdbcConverterFactory instance " +
81                         t.getName(), x);
82             }
83             map.put(name, ans);
84         }
85         return ans;
86     }
87 }
88
Popular Tags