|
15 | 15 | import com.mchange.v2.c3p0.ComboPooledDataSource; |
16 | 16 |
|
17 | 17 | public class C3P0 { |
18 | | - public static final FwLogger logger = new FwLogger(C3P0.class); |
19 | | - private static C3P0 c3p0 = null; |
20 | | - private Map<String, DataSource> dataSources = new HashMap<String, DataSource>(); |
21 | | - public static final String DEFUAT_DB_NAME = "write"; |
22 | | - |
23 | | - private C3P0() { |
24 | | - try { |
25 | | - Map<String, Map<String, Object>> specialPros = initPros(); |
26 | | - for (Iterator<String> iter = specialPros.keySet().iterator(); iter.hasNext();) { |
27 | | - String name = iter.next(); |
28 | | - printPros(name, specialPros.get(name)); |
29 | | - DataSource ds = new ComboPooledDataSource(); |
30 | | - BeanUtils.populate(ds, specialPros.get(name)); |
31 | | - this.dataSources.put(name, ds); |
32 | | - } |
33 | | - } catch (Exception ex) { |
34 | | - throw new RuntimeException(ex); |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - private void printPros(String name, Map<String, Object> pros) { |
39 | | - logger.debug("datasource name:" + name); |
40 | | - for (Iterator<String> iter = pros.keySet().iterator(); iter.hasNext();) { |
41 | | - String key = iter.next(); |
42 | | - logger.debug(key + "=" + (String) pros.get(key)); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - private Map<String, Map<String, Object>> initPros() { |
47 | | - try { |
48 | | - Properties pros = new SysConf().read("c3p0.properties"); |
49 | | - |
50 | | - Map<String, Object> sharePros = new HashMap<String, Object>(); |
51 | | - String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ","); |
52 | | - |
53 | | - Map<String, Map<String, Object>> specialPros = new HashMap<String, Map<String, Object>>(); |
54 | | - for (String name : names) { |
55 | | - specialPros.put(name, new HashMap<String, Object>()); |
56 | | - } |
57 | | - |
58 | | - for (Iterator iter = pros.keySet().iterator(); iter.hasNext();) { |
59 | | - String key = (String) iter.next(); |
60 | | - if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) { |
61 | | - for (String specialName : specialPros.keySet()) { |
62 | | - if (!key.startsWith("c3p0." + specialName)) { |
63 | | - sharePros.put(key.substring(5), pros.get(key)); |
64 | | - } else { |
65 | | - // c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度 |
66 | | - int len = 5 + specialName.length() + 1; |
67 | | - specialPros.get(specialName).put(key.substring(len), pros.get(key)); |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * 把常规属性放到特殊属性里面去 |
75 | | - */ |
76 | | - for (Map<String, Object> ps : specialPros.values()) { |
77 | | - for (Iterator<String> iter = sharePros.keySet().iterator(); iter.hasNext();) { |
78 | | - String key = iter.next(); |
79 | | - ps.put(key, sharePros.get(key)); |
80 | | - } |
81 | | - } |
82 | | - return specialPros; |
83 | | - } catch (Exception ex) { |
84 | | - throw new RuntimeException(ex); |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - public static C3P0 instance() { |
89 | | - if (c3p0 == null) |
90 | | - c3p0 = new C3P0(); |
91 | | - return c3p0; |
92 | | - } |
93 | | - |
94 | | - public DataSource getDataSource() { |
95 | | - return this.getDataSource(this.DEFUAT_DB_NAME); |
96 | | - } |
97 | | - |
98 | | - public DataSource getDataSource(String name) { |
99 | | - return this.dataSources.get(name); |
100 | | - } |
101 | | - |
102 | | - public static void main(String[] args) throws Exception { |
103 | | - ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource(); |
104 | | - System.out.println(ds.getMaxPoolSize()); |
105 | | - } |
| 18 | + public static final FwLogger logger = new FwLogger(C3P0.class); |
| 19 | + private static C3P0 c3p0 = null; |
| 20 | + private Map<String, DataSource> dataSources = new HashMap<>(); |
| 21 | + public static final String DEFUAT_DB_NAME = "write"; |
| 22 | + |
| 23 | + private C3P0() { |
| 24 | + try { |
| 25 | + Map<String, Map<String, Object>> specialPros = initPros(); |
| 26 | + for (Iterator<String> iter = specialPros.keySet().iterator(); iter.hasNext(); ) { |
| 27 | + String name = iter.next(); |
| 28 | + printPros(name, specialPros.get(name)); |
| 29 | + DataSource ds = new ComboPooledDataSource(); |
| 30 | + BeanUtils.populate(ds, specialPros.get(name)); |
| 31 | + this.dataSources.put(name, ds); |
| 32 | + } |
| 33 | + } catch (Exception ex) { |
| 34 | + throw new RuntimeException(ex); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private void printPros(String name, Map<String, Object> pros) { |
| 39 | + logger.debug("datasource name:" + name); |
| 40 | + pros.forEach((k, v) -> logger.debug(k + "=" + v)); |
| 41 | + } |
| 42 | + |
| 43 | + private Map<String, Map<String, Object>> initPros() { |
| 44 | + try { |
| 45 | + Properties pros = new SysConf().read("c3p0.properties"); |
| 46 | + |
| 47 | + Map<String, Object> sharePros = new HashMap<>(); |
| 48 | + String[] names = StringUtil.split(((String) pros.get("c3p0.names")).trim(), ","); |
| 49 | + |
| 50 | + Map<String, Map<String, Object>> specialPros = new HashMap<>(); |
| 51 | + for (String name : names) { |
| 52 | + specialPros.put(name, new HashMap<>()); |
| 53 | + } |
| 54 | + |
| 55 | + for (Iterator iter = pros.keySet().iterator(); iter.hasNext(); ) { |
| 56 | + String key = (String) iter.next(); |
| 57 | + if (key.startsWith("c3p0.") && !key.startsWith("c3p0.names")) { |
| 58 | + for (String specialName : specialPros.keySet()) { |
| 59 | + if (!key.startsWith("c3p0." + specialName)) { |
| 60 | + sharePros.put(key.substring(5), pros.get(key)); |
| 61 | + } else { |
| 62 | + // c3p0. 这个长度是5,最后一个 1是最后还有一个 . 的长度 |
| 63 | + int len = 5 + specialName.length() + 1; |
| 64 | + specialPros.get(specialName).put(key.substring(len), pros.get(key)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 把常规属性放到特殊属性里面去 |
| 72 | + */ |
| 73 | + for (Map<String, Object> ps : specialPros.values()) { |
| 74 | + for (Iterator<String> iter = sharePros.keySet().iterator(); iter.hasNext(); ) { |
| 75 | + String key = iter.next(); |
| 76 | + ps.put(key, sharePros.get(key)); |
| 77 | + } |
| 78 | + } |
| 79 | + return specialPros; |
| 80 | + } catch (Exception ex) { |
| 81 | + throw new RuntimeException(ex); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static C3P0 instance() { |
| 86 | + if (c3p0 == null) |
| 87 | + c3p0 = new C3P0(); |
| 88 | + return c3p0; |
| 89 | + } |
| 90 | + |
| 91 | + public DataSource getDataSource() { |
| 92 | + return this.getDataSource(this.DEFUAT_DB_NAME); |
| 93 | + } |
| 94 | + |
| 95 | + public DataSource getDataSource(String name) { |
| 96 | + return this.dataSources.get(name); |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String[] args) throws Exception { |
| 100 | + ComboPooledDataSource ds = (ComboPooledDataSource) new C3P0().getDataSource(); |
| 101 | + System.out.println(ds.getMaxPoolSize()); |
| 102 | + } |
106 | 103 | } |
0 commit comments