MailErrorReporter.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: MailErrorReporter.java,v 1.6 2005/10/10 18:02:00 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20. */
  21. package org.jdesktop.swingx;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.io.StringWriter;
  25. import java.io.PrintWriter;
  26. import org.jdesktop.swingx.util.MailTransportProxy;
  27. /**
  28.  * This reporter initializes and uses default mail user agent to send information
  29.  * to predefined mail address. To send error message one needs to configure an MailTransportProxy.
  30.  *
  31.  * For example, here is how to use it with <a href="http://jdic.dev.java.net/">JDIC library</a>
  32.  * <pre>
  33.  import org.jdesktop.swingx.util.MailTransportProxy;
  34.  import org.jdesktop.swingx.*;
  35.  import org.jdesktop.jdic.desktop.Message;
  36.  import org.jdesktop.jdic.desktop.Desktop;
  37.  import org.jdesktop.jdic.desktop.DesktopException;
  38.  public class TestApp {
  39.      public static class MyMailTransport implements MailTransportProxy {
  40.          public void mailMessage(java.util.List<String> toAddr,
  41.                                  java.util.List<String> ccAddr,
  42.                                  String subject, String body,
  43.                                  java.util.List<String> attach) throws Error {
  44.              Error result = null;
  45.              Message msg = new Message();
  46.              msg.setToAddrs(toAddr);
  47.              msg.setCcAddrs(ccAddr);
  48.              msg.setSubject(subject);
  49.              msg.setBody(body);
  50.              try {
  51.                  msg.setAttachments(attach);
  52.              } catch (IOException e) {
  53.                  e.printStackTrace();
  54.              }
  55.              try {
  56.                  Desktop.mail(msg);
  57.              } catch (DesktopException e) {
  58.                  result = new Error(e);
  59.                  result.setStackTrace(Thread.currentThread().getStackTrace());
  60.                  throw result;
  61.              }
  62.          }
  63.      }
  64.      public static void main(String args[]) {
  65.          JFrame jf = new JFrame("Main frame");
  66.      ... In the program body ...
  67.          String errorDetails = "The filter factory can't accept this value";
  68.          MailErrorReporter reporter = new MailErrorReporter("someone@the.net");
  69.          reporter.setMailTransportProxy(new MyMailTransport());
  70.          JXErrorDialog.setReporter(reporter);
  71.          JXErrorDialog.showDialog(jf, "Filter Error", new RuntimeException(errorDetails));
  72.      }
  73.  } </pre>
  74.  *
  75.  * @author Alexander Zuev
  76.  * @version 1.0
  77.  */
  78. public class MailErrorReporter extends ErrorReporter {
  79.     private String mailAddr;
  80.     private List<String> toList = new ArrayList<String>();
  81.     private MailTransportProxy mailTransportProxy;
  82.     /**
  83.      * Constructs new MailErrorReporter with the given address assigned as destination
  84.      * address for error report.
  85.      *
  86.      * @param address
  87.      */
  88.     public MailErrorReporter(String address) {
  89.         super();
  90.         this.mailAddr = address;
  91.         toList.add(this.mailAddr);
  92.     }
  93.     /**
  94.      * Get the mail address to which send error report
  95.      *
  96.      * @return mail address
  97.      */
  98.     public String getMailAddr() {
  99.         return mailAddr;
  100.     }
  101.     /**
  102.      * Set the address to which we will send mail
  103.      *
  104.      * @param mailAddr
  105.      */
  106.     public void setMailAddr(String mailAddr) {
  107.         toList.remove(this.mailAddr);
  108.         this.mailAddr = mailAddr;
  109.         toList.add(this.mailAddr);
  110.     }
  111.     public void setMailTransportProxy(MailTransportProxy mailTransportProxy) {
  112.         this.mailTransportProxy = mailTransportProxy;
  113.     }
  114.     /**
  115.      * Report given incident by popping up system default mail user agent with prepared message
  116.      *
  117.      * @param info <code>IncidentInfo</code> which incorporates all the information on error
  118.      */
  119.     public void reportIncident(IncidentInfo info) {
  120.         if(mailTransportProxy != null) {
  121.             try {
  122.                 mailTransportProxy.mailMessage(toList, null, info.getHeader(),
  123.                                                getMessageBody(info), getAttachments(info));
  124.             } catch(Error e) {} // Do nothing
  125.         }
  126.     }
  127.     /**
  128.      * This method is used to extract text message from the provided <code>IncidentInfo</code>.
  129.      * Override this method to change text formatting or contents.
  130.      *
  131.      * @param incident - Incapsulates all the information about error
  132.      * @return String to be used as a body message in report.
  133.      */
  134.     public String getMessageBody(IncidentInfo incident) {
  135.         String body = incident.getBasicErrorMessage();
  136.         if(incident.getDetailedErrorMessage() != null) {
  137.             body.concat("n"+incident.getDetailedErrorMessage());
  138.         }
  139.         if(incident.getErrorException() != null) {
  140.             StringWriter sw = new StringWriter();
  141.             PrintWriter pw = new PrintWriter(sw);
  142.             incident.getErrorException().printStackTrace(pw);
  143.             body = body + "n ----- " + sw.toString();
  144.         }
  145.         return body;
  146.     }
  147.     /**
  148.      * This method is used to extract list of paths to files that we want to send
  149.      * as attachment with the current incident report mwssage.
  150.      *
  151.      * @param incident - Incapsulates all the information about error
  152.      * @return List of Strings containing pathis to files
  153.      */
  154.     public List<String> getAttachments(IncidentInfo incident) {
  155.         return null;
  156.     }
  157. }