View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Various JDBC related utility methods
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  	 * Given a database type, return the corresponding JDBC configuration
44  	 * 
45  	 * @return JDBCConfiguration
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  	 * Given a JDBC url, attempt to locate the corresponding JDBCConfig object
59  	 * 
60  	 * @param url
61  	 * @return JDBCConfiguration
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  	 * Given a JDBC connection URL, extract only the database name.
77  	 * 
78  	 * @param url
79  	 *            a JDBC connection URL
80  	 * @return the database name
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 	 * Given a JDBC connection URL, generate a new connection URL to connect directly to the database server itself (ie:
101 	 * no database specified).
102 	 * 
103 	 * @param url
104 	 *            a JDBC connection URL
105 	 * @return a new JDBC connection URL to connect directly to the database server
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 		// TODO This next line is nasty, but it works for nearly every postgresql server.
118 		// If we have to add another exception to this for another database server, then I highly recommend refactoring
119 		// this to a more elegant solution.
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 			// ignore
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 			// ignore
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 			// ignore
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 			// ignore
187 		}
188 	}
189 }