1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.property.processor;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Properties;
21
22 import org.kuali.common.util.Mode;
23 import org.kuali.common.util.PropertyUtils;
24 import org.kuali.common.util.property.Constants;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.util.Assert;
28
29 public class JdbcUrlProcessor implements PropertyProcessor {
30 private static final Logger logger = LoggerFactory.getLogger(JdbcUrlProcessor.class);
31
32 String jdbcUrlProperty = "jdbc.url";
33 String dbVendorProperty = "db.vendor";
34 Map<String, String> jdbcUrlFragments = getJdbcUrlFragments();
35 Mode propertyOverwriteMode = Constants.DEFAULT_PROPERTY_OVERWRITE_MODE;
36
37 @Override
38 public void process(Properties properties) {
39 Assert.notNull(jdbcUrlProperty);
40 Assert.notNull(dbVendorProperty);
41 String jdbcUrl = properties.getProperty(jdbcUrlProperty);
42 if (jdbcUrl == null) {
43 logger.info(jdbcUrlProperty + " is not set");
44 return;
45 }
46 String databaseVendor = getMatch(jdbcUrl, jdbcUrlFragments);
47 if (databaseVendor != null) {
48 PropertyUtils.addOrOverrideProperty(properties, dbVendorProperty, databaseVendor, propertyOverwriteMode);
49 } else {
50 logger.info("Could not identify a database vendor from url - [{}]", jdbcUrl);
51 }
52 }
53
54
55
56
57
58 protected String getMatch(String string, Map<String, String> fragments) {
59 for (String fragment : fragments.keySet()) {
60 if (string.contains(fragment)) {
61 return fragments.get(fragment);
62 }
63 }
64 return null;
65 }
66
67 protected Map<String, String> getJdbcUrlFragments() {
68 Map<String, String> m = new HashMap<String, String>();
69 m.put(":hsqldb:", "hsql");
70 m.put(":derby:", "derby");
71 m.put(":postgresql:", "postgresql");
72 m.put(":db2:", "db2");
73 m.put(":mysql:", "mysql");
74 m.put(":oracle:", "oracle");
75 m.put(":h2:", "h2");
76 return m;
77 }
78
79 }