Author: bago Date: Mon Jul 24 04:04:25 2006 New Revision: 425008 URL: http://svn.apache.org/viewvc?rev=425008&view=rev Log: Introduced Logger service and enable DI for logging (JSPF-20)
Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java (with props) james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java (with props) james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java (with props) james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java (with props) Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/DNSServiceXBillImpl.java james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFQuery.java james/jspf/trunk/src/main/java/org/apache/james/jspf/macro/MacroExpand.java james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/SPF1Parser.java james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExpModifier.java james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFMailZoneTest.java james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFTest.java james/jspf/trunk/src/test/java/org/apache/james/jspf/macro/MacroExpandTest.java Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/DNSServiceXBillImpl.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/DNSServiceXBillImpl.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/DNSServiceXBillImpl.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/DNSServiceXBillImpl.java Mon Jul 24 04:04:25 2006 @@ -28,10 +28,10 @@ import org.apache.james.jspf.core.DNSService; import org.apache.james.jspf.core.IPAddr; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.exceptions.NoneException; import org.apache.james.jspf.exceptions.PermErrorException; import org.apache.james.jspf.exceptions.TempErrorException; -import org.apache.log4j.Logger; import org.xbill.DNS.AAAARecord; import org.xbill.DNS.ARecord; import org.xbill.DNS.Lookup; @@ -53,7 +53,14 @@ // Set seconds after which we return and TempError private static int timeOut = 20; - private static Logger log = Logger.getLogger(DNSServiceXBillImpl.class); + private Logger log; + + /** + * Default Constructor + */ + public DNSServiceXBillImpl(Logger logger) { + this.log = logger; + } /** * @see org.apache.james.jspf.core.DNSService#getSpfRecord(java.lang.String, @@ -109,7 +116,7 @@ * @throws PermErrorException * if an PermError should be returned */ - private static ArrayList getTXTRecords(String hostname) + private ArrayList getTXTRecords(String hostname) throws NoneException, TempErrorException { ArrayList txtR = new ArrayList(); Record[] records; @@ -418,7 +425,7 @@ * @throws TempErrorException * if the lookup result was "TRY_AGAIN" */ - private static ArrayList getMXNames(String host) throws NoneException, + private ArrayList getMXNames(String host) throws NoneException, TempErrorException { ArrayList mxR = new ArrayList(); Record[] records; Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java?rev=425008&view=auto ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java (added) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java Mon Jul 24 04:04:25 2006 @@ -0,0 +1,207 @@ +/**************************************************************** + * 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.james.jspf; + +import org.apache.james.jspf.core.Logger; +import org.apache.log4j.Level; + +/** + * Implementation of the Logger interface using the Log4J implementation + * strategy. + */ +public class Log4JLogger implements Logger { + private org.apache.log4j.Logger m_Logger; + + Log4JLogger(org.apache.log4j.Logger log4jLogger) { + m_Logger = log4jLogger; + } + + /** + * Log a debug message. + * + * @param message + * the message + */ + public void debug(String message) { + m_Logger.debug(message); + } + + /** + * Log a debug message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + public void debug(String message, Throwable throwable) { + m_Logger.debug(message, throwable); + } + + /** + * Determine if messages of priority "debug" will be logged. + * + * @return true if "debug" messages will be logged + */ + public boolean isDebugEnabled() { + return m_Logger.isDebugEnabled(); + } + + /** + * Log a info message. + * + * @param message + * the message + */ + public void info(String message) { + m_Logger.info(message); + } + + /** + * Log a info message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + public void info(String message, Throwable throwable) { + m_Logger.info(message, throwable); + } + + /** + * Determine if messages of priority "info" will be logged. + * + * @return true if "info" messages will be logged + */ + public boolean isInfoEnabled() { + return m_Logger.isInfoEnabled(); + } + + /** + * Log a warn message. + * + * @param message + * the message + */ + public void warn(String message) { + m_Logger.warn(message); + } + + /** + * Log a warn message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + public void warn(String message, Throwable throwable) { + m_Logger.warn(message, throwable); + } + + /** + * Determine if messages of priority "warn" will be logged. + * + * @return true if "warn" messages will be logged + */ + public boolean isWarnEnabled() { + return m_Logger.isEnabledFor(Level.WARN); + } + + /** + * Log a error message. + * + * @param message + * the message + */ + public void error(String message) { + m_Logger.error(message); + } + + /** + * Log a error message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + public void error(String message, Throwable throwable) { + m_Logger.error(message, throwable); + } + + /** + * Determine if messages of priority "error" will be logged. + * + * @return true if "error" messages will be logged + */ + public boolean isErrorEnabled() { + return m_Logger.isEnabledFor(Level.ERROR); + } + + /** + * Log a fatalError message. + * + * @param message + * the message + */ + public void fatalError(String message) { + m_Logger.fatal(message); + } + + /** + * Log a fatalError message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + public void fatalError(String message, Throwable throwable) { + m_Logger.fatal(message, throwable); + } + + /** + * Determine if messages of priority "fatalError" will be logged. + * + * @return true if "fatalError" messages will be logged + */ + public boolean isFatalErrorEnabled() { + return m_Logger.isEnabledFor(Level.FATAL); + } + + /** + * Create a new child logger. The name of the child logger is + * [current-loggers-name].[passed-in-name] Throws + * <code>IllegalArgumentException</code> if name has an empty element name + * + * @param name + * the subname of this logger + * @return the new logger + */ + public Logger getChildLogger(String name) { + String newName = m_Logger.getName() + "." + name; + org.apache.log4j.Logger childLog4JLogger = org.apache.log4j.Logger + .getLogger(newName); + Log4JLogger child = new Log4JLogger(childLog4JLogger); + return child; + } +} Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/Log4JLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java Mon Jul 24 04:04:25 2006 @@ -22,6 +22,7 @@ import org.apache.james.jspf.core.DNSService; import org.apache.james.jspf.core.Directive; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.Modifier; import org.apache.james.jspf.core.SPF1Constants; import org.apache.james.jspf.core.SPF1Data; @@ -31,7 +32,6 @@ import org.apache.james.jspf.exceptions.TempErrorException; import org.apache.james.jspf.macro.MacroExpand; import org.apache.james.jspf.parser.SPF1Parser; -import org.apache.log4j.Logger; import java.util.Iterator; @@ -42,28 +42,36 @@ public class SPF { - private DNSService dnsProbe = null; + private DNSService dnsProbe; private SPF1Parser parser; - private static Logger log = Logger.getLogger(SPF.class); + private Logger log; /** - * + * Uses default Log4JLogger and DNSJava based dns resolver */ public SPF() { - this(new DNSServiceXBillImpl()); - + this(new Log4JLogger(org.apache.log4j.Logger.getLogger(SPF.class))); + } + + /** + * Uses passed logger and DNSJava based dns resolver + * @param logger logger + */ + public SPF(Logger logger) { + this(new DNSServiceXBillImpl(logger), logger); } /** * @param dnsProbe * the dns provider */ - public SPF(DNSService dnsProbe) { + public SPF(DNSService dnsProbe, Logger logger) { super(); this.dnsProbe = dnsProbe; - this.parser = new SPF1Parser(); + this.parser = new SPF1Parser(logger); + this.log = logger; } /** @@ -92,18 +100,18 @@ result = SPF1Utils.resultToName(resultChar); explanation = res.getExplanation(); } catch (PermErrorException e) { - log.warn(e.getMessage()); + log.warn(e.getMessage(),e); result = SPF1Utils.PERM_ERROR; } catch (NoneException e) { - log.warn(e.getMessage()); + log.warn(e.getMessage(),e); result = SPF1Utils.NONE; } catch (TempErrorException e) { - log.warn(e.getMessage()); + log.warn(e.getMessage(),e); result = SPF1Utils.TEMP_ERROR; } catch (IllegalStateException e) { // this should never happen at all. But anyway we will set the // result to neutral. Safety first .. - log.error(e.getMessage()); + log.error(e.getMessage(),e); result = SPF1Constants.NEUTRAL; } @@ -240,7 +248,7 @@ if (result.equals(SPF1Constants.FAIL)) { if (spfData.getExplanation()==null || spfData.getExplanation().equals("")) { try { - spfData.setExplanation(new MacroExpand(spfData) + spfData.setExplanation(new MacroExpand(spfData, log) .expandExplanation(SPF1Utils.DEFAULT_EXPLANATION)); } catch (PermErrorException e) {} } Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFQuery.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFQuery.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFQuery.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFQuery.java Mon Jul 24 04:04:25 2006 @@ -90,7 +90,7 @@ // check if all needed values was set if (ip != null && sender != null && helo != null) { - SPF spf = new SPF(); + SPF spf = new SPF(new Log4JLogger(logger)); SPFResult result = spf.checkSPF(ip, sender, helo); System.out.println(result.getResult()); System.out.println(result.getHeader()); Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java?rev=425008&view=auto ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java (added) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java Mon Jul 24 04:04:25 2006 @@ -0,0 +1,34 @@ +/**************************************************************** + * 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.james.jspf.core; + +/** + * Components that need to log can implement this interface to be provided + * Loggers. + */ +public interface LogEnabled { + /** + * Provide component with a logger. + * + * @param logger + * the logger. Must not be <code>null</code>. + */ + void enableLogging(Logger logger); +} \ No newline at end of file Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/LogEnabled.java ------------------------------------------------------------------------------ svn:eol-style = native Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java?rev=425008&view=auto ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java (added) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java Mon Jul 24 04:04:25 2006 @@ -0,0 +1,163 @@ +/**************************************************************** + * 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.james.jspf.core; + +/** + * This is a facade for the different logging subsystems. It offers a simplified + * interface that follows IOC patterns and a simplified priority/level/severity + * abstraction. + */ +public interface Logger { + /** + * Log a debug message. + * + * @param message + * the message + */ + void debug(String message); + + /** + * Log a debug message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + void debug(String message, Throwable throwable); + + /** + * Determine if messages of priority "debug" will be logged. + * + * @return true if "debug" messages will be logged + */ + boolean isDebugEnabled(); + + /** + * Log a info message. + * + * @param message + * the message + */ + void info(String message); + + /** + * Log a info message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + void info(String message, Throwable throwable); + + /** + * Determine if messages of priority "info" will be logged. + * + * @return true if "info" messages will be logged + */ + boolean isInfoEnabled(); + + /** + * Log a warn message. + * + * @param message + * the message + */ + void warn(String message); + + /** + * Log a warn message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + void warn(String message, Throwable throwable); + + /** + * Determine if messages of priority "warn" will be logged. + * + * @return true if "warn" messages will be logged + */ + boolean isWarnEnabled(); + + /** + * Log a error message. + * + * @param message + * the message + */ + void error(String message); + + /** + * Log a error message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + void error(String message, Throwable throwable); + + /** + * Determine if messages of priority "error" will be logged. + * + * @return true if "error" messages will be logged + */ + boolean isErrorEnabled(); + + /** + * Log a fatalError message. + * + * @param message + * the message + */ + void fatalError(String message); + + /** + * Log a fatalError message. + * + * @param message + * the message + * @param throwable + * the throwable + */ + void fatalError(String message, Throwable throwable); + + /** + * Determine if messages of priority "fatalError" will be logged. + * + * @return true if "fatalError" messages will be logged + */ + boolean isFatalErrorEnabled(); + + /** + * Create a new child logger. The name of the child logger is + * [current-loggers-name].[passed-in-name] Throws + * <code>IllegalArgumentException</code> if name has an empty element name + * + * @param name + * the subname of this logger + * @return the new logger + */ + Logger getChildLogger(String name); +} Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/core/Logger.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/macro/MacroExpand.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/macro/MacroExpand.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/macro/MacroExpand.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/macro/MacroExpand.java Mon Jul 24 04:04:25 2006 @@ -26,8 +26,8 @@ * */ +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.exceptions.PermErrorException; -import org.apache.log4j.Logger; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -51,11 +51,12 @@ private boolean isExplanation = false; - private static Logger log = Logger.getLogger(MacroExpand.class); + private Logger log; - public MacroExpand(MacroData spfData) { + public MacroExpand(MacroData spfData, Logger logger) { this.spfData = spfData; inputPattern = Pattern.compile(MACRO_REGEX); + log = logger; } /** Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/SPF1Parser.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/SPF1Parser.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/SPF1Parser.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/parser/SPF1Parser.java Mon Jul 24 04:04:25 2006 @@ -22,6 +22,8 @@ import org.apache.james.jspf.core.Configurable; import org.apache.james.jspf.core.Directive; +import org.apache.james.jspf.core.LogEnabled; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.Mechanism; import org.apache.james.jspf.core.Modifier; import org.apache.james.jspf.core.SPF1Constants; @@ -29,7 +31,6 @@ import org.apache.james.jspf.exceptions.NoneException; import org.apache.james.jspf.exceptions.PermErrorException; import org.apache.james.jspf.util.ConfigurationMatch; -import org.apache.log4j.Logger; import java.io.IOException; import java.io.InputStream; @@ -154,7 +155,7 @@ private List matchResultPositions; - private static Logger log = Logger.getLogger(SPF1Parser.class); + private Logger log; private String termFile = "org/apache/james/jspf/parser/jspf.default.terms"; @@ -210,8 +211,8 @@ /** * Constructor. Creates all the values needed to run the parsing */ - public SPF1Parser() { - + public SPF1Parser(Logger logger) { + log = logger; try { InputStream is = Thread.currentThread().getContextClassLoader() .getResourceAsStream(termFile); @@ -494,6 +495,9 @@ .getMatchSize()); try { Object term = c.getTermDef().newInstance(); + if (term instanceof LogEnabled) { + ((LogEnabled) term).enableLogging(log); + } if (term instanceof Configurable) { if (subres == null || subres.groupCount() == 0) { ((Configurable) term).config(null); Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java Mon Jul 24 04:04:25 2006 @@ -53,7 +53,7 @@ String host = expandHost(spfData); try { - host = new MacroExpand(spfData).expandDomain(host); + host = new MacroExpand(spfData, log).expandDomain(host); } catch (Exception e) { throw new PermErrorException(e.getMessage()); } Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExpModifier.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExpModifier.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExpModifier.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/ExpModifier.java Mon Jul 24 04:04:25 2006 @@ -20,6 +20,8 @@ package org.apache.james.jspf.terms; +import org.apache.james.jspf.core.LogEnabled; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.SPF1Constants; import org.apache.james.jspf.core.SPF1Data; import org.apache.james.jspf.exceptions.NoneException; @@ -32,7 +34,7 @@ * This class represent the exp modifier * */ -public class ExpModifier extends GenericModifier { +public class ExpModifier extends GenericModifier implements LogEnabled { /** * ABNF: explanation = "exp" "=" domain-spec @@ -40,6 +42,8 @@ public static final String REGEX = "[eE][xX][pP]" + "\\=" + SPF1Parser.DOMAIN_SPEC_REGEX; + private Logger log; + /** * Generate the explanation and set it in SPF1Data so it can be accessed * easy later if needed @@ -62,7 +66,7 @@ return null; try { - host = new MacroExpand(spfData).expandDomain(host); + host = new MacroExpand(spfData, log).expandDomain(host); try { exp = spfData.getDnsProbe().getTxtCatType(host); } catch (NoneException e) { @@ -74,7 +78,7 @@ } if ((exp != null) && (!exp.equals(""))) { - spfData.setExplanation(new MacroExpand(spfData) + spfData.setExplanation(new MacroExpand(spfData, log) .expandExplanation(exp)); } } catch (PermErrorException e) { @@ -89,6 +93,13 @@ */ public boolean enforceSingleInstance() { return true; + } + + /** + * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger) + */ + public void enableLogging(Logger logger) { + this.log = logger; } } Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/GenericMechanism.java Mon Jul 24 04:04:25 2006 @@ -21,6 +21,8 @@ package org.apache.james.jspf.terms; import org.apache.james.jspf.core.Configurable; +import org.apache.james.jspf.core.LogEnabled; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.Mechanism; import org.apache.james.jspf.core.SPF1Data; import org.apache.james.jspf.exceptions.PermErrorException; @@ -31,7 +33,7 @@ * This abstract class represent a gerneric mechanism * */ -public abstract class GenericMechanism implements Mechanism, Configurable { +public abstract class GenericMechanism implements Mechanism, Configurable, LogEnabled { /** * ABNF: ip4-cidr-length = "/" 1*DIGIT @@ -52,6 +54,8 @@ private String domain; + protected Logger log; + /** * Expand the hostname * @@ -64,7 +68,7 @@ host = spfData.getCurrentDomain(); } else { try { - host = new MacroExpand(spfData).expandDomain(host); + host = new MacroExpand(spfData, log).expandDomain(host); } catch (Exception e) { throw new PermErrorException(e.getMessage()); @@ -89,6 +93,13 @@ */ protected synchronized String getDomain() { return domain; + } + + /** + * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger) + */ + public void enableLogging(Logger logger) { + this.log = logger; } } Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java Mon Jul 24 04:04:25 2006 @@ -22,6 +22,8 @@ import org.apache.james.jspf.SPF; import org.apache.james.jspf.core.Configurable; +import org.apache.james.jspf.core.LogEnabled; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.Mechanism; import org.apache.james.jspf.core.SPF1Constants; import org.apache.james.jspf.core.SPF1Data; @@ -36,7 +38,7 @@ * This class represent the incude mechanism * */ -public class IncludeMechanism implements Mechanism, Configurable { +public class IncludeMechanism implements Mechanism, Configurable, LogEnabled { /** * ABNF: include = "include" ":" domain-spec @@ -45,6 +47,8 @@ + SPF1Parser.DOMAIN_SPEC_REGEX; private String host; + + private Logger log; /** * Set the host which should be used for include @@ -64,7 +68,7 @@ spfData.setCurrentDepth(spfData.getCurrentDepth() + 1); try { - host = new MacroExpand(spfData).expandDomain(host); + host = new MacroExpand(spfData, log).expandDomain(host); } catch (Exception e) { throw new PermErrorException("Error in include modifier: " + host); } @@ -76,7 +80,7 @@ String res = null; try { - res = new SPF(spfData.getDnsProbe()).checkSPF(spfData).getResultChar(); + res = new SPF(spfData.getDnsProbe(),log).checkSPF(spfData).getResultChar(); } catch (NoneException e) { throw new PermErrorException("included checkSPF returned NoneException"); } @@ -111,6 +115,13 @@ */ protected synchronized String getHost() { return host; + } + + /** + * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger) + */ + public void enableLogging(Logger logger) { + this.log = logger; } } Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java (original) +++ james/jspf/trunk/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java Mon Jul 24 04:04:25 2006 @@ -21,6 +21,8 @@ package org.apache.james.jspf.terms; import org.apache.james.jspf.SPF; +import org.apache.james.jspf.core.LogEnabled; +import org.apache.james.jspf.core.Logger; import org.apache.james.jspf.core.SPF1Data; import org.apache.james.jspf.exceptions.NoneException; import org.apache.james.jspf.exceptions.PermErrorException; @@ -32,13 +34,15 @@ * This class represent the redirect modifier * */ -public class RedirectModifier extends GenericModifier { +public class RedirectModifier extends GenericModifier implements LogEnabled { /** * ABNF: redirect = "redirect" "=" domain-spec */ public static final String REGEX = "[rR][eE][dD][iI][rR][eE][cC][tT]" + "\\=" + SPF1Parser.DOMAIN_SPEC_REGEX; + + private Logger log; /** * Set the host which should be used for redirection and set it in SPF1Data @@ -63,7 +67,7 @@ spfData.setCurrentDepth(spfData.getCurrentDepth() + 1); try { - host = new MacroExpand(spfData).expandDomain(host); + host = new MacroExpand(spfData, log).expandDomain(host); } catch (Exception e) { throw new PermErrorException("Error in redirect modifier: " + host); @@ -73,7 +77,7 @@ String res = null; try { - res = new SPF(spfData.getDnsProbe()).checkSPF(spfData).getResultChar(); + res = new SPF(spfData.getDnsProbe(),log).checkSPF(spfData).getResultChar(); } catch (NoneException e) { @@ -98,6 +102,14 @@ */ public boolean enforceSingleInstance() { return true; + } + + + /** + * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger) + */ + public void enableLogging(Logger logger) { + this.log = logger; } } Added: james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java?rev=425008&view=auto ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java (added) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java Mon Jul 24 04:04:25 2006 @@ -0,0 +1,280 @@ +/**************************************************************** + * 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.james.jspf; + +import org.apache.james.jspf.core.Logger; + +/** + * Logger sending everything to the standard output streams. + * This is mainly for the cases when you have a utility that + * does not have a logger to supply. + */ +public final class ConsoleLogger + implements Logger +{ + /** Typecode for debugging messages. */ + public static final int LEVEL_DEBUG = 0; + + /** Typecode for informational messages. */ + public static final int LEVEL_INFO = 1; + + /** Typecode for warning messages. */ + public static final int LEVEL_WARN = 2; + + /** Typecode for error messages. */ + public static final int LEVEL_ERROR = 3; + + /** Typecode for fatal error messages. */ + public static final int LEVEL_FATAL = 4; + + /** Typecode for disabled log levels. */ + public static final int LEVEL_DISABLED = 5; + + private final int m_logLevel; + + /** + * Creates a new ConsoleLogger with the priority set to DEBUG. + */ + public ConsoleLogger() + { + this( LEVEL_DEBUG ); + } + + /** + * Creates a new ConsoleLogger. + * @param logLevel log level typecode + */ + public ConsoleLogger( final int logLevel ) + { + m_logLevel = logLevel; + } + + /** + * Logs a debugging message. + * + * @param message a <code>String</code> value + */ + public void debug( final String message ) + { + debug( message, null ); + } + + /** + * Logs a debugging message and an exception. + * + * @param message a <code>String</code> value + * @param throwable a <code>Throwable</code> value + */ + public void debug( final String message, final Throwable throwable ) + { + if( m_logLevel <= LEVEL_DEBUG ) + { + System.out.print( "[DEBUG] " ); + System.out.println( message ); + + if( null != throwable ) + { + throwable.printStackTrace( System.out ); + } + } + } + + /** + * Returns <code>true</code> if debug-level logging is enabled, false otherwise. + * + * @return <code>true</code> if debug-level logging + */ + public boolean isDebugEnabled() + { + return m_logLevel <= LEVEL_DEBUG; + } + + /** + * Logs an informational message. + * + * @param message a <code>String</code> value + */ + public void info( final String message ) + { + info( message, null ); + } + + /** + * Logs an informational message and an exception. + * + * @param message a <code>String</code> value + * @param throwable a <code>Throwable</code> value + */ + public void info( final String message, final Throwable throwable ) + { + if( m_logLevel <= LEVEL_INFO ) + { + System.out.print( "[INFO] " ); + System.out.println( message ); + + if( null != throwable ) + { + throwable.printStackTrace( System.out ); + } + } + } + + /** + * Returns <code>true</code> if info-level logging is enabled, false otherwise. + * + * @return <code>true</code> if info-level logging is enabled + */ + public boolean isInfoEnabled() + { + return m_logLevel <= LEVEL_INFO; + } + + /** + * Logs a warning message. + * + * @param message a <code>String</code> value + */ + public void warn( final String message ) + { + warn( message, null ); + } + + /** + * Logs a warning message and an exception. + * + * @param message a <code>String</code> value + * @param throwable a <code>Throwable</code> value + */ + public void warn( final String message, final Throwable throwable ) + { + if( m_logLevel <= LEVEL_WARN ) + { + System.out.print( "[WARNING] " ); + System.out.println( message ); + + if( null != throwable ) + { + throwable.printStackTrace( System.out ); + } + } + } + + /** + * Returns <code>true</code> if warn-level logging is enabled, false otherwise. + * + * @return <code>true</code> if warn-level logging is enabled + */ + public boolean isWarnEnabled() + { + return m_logLevel <= LEVEL_WARN; + } + + /** + * Logs an error message. + * + * @param message a <code>String</code> value + */ + public void error( final String message ) + { + error( message, null ); + } + + /** + * Logs an error message and an exception. + * + * @param message a <code>String</code> value + * @param throwable a <code>Throwable</code> value + */ + public void error( final String message, final Throwable throwable ) + { + if( m_logLevel <= LEVEL_ERROR ) + { + System.out.print( "[ERROR] " ); + System.out.println( message ); + + if( null != throwable ) + { + throwable.printStackTrace( System.out ); + } + } + } + + /** + * Returns <code>true</code> if error-level logging is enabled, false otherwise. + * + * @return <code>true</code> if error-level logging is enabled + */ + public boolean isErrorEnabled() + { + return m_logLevel <= LEVEL_ERROR; + } + + /** + * Logs a fatal error message. + * + * @param message a <code>String</code> value + */ + public void fatalError( final String message ) + { + fatalError( message, null ); + } + + /** + * Logs a fatal error message and an exception. + * + * @param message a <code>String</code> value + * @param throwable a <code>Throwable</code> value + */ + public void fatalError( final String message, final Throwable throwable ) + { + if( m_logLevel <= LEVEL_FATAL ) + { + System.out.print( "[FATAL ERROR] " ); + System.out.println( message ); + + if( null != throwable ) + { + throwable.printStackTrace( System.out ); + } + } + } + + /** + * Returns <code>true</code> if fatal-level logging is enabled, false otherwise. + * + * @return <code>true</code> if fatal-level logging is enabled + */ + public boolean isFatalErrorEnabled() + { + return m_logLevel <= LEVEL_FATAL; + } + + /** + * Just returns this logger (<code>ConsoleLogger</code> is not hierarchical). + * + * @param name ignored + * @return this logger + */ + public Logger getChildLogger( final String name ) + { + return this; + } +} \ No newline at end of file Propchange: james/jspf/trunk/src/test/java/org/apache/james/jspf/ConsoleLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java (original) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/LoggingDNSService.java Mon Jul 24 04:04:25 2006 @@ -32,7 +32,7 @@ public class LoggingDNSService implements DNSService { - private DNSService dnsService = new DNSServiceXBillImpl(); + private DNSService dnsService = new DNSServiceXBillImpl(new ConsoleLogger()); public String getSpfRecord(String hostname, String spfVersion) throws PermErrorException, NoneException, TempErrorException { Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java (original) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/SPF1ParserTest.java Mon Jul 24 04:04:25 2006 @@ -50,7 +50,7 @@ } } assertNotNull(data); - parser = new SPF1Parser(); + parser = new SPF1Parser(new ConsoleLogger()); } public static Test suite() throws IOException { @@ -176,7 +176,7 @@ super(); List tests = loadTests(); Iterator i = tests.iterator(); - SPF1Parser parser = new SPF1Parser(); + SPF1Parser parser = new SPF1Parser(new ConsoleLogger()); while (i.hasNext()) { addTest(new SPF1ParserTest((SPF1RecordTestDef) i.next(), parser)); } Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFMailZoneTest.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFMailZoneTest.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFMailZoneTest.java (original) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFMailZoneTest.java Mon Jul 24 04:04:25 2006 @@ -48,7 +48,7 @@ } } assertNotNull(data); - spf = new SPF(new SPF1TestMZMockDNSService()); + spf = new SPF(new SPF1TestMZMockDNSService(), new ConsoleLogger()); } public static Test suite() throws IOException { @@ -233,7 +233,7 @@ super(); List tests = loadTests(); Iterator i = tests.iterator(); - SPF spf = new SPF(new SPF1TestMZMockDNSService()); + SPF spf = new SPF(new SPF1TestMZMockDNSService(), new ConsoleLogger()); while (i.hasNext()) { addTest(new SPFMailZoneTest(spf, (SPFTestDef) i.next())); } Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFTest.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFTest.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFTest.java (original) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/SPFTest.java Mon Jul 24 04:04:25 2006 @@ -48,7 +48,7 @@ } } assertNotNull(data); - spf = new SPF(new SPF1TestMockDNSService()); + spf = new SPF(new SPF1TestMockDNSService(), new ConsoleLogger()); } public static Test suite() throws IOException { @@ -241,7 +241,7 @@ super(); List tests = loadTests(); Iterator i = tests.iterator(); - SPF spf = new SPF(new SPF1TestMockDNSService()); + SPF spf = new SPF(new SPF1TestMockDNSService(), new ConsoleLogger()); while (i.hasNext()) { addTest(new SPFTest(spf, (SPFTestDef) i.next())); } Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/macro/MacroExpandTest.java URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/macro/MacroExpandTest.java?rev=425008&r1=425007&r2=425008&view=diff ============================================================================== --- james/jspf/trunk/src/test/java/org/apache/james/jspf/macro/MacroExpandTest.java (original) +++ james/jspf/trunk/src/test/java/org/apache/james/jspf/macro/MacroExpandTest.java Mon Jul 24 04:04:25 2006 @@ -19,6 +19,7 @@ package org.apache.james.jspf.macro; +import org.apache.james.jspf.ConsoleLogger; import org.apache.james.jspf.exceptions.PermErrorException; import junit.framework.TestCase; @@ -102,8 +103,8 @@ protected void setUp() throws Exception { super.setUp(); - defIp4me = new MacroExpand(new rfcIP4MacroData()); - defIp6me = new MacroExpand(new rfcIP6MacroData()); + defIp4me = new MacroExpand(new rfcIP4MacroData(), new ConsoleLogger()); + defIp6me = new MacroExpand(new rfcIP6MacroData(), new ConsoleLogger()); } public void testPercS() throws PermErrorException { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]