1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.torque.util;
17
18 import static org.apache.commons.lang.StringUtils.isBlank;
19 import static org.apache.commons.lang.StringUtils.isEmpty;
20
21 import java.util.Map;
22
23 import org.apache.commons.beanutils.BeanUtils;
24 import org.kuali.core.db.torque.PropertyHandlingException;
25 import org.kuali.db.DatabaseType;
26 import org.kuali.db.JDBCConfiguration;
27 import org.kuali.db.JDBCUtils;
28
29 public class JdbcConfigurer {
30 JDBCUtils jdbcUtils = new JDBCUtils();
31
32 @SuppressWarnings("unchecked")
33 protected boolean hasProperty(String name, Object bean) throws PropertyHandlingException {
34 try {
35 Map<String, Object> description = BeanUtils.describe(bean);
36 return description.containsKey(name);
37 } catch (Exception e) {
38 throw new PropertyHandlingException(e);
39 }
40
41 }
42
43 @SuppressWarnings("unchecked")
44 protected <T> T getProperty(String name, Object bean) {
45 try {
46 if (hasProperty(name, bean)) {
47 return (T) BeanUtils.getProperty(bean, name);
48 } else {
49 return null;
50 }
51 } catch (Exception e) {
52 throw new RuntimeException(e);
53 }
54 }
55
56 protected void setProperty(Object bean, String name, Object value) throws PropertyHandlingException {
57 try {
58 BeanUtils.copyProperty(bean, name, value);
59 } catch (Exception e) {
60 throw new PropertyHandlingException(e);
61 }
62 }
63
64 public void updateConfiguration(Object bean) throws PropertyHandlingException {
65 String url = getProperty("url", bean);
66 if (isEmpty(url)) {
67
68 return;
69 }
70
71 JDBCConfiguration config = jdbcUtils.getDatabaseConfiguration(url);
72 if (config.equals(JDBCConfiguration.UNKNOWN_CONFIG)) {
73 return;
74 }
75
76 String driver = getProperty("driver", bean);
77 if (isBlank(driver)) {
78 setProperty(bean, "driver", config.getDriver());
79 }
80
81 String targetDatabase = getProperty("targetDatabase", bean);
82 if (isBlank(targetDatabase)) {
83 setProperty(bean, "targetDatabase", config.getType().toString().toLowerCase());
84 }
85 }
86
87 public void validateConfiguration(Object bean) {
88 String driver = getProperty("driver", bean);
89 if (isBlank(driver)) {
90 throw new IllegalArgumentException("No database driver. Specify one in the plugin configuration.");
91 }
92
93 String url = getProperty("url", bean);
94 if (isBlank(url)) {
95 throw new IllegalArgumentException(getEmptyURLErrorMessage());
96 }
97
98 String targetDatabase = getProperty("targetDatabase", bean);
99 try {
100 DatabaseType.valueOf(targetDatabase.toUpperCase());
101 } catch (IllegalArgumentException e) {
102 throw new IllegalArgumentException("Database type of '" + targetDatabase + "' is invalid. Valid values: " + org.springframework.util.StringUtils.arrayToCommaDelimitedString(DatabaseType.values()));
103 }
104
105 try {
106 Class.forName(driver);
107 } catch (ClassNotFoundException e) {
108 throw new IllegalArgumentException("Can't load driver class " + driver + ". Be sure to include it as a plugin dependency.");
109 }
110 }
111
112 protected String getEmptyURLErrorMessage() {
113 StringBuffer sb = new StringBuffer();
114 sb.append("\n\n");
115 sb.append("No url was supplied.\n");
116 sb.append("You can specify a url in the plugin configuration or provide it as a system property.\n\n");
117 sb.append("For example:\n\n");
118 sb.append("-Durl=jdbc:oracle:thin:@localhost:1521:XE (oracle)\n");
119 sb.append("-Durl=jdbc:mysql://localhost:3306/<database> (mysql)\n");
120 sb.append("\n.");
121 return sb.toString();
122 }
123 }