logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Moore <jim.mo...@veritas.com>
Subject [PATCH] SMTPAppender.java
Date Fri, 13 Apr 2001 15:24:29 GMT
The following patch simply makes SMTPAppender extensible.  The patch is
against rev 1.22 in CVS.

Specific changes:
  * Private members are declared to be private.
  * Private members that is needed by subclasses (like the CyclicBuffer,
TriggerEventEvaluator and Message objects) now have protected methods for
setting and getting those object.
  * The Session member was removed as a member because it's only used in one
method, and therefore made local.

-Jim Moore

====================================
--- SMTPAppender.old	Fri Apr 13 10:45:00 2001
+++ SMTPAppender.java	Fri Apr 13 10:04:24 2001
@@ -46,16 +46,15 @@
    @author Ceki G&uuml;lc&uuml;
    @since 1.0 */
 public class SMTPAppender extends AppenderSkeleton {
-  String to;
-  String from;
-  String subject;
-  String smtpHost;
-  int bufferSize = 512;
-
-  CyclicBuffer cb = new CyclicBuffer(bufferSize);
-  Session session;
-  Message msg;
-  boolean locationInfo = false;
+  private String to;
+  private String from;
+  private String subject;
+  private String smtpHost;
+  private int bufferSize = 512;
+
+  private CyclicBuffer _cb;
+  private Message _msg;
+  private boolean locationInfo = false;
 
 
  /**
@@ -164,6 +163,7 @@
   public 
   SMTPAppender(TriggeringEventEvaluator evaluator) {
     this.evaluator = evaluator;
+    setCyclicBuffer(new CyclicBuffer(bufferSize));
   }
 
  /**
@@ -192,9 +192,9 @@
     if (smtpHost != null)
       props.put("mail.smtp.host", smtpHost);
 
-     session = Session.getDefaultInstance(props, null);
+     Session session = Session.getDefaultInstance(props, null);
      //session.setDebug(true);
-     msg = new MimeMessage(session);
+     MimeMessage msg = new MimeMessage(session);
      
      try {
        if (from != null)
@@ -208,6 +208,8 @@
      } catch(MessagingException e) {
        LogLog.error("Could not activate SMTPAppender options.", e );
      }
+
+     setMessage(msg);
   }
   
   /**
@@ -226,7 +228,7 @@
     if(locationInfo) {
       event.getLocationInformation();	
     }
-    cb.add(event);    
+    getCyclicBuffer().add(event);
     if(evaluator.isTriggeringEvent(event)) {
       sendBuffer();
     }
@@ -240,7 +242,7 @@
      value <code>false</code> is returned. */
   protected
   boolean checkEntryConditions() {
-    if(this.msg == null) {
+    if(getMessage() == null) {
       errorHandler.error("Message object not configured.");
       return false;
     }
@@ -318,6 +320,7 @@
       String t = layout.getHeader();
       if(t != null)
 	sbuf.append(t);
+      CyclicBuffer cb = getCyclicBuffer();
       int len =  cb.length(); 
       for(int i = 0; i < len; i++) {
 	//sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
@@ -339,6 +342,7 @@
 
       Multipart mp = new MimeMultipart();
       mp.addBodyPart(part);
+      Message msg = getMessage();
       msg.setContent(mp);
 
       msg.setSentDate(new Date());
@@ -348,7 +352,38 @@
     }
   }
   
+  /**
+   * Sets the CyclicBuffer this will use for keeping track
+   *   of {@link LoggingEvent}s.
+   * 
+   * @param cb     the CyclicBuffer to store the events in
+   */
+  protected void setCyclicBuffer(CyclicBuffer cb) {
+    _cb = cb;
+  }
+
+  /**
+   * Returns the CyclicBuffer the {@link LoggingEvent}s are stored in.
+   */
+  protected CyclicBuffer getCyclicBuffer() {
+    return _cb;
+  }
+
+  /**
+   * Sets the Message object used for sending the emails.
+   * 
+   * @param msg    the Message object to use for the emails.
+   */
+  protected void setMessage(Message msg) {
+    _msg = msg;
+  }
 
+  /**
+   * Gets the Message object used for sending the emails.
+   */
+  protected Message getMessage() {
+    return _msg;
+  }
 
   /**
      Returns value of the <b>EvaluatorClass</b> option.
@@ -359,6 +394,14 @@
   }
   
   /**
+     Returns the TriggeringEventEvaluator this uses.
+   */
+  protected
+  TriggeringEventEvaluator getEvaluator() {
+    return evaluator;
+  }
+  
+  /**
      Returns value of the <b>From</b> option.
    */
   public
@@ -398,7 +441,7 @@
                                                        evaluator);    
     } else if (option.equals(BUFFER_SIZE_OPTION)) {
       bufferSize = OptionConverter.toInt(value, bufferSize);    
-      cb.resize(bufferSize);
+      getCyclicBuffer().resize(bufferSize);
     } else if (option.equals(LOCATION_INFO_OPTION))
       locationInfo = OptionConverter.toBoolean(value, locationInfo);
   }
@@ -435,7 +478,7 @@
   public
   void setBufferSize(int bufferSize) {
     this.bufferSize = bufferSize;
-    cb.resize(bufferSize);
+    getCyclicBuffer().resize(bufferSize);
   }
   
   /**

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message