View Javadoc

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   * Various JDBC related utility methods
18   */
19  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  	ApplicationContext context = new ClassPathXmlApplicationContext(JDBC_CONTEXT);
23  
24  	@SuppressWarnings("unchecked")
25  	List<JDBCConfiguration> jdbcConfigs = (List<JDBCConfiguration>) context.getBean(JDBC_CONFIGURATIONS);
26  
27  	/**
28  	 * Given a database type, return the corresponding JDBC configuration
29  	 * 
30  	 * @return JDBCConfiguration
31  	 */
32  
33  	public JDBCConfiguration getDatabaseConfiguration(DatabaseType type) {
34  		for (JDBCConfiguration jdbcConfig : jdbcConfigs) {
35  			if (jdbcConfig.getType().equals(type)) {
36  				return jdbcConfig;
37  			}
38  		}
39  		return JDBCConfiguration.UNKNOWN_CONFIG;
40  	}
41  
42  	/**
43  	 * Given a JDBC url, attempt to locate the corresponding JDBCConfig object
44  	 * 
45  	 * @param url
46  	 * @return JDBCConfiguration
47  	 */
48  	public JDBCConfiguration getDatabaseConfiguration(String url) {
49  		Validate.isTrue(isNotEmpty(url));
50  
51  		for (JDBCConfiguration jdbcConfig : jdbcConfigs) {
52  			String urlFragment = jdbcConfig.getUrlFragment();
53  			if (url.contains(urlFragment)) {
54  				return jdbcConfig;
55  			}
56  		}
57  		return JDBCConfiguration.UNKNOWN_CONFIG;
58  	}
59  
60  	/**
61  	 * Given a JDBC connection URL, extract only the database name.
62  	 * 
63  	 * @param url
64  	 *            a JDBC connection URL
65  	 * @return the database name
66  	 */
67  	public String getDatabaseName(String url) {
68  		int leftIndex = url.lastIndexOf("/");
69  		if (leftIndex == -1) {
70  			leftIndex = url.lastIndexOf(":");
71  		}
72  		leftIndex++;
73  
74  		int rightIndex = url.length();
75  		if (url.indexOf("?") != -1) {
76  			rightIndex = url.indexOf("?");
77  		} else if (url.indexOf(";") != -1) {
78  			rightIndex = url.indexOf(";");
79  		}
80  
81  		return url.substring(leftIndex, rightIndex);
82  	}
83  
84  	/**
85  	 * Given a JDBC connection URL, generate a new connection URL to connect directly to the database server itself (ie:
86  	 * no database specified).
87  	 * 
88  	 * @param url
89  	 *            a JDBC connection URL
90  	 * @return a new JDBC connection URL to connect directly to the database server
91  	 */
92  	public String getServerUrl(String url) {
93  		int rightIndex = url.length();
94  		if (url.lastIndexOf("/") != -1) {
95  			rightIndex = url.lastIndexOf("/");
96  		} else if (url.lastIndexOf(":") != -1) {
97  			rightIndex = url.lastIndexOf(":");
98  		}
99  
100 		String baseUrl = url.substring(0, rightIndex);
101 
102 		// TODO This next line is nasty, but it works for nearly every postgresql server.
103 		// If we have to add another exception to this for another database server, then I highly recommend refactoring
104 		// this to a more elegant solution.
105 		if (POSTGRESQL.equals(getDatabaseConfiguration(url).getType())) {
106 			baseUrl += "/postgres";
107 		}
108 
109 		String options = "";
110 		int optionsIndex = url.indexOf("?");
111 		if (optionsIndex == -1) {
112 			optionsIndex = url.indexOf(";");
113 		}
114 		if (optionsIndex != -1) {
115 			options = url.substring(optionsIndex);
116 		}
117 
118 		return baseUrl + options;
119 	}
120 
121 	public static void closeQuietly(ResultSet rs, Statement stmt, Connection conn) {
122 		closeQuietly(rs);
123 		closeQuietly(stmt);
124 		closeQuietly(conn);
125 	}
126 
127 	public static void closeQuietly(Statement stmt, Connection conn) {
128 		closeQuietly(null, stmt, conn);
129 	}
130 
131 	public static void closeQuietly(ResultSet rs) {
132 		if (rs == null) {
133 			return;
134 		}
135 		try {
136 			rs.close();
137 		} catch (SQLException e) {
138 			// ignore
139 		}
140 	}
141 
142 	public static void closeQuietly(Statement stmt) {
143 		if (stmt == null) {
144 			return;
145 		}
146 		try {
147 			stmt.close();
148 		} catch (SQLException e) {
149 			// ignore
150 		}
151 	}
152 
153 	public static void closeQuietly(Connection conn) {
154 		if (conn == null) {
155 			return;
156 		}
157 		try {
158 			conn.close();
159 		} catch (SQLException e) {
160 			// ignore
161 		}
162 	}
163 
164 	public static void rollbackQuietly(Connection conn) {
165 		if (conn == null) {
166 			return;
167 		}
168 		try {
169 			conn.rollback();
170 		} catch (SQLException e) {
171 			// ignore
172 		}
173 	}
174 }