View Javadoc

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