View Javadoc

1   /*
2   Copyright (c) 2005, CodeSmarts
3    All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are
7   met:
8   	* 	Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
10  	* 	Redistributions in binary form must reproduce the above
11  copyright notice, this list of conditions and the following disclaimer
12  in the documentation and/or other materials provided with the
13  distribution.
14  	* 	Neither the name of the CodeSmarts nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17  
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  
31  package net.codesmarts.log4j;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  import net.codesmarts.log4j.AbstractEmailBugReportAppender;
36  import org.apache.log4j.spi.LoggingEvent;
37  
38  /***
39   *
40   * @author Fred McCann
41   */
42  public class SMSBugReportAppender extends AbstractEmailBugReportAppender {
43      
44      private String subject;
45      
46      /***
47       * Sends the first thresholdPriority event message as the message
48       * @see net.codesmarts.log4j.AbstractEmailBugReportAppender#getMessage(net.codesmarts.log4j.BugReport)
49       */
50      public String getMessage(BugReport report) {
51          String message = null;
52  
53          List events = report.getEvents();
54          LoggingEvent event = null;
55          
56          Iterator i = events.iterator();
57  
58          while (i.hasNext() && message==null) {
59              event = (LoggingEvent)i.next();
60              if (event.getLevel().isGreaterOrEqual(this.getThresholdPriority()))
61                  message = event.getMessage().toString();
62          }
63          
64          return message;
65      }
66      
67      
68      
69      /***
70       * @see net.codesmarts.log4j.AbstractBugReportAppender#init()
71       */
72      public void init() {
73          super.init();
74          
75          if (subject==null) {
76              getErrorHandler().error("Must set \"subject\" for "+this.getClass().getName());
77              close();
78          }        
79      }
80      
81      /***
82       * Set the subject for all SMS messages
83       * @param subject The subject to set.
84       */
85      public void setSubject(String subject) {
86          this.subject = subject;
87      }
88      
89      /***
90       * @see net.codesmarts.log4j.AbstractEmailBugReportAppender#getSubject(net.codesmarts.log4j.BugReport)
91       */
92      public String getSubject(BugReport report) {
93          return subject;
94      }
95      
96      /***
97       * @see org.apache.log4j.Appender#requiresLayout()
98       */
99      public boolean requiresLayout() {
100         return false;
101     }    
102 }