KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > MailLog_Helper


1 package com.protomatter.syslog.xml;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import com.protomatter.xml.*;
59 import com.protomatter.syslog.*;
60 import org.jdom.*;
61
62 /**
63  * XML configuration helper for <tt>MailLog</tt>.
64  */

65 public class MailLog_Helper
66 extends BasicLogger_Helper
67 {
68   /**
69    * Configure this logger given the XML element.
70    * The <tt>&lt;Logger&gt;</tt> element should look like this:<P>
71    *
72    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
73    * <TR><TD>
74    * <PRE><B>
75    *
76    * &lt;Logger class="com.protomatter.syslog.MailLog" &gt;
77    *
78    * <font color="#888888">&lt;!--
79    * Config params from {@link BasicLogger_Helper#configure(Object,Element) BasicLogger_Helper} can
80    * get inserted here.
81    * --&gt;</font>
82    *
83    * &lt;SubjectFormat class="<i>MessageSubjectFormatClass</i>" &gt;
84    * <font color="#888888">&lt;!-- message subject formatter elements --&gt;</font>
85    * &lt;/SubjectFormat&gt;
86    *
87    * &lt;workQueue&gt;<i>queueName</i>&lt;/workQueue&gt;
88    * &lt;server&gt;<i>hostname</i>&lt;/server&gt;
89    * &lt;port&gt;<i>port</i>&lt;/port&gt;
90    * &lt;html&gt;<i>true|false</i>&lt;/html&gt;
91    * &lt;to&gt;<i>to-address-list</i>&lt;/to&gt;
92    * &lt;cc&gt;<i>cc-address-list</i>&lt;/cc&gt;
93    * &lt;bcc&gt;<i>bcc-address-list</i>&lt;/bcc&gt;
94    * &lt;from&gt;<i>from-address</i>&lt;/from&gt;
95    * &lt;fromName&gt;<i>from-name</i>&lt;/fromName&gt;
96    * &lt;transportAgentName&gt;<i>ta-name</i>&lt;/transportAgentName&gt;
97    *
98    * &lt;/Logger&gt;
99    * </B></PRE>
100    * </TD></TR></TABLE><P>
101    *
102    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
103    * <TR CLASS="TableHeadingColor">
104    * <TD COLSPAN=3><B>Element</B></TD>
105    * </TR>
106    * <TR CLASS="TableHeadingColor">
107    * <TD><B>name</B></TD>
108    * <TD><B>value</B></TD>
109    * <TD><B>required</B></TD>
110    * </TR>
111    *
112    * <TR CLASS="TableRowColor">
113    * <TD VALIGN=TOP><TT>workQueue</TT></TD>
114    * <TD VALIGN=TOP>The symbolic name of a work queue
115    * to use for sending messages.
116    * </TD>
117    * <TD VALIGN=TOP>yes</TD>
118    * </TR>
119    *
120    * <TR CLASS="TableRowColor">
121    * <TD VALIGN=TOP><TT>server</TT></TD>
122    * <TD VALIGN=TOP>The SMTP server hostname or IP address.
123    * </TD>
124    * <TD VALIGN=TOP>yes</TD>
125    * </TR>
126    *
127    * <TR CLASS="TableRowColor">
128    * <TD VALIGN=TOP><TT>port</TT></TD>
129    * <TD VALIGN=TOP>The port number on the SMTP server to connect to.
130    * </TD>
131    * <TD VALIGN=TOP>no (default is <tt>25</tt>)</TD>
132    * </TR>
133    *
134    * <TR CLASS="TableRowColor">
135    * <TD VALIGN=TOP><TT>html</TT></TD>
136    * <TD VALIGN=TOP>Should the message be sent as a MIME message?
137    * <tt>true</tt> or <tt>false</tt>. If this is
138    * set to <tt>true</tt>, it is expected that the
139    * message formatter will actually produce HTML.
140    * </TD>
141    * <TD VALIGN=TOP>no (default is <tt>false</tt>)</TD>
142    * </TR>
143    *
144    * <TR CLASS="TableRowColor">
145    * <TD VALIGN=TOP><TT>to</TT></TD>
146    * <TD VALIGN=TOP>A comma-separated list of email addresses to send
147    * messages to.
148    * </TD>
149    * <TD VALIGN=TOP>yes</TD>
150    * </TR>
151    *
152    * <TR CLASS="TableRowColor">
153    * <TD VALIGN=TOP><TT>cc</TT></TD>
154    * <TD VALIGN=TOP>A comma-separated list of email addresses to
155    * include in the CC (carbon-copy) list.
156    * </TD>
157    * <TD VALIGN=TOP>no</TD>
158    * </TR>
159    *
160    * <TR CLASS="TableRowColor">
161    * <TD VALIGN=TOP><TT>bcc</TT></TD>
162    * <TD VALIGN=TOP>A comma-separated list of email addresses to
163    * include in the BCC (blind carbon-copy) list.
164    * </TD>
165    * <TD VALIGN=TOP>no</TD>
166    * </TR>
167    *
168    * <TR CLASS="TableRowColor">
169    * <TD VALIGN=TOP><TT>from</TT></TD>
170    * <TD VALIGN=TOP>The email address that the mail should appear
171    * to be from.
172    * </TD>
173    * <TD VALIGN=TOP>yes</TD>
174    * </TR>
175    *
176    * <TR CLASS="TableRowColor">
177    * <TD VALIGN=TOP><TT>fromName</TT></TD>
178    * <TD VALIGN=TOP>The name (not address) that the mail should
179    * appear to be from.
180    * </TD>
181    * <TD VALIGN=TOP>no</TD>
182    * </TR>
183    *
184    * <TR CLASS="TableRowColor">
185    * <TD VALIGN=TOP><TT>transportAgentName</TT></TD>
186    * <TD VALIGN=TOP>The transport agent name that this
187    * SMTP client identifies itself as. The default is
188    * "<TT>com.protomatter.syslog.SMTPMailTransport</TT>".
189    * </TD>
190    * <TD VALIGN=TOP>no</TD>
191    * </TR>
192    *
193    * </TABLE><P>
194    */

195   public void configure(Object JavaDoc o, Element e)
196   throws SyslogInitException
197   {
198     super.configure(o, e);
199
200     MailLog log = (MailLog)o;
201
202     String JavaDoc smtpServer = null;
203     int port = 25;
204
205     String JavaDoc fromName = "Syslog";
206     String JavaDoc fromAddress = null;
207     Vector to = new Vector();
208     Vector cc = new Vector();
209     Vector bcc = new Vector();
210     boolean html = false;
211
212     String JavaDoc sf = "SubjectFormat";
213     String JavaDoc classString = "class";
214
215     Element formatElement = e.getChild(sf, e.getNamespace());
216     if (formatElement != null)
217     {
218       String JavaDoc formatClass = formatElement.getAttributeValue(classString);
219       if (formatClass == null)
220       {
221         throw new IllegalArgumentException JavaDoc(
222           MessageFormat.format(
223             Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_ATTRIBUTE_MESSAGE),
224             new Object JavaDoc[] { classString, sf } ));
225       }
226       try
227       {
228         SyslogMailSubjectFormatter subjectFormat =
229           (SyslogMailSubjectFormatter)Class.forName(formatClass).newInstance();
230         XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(subjectFormat);
231         try
232         {
233           helper.configure(subjectFormat, formatElement);
234         }
235         catch (Exception JavaDoc x)
236         {
237           x.printStackTrace();
238           throw new IllegalArgumentException JavaDoc(MessageFormat.format(
239             Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_CONFIGURE_SUBJECT_FORMAT_MESSAGE),
240             new Object JavaDoc[] { formatClass }));
241         }
242       }
243       catch (Exception JavaDoc x)
244       {
245         x.printStackTrace();
246         throw new IllegalArgumentException JavaDoc(MessageFormat.format(
247           Syslog.getResourceString(MessageConstants.MAILLOG_CANNOT_CONFIGURE_SUBJECT_FORMAT_MESSAGE),
248           new Object JavaDoc[] { formatClass }));
249       }
250     }
251
252     String JavaDoc tmp = e.getChildTextTrim("server", e.getNamespace());
253     if (tmp != null)
254     {
255       log.setMailServer(tmp);
256     }
257     else
258     {
259       throw new IllegalArgumentException JavaDoc(
260         MessageFormat.format(
261           Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
262           new Object JavaDoc[] { "server" } ));
263     }
264
265     tmp = e.getChildTextTrim("workQueue", e.getNamespace());
266     if (tmp != null)
267     {
268       log.setWorkQueue(tmp);
269     }
270     else
271     {
272       throw new IllegalArgumentException JavaDoc(
273         MessageFormat.format(
274           Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
275           new Object JavaDoc[] { "workQueue" } ));
276     }
277
278     tmp = e.getChildTextTrim("port", e.getNamespace());
279     if (tmp != null)
280       log.setMailPort(Integer.parseInt(tmp));
281
282     tmp = e.getChildTextTrim("html", e.getNamespace());
283     log.setHTML("true".equalsIgnoreCase(tmp));
284
285     tmp = e.getChildTextTrim("from", e.getNamespace());
286     if (tmp != null)
287     {
288       log.setFromAddress(tmp);
289     }
290     else
291     {
292       throw new IllegalArgumentException JavaDoc(
293         MessageFormat.format(
294           Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
295           new Object JavaDoc[] { "from" } ));
296     }
297
298     log.setFromName(e.getChildTextTrim("fromName", e.getNamespace()));
299
300     tmp = e.getChildTextTrim("to", e.getNamespace());
301     if (tmp != null)
302     {
303       StringTokenizer st = new StringTokenizer(tmp, ", ");
304       while (st.hasMoreTokens())
305         to.add(st.nextToken());
306       log.setTo(to);
307     }
308
309     tmp = e.getChildTextTrim("cc", e.getNamespace());
310     if (tmp != null)
311     {
312       StringTokenizer st = new StringTokenizer(tmp, ", ");
313       while (st.hasMoreTokens())
314         cc.add(st.nextToken());
315       log.setCC(cc);
316     }
317
318     tmp = e.getChildTextTrim("bcc", e.getNamespace());
319     if (tmp != null)
320     {
321       StringTokenizer st = new StringTokenizer(tmp, ", ");
322       while (st.hasMoreTokens())
323         bcc.add(st.nextToken());
324       log.setBCC(bcc);
325     }
326
327     if (to.size() == 0)
328     {
329       throw new IllegalArgumentException JavaDoc(
330         MessageFormat.format(
331           Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
332           new Object JavaDoc[] { "to" } ));
333     }
334
335     tmp = e.getChildTextTrim("transportAgentName", e.getNamespace());
336     if (tmp != null)
337     {
338       log.setTransportAgent(tmp);
339     }
340
341     log.init();
342   }
343
344   public Element getConfiguration(Object JavaDoc o, Element element)
345   {
346     Element e = super.getConfiguration(o, element);
347
348     MailLog log = (MailLog)o;
349
350     Vector to = log.getTo();
351     Vector cc = log.getCC();
352     Vector bcc = log.getBCC();
353
354     e.getChildren().add((new Element("workQueue"))
355       .setText(log.getWorkQueue()));
356     e.getChildren().add((new Element("server"))
357       .setText(log.getMailServer()));
358     e.getChildren().add((new Element("port"))
359       .setText(String.valueOf(log.getMailPort())));
360     e.getChildren().add((new Element("html"))
361       .setText(String.valueOf(log.getHTML())));
362     e.getChildren().add((new Element("from"))
363       .setText(log.getFromAddress()));
364
365     if (log.getFromName() != null)
366       e.getChildren().add((new Element("fromName")).setText(log.getFromName()));
367
368     e.getChildren().add((new Element("to")).setText(assembleAddressList(to)));
369
370     if (cc.size() > 0)
371       e.getChildren().add((new Element("cc")).setText(assembleAddressList(cc)));
372     if (bcc.size() > 0)
373       e.getChildren().add((new Element("bcc")).setText(assembleAddressList(bcc)));
374
375     Object JavaDoc formatClass = log.getSubjectFormatter();
376     Element formatElement = new Element("SubjectFormat");
377     formatElement.setAttribute("class", formatClass.getClass().getName());
378
379     try
380     {
381       XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(formatClass);
382       formatElement = helper.getConfiguration(formatClass, formatElement);
383       e.getChildren().add(formatElement);
384     }
385     catch (Exception JavaDoc x)
386     {
387       x.printStackTrace();
388     }
389
390     e.getChildren().add((new Element("transportAgentName")).setText(log.getTransportAgent()));
391
392     return e;
393   }
394
395   private String JavaDoc assembleAddressList(Vector list)
396   {
397     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
398     Enumeration e = list.elements();
399     while (e.hasMoreElements())
400     {
401       b.append(e.nextElement());
402       if (e.hasMoreElements())
403         b.append(", ");
404     }
405     return b.toString();
406   }
407 }
408
Popular Tags