Author: arminw
Date: Tue Sep 26 04:53:02 2006
New Revision: 450006
URL: http://svn.apache.org/viewvc?view=rev&rev=450006
Log:
remove redundant code, add new method
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
URL:
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/ExceptionHelper.java?view=diff&rev=450006&r1=450005&r2=450006
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/util/ExceptionHelper.java
Tue Sep 26 04:53:02 2006
@@ -15,8 +15,8 @@
* limitations under the License.
*/
-import java.sql.SQLException;
import java.sql.BatchUpdateException;
+import java.sql.SQLException;
import java.sql.SQLWarning;
import org.apache.commons.lang.SystemUtils;
@@ -48,7 +48,8 @@
* @return A new created [EMAIL PROTECTED]
org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
* arguments.
*/
- public static PersistenceBrokerSQLException generateException(String
message, SQLException ex, String sql, Logger logger)
+ public static PersistenceBrokerSQLException generateException(String
message, SQLException ex,
+ String sql,
Logger logger)
{
return generateException(message, ex, sql, null, null, logger, null);
}
@@ -66,7 +67,8 @@
* @return A new created [EMAIL PROTECTED]
org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
* arguments.
*/
- public static PersistenceBrokerSQLException generateException(SQLException
ex, String sql, ClassDescriptor cld, Logger logger, Object obj)
+ public static PersistenceBrokerSQLException generateException(SQLException
ex, String sql,
+
ClassDescriptor cld, Logger logger, Object obj)
{
return generateException(ex, sql, cld, null, logger, obj);
}
@@ -85,7 +87,9 @@
* @return A new created [EMAIL PROTECTED]
org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
* arguments.
*/
- public static PersistenceBrokerSQLException generateException(SQLException
ex, String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger,
Object obj)
+ public static PersistenceBrokerSQLException generateException(SQLException
ex, String sql,
+
ClassDescriptor cld, ValueContainer[] values,
+ Logger
logger, Object obj)
{
return generateException(null, ex, sql, cld, values, logger, obj);
}
@@ -105,7 +109,89 @@
* @return A new created [EMAIL PROTECTED]
org.apache.ojb.broker.PersistenceBrokerSQLException} based on the specified
* arguments.
*/
- public static PersistenceBrokerSQLException generateException(String
message, SQLException ex, String sql, ClassDescriptor cld, ValueContainer[]
values, Logger logger, Object obj)
+ public static PersistenceBrokerSQLException generateException(String
message, SQLException ex,
+ String sql,
ClassDescriptor cld,
+
ValueContainer[] values, Logger logger,
+ Object obj)
+ {
+ StringBuffer msg = new StringBuffer();
+ String eol = SystemUtils.LINE_SEPARATOR;
+ // message string for PB exception
+ String shortMsg = generateMessage(message, ex, sql, cld, values, obj,
false);
+ if(ex != null)
+ {
+ // add causing stack trace
+ Throwable rootCause = ExceptionUtils.getRootCause(ex);
+ if(rootCause == null) rootCause = ex;
+ msg.append(eol).append("The root stack trace is --> ");
+ String rootStack = ExceptionUtils.getStackTrace(rootCause);
+ msg.append(eol).append(rootStack);
+ }
+ msg.append(SystemUtils.LINE_SEPARATOR).append("**");
+
+ // log error message
+ if(logger != null) logger.error(msg.toString());
+
+ // throw a specific type of runtime exception for a key constraint.
+ String stateCode = ex != null ? ex.getSQLState() : null;
+ if("23000".equals(stateCode) || "23505".equals(stateCode))
+ {
+ throw new KeyConstraintViolatedException(shortMsg, ex);
+ }
+ else
+ {
+ throw new PersistenceBrokerSQLException(shortMsg, ex);
+ }
+ }
+
+ /**
+ * Generates a detailed error message based on the specified exception.
+ *
+ * @param message The error message to use, if <em>null</em> a standard
message is used.
+ * @param ex The exception to convert (mandatory).
+ * @param sql The used sql-statement or <em>null</em>.
+ * @return A detailed error message.
+ */
+ public static String generateMessage(String message, SQLException ex,
String sql)
+ {
+ return generateMessage(message, ex, sql, null, null, null);
+ }
+
+ /**
+ * Generates a detailed error message based on the specified exception.
+ *
+ * @param message The error message to use, if <em>null</em> a standard
message is used.
+ * @param ex The exception to convert (mandatory).
+ * @param sql The used sql-statement or <em>null</em>.
+ * @param cld The [EMAIL PROTECTED]
org.apache.ojb.broker.metadata.ClassDescriptor} of the target object or
<em>null</em>.
+ * @param values The values set in prepared statement or <em>null</em>.
+ * @param obj The target object or <em>null</em>.
+ * @return A detailed error message.
+ */
+ public static String generateMessage(String message, SQLException ex,
String sql,
+ ClassDescriptor cld,
+ ValueContainer[] values,
+ Object obj)
+ {
+ return generateMessage(message, ex, sql, cld, values, obj, true);
+ }
+
+ /**
+ * Generates a detailed error message based on the specified exception.
+ *
+ * @param message The error message to use, if <em>null</em> a standard
message is used.
+ * @param ex The exception to convert (mandatory).
+ * @param sql The used sql-statement or <em>null</em>.
+ * @param cld The [EMAIL PROTECTED]
org.apache.ojb.broker.metadata.ClassDescriptor} of the target object or
<em>null</em>.
+ * @param values The values set in prepared statement or <em>null</em>.
+ * @param obj The target object or <em>null</em>.
+ * @param withRootStack If <em>true</em> the root stack trace will be
added too.
+ * @return A detailed error message.
+ */
+ public static String generateMessage(String message, SQLException ex,
String sql,
+ ClassDescriptor cld,
+ ValueContainer[] values,
+ Object obj, boolean withRootStack)
{
/*
X/OPEN codes within class 23:
@@ -158,14 +244,14 @@
if(sql != null)
{
- msg.append(eol).append("sql statement was
'").append(sql).append("'");
+ msg.append(eol).append("sql statement: ").append(sql);
}
- String stateCode = null;
+ String stateCode;
if(ex != null)
{
- msg.append(eol).append("Exception message is
[").append(ex.getMessage()).append("]");
- msg.append(eol).append("Vendor error code
[").append(ex.getErrorCode()).append("]");
- msg.append(eol).append("SQL state code [");
+ msg.append(eol).append("Exception message:
").append(ex.getMessage());
+ msg.append(eol).append("Vendor error code:
").append(ex.getErrorCode());
+ msg.append(eol).append("SQL state code: ");
stateCode = ex.getSQLState();
if("23000".equalsIgnoreCase(stateCode))
msg.append(stateCode).append("=INTEGRITY CONSTRAINT VIOLATION");
@@ -175,7 +261,6 @@
else if("23505".equalsIgnoreCase(stateCode))
msg.append(stateCode).append("=UNIQUE VIOLATION");
else if("23514".equalsIgnoreCase(stateCode))
msg.append(stateCode).append("=CHECK VIOLATION");
else msg.append(stateCode);
- msg.append("]");
}
if(cld != null)
@@ -229,10 +314,7 @@
}
}
- // message string for PB exception
- String shortMsg = msg.toString();
-
- if(ex != null)
+ if(ex != null && withRootStack)
{
// add causing stack trace
Throwable rootCause = ExceptionUtils.getRootCause(ex);
@@ -243,17 +325,17 @@
}
msg.append(SystemUtils.LINE_SEPARATOR).append("**");
- // log error message
- if(logger != null) logger.error(msg.toString());
+ return msg.toString();
+ }
- // throw a specific type of runtime exception for a key constraint.
- if("23000".equals(stateCode) || "23505".equals(stateCode))
- {
- throw new KeyConstraintViolatedException(shortMsg, ex);
- }
- else
- {
- throw new PersistenceBrokerSQLException(shortMsg, ex);
- }
+ /**
+ * Returns a stack trace string.
+ *
+ * @param message The message used by the internal created exception.
+ * @return Stack trace of current caller.
+ */
+ public static String buildExceptionStack(String message)
+ {
+ return ExceptionUtils.getStackTrace(new Exception(message));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]