1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.data.platform;
17
18 import org.springframework.dao.DataAccessException;
19 import org.springframework.jdbc.core.ConnectionCallback;
20 import org.springframework.jdbc.core.JdbcTemplate;
21
22 import javax.sql.DataSource;
23 import java.sql.Connection;
24 import java.sql.DatabaseMetaData;
25 import java.sql.SQLException;
26 import java.util.Collections;
27 import java.util.IdentityHashMap;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.ConcurrentMap;
31
32
33
34
35
36
37
38
39
40
41
42 public final class DatabasePlatforms {
43
44
45
46
47 public static final String ORACLE = "Oracle";
48
49
50
51
52 public static final String MYSQL = "MySQL";
53
54 private static final Map<DataSource, DatabasePlatformInfo> platformCache
55 = Collections.synchronizedMap(new IdentityHashMap<DataSource, DatabasePlatformInfo>(8));
56
57
58
59
60
61
62
63 public static DatabasePlatformInfo detectPlatform(DataSource dataSource) {
64 if (dataSource == null) {
65 throw new IllegalArgumentException("DataSource must not be null.");
66 }
67 DatabasePlatformInfo platformInfo = platformCache.get(dataSource);
68 if (platformInfo == null) {
69 JdbcTemplate template = new JdbcTemplate(dataSource);
70 platformCache.put(dataSource, template.execute(new ConnectionCallback<DatabasePlatformInfo>() {
71 @Override
72 public DatabasePlatformInfo doInConnection(
73 Connection connection) throws SQLException, DataAccessException {
74 DatabaseMetaData metadata = connection.getMetaData();
75 String vendorName = metadata.getDatabaseProductName();
76 int version = metadata.getDatabaseMajorVersion();
77 return new DatabasePlatformInfo(vendorName, version);
78 }
79 }));
80 if (platformInfo == null) {
81 platformInfo = platformCache.get(dataSource);
82 }
83 }
84 return platformInfo;
85 }
86
87
88
89
90 private DatabasePlatforms() {}
91
92 }