Author: carnold
Date: Tue Aug 21 22:28:34 2007
New Revision: 568424

URL: http://svn.apache.org/viewvc?rev=568424&view=rev
Log:
Bug 32582: Add configurable triggeringPolicy for SMTPAppender

Added:
    logging/log4j/trunk/tests/input/xml/smtpAppender1.xml
    
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SMTPAppenderTest.java
Modified:
    logging/log4j/trunk/pom.xml
    logging/log4j/trunk/src/changes/changes.xml
    logging/log4j/trunk/src/main/java/org/apache/log4j/net/SMTPAppender.java
    logging/log4j/trunk/tests/build.xml
    logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java

Modified: logging/log4j/trunk/pom.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/pom.xml?rev=568424&r1=568423&r2=568424&view=diff
==============================================================================
--- logging/log4j/trunk/pom.xml (original)
+++ logging/log4j/trunk/pom.xml Tue Aug 21 22:28:34 2007
@@ -210,7 +210,9 @@
                          <ant dir="tests" target="runAll">
                                <property name="junit.jar" 
location="${user.home}/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar" />
                                <property name="jakarta.oro.jar" 
location="${user.home}/.m2/repository/oro/oro/2.0.8/oro-2.0.8.jar" />
-                        <property name="log4j.jar" location="target/classes" />
+                <property name="javamail.jar"  
location="${user.home}/.m2/repository/javax/mail/mail/1.4/mail-1.4.jar"/>
+                <property name="activation.jar" 
location="${user.home}/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar"/>
+                <property name="log4j.jar" location="target/classes" />
                                <property name="project.lib.home" 
location="target" />
                     </ant> 
                 </tasks>

Modified: logging/log4j/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/src/changes/changes.xml?rev=568424&r1=568423&r2=568424&view=diff
==============================================================================
--- logging/log4j/trunk/src/changes/changes.xml (original)
+++ logging/log4j/trunk/src/changes/changes.xml Tue Aug 21 22:28:34 2007
@@ -22,6 +22,7 @@
   <body>
   
     <release version="1.2.15" date="2007-06-27" description="SyslogAppender 
enhancements, NTEventLogAppender and Maven build.">
+       <action action="add" issue="32572">Add configuration triggeringPolicy 
for SMTPAppender.</action> 
        <action action="fix" issue="43181">NullPointerException in MDC on 
Tomcat reload.</action>
         <action action="fix" issue="34874">Notice to use UTF-8 or UTF-16 
encoding added to XML and HTMLLayout javadoc.</action>
        <action action="fix" issue="34875">XML and HTMLLayout do not always 
escape special characters.</action>

Modified: 
logging/log4j/trunk/src/main/java/org/apache/log4j/net/SMTPAppender.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/net/SMTPAppender.java?rev=568424&r1=568423&r2=568424&view=diff
==============================================================================
--- logging/log4j/trunk/src/main/java/org/apache/log4j/net/SMTPAppender.java 
(original)
+++ logging/log4j/trunk/src/main/java/org/apache/log4j/net/SMTPAppender.java 
Tue Aug 21 22:28:34 2007
@@ -20,12 +20,16 @@
 import org.apache.log4j.AppenderSkeleton;
 import org.apache.log4j.Level;
 import org.apache.log4j.Layout;
+import org.apache.log4j.xml.UnrecognizedElementHandler;
 import org.apache.log4j.helpers.CyclicBuffer;
 import org.apache.log4j.helpers.OptionConverter;
 import org.apache.log4j.helpers.LogLog;
 import org.apache.log4j.spi.LoggingEvent;
 import org.apache.log4j.spi.ErrorCode;
 import org.apache.log4j.spi.TriggeringEventEvaluator;
+import org.apache.log4j.spi.OptionHandler;
+import org.w3c.dom.Element;
+
 import java.util.Properties;
 import java.util.Date;
 
@@ -53,9 +57,18 @@
    keeps memory requirements at a reasonable level while still
    delivering useful application context.
 
+   By default, an email message will be sent when an ERROR or higher
+   severity message is appended.  The triggering criteria can be
+   modified by setting the evaluatorClass property with the name
+   of a class implementing TriggeringEventEvaluator, setting the evaluator
+   property with an instance of TriggeringEventEvaluator or
+   nesting a triggeringPolicy element where the specified
+   class implements TriggeringEventEvaluator.
+
    @author Ceki G&uuml;lc&uuml;
    @since 1.0 */
-public class SMTPAppender extends AppenderSkeleton {
+public class SMTPAppender extends AppenderSkeleton
+        implements UnrecognizedElementHandler {
   private String to;
   /**
    * Comma separated list of cc recipients.
@@ -116,6 +129,10 @@
      } catch(MessagingException e) {
        LogLog.error("Could not activate SMTPAppender options.", e );
      }
+
+     if (evaluator instanceof OptionHandler) {
+         ((OptionHandler) evaluator).activateOptions();
+     }
   }
   
   /**
@@ -533,6 +550,44 @@
   public boolean getSMTPDebug() {
     return smtpDebug;
   }
+
+    /**
+     * Sets triggering evaluator.
+     * @param trigger triggering event evaluator.
+     * @since 1.2.15
+     */
+  public final void setEvaluator(final TriggeringEventEvaluator trigger) {
+      if (trigger == null) {
+          throw new NullPointerException("trigger");
+      }
+      this.evaluator = trigger;
+  }
+
+    /**
+     * Get triggering evaluator.
+     * @return triggering event evaluator.
+     * @since 1.2.15
+     */
+  public final TriggeringEventEvaluator getEvaluator() {
+      return evaluator;
+  }
+
+  /** [EMAIL PROTECTED] */
+  public boolean parseUnrecognizedElement(final Element element,
+                                          final Properties props) throws 
Exception {
+      if ("triggeringPolicy".equals(element.getNodeName())) {
+          Object triggerPolicy =
+                  org.apache.log4j.xml.DOMConfigurator.parseElement(
+                          element, props, TriggeringEventEvaluator.class);
+          if (triggerPolicy instanceof TriggeringEventEvaluator) {
+              setEvaluator((TriggeringEventEvaluator) triggerPolicy);
+          }
+          return true;
+      }
+
+      return false;
+  }
+
 }
 
 class DefaultEvaluator implements TriggeringEventEvaluator {

Modified: logging/log4j/trunk/tests/build.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/build.xml?rev=568424&r1=568423&r2=568424&view=diff
==============================================================================
--- logging/log4j/trunk/tests/build.xml (original)
+++ logging/log4j/trunk/tests/build.xml Tue Aug 21 22:28:34 2007
@@ -34,6 +34,8 @@
   <property name="jakarta.oro.jar" 
location="${m2_repo}/oro/oro/${oro.version}/oro-${oro.version}.jar"/>
   <property name="checkstyle.version" value="4.1"/>
   <property name="checkstyle.jar" 
location="${m2_repo}/checkstyle/checkstyle/${checkstyle.version}/checkstyle-${checkstyle.version}.jar"/>
+  <property name="javamail.jar" 
location="${m2_repo}/javax/mail/mail/1.4/mail-1.4.jar"/>
+  <property name="activation.jar" 
location="${m2_repo}/javax/activation/activation/1.1/activation-1.1.jar"/>
 
 
   <!-- Read the system environment variables and stores them in properties, -->
@@ -65,6 +67,8 @@
     <pathelement location="${jaxp.parser.jar}"/>
     <pathelement location="${clover.jar}"/>
     <pathelement location="${junit.jar}"/>
+    <pathelement location="${javamail.jar}"/>
+    <pathelement location="${activation.jar}"/>
     <pathelement path="${java.class.path}"/>
   </path>
 

Added: logging/log4j/trunk/tests/input/xml/smtpAppender1.xml
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/input/xml/smtpAppender1.xml?rev=568424&view=auto
==============================================================================
--- logging/log4j/trunk/tests/input/xml/smtpAppender1.xml (added)
+++ logging/log4j/trunk/tests/input/xml/smtpAppender1.xml Tue Aug 21 22:28:34 
2007
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/";>
+  <appender name="A1" class="org.apache.log4j.net.SMTPAppender">
+
+    <param name="from"   value="[EMAIL PROTECTED]" />
+    <param name="to" value="[EMAIL PROTECTED]" />
+    <param name="subject" value="Test message" />
+    <param name="SMTPHost" value="localhost"/>
+    <triggeringPolicy 
class="org.apache.log4j.net.SMTPAppenderTest$MockTriggeringEventEvaluator"/>
+
+    <layout class="org.apache.log4j.PatternLayout">
+      <param name="ConversionPattern" value="%-5p %c{2} - %m%n"/>
+    </layout>
+  </appender>
+
+  <root>
+    <priority value ="trace" />
+    <appender-ref ref="A1" />
+  </root>
+
+</log4j:configuration>

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java?rev=568424&r1=568423&r2=568424&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java 
(original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java Tue 
Aug 21 22:28:34 2007
@@ -50,6 +50,7 @@
         s.addTestSuite(org.apache.log4j.spi.ThrowableInformationTest.class);
         s.addTestSuite(org.apache.log4j.spi.LocationInfoTest.class);
         s.addTestSuite(org.apache.log4j.PropertyConfiguratorTest.class);
+        s.addTestSuite(org.apache.log4j.net.SMTPAppenderTest.class);
         return s;
     }
 }

Added: 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SMTPAppenderTest.java
URL: 
http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SMTPAppenderTest.java?rev=568424&view=auto
==============================================================================
--- 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SMTPAppenderTest.java 
(added)
+++ 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SMTPAppenderTest.java 
Tue Aug 21 22:28:34 2007
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j.net;
+
+import junit.framework.TestCase;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.spi.TriggeringEventEvaluator;
+import org.apache.log4j.xml.DOMConfigurator;
+
+/**
+ * Tests for SMTPAppender.
+ */
+public class SMTPAppenderTest extends TestCase {
+    public SMTPAppenderTest(final String testName) {
+        super(testName);
+    }
+
+    /**
+     * Reset configuration after every test.
+     */
+  public void tearDown() {
+      LogManager.resetConfiguration();
+  }
+
+    /**
+     * Trivial implementation of TriggeringEventEvaluator.
+     */
+  public static final class MockTriggeringEventEvaluator implements 
TriggeringEventEvaluator {
+        /**
+         * [EMAIL PROTECTED]
+         */
+      public boolean isTriggeringEvent(final LoggingEvent event) {
+          return true;
+      }
+  }
+
+    /**
+     * Tests that triggeringPolicy element will set evaluator.
+     */
+  public void testTrigger() {
+      DOMConfigurator.configure("input/xml/smtpAppender1.xml");
+      SMTPAppender appender = (SMTPAppender) 
Logger.getRootLogger().getAppender("A1");
+      TriggeringEventEvaluator evaluator = appender.getEvaluator();
+      assertTrue(evaluator instanceof MockTriggeringEventEvaluator);
+  }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to