View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  			// If the url is empty, there is nothing to do
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 }