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