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