java/code/src/com/redhat/rhn/common/db/NamedPreparedStatement.java | 14 - java/code/src/com/redhat/rhn/common/db/datasource/CachedStatement.java | 10 java/code/src/com/redhat/rhn/common/hibernate/HibernateHelper.java | 7 java/code/src/com/redhat/rhn/common/translation/ExceptionTranslator.java | 109 ---------- java/code/src/com/redhat/rhn/common/translation/SqlExceptionTranslator.java | 87 +++++++ java/code/src/com/redhat/rhn/common/translation/test/ExceptionsWrapperTest.java | 10 6 files changed, 109 insertions(+), 128 deletions(-)
New commits: commit dcbe302ca869e7511b92be74563f5bec204fe601 Author: Tomas Lestach tlestach@redhat.com Date: Thu Jul 1 14:22:18 2010 +0200
replacing ExceptionTranslator for SqlExceptionTranslator and its convert() method for sqlException()
ExceptionTranslator.convert() cycled because this function was picked up as the best candidate for exception translation (1 param, returns RuntimeException)
diff --git a/java/code/src/com/redhat/rhn/common/db/NamedPreparedStatement.java b/java/code/src/com/redhat/rhn/common/db/NamedPreparedStatement.java index 2f780a7..e24b8f4 100644 --- a/java/code/src/com/redhat/rhn/common/db/NamedPreparedStatement.java +++ b/java/code/src/com/redhat/rhn/common/db/NamedPreparedStatement.java @@ -14,7 +14,7 @@ */ package com.redhat.rhn.common.db;
-import com.redhat.rhn.common.translation.ExceptionTranslator; +import com.redhat.rhn.common.translation.SqlExceptionTranslator;
import java.sql.CallableStatement; import java.sql.PreparedStatement; @@ -106,16 +106,17 @@ public final class NamedPreparedStatement { * @param outParams A map of parameter name to Integer object of * SQL Types representing the type of data to be returned. * @return true if CallableStatement executed without error, false otherwise. + * @throws RuntimeException in case of SQLException */ public static boolean execute(CallableStatement cs, Map parameterMap, - Map inParams, Map outParams) { + Map inParams, Map outParams) throws RuntimeException { try { setVars(cs, parameterMap, inParams); setOutputVars(cs, parameterMap, outParams); return cs.execute(); } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } }
@@ -128,6 +129,7 @@ public final class NamedPreparedStatement { * @see java.sql.PreparedStatement#execute() * @return true if PreparedStatement received a result set * false if PreparedStatement received an update count + * @throws RuntimeException in case of SQLException */ public static boolean execute(PreparedStatement ps, Map parameterMap, @@ -137,7 +139,7 @@ public final class NamedPreparedStatement { return ps.execute(); } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } }
@@ -193,7 +195,7 @@ public final class NamedPreparedStatement { } } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } } } @@ -217,7 +219,7 @@ public final class NamedPreparedStatement { cs.registerOutParameter(pos.intValue(), type.intValue()); } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } } } diff --git a/java/code/src/com/redhat/rhn/common/db/datasource/CachedStatement.java b/java/code/src/com/redhat/rhn/common/db/datasource/CachedStatement.java index 58d73ee..530c15d 100644 --- a/java/code/src/com/redhat/rhn/common/db/datasource/CachedStatement.java +++ b/java/code/src/com/redhat/rhn/common/db/datasource/CachedStatement.java @@ -19,7 +19,7 @@ import com.redhat.rhn.common.db.NamedPreparedStatement; import com.redhat.rhn.common.hibernate.HibernateFactory; import com.redhat.rhn.common.hibernate.HibernateHelper; import com.redhat.rhn.common.hibernate.HibernateRuntimeException; -import com.redhat.rhn.common.translation.ExceptionTranslator; +import com.redhat.rhn.common.translation.SqlExceptionTranslator; import com.redhat.rhn.common.util.MethodUtil; import com.redhat.rhn.common.util.StringUtil;
@@ -463,7 +463,7 @@ public class CachedStatement { return new Integer(ps.getUpdateCount()); } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (HibernateException he) { throw new @@ -509,7 +509,7 @@ public class CachedStatement { return processOutputParams(cs, outParams); } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (HibernateException he) { throw new @@ -618,7 +618,7 @@ public class CachedStatement { return dr; } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (ClassNotFoundException e) { throw new ObjectCreateWrapperException("Could not create " + @@ -788,7 +788,7 @@ public class CachedStatement { return columns; } catch (SQLException e) { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } }
diff --git a/java/code/src/com/redhat/rhn/common/hibernate/HibernateHelper.java b/java/code/src/com/redhat/rhn/common/hibernate/HibernateHelper.java index d080415..c9356a3 100644 --- a/java/code/src/com/redhat/rhn/common/hibernate/HibernateHelper.java +++ b/java/code/src/com/redhat/rhn/common/hibernate/HibernateHelper.java @@ -14,7 +14,7 @@ */ package com.redhat.rhn.common.hibernate;
-import com.redhat.rhn.common.translation.ExceptionTranslator; +import com.redhat.rhn.common.translation.SqlExceptionTranslator;
import java.sql.ResultSet; import java.sql.SQLException; @@ -51,8 +51,9 @@ public class HibernateHelper { * Helper function for cleaning up all DB objects * @param rs The resultSet to close * @param stmt The preparedStatement to close + * @throws RuntimeException in case of SQLException */ - public static void cleanupDB(ResultSet rs, Statement stmt) { + public static void cleanupDB(ResultSet rs, Statement stmt) throws RuntimeException { SQLException caught = null; try { if (rs != null) { @@ -73,7 +74,7 @@ public class HibernateHelper { }
if (caught != null) { - throw ExceptionTranslator.convert(caught); + throw SqlExceptionTranslator.sqlException(caught); } } } diff --git a/java/code/src/com/redhat/rhn/common/translation/ExceptionTranslator.java b/java/code/src/com/redhat/rhn/common/translation/ExceptionTranslator.java deleted file mode 100644 index 8ed073c..0000000 --- a/java/code/src/com/redhat/rhn/common/translation/ExceptionTranslator.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright (c) 2009--2010 Red Hat, Inc. - * - * This software is licensed to you under the GNU General Public License, - * version 2 (GPLv2). There is NO WARRANTY for this software, express or - * implied, including the implied warranties of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 - * along with this software; if not, see - * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. - * - * Red Hat trademarks are not licensed under GPLv2. No permission is - * granted to use or replicate Red Hat trademarks that are incorporated - * in this software or its documentation. - */ - -package com.redhat.rhn.common.translation; - -import com.redhat.rhn.common.conf.ConfigDefaults; -import com.redhat.rhn.common.db.ConstraintViolationException; -import com.redhat.rhn.common.db.WrappedSQLException; - -import java.sql.SQLException; - -/** - * Translator The class that actually does the object translations for us. - * - * @version $Rev$ - */ - -public class ExceptionTranslator extends Translations { - - private ExceptionTranslator() { - } - - /** - * Translate from from one object type to another. - * @param have The object to convert - * @return The translated RuntimeException, which includes a reference to - * the passed in exception. - */ - public static RuntimeException convert(Object have) { - RuntimeException e = (RuntimeException)convert(ExceptionTranslator.class, - have, RuntimeException.class); - postProcess(e); - return e; - } - - // Remove any translation stackElements from the exception that is created. - private static void postProcess(RuntimeException e) { - Throwable cause = e.getCause(); - if (cause == null) { - return; - } - e.setStackTrace(cause.getStackTrace()); - } - - /** - * Gets the appropriate runtime exception depending on whether the DB is oracle ornot - * @param e the exception - * @return the RuntimeExcetoion of the wrapped exception - */ - public static RuntimeException sqlException(SQLException e) { - if (ConfigDefaults.get().isOracle()) { - return oracleSQLException(e); - } - else { - return postgreSqlException(e); - } - } - - /** - * Convert from PSQLException to some RuntimeException sub-class. - * - * @param e Exception to translate. - * @return Translated RuntimeException with reference to the original. - */ - private static RuntimeException postgreSqlException(SQLException e) { - return new WrappedSQLException(e.getMessage(), e); - } - - // This will currently only work for SQLException's that come from - // Oracle. To add more Databases, make this a private method, add a - // public method with a different name, but the same signature, in the - // new method, determine DB type, and call the correct translator. - - /** - * Convert from SQLException to some other Exception type - * @param e The SQLException to translate - * @return The translated RuntimeException, which includes a reference - * to the SQLException that was passed in. - */ - private static RuntimeException oracleSQLException(SQLException e) { - int code = e.getErrorCode(); - String msg = e.getMessage(); - switch(code) { - case 1: - int ind = msg.indexOf("(") + 1; - String desc = msg.substring(ind, msg.indexOf(")", ind)); - return new ConstraintViolationException( - ExceptionConstants.VALUE_TOO_LARGE, desc, msg, e); - case 1401: - case 12899: - return new ConstraintViolationException( - ExceptionConstants.VALUE_TOO_LARGE, null, msg, e); - default: - return new WrappedSQLException(e.getMessage(), e); - } - } -} diff --git a/java/code/src/com/redhat/rhn/common/translation/SqlExceptionTranslator.java b/java/code/src/com/redhat/rhn/common/translation/SqlExceptionTranslator.java new file mode 100644 index 0000000..1bbb93c --- /dev/null +++ b/java/code/src/com/redhat/rhn/common/translation/SqlExceptionTranslator.java @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2009--2010 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ + +package com.redhat.rhn.common.translation; + +import com.redhat.rhn.common.conf.ConfigDefaults; +import com.redhat.rhn.common.db.ConstraintViolationException; +import com.redhat.rhn.common.db.WrappedSQLException; + +import java.sql.SQLException; + +/** + * Translator The class that actually does the object translations for us. + * + * @version $Rev$ + */ + +public class SqlExceptionTranslator extends Translations { + + private SqlExceptionTranslator() { + } + + /** + * Gets the appropriate runtime exception depending on whether the DB is oracle ornot + * @param e the exception + * @return the RuntimeExcetoion of the wrapped exception + */ + public static RuntimeException sqlException(SQLException e) { + if (ConfigDefaults.get().isOracle()) { + return oracleSQLException(e); + } + else { + return postgreSqlException(e); + } + } + + /** + * Convert from PSQLException to some RuntimeException sub-class. + * + * @param e Exception to translate. + * @return Translated RuntimeException with reference to the original. + */ + private static RuntimeException postgreSqlException(SQLException e) { + return new WrappedSQLException(e.getMessage(), e); + } + + // This will currently only work for SQLException's that come from + // Oracle. To add more Databases, make this a private method, add a + // public method with a different name, but the same signature, in the + // new method, determine DB type, and call the correct translator. + + /** + * Convert from SQLException to some other Exception type + * @param e The SQLException to translate + * @return The translated RuntimeException, which includes a reference + * to the SQLException that was passed in. + */ + private static RuntimeException oracleSQLException(SQLException e) { + int code = e.getErrorCode(); + String msg = e.getMessage(); + switch(code) { + case 1: + int ind = msg.indexOf("(") + 1; + String desc = msg.substring(ind, msg.indexOf(")", ind)); + return new ConstraintViolationException( + ExceptionConstants.VALUE_TOO_LARGE, desc, msg, e); + case 1401: + case 12899: + return new ConstraintViolationException( + ExceptionConstants.VALUE_TOO_LARGE, null, msg, e); + default: + return new WrappedSQLException(e.getMessage(), e); + } + } +} diff --git a/java/code/src/com/redhat/rhn/common/translation/test/ExceptionsWrapperTest.java b/java/code/src/com/redhat/rhn/common/translation/test/ExceptionsWrapperTest.java index ed16b89..bea267d 100644 --- a/java/code/src/com/redhat/rhn/common/translation/test/ExceptionsWrapperTest.java +++ b/java/code/src/com/redhat/rhn/common/translation/test/ExceptionsWrapperTest.java @@ -19,7 +19,7 @@ import com.redhat.rhn.common.db.WrappedSQLException; import com.redhat.rhn.common.hibernate.HibernateFactory; import com.redhat.rhn.common.hibernate.HibernateHelper; import com.redhat.rhn.common.translation.ExceptionConstants; -import com.redhat.rhn.common.translation.ExceptionTranslator; +import com.redhat.rhn.common.translation.SqlExceptionTranslator;
import org.apache.log4j.Logger; import org.hibernate.Session; @@ -61,7 +61,7 @@ public class ExceptionsWrapperTest extends TestCase { } catch (SQLException e) { try { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (ConstraintViolationException c) { assertNull(c.getConstraint()); @@ -88,7 +88,7 @@ public class ExceptionsWrapperTest extends TestCase { } catch (SQLException e) { try { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (ConstraintViolationException c) { assertTrue(c.getConstraint().indexOf("EXCEPTIONS_TEST_PK") >= 0); @@ -115,7 +115,7 @@ public class ExceptionsWrapperTest extends TestCase { } catch (SQLException e) { try { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (WrappedSQLException c) { // Expected WrappedSQLException @@ -142,7 +142,7 @@ public class ExceptionsWrapperTest extends TestCase { } catch (SQLException e) { try { - throw ExceptionTranslator.convert(e); + throw SqlExceptionTranslator.sqlException(e); } catch (WrappedSQLException c) { StackTraceElement[] elements = c.getStackTrace();
spacewalk-commits@lists.fedorahosted.org