001 package org.springframework.richclient.exceptionhandling; 002 003 import java.io.PrintWriter; 004 import java.io.StringWriter; 005 import java.util.Arrays; 006 007 import org.apache.commons.lang.StringUtils; 008 import org.jdesktop.jdic.desktop.Desktop; 009 import org.jdesktop.jdic.desktop.DesktopException; 010 import org.jdesktop.jdic.desktop.Message; 011 import org.jdesktop.swingx.error.ErrorInfo; 012 import org.jdesktop.swingx.error.ErrorReporter; 013 import org.springframework.beans.factory.BeanNameAware; 014 import org.springframework.beans.factory.InitializingBean; 015 import org.springframework.context.support.MessageSourceAccessor; 016 import org.springframework.richclient.application.ApplicationServicesLocator; 017 018 /** 019 * <p> 020 * This email reporter can be added as {@link ErrorReporter} to the 021 * {@link JXErrorDialogExceptionHandler}. The email reporter uses the JDIC ( 022 * {@link https://jdic.dev.java.net/}) library to access your mail client. To 023 * use and deploy this correctly, you need to have the correct native libraries 024 * for your platform and have them added to your VM startup 025 * (-Djava.library.path). 026 * </p> 027 * <p> 028 * The following libs are needed: 029 * </p> 030 * <ul> 031 * <li><em>jdic-shared</em>: this one is always needed, shared across 032 * platforms.</li> 033 * <li><em>jdic-stub-{linux/windows}</em>: platform specific java classes.</li> 034 * <li><em>jdic-native-{linux/windows}</em>: platform specific native 035 * libraries.</li> 036 * </ul> 037 * 038 * <p> 039 * During development, maven can add the correct jars to your classpath by using 040 * a profile that is os specific (see pom.xml of spring-richclient-jdk5). Note 041 * that you still have to add the native libraries to your environment. You can 042 * do this by unpacking the jdic-native-* file and setting the 043 * -Djava.library.path to that directory. 044 * </p> 045 * <p> 046 * In production the same setup should be used. A webstart app should use os 047 * specific and native dependencies to have the correct jars downloaded and the 048 * native library unpacked and added to the environment. 049 * </p> 050 * 051 * @author Jan Hoskens 052 * 053 */ 054 public class EmailNotifierErrorReporter implements ErrorReporter, BeanNameAware, InitializingBean { 055 056 private MessageSourceAccessor messageSourceAccessor; 057 058 private String id; 059 060 public void afterPropertiesSet() { 061 if (messageSourceAccessor == null) { 062 messageSourceAccessor = (MessageSourceAccessor) ApplicationServicesLocator.services().getService( 063 MessageSourceAccessor.class); 064 } 065 if (getId() == null) { 066 setId(StringUtils.uncapitalize(getClass().getSimpleName())); 067 } 068 } 069 070 public void reportError(ErrorInfo info) throws NullPointerException { 071 Message mail = new Message(); 072 073 Object params[] = new Object[] { info.getBasicErrorMessage(), info.getDetailedErrorMessage() }; 074 if (info.getErrorException() != null) { 075 params = new Object[] { info.getErrorException(), getStackTraceString(info.getErrorException()) }; 076 } 077 078 String body = messageSourceAccessor.getMessage(getId() + ".body", params, ""); 079 String title = messageSourceAccessor.getMessage(getId() + ".title", ""); 080 081 String adresses = messageSourceAccessor.getMessage(getId() + ".mailTo", ""); 082 if (!StringUtils.isEmpty(adresses)) { 083 mail.setToAddrs(Arrays.<String> asList(adresses.split(";"))); 084 } 085 086 mail.setSubject(title); 087 mail.setBody(body); 088 089 try { 090 Desktop.mail(mail); 091 } 092 catch (DesktopException e) { 093 String mailExceptionMessage = messageSourceAccessor.getMessage(getId() + ".mailException", ""); 094 throw new RuntimeException(mailExceptionMessage, e); 095 } 096 097 } 098 099 protected String getStackTraceString(Throwable t) { 100 StringWriter sw = new StringWriter(); 101 PrintWriter pw = new PrintWriter(sw, true); 102 t.printStackTrace(pw); 103 pw.flush(); 104 sw.flush(); 105 return sw.toString(); 106 } 107 108 public String getId() { 109 return id; 110 } 111 112 public void setId(String id) { 113 this.id = id; 114 } 115 116 public void setBeanName(String name) { 117 if (getId() == null) { 118 setId(name); 119 } 120 } 121 }