[logback-dev] svn commit: r922 - in logback/trunk: logback-access/src/main/java/ch/qos/logback/access/net logback-access/src/test/java/ch/qos/logback/access/net logback-access/src/test/java/ch/qos/logback/access/pattern/helpers logback-classic/src/main/java/ch/qos/logback/classic/net logback-classic/src/test/java/ch/qos/logback/classic/net logback-core/src/main/java/ch/qos/logback/core/net logback-examples/src/main/java/chapter4/mail logback-site/src/site/xdocTemplates/manual

noreply.seb at qos.ch noreply.seb at qos.ch
Wed Nov 15 17:18:02 CET 2006


Author: seb
Date: Wed Nov 15 17:18:02 2006
New Revision: 922

Added:
   logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedEvaluator.java
      - copied, changed from r921, /logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedTP.java
Removed:
   logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedTP.java
Modified:
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java
   logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
   logback/trunk/logback-examples/src/main/java/chapter4/mail/EMail.java
   logback/trunk/logback-examples/src/main/java/chapter4/mail/mail3.xml
   logback/trunk/logback-site/src/site/xdocTemplates/manual/appenders.xml

Log:
changed SMTPAppender triggering policy to evluator, updated examples, doc and tests

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java	Wed Nov 15 17:18:02 2006
@@ -1,39 +1,24 @@
 package ch.qos.logback.access.net;
 
 import ch.qos.logback.access.boolex.JaninoEventEvaluator;
-import ch.qos.logback.core.Context;
-import ch.qos.logback.core.boolex.EvaluationException;
 
 public class DefaultSMTPEvaluator extends JaninoEventEvaluator {
   
-  private static final long ONE_DAY = 1000*60*60*24;
-  private long LAST_TRIGGER_DATE = 0L;
+  private String url;
   
-  public DefaultSMTPEvaluator(Context context) {
-    this.context = context;
-    setName("SMTPAppender's default event evaluator");
-    setExpression("event.getStatusCode() >= 500");
+  public DefaultSMTPEvaluator() { 
   }
- 
-  /**
-   * Is this <code>event</code> the e-mail triggering event?
-   * 
-   * <p>
-   * This method returns <code>true</code>, if the event is
-   * evaluated to true. Otherwise it returns <code>false</code>.
-   * 
-   * Once an email is sent, the next one will not be sent unless a certain amount
-   * of time passed.
-   */
+  
+  public void setUrl(String url) {
+    this.url = url;
+  }
+  
   @Override
-  public boolean evaluate(Object event) throws EvaluationException {
-    
-    if (super.evaluate(event)) {
-      if (System.currentTimeMillis() >= LAST_TRIGGER_DATE + ONE_DAY) {
-        LAST_TRIGGER_DATE = System.currentTimeMillis();
-        return true;
-      } 
+  public void start() {
+    if (getContext() == null) {
+      System.out.println("CONTEXT NULL");
     }
-    return false;
+    setExpression("request.getRequestURL().toString().contains(\"" + url + "\")");
+    super.start();
   }
 }

Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java	(original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java	Wed Nov 15 17:18:02 2006
@@ -51,8 +51,6 @@
    * ERROR or higher.
    */
   public SMTPAppender() {
-    DefaultSMTPEvaluator evaluator = new DefaultSMTPEvaluator(getContext());
-    setEventEvaluator(evaluator);
   }
 
   /**
@@ -62,12 +60,6 @@
   public SMTPAppender(EventEvaluator evaluator) {
     this.eventEvaluator = evaluator;
   }
-  
-  @Override
-  public void start() {
-    ((DefaultSMTPEvaluator) eventEvaluator).start();
-    super.start();
-  }
 
   /**
    * Perform SMTPAppender specific appending actions, mainly adding the event to

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java	Wed Nov 15 17:18:02 2006
@@ -11,7 +11,7 @@
 
 public class DefaultSMTPEvaluatorTest extends TestCase {
 
-  
+  final String expectedURL = "testUrl";
   Context context = new ContextBase();
   DefaultSMTPEvaluator evaluator;    
   DummyRequest request;
@@ -19,7 +19,9 @@
   DummyServerAdapter serverAdapter;
   
   public void setUp() throws Exception {
-    evaluator = new DefaultSMTPEvaluator(context);
+    evaluator = new DefaultSMTPEvaluator();
+    evaluator.setContext(context);
+    evaluator.setUrl(expectedURL);
     evaluator.start();
     request = new DummyRequest();
     response = new DummyResponse();
@@ -37,25 +39,14 @@
   }
   
   public void testExpectFalseBecauseOfStatus() throws EvaluationException {
+    request.setRequestUrl("test");
     AccessEvent ae = new AccessEvent(request, response, serverAdapter);
-    response.setStatus(100);
     assertFalse(evaluator.evaluate(ae));
   }
   
   public void testExpectTrue() throws EvaluationException {
+    request.setRequestUrl(expectedURL);    
     AccessEvent ae = new AccessEvent(request, response, serverAdapter);
-    response.setStatus(500);
     assertTrue(evaluator.evaluate(ae));    
   }
-  
-  public void testExpectFalseBecauseOfTime() throws EvaluationException {
-    AccessEvent ae = new AccessEvent(request, response, serverAdapter);
-    response.setStatus(500);
-    //this returns true and sets the time
-    evaluator.evaluate(ae);
-    //this should return false because 24h have not passed yet.
-    assertFalse(evaluator.evaluate(ae));
-  }
-  
-  
 }

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyRequest.java	Wed Nov 15 17:18:02 2006
@@ -18,6 +18,7 @@
 public class DummyRequest implements HttpServletRequest {
 
   Hashtable<String, String> headerNames;
+  String url;
 
   public DummyRequest() {
     headerNames = new Hashtable<String, String>();
@@ -83,7 +84,7 @@
   }
 
   public StringBuffer getRequestURL() {
-    return null;
+    return new StringBuffer(url);
   }
 
   public String getRequestedSessionId() {
@@ -243,4 +244,8 @@
   public void setCharacterEncoding(String arg0)
       throws UnsupportedEncodingException {
   }
+  
+  public void setRequestUrl(String url) {
+    this.url = url;
+  }
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	Wed Nov 15 17:18:02 2006
@@ -57,12 +57,6 @@
   public SMTPAppender(EventEvaluator eventEvaluator) {
     this.eventEvaluator = eventEvaluator;
   }
-  
-  @Override
-  public void start() {
-    ((JaninoEventEvaluator) eventEvaluator).start();
-    super.start();
-  }
 
   /**
    * Perform SMTPAppender specific appending actions, mainly adding the event to

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java	Wed Nov 15 17:18:02 2006
@@ -75,7 +75,7 @@
   }
 
   public void setTriggeringPolicy() {
-    appender.setEventEvaluator(null);
+    appender.setEvaluator(null);
     appender.checkEntryConditions();
     assertEquals(1, appender.getContext().getStatusManager().getCount());
   }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	Wed Nov 15 17:18:02 2006
@@ -87,8 +87,6 @@
       
       subjectLayout = makeSubjectLayout(subjectStr);
       
-      
-      
       started = true;
 
     } catch (MessagingException e) {
@@ -284,7 +282,7 @@
    * interface. A corresponding object will be instantiated and assigned as the
    * event evaluator for the SMTPAppender.
    */
-  public void setEventEvaluator(EventEvaluator eventEvaluator) {
+  public void setEvaluator(EventEvaluator eventEvaluator) {
     this.eventEvaluator = eventEvaluator;
   }
 

Copied: logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedEvaluator.java (from r921, /logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedTP.java)
==============================================================================
--- /logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedTP.java	(original)
+++ logback/trunk/logback-examples/src/main/java/chapter4/mail/CounterBasedEvaluator.java	Wed Nov 15 17:18:02 2006
@@ -10,20 +10,22 @@
 
 package chapter4.mail;
 
-import java.io.File;
-import ch.qos.logback.core.rolling.TriggeringPolicyBase;
-
+import ch.qos.logback.core.boolex.EvaluationException;
+import ch.qos.logback.core.boolex.EventEvaluator;
+import ch.qos.logback.core.spi.ContextAwareBase;
 
 /**
- *  A simple TriggeringPolicy implementation that triggers
- *  email transmission after 1024 events regardless of event level.
- * */
-public class CounterBasedTP extends TriggeringPolicyBase {
-  boolean started;
+ * A simple EventEvaluator implementation that triggers email transmission after
+ * 1024 events regardless of event level.
+ */
+public class CounterBasedEvaluator extends ContextAwareBase implements EventEvaluator {
+
   static int LIMIT = 1024;
   int counter = 0;
+  String name;
 
-  public boolean isTriggeringEvent(File activeFile, Object event) {
+  public boolean evaluate(Object event) throws NullPointerException,
+      EvaluationException {
     counter++;
 
     if (counter == LIMIT) {
@@ -34,4 +36,12 @@
       return false;
     }
   }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
 }

Modified: logback/trunk/logback-examples/src/main/java/chapter4/mail/EMail.java
==============================================================================
--- logback/trunk/logback-examples/src/main/java/chapter4/mail/EMail.java	(original)
+++ logback/trunk/logback-examples/src/main/java/chapter4/mail/EMail.java	Wed Nov 15 17:18:02 2006
@@ -15,6 +15,7 @@
 
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.util.StatusPrinter;
 
 
 
@@ -38,7 +39,7 @@
       lc.shutdownAndReset();
       configurator.setContext(lc);
       configurator.doConfigure(configFile);
-      //StatusPrinter.print(lc);
+      StatusPrinter.print(lc);
     }
 
     Logger logger = LoggerFactory.getLogger(EMail.class);

Modified: logback/trunk/logback-examples/src/main/java/chapter4/mail/mail3.xml
==============================================================================
--- logback/trunk/logback-examples/src/main/java/chapter4/mail/mail3.xml	(original)
+++ logback/trunk/logback-examples/src/main/java/chapter4/mail/mail3.xml	Wed Nov 15 17:18:02 2006
@@ -7,7 +7,7 @@
 
 <configuration>
   <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
-    <TriggeringPolicy class="chapter4.mail.CounterBasedTP" />
+    <Evaluator class="chapter4.mail.CounterBasedEvaluator" />
     <BufferSize>1050</BufferSize>
     <SMTPHost>${smtpHost}</SMTPHost>
     <To>${to}</To>

Modified: logback/trunk/logback-site/src/site/xdocTemplates/manual/appenders.xml
==============================================================================
--- logback/trunk/logback-site/src/site/xdocTemplates/manual/appenders.xml	(original)
+++ logback/trunk/logback-site/src/site/xdocTemplates/manual/appenders.xml	Wed Nov 15 17:18:02 2006
@@ -1,4 +1,3 @@
-<document>
 	<!-- 
 		
 		Warning: do not use any auto-format function on this file.
@@ -1534,20 +1533,20 @@
 			</td>
 		</tr>
 		<tr>
-			<td><b><span class="option">TriggeringPolicy</span></b></td>
+			<td><b><span class="option">Evaluator</span></b></td>
 			<td><code>String</code></td>
 			<td>
-				<p>This option is declared by creating a new <code>&lt;TriggeringPolicy/></code>
+				<p>This option is declared by creating a new <code>&lt;EventEvaluator/></code>
 				element. The name of the class that the user wishes to use as the
-				<code>SMTPAppender</code>'s <code>TriggeringPolicy</code> can be given
+				<code>SMTPAppender</code>'s <code>Evaluator</code> can be given
 				by adding an attribute to the newly created element.
 				</p>
 				<p>
-				More details about the use of triggering policies with <code>SMTPAppender</code>
+				More details about the use of event evaluators with <code>SMTPAppender</code>
 				follow further down this document.
 				</p>
 				<p>In the absence of this option, 
-				<code>SMTPAppender</code> is assigned a default triggering policy which triggers 
+				<code>SMTPAppender</code> is assigned a default event evaluator which triggers 
 				email transmission as a response to any event of level <em>ERROR</em> or higher.  
 				</p>
 			</td>
@@ -1657,32 +1656,36 @@
 			By default, the <code>SMTPAppender</code> will initiate the transmission of an email 
 			message as a response to an event of level <em>ERROR</em> or higher. 
 			However, it is possible to override this default behavior by providing a custom 
-			implementation of the <code>TriggeringPolicy</code> interface. 
+			implementation of the <code>EventEvaluator</code> interface. 
 		</p>
 		
 		<p>
 			The <code>SMTPAppender</code> submits each incoming event to its evaluator 
-			by calling <code>isTriggeringEvent()</code> method in order to check whether 
+			by calling <code>evaluate()</code> method in order to check whether 
 			the event should trigger an email or just be placed in the cyclic buffer. 
+			When the evaluator gives a positive answer to its evaluation, an email is sent.
 			The <code>SMTPAppender</code> contains one and only one evaluator object. 
 			This object may possess its own state. For illustrative purposes, 
-			the <code>CounterBasedTP</code> class listed next, implements a triggering policy whereby 
-			every 1024th event triggers an email message.
+			the <code>CounterBasedEvaluator</code> class listed next, implements an
+			event evaluator whereby every 1024th event triggers an email message.
 		</p>
 
-<em>Example 4.9: A <code>TriggeringPolicy</code> implementation
-that triggers every 1024th event (<a href="../xref/chapter4/mail/CounterBasedTP.html">logback-examples/src/main/java/chapter4/mail/CounterBasedTP.java</a>)</em>
+<em>Example 4.9: A <code>EventEvaluator</code> implementation
+that evaluates to <code>true</code> every 1024th event (<a href="../xref/chapter4/mail/CounterBasedEvaluator.html">logback-examples/src/main/java/chapter4/mail/CounterBasedEvaluator.java</a>)</em>
 <div class="source"><pre>package chapter4.mail;
 
-import java.io.File;
-import ch.qos.logback.core.rolling.TriggeringPolicyBase;
+import ch.qos.logback.core.boolex.EvaluationException;
+import ch.qos.logback.core.boolex.EventEvaluator;
+import ch.qos.logback.core.spi.ContextAwareBase;
+
+public class CounterBasedEvaluator extends ContextAwareBase implements EventEvaluator {
 
-public class CounterBasedTP extends TriggeringPolicyBase {
-  boolean started;
   static int LIMIT = 1024;
   int counter = 0;
+  String name;
 
-  public boolean isTriggeringEvent(File activeFile, Object event) {
+  <b>public boolean evaluate(Object event) throws NullPointerException,
+      EvaluationException {
     counter++;
 
     if (counter == LIMIT) {
@@ -1692,30 +1695,38 @@
     } else {
       return false;
     }
+  }</b>
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
   }
 }</pre></div>
 
 		<p>
-			Note that this implementation extends <code>TriggeringPolicyBase</code> instead
-			of implementing <code>TriggeringPolicy</code>. This allows the user to concentrate
-			on the core functions of her <code>TriggeringPolicy</code> and let the base class
+			Note that this implementation extends <code>ContextAwareBase</code> and
+			implements <code>EventEvaluator</code>. This allows the user to concentrate
+			on the core functions of her <code>EventEvaluator</code> and let the base class
 			provide the common functionnality.
 		</p>
 
 		<p>
-			Setting the <span class="option">TriggeringPolicy</span> option of 
-			<code>SMTPAppender</code> instructs it to use a custom triggering policy. 
+			Setting the <span class="option">EventEvaluator</span> option of 
+			<code>SMTPAppender</code> instructs it to use a custom evaluator. 
 			The next configuration file attaches a <code>SMTPAppender</code> to the root logger. 
-			This appender has a buffer size of 2048 and uses a <code>CounterBasedTP</code> instance 
-			as its triggering policy.
+			This appender has a buffer size of 2048 and uses a <code>CounterBasedEvaluator</code> instance 
+			as its event evaluator.
 		</p>
 
 <em>Example 4.10: <code>SMTPAppender</code> with custom 
-<code>TriggeringPolicy</code> and buffer size (logback-examples/src/main/java/chapter4/mail/mail3.xml)</em>
+<code>Evaluator</code> and buffer size (logback-examples/src/main/java/chapter4/mail/mail3.xml)</em>
 
 <div class="source"><pre>&lt;configuration>
   &lt;appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
-    <b>&lt;TriggeringPolicy class="chapter4.mail.CounterBasedTP" /></b>
+    <b>&lt;Evaluator class="chapter4.mail.CounterBasedEvaluator" /></b>
     &lt;BufferSize>1050&lt;/BufferSize>
     &lt;SMTPHost>${smtpHost}&lt;/SMTPHost>
     &lt;To>${to}&lt;/To>



More information about the logback-dev mailing list