001 /* 002 * $Id: MailErrorReporter.java,v 1.7 2005/10/26 14:29:53 kleopatra Exp $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022 package org.jdesktop.swingx; 023 024 import java.io.PrintWriter; 025 import java.io.StringWriter; 026 import java.util.ArrayList; 027 import java.util.List; 028 029 import org.jdesktop.swingx.util.MailTransportProxy; 030 031 /** 032 * This reporter initializes and uses default mail user agent to send information 033 * to predefined mail address. To send error message one needs to configure an MailTransportProxy. 034 * 035 * For example, here is how to use it with <a href="http://jdic.dev.java.net/">JDIC library</a> 036 * <pre> 037 038 import org.jdesktop.swingx.util.MailTransportProxy; 039 import org.jdesktop.swingx.*; 040 import org.jdesktop.jdic.desktop.Message; 041 import org.jdesktop.jdic.desktop.Desktop; 042 import org.jdesktop.jdic.desktop.DesktopException; 043 044 public class TestApp { 045 public static class MyMailTransport implements MailTransportProxy { 046 public void mailMessage(java.util.List<String> toAddr, 047 java.util.List<String> ccAddr, 048 String subject, String body, 049 java.util.List<String> attach) throws Error { 050 Error result = null; 051 052 Message msg = new Message(); 053 msg.setToAddrs(toAddr); 054 msg.setCcAddrs(ccAddr); 055 msg.setSubject(subject); 056 msg.setBody(body); 057 try { 058 msg.setAttachments(attach); 059 } catch (IOException e) { 060 e.printStackTrace(); 061 } 062 try { 063 Desktop.mail(msg); 064 } catch (DesktopException e) { 065 result = new Error(e); 066 result.setStackTrace(Thread.currentThread().getStackTrace()); 067 throw result; 068 } 069 } 070 } 071 public static void main(String args[]) { 072 JFrame jf = new JFrame("Main frame"); 073 ... In the program body ... 074 String errorDetails = "The filter factory can't accept this value"; 075 MailErrorReporter reporter = new MailErrorReporter("someone@the.net"); 076 reporter.setMailTransportProxy(new MyMailTransport()); 077 JXErrorDialog.setReporter(reporter); 078 JXErrorDialog.showDialog(jf, "Filter Error", new RuntimeException(errorDetails)); 079 } 080 } </pre> 081 * 082 * @author Alexander Zuev 083 * @version 1.0 084 */ 085 public class MailErrorReporter extends ErrorReporter { 086 private String mailAddr; 087 private List<String> toList = new ArrayList<String>(); 088 private MailTransportProxy mailTransportProxy; 089 090 /** 091 * Constructs new MailErrorReporter with the given address assigned as destination 092 * address for error report. 093 * 094 * @param address 095 */ 096 public MailErrorReporter(String address) { 097 super(); 098 this.mailAddr = address; 099 toList.add(this.mailAddr); 100 } 101 102 /** 103 * Get the mail address to which send error report 104 * 105 * @return mail address 106 */ 107 public String getMailAddr() { 108 return mailAddr; 109 } 110 111 /** 112 * Set the address to which we will send mail 113 * 114 * @param mailAddr 115 */ 116 public void setMailAddr(String mailAddr) { 117 toList.remove(this.mailAddr); 118 this.mailAddr = mailAddr; 119 toList.add(this.mailAddr); 120 } 121 122 public void setMailTransportProxy(MailTransportProxy mailTransportProxy) { 123 this.mailTransportProxy = mailTransportProxy; 124 } 125 126 /** 127 * Report given incident by popping up system default mail user agent with prepared message 128 * 129 * @param info <code>IncidentInfo</code> which incorporates all the information on error 130 */ 131 public void reportIncident(IncidentInfo info) { 132 if(mailTransportProxy != null) { 133 try { 134 mailTransportProxy.mailMessage(toList, null, info.getHeader(), 135 getMessageBody(info), getAttachments(info)); 136 } catch(Error e) {} // Do nothing 137 } 138 } 139 140 /** 141 * This method is used to extract text message from the provided <code>IncidentInfo</code>. 142 * Override this method to change text formatting or contents. 143 * 144 * @param incident - Incapsulates all the information about error 145 * @return String to be used as a body message in report. 146 */ 147 public String getMessageBody(IncidentInfo incident) { 148 String body = incident.getBasicErrorMessage(); 149 if(incident.getDetailedErrorMessage() != null) { 150 body.concat("\n"+incident.getDetailedErrorMessage()); 151 } 152 if(incident.getErrorException() != null) { 153 StringWriter sw = new StringWriter(); 154 PrintWriter pw = new PrintWriter(sw); 155 incident.getErrorException().printStackTrace(pw); 156 body = body + "\n ----- " + sw.toString(); 157 } 158 return body; 159 } 160 161 /** 162 * This method is used to extract list of paths to files that we want to send 163 * as attachment with the current incident report mwssage. 164 * 165 * @param incident - Incapsulates all the information about error 166 * @return List of Strings containing pathis to files 167 */ 168 public List<String> getAttachments(IncidentInfo incident) { 169 return null; 170 } 171 }