1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21
22
23
24 public class LanguageDriverRegistry {
25
26 private final Map<Class<?>, LanguageDriver> LANGUAGE_DRIVER_MAP = new HashMap<Class<?>, LanguageDriver>();
27
28 private Class<?> defaultDriverClass = null;
29
30 public void register(Class<?> cls) {
31 if (cls == null) {
32 throw new IllegalArgumentException("null is not a valid Language Driver");
33 }
34 LanguageDriver driver = LANGUAGE_DRIVER_MAP.get(cls);
35 if (driver == null) {
36 try {
37 driver = (LanguageDriver) cls.newInstance();
38 LANGUAGE_DRIVER_MAP.put(cls, driver);
39 } catch (Exception ex) {
40 throw new ScriptingException("Failed to load language driver for " + cls.getName(), ex);
41 }
42 }
43 }
44
45 public void register(LanguageDriver instance) {
46 if (instance == null) {
47 throw new IllegalArgumentException("null is not a valid Language Driver");
48 }
49 LanguageDriver driver = LANGUAGE_DRIVER_MAP.get(instance.getClass());
50 if (driver == null) {
51 LANGUAGE_DRIVER_MAP.put(instance.getClass(), driver);
52 }
53 }
54
55 public LanguageDriver getDriver(Class<?> cls) {
56 return LANGUAGE_DRIVER_MAP.get(cls);
57 }
58
59 public LanguageDriver getDefaultDriver() {
60 return getDriver(getDefaultDriverClass());
61 }
62
63 public Class<?> getDefaultDriverClass() {
64 return defaultDriverClass;
65 }
66
67 public void setDefaultDriverClass(Class<?> defaultDriverClass) {
68 register(defaultDriverClass);
69 this.defaultDriverClass = defaultDriverClass;
70 }
71
72 }