001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.torque.util;
017
018 import static org.apache.commons.lang.StringUtils.isBlank;
019 import static org.apache.commons.lang.StringUtils.isEmpty;
020
021 import java.util.Map;
022
023 import org.apache.commons.beanutils.BeanUtils;
024 import org.kuali.core.db.torque.PropertyHandlingException;
025 import org.kuali.db.DatabaseType;
026 import org.kuali.db.JDBCConfiguration;
027 import org.kuali.db.JDBCUtils;
028
029 public class JdbcConfigurer {
030 JDBCUtils jdbcUtils = new JDBCUtils();
031
032 @SuppressWarnings("unchecked")
033 protected boolean hasProperty(String name, Object bean) throws PropertyHandlingException {
034 try {
035 Map<String, Object> description = BeanUtils.describe(bean);
036 return description.containsKey(name);
037 } catch (Exception e) {
038 throw new PropertyHandlingException(e);
039 }
040
041 }
042
043 @SuppressWarnings("unchecked")
044 protected <T> T getProperty(String name, Object bean) {
045 try {
046 if (hasProperty(name, bean)) {
047 return (T) BeanUtils.getProperty(bean, name);
048 } else {
049 return null;
050 }
051 } catch (Exception e) {
052 throw new RuntimeException(e);
053 }
054 }
055
056 protected void setProperty(Object bean, String name, Object value) throws PropertyHandlingException {
057 try {
058 BeanUtils.copyProperty(bean, name, value);
059 } catch (Exception e) {
060 throw new PropertyHandlingException(e);
061 }
062 }
063
064 public void updateConfiguration(Object bean) throws PropertyHandlingException {
065 String url = getProperty("url", bean);
066 if (isEmpty(url)) {
067 // If the url is empty, there is nothing to do
068 return;
069 }
070
071 JDBCConfiguration config = jdbcUtils.getDatabaseConfiguration(url);
072 if (config.equals(JDBCConfiguration.UNKNOWN_CONFIG)) {
073 return;
074 }
075
076 String driver = getProperty("driver", bean);
077 if (isBlank(driver)) {
078 setProperty(bean, "driver", config.getDriver());
079 }
080
081 String targetDatabase = getProperty("targetDatabase", bean);
082 if (isBlank(targetDatabase)) {
083 setProperty(bean, "targetDatabase", config.getType().toString().toLowerCase());
084 }
085 }
086
087 public void validateConfiguration(Object bean) {
088 String driver = getProperty("driver", bean);
089 if (isBlank(driver)) {
090 throw new IllegalArgumentException("No database driver. Specify one in the plugin configuration.");
091 }
092
093 String url = getProperty("url", bean);
094 if (isBlank(url)) {
095 throw new IllegalArgumentException(getEmptyURLErrorMessage());
096 }
097
098 String targetDatabase = getProperty("targetDatabase", bean);
099 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 }