001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsDbSqlException.java,v $
003: * Date : $Date: 2008-02-27 12:05:43 $
004: * Version: $Revision: 1.10 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.db;
033:
034: import org.opencms.i18n.CmsMessageContainer;
035: import org.opencms.main.CmsException;
036: import org.opencms.main.CmsLog;
037:
038: import java.sql.Statement;
039:
040: import org.apache.commons.dbcp.DelegatingPreparedStatement;
041: import org.apache.commons.logging.Log;
042:
043: /**
044: * Used to signal sql related issues.<p>
045: *
046: * @author Michael Moossen
047: *
048: * @version $Revision: 1.10 $
049: *
050: * @since 6.0.0
051: */
052: public class CmsDbSqlException extends CmsDbException {
053:
054: /** The log object for this class. */
055: private static final Log LOG = CmsLog
056: .getLog(CmsDriverManager.class);
057:
058: /** Serial version UID required for safe serialization. */
059: private static final long serialVersionUID = -286617872967617367L;
060:
061: /**
062: * Creates a new localized Exception.<p>
063: *
064: * @param container the localized message container to use
065: */
066: public CmsDbSqlException(CmsMessageContainer container) {
067:
068: super (container);
069: // log all sql exceptions
070: if (LOG.isWarnEnabled()) {
071: LOG.warn(container.key(), this );
072: }
073: }
074:
075: /**
076: * Creates a new localized Exception that also containes a root cause.<p>
077: *
078: * @param container the localized message container to use
079: * @param cause the Exception root cause
080: */
081: public CmsDbSqlException(CmsMessageContainer container,
082: Throwable cause) {
083:
084: super (container, cause);
085: // log all sql exceptions
086: if (LOG.isWarnEnabled()) {
087: LOG.warn(container.key(), this );
088: }
089: }
090:
091: /**
092: * Returns the query that let the statement crash.<p>
093: *
094: * @param stmt the Statement to get the crashed query from
095: * @return the crashed query
096: */
097: public static String getErrorQuery(Statement stmt) {
098:
099: if (stmt != null) {
100: // unfortunately, DelegatingPreparedStatement has no toString() method implementation
101: Statement s = stmt;
102: while (s instanceof DelegatingPreparedStatement) {
103: s = ((DelegatingPreparedStatement) s)
104: .getInnermostDelegate();
105: }
106: if (s != null) {
107: // the query that crashed
108: return s.toString();
109: }
110: }
111: return "";
112: }
113:
114: /**
115: * @see org.opencms.main.CmsException#createException(org.opencms.i18n.CmsMessageContainer, java.lang.Throwable)
116: */
117: public CmsException createException(CmsMessageContainer container,
118: Throwable cause) {
119:
120: return new CmsDbSqlException(container, cause);
121: }
122: }
|