~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/mail/MailCaptureListener.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.mail;
3
 
 
4
 
import java.io.PrintWriter;
5
 
import java.io.StringWriter;
6
 
import java.util.Date;
7
 
import java.util.Properties;
8
 
 
9
 
import javax.mail.Address;
10
 
import javax.mail.Message;
11
 
import javax.mail.MessagingException;
12
 
import javax.mail.SendFailedException;
13
 
import javax.mail.Session;
14
 
import javax.mail.Transport;
15
 
import javax.mail.internet.InternetAddress;
16
 
import javax.mail.internet.MimeMessage;
17
 
 
18
 
import org.crosswire.util.Level;
19
 
import org.crosswire.util.Logger;
20
 
import org.crosswire.util.Reporter;
21
 
import org.crosswire.util.event.ReporterEvent;
22
 
import org.crosswire.util.event.ReporterListener;
23
 
 
24
 
/**
25
 
 * This class listens to Log captures and copies them to a
26
 
 * mail server.
27
 
 *
28
 
 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
29
 
 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
30
 
 * Distribution Licence:<br />
31
 
 * Project B is free software; you can redistribute it
32
 
 * and/or modify it under the terms of the GNU General Public License,
33
 
 * version 2 as published by the Free Software Foundation.<br />
34
 
 * This program is distributed in the hope that it will be useful,
35
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37
 
 * General Public License for more details.<br />
38
 
 * The License is available on the internet
39
 
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
40
 
 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
41
 
 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
42
 
 * The copyright to this program is held by it's authors.
43
 
 * </font></td></tr></table>
44
 
 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
45
 
 * @see <{docs.Licence}>
46
 
 * @author Joe Walker
47
 
 */
48
 
public class MailCaptureListener implements ReporterListener
49
 
{
50
 
    /**
51
 
     * Called whenever log.capture() is passed an Exception
52
 
     * @param ev Object describing the exception
53
 
     */
54
 
    public void reportException(ReporterEvent ev)
55
 
    {
56
 
        // Create a string from the exception
57
 
        StringWriter str = new StringWriter();
58
 
        ev.getException().printStackTrace(new PrintWriter(str));
59
 
        String message = new String(str.getBuffer());
60
 
 
61
 
        sendMail("Exception from "+ev.getSourceName(), message);
62
 
    }
63
 
 
64
 
    /**
65
 
     * Called whenever log.capture() is passed an Exception
66
 
     * @param ev Object describing the exception
67
 
     */
68
 
    public void reportMessage(ReporterEvent ev)
69
 
    {
70
 
        sendMail("Message from "+ev.getSourceName(), ev.getMessage());
71
 
    }
72
 
 
73
 
    /**
74
 
     * Called whenever log.capture() is passed an Exception
75
 
     * @param ev Object describing the exception
76
 
     */
77
 
    public void sendMail(String subject, String body)
78
 
    {
79
 
        // create some properties and get the default Session
80
 
        Properties props = new Properties();
81
 
        props.put("mail.smtp.host", mail_server);
82
 
        props.put("mail.smtp.port", ""+mail_port);
83
 
 
84
 
        Session session = Session.getDefaultInstance(props, null);
85
 
 
86
 
        if (mail_debug)
87
 
        {
88
 
            props.put("mail.debug", "true");
89
 
            session.setDebug(mail_debug);
90
 
        }
91
 
 
92
 
        try
93
 
        {
94
 
            // Create the basic message
95
 
            MimeMessage msg = new MimeMessage(session);
96
 
            msg.setFrom(new InternetAddress(mail_fromaddr));
97
 
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail_toaddr, false));
98
 
            msg.setSubject(subject);
99
 
            msg.setSentDate(new Date());
100
 
            msg.setHeader("X-Mailer", "MailCaptureListener");
101
 
            msg.setText(body /*default charset*/);
102
 
 
103
 
            /*
104
 
            // Create and fill the second message part
105
 
            MimeBodyPart mbp = new MimeBodyPart();
106
 
            mbp.setText(message, "us-ascii");
107
 
 
108
 
            // Create the Multipart and its parts to it
109
 
            Multipart mp = new MimeMultipart();
110
 
            mp.addBodyPart(mbp);
111
 
 
112
 
            // Add the Multipart to the message
113
 
            msg.setContent(mp);
114
 
            */
115
 
 
116
 
            // Send the message
117
 
            Transport.send(msg);
118
 
        }
119
 
        catch (MessagingException ex)
120
 
        {
121
 
            // log.fine("Warning failure in MailException reporting. Turning Mail reporting off.");
122
 
            //if (ex instanceof MessagingException)
123
 
            {
124
 
                printMessagingException(ex);
125
 
                throw new IllegalAccessError("Mail system failed. See system output for details.");
126
 
            }
127
 
            /*
128
 
            else
129
 
            {
130
 
                log.log(Level.INFO, "Failure during reporting", ex);
131
 
            }
132
 
 
133
 
            // Turn ourselves off and then throw again.
134
 
            // Is this a good idea? What if there is more than one thing that
135
 
            // handles Exceptions? Hmmmm.
136
 
            setHelpDeskListener(false);
137
 
            log.capture(this, new Exception("Mail system failed. See system output for details. Disabling Mail."));
138
 
            */
139
 
        }
140
 
    }
141
 
 
142
 
    /**
143
 
     * You must call getHelpDeskListener() in order to start displaying
144
 
     * Exceptions sent to the Log, and in order to properly
145
 
     * close this class you must call it again (with false).
146
 
     * @param joined Are we listening to the Log
147
 
     */
148
 
    public static void setHelpDeskListener(boolean joined)
149
 
    {
150
 
        if (joined && li == null)
151
 
        {
152
 
            li = new MailCaptureListener();
153
 
            Reporter.addReporterListener(li);
154
 
        }
155
 
 
156
 
        if (!joined && li != null)
157
 
        {
158
 
            Reporter.removeReporterListener(li);
159
 
            li = null;
160
 
        }
161
 
    }
162
 
 
163
 
    /**
164
 
     * Get the listening status
165
 
     */
166
 
    public static boolean getHelpDeskListener()
167
 
    {
168
 
        return (li != null);
169
 
    }
170
 
 
171
 
    /**
172
 
     * Some debug for when we get an exception
173
 
     * @param out The stream to write some debug to
174
 
     * @param ex The MessagingException to print
175
 
     */
176
 
    private static void printMessagingException(Exception ex)
177
 
    {
178
 
        log.log(Level.INFO, "Mail Exception", ex);
179
 
 
180
 
        if (ex instanceof SendFailedException)
181
 
        {
182
 
            SendFailedException sfex = (SendFailedException) ex;
183
 
 
184
 
            Address[] invalid = sfex.getInvalidAddresses();
185
 
            if (invalid != null)
186
 
            {
187
 
                log.info("Invalid Addresses:");
188
 
                for (int i=0; i<invalid.length; i++)
189
 
                    log.info("  "+invalid[i]);
190
 
            }
191
 
 
192
 
            Address[] unsent = sfex.getValidUnsentAddresses();
193
 
            if (unsent != null)
194
 
            {
195
 
                log.info("Unsent Addresses");
196
 
                for (int i=0; i<unsent.length; i++)
197
 
                    log.info("  "+unsent[i]);
198
 
            }
199
 
 
200
 
            Address[] sent = sfex.getValidSentAddresses();
201
 
            if (sent != null)
202
 
            {
203
 
                log.info("Sent Addresses");
204
 
                for (int i=0; i<sent.length; i++)
205
 
                    log.info("  "+sent[i]);
206
 
            }
207
 
        }
208
 
 
209
 
        if (ex instanceof MessagingException)
210
 
        {
211
 
            printMessagingException(((MessagingException) ex).getNextException());
212
 
        }
213
 
    }
214
 
 
215
 
    /**
216
 
     * E-Mail - the from username
217
 
     */
218
 
    public static boolean getMailDebug()
219
 
    {
220
 
        return mail_debug;
221
 
    }
222
 
 
223
 
    /**
224
 
     * E-Mail - the from username
225
 
     */
226
 
    public static void setMailDebug(boolean value)
227
 
    {
228
 
        mail_debug = value;
229
 
    }
230
 
 
231
 
    /**
232
 
     * E-Mail - the from address
233
 
     */
234
 
    public static String getMailFromAddr()
235
 
    {
236
 
        return mail_fromaddr;
237
 
    }
238
 
 
239
 
    /**
240
 
     * E-Mail - the from address
241
 
     */
242
 
    public static void setMailFromAddr(String value)
243
 
    {
244
 
        mail_fromaddr = value;
245
 
    }
246
 
 
247
 
    /**
248
 
     * E-Mail - the to address
249
 
     */
250
 
    public static String getMailToAddr()
251
 
    {
252
 
        return mail_toaddr;
253
 
    }
254
 
 
255
 
    /**
256
 
     * E-Mail - the to address
257
 
     */
258
 
    public static void setMailToAddr(String value)
259
 
    {
260
 
        mail_toaddr = value;
261
 
    }
262
 
 
263
 
    /**
264
 
     * E-Mail - the server name
265
 
     */
266
 
    public static String getMailServer()
267
 
    {
268
 
        return mail_server;
269
 
    }
270
 
 
271
 
    /**
272
 
     * E-Mail - the server name
273
 
     */
274
 
    public static void setMailServer(String value)
275
 
    {
276
 
        mail_server = value;
277
 
    }
278
 
 
279
 
    /**
280
 
     * E-Mail - the server port
281
 
     */
282
 
    public static int getMailPort()
283
 
    {
284
 
        return mail_port;
285
 
    }
286
 
 
287
 
    /**
288
 
     * E-Mail - the server port
289
 
     */
290
 
    public static void setMailPort(int value)
291
 
    {
292
 
        mail_port = value;
293
 
    }
294
 
 
295
 
    /** E-Mail - the from username */
296
 
    private static boolean mail_debug = false;
297
 
 
298
 
    /** E-Mail - the from address */
299
 
    private static String mail_fromaddr = "source@nowhere.com";
300
 
 
301
 
    /** E-Mail - the to address */
302
 
    private static String mail_toaddr = "dest@nowhere.com";
303
 
 
304
 
    /** E-Mail - the server name */
305
 
    private static String mail_server = "smtp";
306
 
 
307
 
    /** E-Mail - the server port */
308
 
    private static int mail_port = 25;
309
 
 
310
 
    /** The listener that pops up the ExceptionPanes */
311
 
    private static MailCaptureListener li = null;
312
 
 
313
 
    /** The log stream */
314
 
    protected static Logger log = Logger.getLogger(MailCaptureListener.class);
315
 
}