| 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 | 0 | public class JdbcConfigurer { |
| 15 | 0 | JDBCUtils jdbcUtils = new JDBCUtils(); |
| 16 | |
|
| 17 | |
@SuppressWarnings("unchecked") |
| 18 | |
protected boolean hasProperty(String name, Object bean) throws PropertyHandlingException { |
| 19 | |
try { |
| 20 | 0 | Map<String, Object> description = BeanUtils.describe(bean); |
| 21 | 0 | return description.containsKey(name); |
| 22 | 0 | } catch (Exception e) { |
| 23 | 0 | throw new PropertyHandlingException(e); |
| 24 | |
} |
| 25 | |
|
| 26 | |
} |
| 27 | |
|
| 28 | |
@SuppressWarnings("unchecked") |
| 29 | |
protected <T> T getProperty(String name, Object bean) { |
| 30 | |
try { |
| 31 | 0 | if (hasProperty(name, bean)) { |
| 32 | 0 | return (T) BeanUtils.getProperty(bean, name); |
| 33 | |
} else { |
| 34 | 0 | return null; |
| 35 | |
} |
| 36 | 0 | } catch (Exception e) { |
| 37 | 0 | throw new RuntimeException(e); |
| 38 | |
} |
| 39 | |
} |
| 40 | |
|
| 41 | |
protected void setProperty(Object bean, String name, Object value) throws PropertyHandlingException { |
| 42 | |
try { |
| 43 | 0 | BeanUtils.copyProperty(bean, name, value); |
| 44 | 0 | } catch (Exception e) { |
| 45 | 0 | throw new PropertyHandlingException(e); |
| 46 | 0 | } |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
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 | 0 | } |
| 71 | |
|
| 72 | |
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 | |
try { |
| 85 | 0 | DatabaseType.valueOf(targetDatabase.toUpperCase()); |
| 86 | 0 | } catch (IllegalArgumentException e) { |
| 87 | 0 | throw new IllegalArgumentException("Database type of '" + targetDatabase + "' is invalid. Valid values: " + org.springframework.util.StringUtils.arrayToCommaDelimitedString(DatabaseType.values())); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
try { |
| 91 | 0 | Class.forName(driver); |
| 92 | 0 | } catch (ClassNotFoundException e) { |
| 93 | 0 | throw new IllegalArgumentException("Can't load driver class " + driver + ". Be sure to include it as a plugin dependency."); |
| 94 | 0 | } |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
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 | |
} |