Revision: 1435 http://svn.sourceforge.net/spring-rich-c/?rev=1435&view=rev Author: ge0ffrey Date: 2006-09-22 05:55:18 -0700 (Fri, 22 Sep 2006)
Log Message: ----------- HibernateValidatorException Modified Paths: -------------- trunk/spring-richclient/samples/petclinic/gui/src/main/java/org/springframework/richclient/samples/petclinic/exceptionhandling/ExceptionHandlingView.java trunk/spring-richclient/samples/petclinic/gui/src/main/resources/org/springframework/richclient/samples/petclinic/ctx/common/richclient-application-context.xml trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages.properties trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages_nl.properties trunk/spring-richclient/tiger/pom.xml trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/MessagesDialogExceptionHandler.java Added Paths: ----------- trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/HibernateValidatorDialogExceptionHandler.java Modified: trunk/spring-richclient/samples/petclinic/gui/src/main/java/org/springframework/richclient/samples/petclinic/exceptionhandling/ExceptionHandlingView.java =================================================================== --- trunk/spring-richclient/samples/petclinic/gui/src/main/java/org/springframework/richclient/samples/petclinic/exceptionhandling/ExceptionHandlingView.java 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/samples/petclinic/gui/src/main/java/org/springframework/richclient/samples/petclinic/exceptionhandling/ExceptionHandlingView.java 2006-09-22 12:55:18 UTC (rev 1435) @@ -65,9 +65,9 @@ layoutBuilder.row(); layoutBuilder.relatedGapRow(); - JButton nullPointer = new JButton(new AbstractAction("Cause a null pointer exception"){ + JButton nullPointer = new JButton(new AbstractAction("Cause a NumberFormatException"){ public void actionPerformed(ActionEvent e) { - causeNullPointerException(); + causeNumberFormatException(); } }); layoutBuilder.cell(nullPointer); @@ -127,9 +127,8 @@ } - private void causeNullPointerException() { - String s = null; - s = s.replace(" ", ""); + private void causeNumberFormatException() { + Integer.parseInt("eight"); } /** Modified: trunk/spring-richclient/samples/petclinic/gui/src/main/resources/org/springframework/richclient/samples/petclinic/ctx/common/richclient-application-context.xml =================================================================== --- trunk/spring-richclient/samples/petclinic/gui/src/main/resources/org/springframework/richclient/samples/petclinic/ctx/common/richclient-application-context.xml 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/samples/petclinic/gui/src/main/resources/org/springframework/richclient/samples/petclinic/ctx/common/richclient-application-context.xml 2006-09-22 12:55:18 UTC (rev 1435) @@ -58,12 +58,29 @@ <value type="java.lang.Class">org.hibernate.validator.InvalidStateException</value> </property> <property name="exceptionHandler"> - <bean class="org.springframework.richclient.exceptionhandling.MessagesDialogExceptionHandler"> + <bean class="org.springframework.richclient.exceptionhandling.HibernateValidatorDialogExceptionHandler"> <property name="logLevel" value="INFO"/> <property name="shutdownPolicy" value="NONE"/> </bean> </property> </bean> + <!-- + Warning: When validating non-null columns with Hibernate's DAO based validation, + Hibernate's can also throws org.hibernate.PropertyValueException which are wrapped by spring + See http://opensource.atlassian.com/projects/hibernate/browse/ANN-396 + --> + <!--<bean class="org.springframework.richclient.exceptionhandling.DelegatingExceptionHandlerDelegate">--> + <!--<property name="throwableClass">--> + <!--<value type="java.lang.Class">org.springframework.orm.hibernate3.HibernateSystemException</value>--> + <!--</property>--> + <!--<property name="exceptionHandler">--> + <!--<bean class="org.springframework.richclient.exceptionhandling.MessagesDialogExceptionHandler">--> + <!--<property name="logLevel" value="INFO"/>--> + <!--<property name="shutdownPolicy" value="NONE"/>--> + <!--<property name="evaluatedChainedIndex" value="1"/>--> + <!--</bean>--> + <!--</property>--> + <!--</bean>--> <bean class="org.springframework.richclient.exceptionhandling.DelegatingExceptionHandlerDelegate"> <property name="throwableClass"> <value type="java.lang.Class">java.lang.Error</value> Modified: trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages.properties =================================================================== --- trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages.properties 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages.properties 2006-09-22 12:55:18 UTC (rev 1435) @@ -7,6 +7,8 @@ dialogExceptionHandler.ask.continue=Continue dialogExceptionHandler.obligate.shutdown=Shutdown +hibernateValidatorDialogExceptionHandler.caption=Invalid data +hibernateValidatorDialogExceptionHandler.explanation=The following data is not valid: # menus Modified: trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages_nl.properties =================================================================== --- trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages_nl.properties 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/support/src/main/resources/org/springframework/richclient/application/messages_nl.properties 2006-09-22 12:55:18 UTC (rev 1435) @@ -7,6 +7,9 @@ dialogExceptionHandler.ask.continue=Ga verder dialogExceptionHandler.obligate.shutdown=Sluit applicatie +hibernateValidatorDialogExceptionHandler.caption=Ongeldige gegevens +hibernateValidatorDialogExceptionHandler.explanation=Devolgende gegevens zijn niet geldig: + # menus fileMenu.label=&Bestand Modified: trunk/spring-richclient/tiger/pom.xml =================================================================== --- trunk/spring-richclient/tiger/pom.xml 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/tiger/pom.xml 2006-09-22 12:55:18 UTC (rev 1435) @@ -74,10 +74,23 @@ <artifactId>spring-context</artifactId> </dependency> + <!-- Validation with Hibernate validator --> + <dependency> + <groupId>javax.persistence</groupId> + <artifactId>persistence-api</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-annotations</artifactId> + <optional>true</optional> + </dependency> + <!-- JNLP --> <dependency> <groupId>javax.jnlp</groupId> <artifactId>jnlp-api</artifactId> + <optional>true</optional> </dependency> </dependencies> Added: trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/HibernateValidatorDialogExceptionHandler.java =================================================================== --- trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/HibernateValidatorDialogExceptionHandler.java (rev 0) +++ trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/HibernateValidatorDialogExceptionHandler.java 2006-09-22 12:55:18 UTC (rev 1435) @@ -0,0 +1,52 @@ +package org.springframework.richclient.exceptionhandling; + +import org.springframework.context.support.DefaultMessageSourceResolvable; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.WordUtils; +import org.hibernate.validator.InvalidStateException; + +import javax.swing.JPanel; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.ScrollPaneConstants; +import java.util.List; +import java.util.ArrayList; +import java.util.StringTokenizer; +import java.awt.BorderLayout; + +/** + * Displays the validation errors to the user. + * + * @author Geoffrey De Smet + */ +public class HibernateValidatorDialogExceptionHandler extends AbstractDialogExceptionHandler { + + private static final String CAPTION_KEY = "hibernateValidatorDialogExceptionHandler.caption"; + private static final String EXPLANATION_KEY = "hibernateValidatorDialogExceptionHandler.explanation"; + + public String resolveExceptionCaption(Throwable throwable) { + return messageSourceAccessor.getMessage(CAPTION_KEY, CAPTION_KEY); + } + + public Object createExceptionContent(Throwable throwable) { + if (!(throwable instanceof InvalidStateException)) { + String ILLEGAL_THROWABLE_ARGUMENT + = "Could not handle exception: throwable is not an InvalidStateException:\n" + + throwable.getClass().getName(); + logger.error(ILLEGAL_THROWABLE_ARGUMENT); + return ILLEGAL_THROWABLE_ARGUMENT; + } + InvalidStateException invalidStateException = (InvalidStateException) throwable; + String explanation = messageSourceAccessor.getMessage(EXPLANATION_KEY, EXPLANATION_KEY); + JPanel panel = new JPanel(new BorderLayout()); + JLabel explanationLabel = new JLabel(explanation); + panel.add(explanationLabel, BorderLayout.NORTH); + JList invalidValuesJList = new JList(invalidStateException.getInvalidValues()); + JScrollPane invalidValuesScrollPane = new JScrollPane(invalidValuesJList, + ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); + panel.add(invalidValuesScrollPane, BorderLayout.CENTER); + return panel; + } + +} Modified: trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/MessagesDialogExceptionHandler.java =================================================================== --- trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/MessagesDialogExceptionHandler.java 2006-09-21 14:53:26 UTC (rev 1434) +++ trunk/spring-richclient/tiger/src/main/java/org/springframework/richclient/exceptionhandling/MessagesDialogExceptionHandler.java 2006-09-22 12:55:18 UTC (rev 1435) @@ -9,14 +9,35 @@ import java.util.StringTokenizer; /** + * Displays a message to the user which is fetched from the I18N files + * based on the class and superclasses of the throwable. + * + * For example if an IllegalArgumentException is thrown, it will search for + * java.lang.IllegalArgumentException.caption and java.lang.IllegalArgumentException.description first, + * and if it cant find that it will try in order: + * java.lang.RuntimeException.caption/description, java.lang.Exception.caption/description and + * java.lang.Throwable.caption/description. + * + * The exception message is passed as a parameter, but is idented and wrapped first. + * * @author Geoffrey De Smet */ public class MessagesDialogExceptionHandler extends AbstractDialogExceptionHandler { + private int evaluatedChainedIndex = 0; private int wrapLength = 120; private int identLength = 2; /** + * If this is bigger then 0, instead of finding a message for the giving throwable, + * it will use a recursive chained exception. + * @param evaluatedChainedIndex the number of times that should be recursed + */ + public void setEvaluatedChainedIndex(int evaluatedChainedIndex) { + this.evaluatedChainedIndex = evaluatedChainedIndex; + } + + /** * Sets the wrap length applied on the exception message passed as a parameter. * Defaults to 120. * @param wrapLength @@ -36,6 +57,7 @@ public String resolveExceptionCaption(Throwable throwable) { List<String> messageCaptionKeyList = new ArrayList<String>(); + Throwable evaluatedThrowable = determineEvaluatedThrowable(throwable); Class clazz = throwable.getClass(); while (clazz != Object.class) { messageCaptionKeyList.add(clazz.getName() + ".caption"); @@ -48,17 +70,31 @@ public Object createExceptionContent(Throwable throwable) { List<String> messageDescriptionKeyList = new ArrayList<String>(); - Class clazz = throwable.getClass(); + Throwable evaluatedThrowable = determineEvaluatedThrowable(throwable); + Class clazz = evaluatedThrowable.getClass(); while (clazz != Object.class) { messageDescriptionKeyList.add(clazz.getName() + ".description"); clazz = clazz.getSuperclass(); } String[] codes = messageDescriptionKeyList.toArray(new String[messageDescriptionKeyList.size()]); - String[] parameters = new String[]{formatMessage(throwable.getMessage())}; + String[] parameters = new String[]{formatMessage(evaluatedThrowable.getMessage())}; return messageSourceAccessor.getMessage(new DefaultMessageSourceResolvable( codes, parameters, codes[0])); } + private Throwable determineEvaluatedThrowable(Throwable throwable) { + Throwable evaluatedThrowable = throwable; + for (int i = 0; i < evaluatedChainedIndex; i++) { + Throwable cause = evaluatedThrowable.getCause(); + if (cause == null || cause == evaluatedThrowable) { + break; + } else { + evaluatedThrowable = cause; + } + } + return evaluatedThrowable; + } + public String formatMessage(String message) { if (message == null) { return ""; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ spring-rich-c-cvs mailing list spring-rich-c-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs