|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| package org.globus.cas.impl.databaseAccess; |
|
12 |
| |
|
13 |
| import org.apache.commons.logging.Log; |
|
14 |
| import org.apache.commons.logging.LogFactory; |
|
15 |
| |
|
16 |
| import java.sql.Connection; |
|
17 |
| import java.sql.DriverManager; |
|
18 |
| import java.sql.Statement; |
|
19 |
| import java.sql.ResultSet; |
|
20 |
| import java.sql.SQLException; |
|
21 |
| |
|
22 |
| import java.util.Vector; |
|
23 |
| |
|
24 |
| |
|
25 |
| import org.apache.commons.pool.ObjectPool; |
|
26 |
| import org.apache.commons.pool.impl.GenericObjectPool; |
|
27 |
| import org.apache.commons.dbcp.ConnectionFactory; |
|
28 |
| import org.apache.commons.dbcp.PoolingDriver; |
|
29 |
| import org.apache.commons.dbcp.PoolableConnectionFactory; |
|
30 |
| import org.apache.commons.dbcp.DriverManagerConnectionFactory; |
|
31 |
| |
|
32 |
| import org.globus.util.I18n; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| public class CasDBStorage { |
|
38 |
| |
|
39 |
| static Log logger = LogFactory.getLog (CasDBStorage.class.getName() ); |
|
40 |
| |
|
41 |
| private static I18n i18n = |
|
42 |
| I18n.getI18n("org.globus.cas.impl.databaseAccess.errors", |
|
43 |
| CasDBStorage.class.getClassLoader()); |
|
44 |
| |
|
45 |
| private static boolean driverSetup = false; |
|
46 |
| private static String driverName; |
|
47 |
| |
|
48 |
| final public static String baseURI = "jdbc:apache:commons:dbcp:"; |
|
49 |
| final public static String managedJobURI = "cas"; |
|
50 |
| |
|
51 |
| |
|
52 |
| static int connNumber; |
|
53 |
| |
|
54 |
| static GenericObjectPool connectionPool = null; |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
0
| private CasDBStorage() {
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
2
| public synchronized static
|
|
66 |
| void setupDBConnection(CasDBOptions casDbOptions_) |
|
67 |
| throws CasDBException { |
|
68 |
| |
|
69 |
2
| logger.debug(" CasDBStorage setup connection called");
|
|
70 |
2
| if (!driverSetup) {
|
|
71 |
2
| logger.debug("dbOptions: " + casDbOptions_.toString());
|
|
72 |
2
| driverName = casDbOptions_.getDriver();
|
|
73 |
2
| try {
|
|
74 |
2
| setupDriver(casDbOptions_.getConnectionURL (),
|
|
75 |
| casDbOptions_.getUserName (), |
|
76 |
| casDbOptions_.getPassword (), |
|
77 |
| casDbOptions_.getActiveConnections(), |
|
78 |
| casDbOptions_.getOnExhaustAction(), |
|
79 |
| casDbOptions_.getMaxWait(), |
|
80 |
| casDbOptions_.getIdleConnections()); |
|
81 |
| } |
|
82 |
| catch(Exception e) { |
|
83 |
0
| logger.error(i18n.getMessage("setupErr"), e);
|
|
84 |
0
| throw new CasDBException(i18n.getMessage("setupErr")
|
|
85 |
| + e.getMessage(), e); |
|
86 |
| } |
|
87 |
2
| driverSetup = true;
|
|
88 |
| } |
|
89 |
2
| connNumber = 0;
|
|
90 |
| } |
|
91 |
| |
|
92 |
2
| public static Connection getDBConnection() throws CasDBException {
|
|
93 |
2
| if (!driverSetup) {
|
|
94 |
0
| throw new CasDBException(i18n.getMessage("driverNotInit"));
|
|
95 |
| } |
|
96 |
| |
|
97 |
2
| try {
|
|
98 |
2
| Class.forName(driverName);
|
|
99 |
2
| Connection connection =
|
|
100 |
| DriverManager.getConnection(baseURI + managedJobURI); |
|
101 |
0
| connNumber++;
|
|
102 |
0
| if (logger.isDebugEnabled()) {
|
|
103 |
0
| logger.debug("Added " + connNumber + " active "
|
|
104 |
| + connectionPool.getNumActive() |
|
105 |
| + " idle " + connectionPool.getNumIdle()); |
|
106 |
| } |
|
107 |
0
| return connection;
|
|
108 |
| } |
|
109 |
| catch(SQLException e) { |
|
110 |
0
| logger.error(i18n.getMessage("dbConnErr"));
|
|
111 |
0
| throw new CasDBException(i18n.getMessage("dbConnErr")
|
|
112 |
| + e.getMessage(), e); |
|
113 |
| } |
|
114 |
| catch(ClassNotFoundException e) { |
|
115 |
0
| logger.error(i18n.getMessage("dbDriverClass"), e);
|
|
116 |
0
| throw new CasDBException(i18n.getMessage("dbDriverClass")
|
|
117 |
| + e.getMessage(), e); |
|
118 |
| } |
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
2
| private static void setupDriver(String connectURI, String username,
|
|
123 |
| String password, int activeConnections, |
|
124 |
| byte onExhaustAction, long maxWait, |
|
125 |
| int idleConnections) |
|
126 |
| throws Exception { |
|
127 |
| |
|
128 |
| |
|
129 |
2
| connectionPool = new GenericObjectPool(null, activeConnections,
|
|
130 |
| onExhaustAction, maxWait, |
|
131 |
| idleConnections); |
|
132 |
| |
|
133 |
2
| ConnectionFactory connectionFactory =
|
|
134 |
| new DriverManagerConnectionFactory(connectURI,username,password); |
|
135 |
| |
|
136 |
| |
|
137 |
2
| PoolableConnectionFactory poolableConnectionFactory =
|
|
138 |
| new PoolableConnectionFactory(connectionFactory, |
|
139 |
| (ObjectPool)connectionPool, |
|
140 |
| null,null,false,true); |
|
141 |
| |
|
142 |
| |
|
143 |
2
| PoolingDriver driver = new PoolingDriver();
|
|
144 |
2
| driver.registerPool(managedJobURI,connectionPool);
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
0
| public static void returnDBConnection(Connection connection)
|
|
152 |
| throws CasDBException { |
|
153 |
| |
|
154 |
0
| if (!driverSetup) {
|
|
155 |
0
| throw new CasDBException(i18n.getMessage("driverNotInit"));
|
|
156 |
| } |
|
157 |
| |
|
158 |
0
| if (connection == null) {
|
|
159 |
0
| return;
|
|
160 |
| } |
|
161 |
| |
|
162 |
0
| try {
|
|
163 |
0
| connection.close();
|
|
164 |
0
| connNumber--;
|
|
165 |
0
| if (logger.isDebugEnabled()) {
|
|
166 |
0
| logger.debug("Reduced " + connNumber + " active "
|
|
167 |
| + connectionPool.getNumActive() |
|
168 |
| + " idle " + connectionPool.getNumIdle()); |
|
169 |
| } |
|
170 |
| } |
|
171 |
| catch (SQLException sqlExcep) { |
|
172 |
0
| logger.error(i18n.getMessage("dbConnReturnErr"), sqlExcep);
|
|
173 |
0
| throw new CasDBException(i18n.getMessage("dbConnReturnErr")
|
|
174 |
| + sqlExcep.getMessage(), sqlExcep); |
|
175 |
| } |
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
0
| public static int runUpdateQuery(String query)
|
|
180 |
| throws CasDBException { |
|
181 |
0
| logger.debug("run update query: Query is " + query);
|
|
182 |
0
| Connection connection = getDBConnection();
|
|
183 |
0
| Statement statement = null;
|
|
184 |
0
| int result = -1;
|
|
185 |
0
| try {
|
|
186 |
0
| statement = connection.createStatement();
|
|
187 |
0
| result = statement.executeUpdate(query);
|
|
188 |
| } |
|
189 |
| catch (SQLException exp) { |
|
190 |
0
| logger.error(i18n.getMessage("stmtExecErr", query), exp);
|
|
191 |
0
| throw new CasDBException(i18n.getMessage("stmtExecErr", query)
|
|
192 |
| + exp.getMessage(), exp); |
|
193 |
| } |
|
194 |
| finally { |
|
195 |
| |
|
196 |
| |
|
197 |
0
| returnDBConnection(connection);
|
|
198 |
0
| try {
|
|
199 |
0
| if (statement != null)
|
|
200 |
0
| statement.close();
|
|
201 |
| } catch (SQLException exp) { |
|
202 |
0
| logger.warn(i18n.getMessage("updateErrClose"), exp);
|
|
203 |
| } |
|
204 |
| } |
|
205 |
0
| return result;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
0
| public static String[] runListQuery(String query, String colName)
|
|
210 |
| throws CasDBException { |
|
211 |
0
| logger.debug("run list query: Query is " + query);
|
|
212 |
0
| Vector listResult = null ;
|
|
213 |
0
| Connection connection = getDBConnection();
|
|
214 |
0
| Statement statement = null;
|
|
215 |
0
| ResultSet resultSet = null;
|
|
216 |
0
| try {
|
|
217 |
0
| statement = connection.createStatement();
|
|
218 |
0
| resultSet = statement.executeQuery(query);
|
|
219 |
0
| if ((resultSet!=null) && (resultSet.next())) {
|
|
220 |
0
| int i = 0;
|
|
221 |
0
| listResult = new Vector(resultSet.getFetchSize());
|
|
222 |
0
| do {
|
|
223 |
0
| listResult.add(resultSet.getString(colName.trim()));
|
|
224 |
0
| logger.debug(listResult.get(i));
|
|
225 |
0
| i++;
|
|
226 |
0
| } while (resultSet.next());
|
|
227 |
| } |
|
228 |
| } |
|
229 |
| catch (SQLException exp) { |
|
230 |
0
| logger.error(i18n.getMessage("stmtExecErr", query), exp);
|
|
231 |
0
| throw new CasDBException(i18n.getMessage("stmtExecErr", query)
|
|
232 |
| + exp.getMessage(), exp); |
|
233 |
| } |
|
234 |
| finally { |
|
235 |
0
| returnDBConnection(connection);
|
|
236 |
0
| try {
|
|
237 |
0
| if (resultSet != null)
|
|
238 |
0
| resultSet.close();
|
|
239 |
0
| if (statement != null)
|
|
240 |
0
| statement.close();
|
|
241 |
| } |
|
242 |
| catch (SQLException exp) { |
|
243 |
0
| logger.warn(i18n.getMessage("listErrClose"), exp);
|
|
244 |
| } |
|
245 |
| } |
|
246 |
0
| if (listResult != null) {
|
|
247 |
0
| String[] returnStrings = new String[listResult.size()];
|
|
248 |
0
| listResult.toArray(returnStrings);
|
|
249 |
0
| return returnStrings;
|
|
250 |
| } else { |
|
251 |
0
| return null;
|
|
252 |
| } |
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
0
| public static String[] vectorToStringArray(Vector vec) {
|
|
259 |
0
| if (vec != null) {
|
|
260 |
0
| String[] retStrings = new String[vec.size()];
|
|
261 |
0
| vec.toArray(retStrings);
|
|
262 |
0
| return retStrings;
|
|
263 |
| } else { |
|
264 |
0
| return null;
|
|
265 |
| } |
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
0
| public static boolean rowExists(String tableName, String columnName,
|
|
274 |
| String value) |
|
275 |
| throws CasDBException { |
|
276 |
| |
|
277 |
0
| String query = "select" + columnName + " from" + tableName + " where"
|
|
278 |
| + columnName ; |
|
279 |
0
| if (tableName.equals(CasDBConstants.TABLE_OBJECT))
|
|
280 |
0
| query = query + " = " + value.trim();
|
|
281 |
| else |
|
282 |
0
| query = query + "='" + value + "'";
|
|
283 |
0
| return rowExists(query);
|
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
0
| public static boolean rowExists(String query) throws CasDBException {
|
|
290 |
0
| logger.debug("\n Query is \n" + query + "\n");
|
|
291 |
0
| boolean result = false;
|
|
292 |
0
| Connection connection = CasDBStorage.getDBConnection();
|
|
293 |
0
| Statement statement = null;
|
|
294 |
0
| ResultSet resultSet = null;
|
|
295 |
0
| try {
|
|
296 |
0
| statement = connection.createStatement();
|
|
297 |
0
| resultSet = statement.executeQuery(query);
|
|
298 |
0
| if ((resultSet!=null) && (resultSet.next())) {
|
|
299 |
0
| result = true;
|
|
300 |
| } |
|
301 |
| } |
|
302 |
| catch (SQLException exp) { |
|
303 |
0
| logger.error(i18n.getMessage("dataExistenceErr", query), exp);
|
|
304 |
0
| throw new CasDBException(i18n.getMessage("dataExistenceErr", query)
|
|
305 |
| + exp.getMessage(), exp); |
|
306 |
| } |
|
307 |
| finally { |
|
308 |
| |
|
309 |
| |
|
310 |
0
| CasDBStorage.returnDBConnection(connection);
|
|
311 |
0
| try {
|
|
312 |
0
| if (resultSet != null)
|
|
313 |
0
| resultSet.close();
|
|
314 |
0
| if (statement != null)
|
|
315 |
0
| statement.close();
|
|
316 |
| } |
|
317 |
| catch (SQLException exp) { |
|
318 |
0
| logger.warn(i18n.getMessage("dataExistenceCloseErr"), exp);
|
|
319 |
| } |
|
320 |
| } |
|
321 |
0
| return result;
|
|
322 |
| } |
|
323 |
| } |