1 31 32 package org.opencms.db.generic; 33 34 import org.opencms.db.CmsDbContext; 35 import org.opencms.db.CmsDbPool; 36 import org.opencms.file.CmsDataAccessException; 37 import org.opencms.file.CmsProject; 38 import org.opencms.main.CmsLog; 39 import org.opencms.util.CmsStringUtil; 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.Serializable ; 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.sql.Statement ; 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 import java.util.Properties ; 52 53 import org.apache.commons.logging.Log; 54 55 64 public class CmsSqlManager extends org.opencms.db.CmsSqlManager implements Serializable , Cloneable { 65 66 67 protected static final String QUERY_PROJECT_SEARCH_PATTERN = "_${PROJECT}_"; 68 69 70 private static final Log LOG = CmsLog.getLog(CmsSqlManager.class); 71 72 73 private static final String QUERY_PROPERTIES = "org/opencms/db/generic/query.properties"; 74 75 76 private static final long serialVersionUID = -5994026786008303964L; 77 78 79 protected Map m_cachedQueries; 80 81 82 protected int m_driverType; 83 84 85 protected String m_poolUrl; 86 87 88 protected Map m_queries; 89 90 93 public CmsSqlManager() { 94 95 m_cachedQueries = new HashMap (); 96 m_queries = new HashMap (); 97 loadQueryProperties(QUERY_PROPERTIES); 98 } 99 100 107 public static org.opencms.db.generic.CmsSqlManager getInstance(String classname) { 108 109 org.opencms.db.generic.CmsSqlManager sqlManager; 110 111 try { 112 Object objectInstance = Class.forName(classname).newInstance(); 113 sqlManager = (org.opencms.db.generic.CmsSqlManager)objectInstance; 114 } catch (Throwable t) { 115 LOG.error(Messages.get().getBundle().key(Messages.LOG_SQL_MANAGER_INIT_FAILED_1, classname), t); 116 sqlManager = null; 117 } 118 119 if (CmsLog.INIT.isInfoEnabled()) { 120 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DRIVER_SQL_MANAGER_1, classname)); 121 } 122 123 return sqlManager; 124 125 } 126 127 135 protected static String replaceProjectPattern(int projectId, String query) { 136 137 String replacePattern = (projectId == CmsProject.ONLINE_PROJECT_ID || projectId < 0) ? "_ONLINE_" : "_OFFLINE_"; 139 query = CmsStringUtil.substitute(query, QUERY_PROJECT_SEARCH_PATTERN, replacePattern); 140 141 return query; 142 } 143 144 152 public void closeAll(CmsDbContext dbc, Connection con, Statement stmnt, ResultSet res) { 153 154 159 if (dbc == null) { 160 LOG.error(Messages.get().getBundle().key(Messages.LOG_NULL_DB_CONTEXT_0)); 161 } 162 163 try { 164 if (con != null && !con.isClosed()) { 166 con.close(); 167 } 168 } catch (SQLException e) { 169 } finally { 171 con = null; 172 } 173 174 try { 175 if (stmnt != null) { 177 stmnt.close(); 178 } 179 } catch (SQLException e) { 180 } finally { 182 stmnt = null; 183 } 184 185 try { 186 if (res != null) { 188 res.close(); 189 } 190 } catch (SQLException e) { 191 } finally { 193 res = null; 194 } 195 196 } 197 198 210 public byte[] getBytes(ResultSet res, String attributeName) throws SQLException { 211 212 return res.getBytes(attributeName); 213 } 214 215 225 public Connection getConnection(CmsDbContext dbc) throws SQLException { 226 227 return getConnection(dbc, 0); 228 } 229 230 242 public Connection getConnection(CmsDbContext dbc, int projectId) throws SQLException { 243 244 if (dbc == null) { 245 LOG.error(Messages.get().getBundle().key(Messages.LOG_NULL_DB_CONTEXT_0)); 246 } 247 return getConnection(projectId); 248 } 249 250 260 public PreparedStatement getPreparedStatement(Connection con, CmsProject project, String queryKey) 261 throws SQLException { 262 263 return getPreparedStatement(con, project.getId(), queryKey); 264 } 265 266 276 public PreparedStatement getPreparedStatement(Connection con, int projectId, String queryKey) throws SQLException { 277 278 String rawSql = readQuery(projectId, queryKey); 279 return getPreparedStatementForSql(con, rawSql); 280 } 281 282 290 public PreparedStatement getPreparedStatement(Connection con, String queryKey) throws SQLException { 291 292 String rawSql = readQuery(0, queryKey); 293 return getPreparedStatementForSql(con, rawSql); 294 } 295 296 304 public PreparedStatement getPreparedStatementForSql(Connection con, String query) throws SQLException { 305 306 return con.prepareStatement(query); 309 } 310 311 317 public void init(int driverType, String poolUrl) { 318 319 if (!poolUrl.startsWith(CmsDbPool.DBCP_JDBC_URL_PREFIX)) { 320 poolUrl = CmsDbPool.DBCP_JDBC_URL_PREFIX + poolUrl; 321 } 322 323 m_driverType = driverType; 324 m_poolUrl = poolUrl; 325 326 } 327 328 337 public int nextId(String tableName) throws CmsDataAccessException { 338 339 return org.opencms.db.CmsDbUtil.nextId(m_poolUrl, tableName); 340 } 341 342 349 public String readQuery(CmsProject project, String queryKey) { 350 351 return readQuery(project.getId(), queryKey); 352 } 353 354 365 public String readQuery(int projectId, String queryKey) { 366 367 String key; 368 if (projectId != 0) { 369 StringBuffer buffer = new StringBuffer (128); 371 buffer.append(queryKey); 372 if (projectId == CmsProject.ONLINE_PROJECT_ID || projectId < 0) { 373 buffer.append("_ONLINE"); 374 } else { 375 buffer.append("_OFFLINE"); 376 } 377 key = buffer.toString(); 378 } else { 379 key = queryKey; 380 } 381 382 String query = (String )m_cachedQueries.get(key); 384 385 if (query == null) { 386 query = readQuery(queryKey); 389 390 query = CmsStringUtil.substitute(query, "\t", " "); 392 query = CmsStringUtil.substitute(query, "\n", " "); 393 394 if (projectId != 0) { 395 query = CmsSqlManager.replaceProjectPattern(projectId, query); 398 } 399 400 m_cachedQueries.put(key, query); 402 } 403 404 return query; 405 } 406 407 413 public String readQuery(String queryKey) { 414 415 String value = (String )m_queries.get(queryKey); 416 if (value == null) { 417 if (LOG.isErrorEnabled()) { 418 LOG.error(Messages.get().getBundle().key(Messages.LOG_QUERY_NOT_FOUND_1, queryKey)); 419 } 420 } 421 return value; 422 } 423 424 435 public void setBytes(PreparedStatement statement, int pos, byte[] content) throws SQLException { 436 437 if (content.length < 2000) { 438 statement.setBytes(pos, content); 439 } else { 440 statement.setBinaryStream(pos, new ByteArrayInputStream (content), content.length); 441 } 442 } 443 444 450 public String validateEmpty(String value) { 451 452 if (CmsStringUtil.isNotEmpty(value)) { 453 return value; 454 } 455 456 return " "; 457 } 458 459 462 protected void finalize() throws Throwable { 463 464 try { 465 if (m_cachedQueries != null) { 466 m_cachedQueries.clear(); 467 } 468 469 if (m_queries != null) { 470 m_queries.clear(); 471 } 472 } catch (Throwable t) { 473 } finally { 475 m_cachedQueries = null; 476 m_queries = null; 477 m_poolUrl = null; 478 } 479 480 super.finalize(); 481 } 482 483 495 protected Connection getConnection(int projectId) throws SQLException { 496 497 501 if (projectId < 0) { 502 throw new SQLException (Messages.get().getBundle().key( 503 Messages.ERR_JDBC_CONN_INVALID_PROJECT_ID_1, 504 new Integer (projectId))); 505 } 506 507 return getConnectionByUrl(m_poolUrl); 509 } 510 511 516 protected void loadQueryProperties(String propertyFilename) { 517 518 Properties properties = new Properties (); 519 520 try { 521 properties.load(getClass().getClassLoader().getResourceAsStream(propertyFilename)); 522 m_queries.putAll(properties); 523 replaceQuerySearchPatterns(); 524 } catch (Throwable t) { 525 if (LOG.isErrorEnabled()) { 526 LOG.error( 527 Messages.get().getBundle().key(Messages.LOG_LOAD_QUERY_PROP_FILE_FAILED_1, propertyFilename), 528 t); 529 } 530 531 properties = null; 532 } 533 } 534 535 538 protected synchronized void replaceQuerySearchPatterns() { 539 540 String currentKey = null; 541 String currentValue = null; 542 int startIndex = 0; 543 int endIndex = 0; 544 int lastIndex = 0; 545 546 Iterator allKeys = m_queries.keySet().iterator(); 547 while (allKeys.hasNext()) { 548 currentKey = (String )allKeys.next(); 549 currentValue = (String )m_queries.get(currentKey); 550 startIndex = 0; 551 endIndex = 0; 552 lastIndex = 0; 553 554 while ((startIndex = currentValue.indexOf("${", lastIndex)) != -1) { 555 endIndex = currentValue.indexOf('}', startIndex); 556 if (endIndex != -1 && !currentValue.startsWith(QUERY_PROJECT_SEARCH_PATTERN, startIndex - 1)) { 557 558 String replaceKey = currentValue.substring(startIndex + 2, endIndex); 559 String searchPattern = currentValue.substring(startIndex, endIndex + 1); 560 String replacePattern = this.readQuery(replaceKey); 561 562 if (replacePattern != null) { 563 currentValue = CmsStringUtil.substitute(currentValue, searchPattern, replacePattern); 564 } 565 } 566 567 lastIndex = endIndex + 2; 568 } 569 m_queries.put(currentKey, currentValue); 570 } 571 } 572 } | Popular Tags |