| 1 | |
package org.kuali.db; |
| 2 | |
|
| 3 | |
import static org.apache.commons.lang.StringUtils.isNotEmpty; |
| 4 | |
import static org.kuali.db.DatabaseType.POSTGRESQL; |
| 5 | |
|
| 6 | |
import java.sql.Connection; |
| 7 | |
import java.sql.ResultSet; |
| 8 | |
import java.sql.SQLException; |
| 9 | |
import java.sql.Statement; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
import org.apache.commons.lang.Validate; |
| 13 | |
import org.springframework.context.ApplicationContext; |
| 14 | |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | 0 | public class JDBCUtils { |
| 20 | |
public static final String JDBC_CONTEXT = "org/kuali/db/jdbc-context.xml"; |
| 21 | |
public static final String JDBC_CONFIGURATIONS = "jdbcConfigurations"; |
| 22 | 0 | ApplicationContext context = new ClassPathXmlApplicationContext(JDBC_CONTEXT); |
| 23 | |
|
| 24 | 0 | @SuppressWarnings("unchecked") |
| 25 | |
List<JDBCConfiguration> jdbcConfigs = (List<JDBCConfiguration>) context.getBean(JDBC_CONFIGURATIONS); |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public JDBCConfiguration getDatabaseConfiguration(DatabaseType type) { |
| 34 | 0 | for (JDBCConfiguration jdbcConfig : jdbcConfigs) { |
| 35 | 0 | if (jdbcConfig.getType().equals(type)) { |
| 36 | 0 | return jdbcConfig; |
| 37 | |
} |
| 38 | |
} |
| 39 | 0 | return JDBCConfiguration.UNKNOWN_CONFIG; |
| 40 | |
} |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public JDBCConfiguration getDatabaseConfiguration(String url) { |
| 49 | 0 | Validate.isTrue(isNotEmpty(url)); |
| 50 | |
|
| 51 | 0 | for (JDBCConfiguration jdbcConfig : jdbcConfigs) { |
| 52 | 0 | String urlFragment = jdbcConfig.getUrlFragment(); |
| 53 | 0 | if (url.contains(urlFragment)) { |
| 54 | 0 | return jdbcConfig; |
| 55 | |
} |
| 56 | 0 | } |
| 57 | 0 | return JDBCConfiguration.UNKNOWN_CONFIG; |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public String getDatabaseName(String url) { |
| 68 | 0 | int leftIndex = url.lastIndexOf("/"); |
| 69 | 0 | if (leftIndex == -1) { |
| 70 | 0 | leftIndex = url.lastIndexOf(":"); |
| 71 | |
} |
| 72 | 0 | leftIndex++; |
| 73 | |
|
| 74 | 0 | int rightIndex = url.length(); |
| 75 | 0 | if (url.indexOf("?") != -1) { |
| 76 | 0 | rightIndex = url.indexOf("?"); |
| 77 | 0 | } else if (url.indexOf(";") != -1) { |
| 78 | 0 | rightIndex = url.indexOf(";"); |
| 79 | |
} |
| 80 | |
|
| 81 | 0 | return url.substring(leftIndex, rightIndex); |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public String getServerUrl(String url) { |
| 93 | 0 | int rightIndex = url.length(); |
| 94 | 0 | if (url.lastIndexOf("/") != -1) { |
| 95 | 0 | rightIndex = url.lastIndexOf("/"); |
| 96 | 0 | } else if (url.lastIndexOf(":") != -1) { |
| 97 | 0 | rightIndex = url.lastIndexOf(":"); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | String baseUrl = url.substring(0, rightIndex); |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | 0 | if (POSTGRESQL.equals(getDatabaseConfiguration(url).getType())) { |
| 106 | 0 | baseUrl += "/postgres"; |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | String options = ""; |
| 110 | 0 | int optionsIndex = url.indexOf("?"); |
| 111 | 0 | if (optionsIndex == -1) { |
| 112 | 0 | optionsIndex = url.indexOf(";"); |
| 113 | |
} |
| 114 | 0 | if (optionsIndex != -1) { |
| 115 | 0 | options = url.substring(optionsIndex); |
| 116 | |
} |
| 117 | |
|
| 118 | 0 | return baseUrl + options; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public static void closeQuietly(ResultSet rs, Statement stmt, Connection conn) { |
| 122 | 0 | closeQuietly(rs); |
| 123 | 0 | closeQuietly(stmt); |
| 124 | 0 | closeQuietly(conn); |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
public static void closeQuietly(Statement stmt, Connection conn) { |
| 128 | 0 | closeQuietly(null, stmt, conn); |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
public static void closeQuietly(ResultSet rs) { |
| 132 | 0 | if (rs == null) { |
| 133 | 0 | return; |
| 134 | |
} |
| 135 | |
try { |
| 136 | 0 | rs.close(); |
| 137 | 0 | } catch (SQLException e) { |
| 138 | |
|
| 139 | 0 | } |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
public static void closeQuietly(Statement stmt) { |
| 143 | 0 | if (stmt == null) { |
| 144 | 0 | return; |
| 145 | |
} |
| 146 | |
try { |
| 147 | 0 | stmt.close(); |
| 148 | 0 | } catch (SQLException e) { |
| 149 | |
|
| 150 | 0 | } |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
public static void closeQuietly(Connection conn) { |
| 154 | 0 | if (conn == null) { |
| 155 | 0 | return; |
| 156 | |
} |
| 157 | |
try { |
| 158 | 0 | conn.close(); |
| 159 | 0 | } catch (SQLException e) { |
| 160 | |
|
| 161 | 0 | } |
| 162 | 0 | } |
| 163 | |
|
| 164 | |
public static void rollbackQuietly(Connection conn) { |
| 165 | 0 | if (conn == null) { |
| 166 | 0 | return; |
| 167 | |
} |
| 168 | |
try { |
| 169 | 0 | conn.rollback(); |
| 170 | 0 | } catch (SQLException e) { |
| 171 | |
|
| 172 | 0 | } |
| 173 | 0 | } |
| 174 | |
} |