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<? extends LanguageDriver>, LanguageDriver> LANGUAGE_DRIVER_MAP = new HashMap<>();
27
28 private Class<? extends LanguageDriver> defaultDriverClass;
29
30 public void register(Class<? extends LanguageDriver> cls) {
31 if (cls == null) {
32 throw new IllegalArgumentException("null is not a valid Language Driver");
33 }
34 LANGUAGE_DRIVER_MAP.computeIfAbsent(cls, k -> {
35 try {
36 return k.getDeclaredConstructor().newInstance();
37 } catch (Exception ex) {
38 throw new ScriptingException("Failed to load language driver for " + cls.getName(), ex);
39 }
40 });
41 }
42
43 public void register(LanguageDriver instance) {
44 if (instance == null) {
45 throw new IllegalArgumentException("null is not a valid Language Driver");
46 }
47 Class<? extends LanguageDriver> cls = instance.getClass();
48 if (!LANGUAGE_DRIVER_MAP.containsKey(cls)) {
49 LANGUAGE_DRIVER_MAP.put(cls, instance);
50 }
51 }
52
53 public LanguageDriver getDriver(Class<? extends LanguageDriver> cls) {
54 return LANGUAGE_DRIVER_MAP.get(cls);
55 }
56
57 public LanguageDriver getDefaultDriver() {
58 return getDriver(getDefaultDriverClass());
59 }
60
61 public Class<? extends LanguageDriver> getDefaultDriverClass() {
62 return defaultDriverClass;
63 }
64
65 public void setDefaultDriverClass(Class<? extends LanguageDriver> defaultDriverClass) {
66 register(defaultDriverClass);
67 this.defaultDriverClass = defaultDriverClass;
68 }
69
70 }