Skip to content

Commit

Permalink
JAMES-2114 Improve a bit JSieve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Aug 24, 2017
1 parent d66efa3 commit ecccda0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 52 deletions.
Expand Up @@ -23,7 +23,6 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.joda.time.DateTime;
Expand All @@ -47,12 +46,6 @@ public interface ActionContext {
* @return Recipient receiving the given eMail
*/
MailAddress getRecipient();

/**
* Gets the log.
* @return not null
*/
public Log getLog();

/**
* Experimental mail delivery.
Expand Down
Expand Up @@ -23,13 +23,15 @@

import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility methods helpful for actions.
*/
public class ActionUtils
{
public class ActionUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ActionUtils.class);

private final static String ATTRIBUTE_PREFIX = ActionUtils.class.getPackage().getName() + ".";

/**
Expand Down Expand Up @@ -71,7 +73,7 @@ public static void detectAndHandleLocalLooping(Mail aMail, ActionContext context
MessagingException ex = new MessagingException(
"This message is looping! Message ID: "
+ aMail.getMessage().getMessageID());
context.getLog().warn(ex.getMessage(), ex);
LOGGER.warn(ex.getMessage(), ex);
throw ex;
}
aMail.setAttribute(ATTRIBUTE_PREFIX + anAttributeSuffix,
Expand Down
Expand Up @@ -20,19 +20,21 @@

import javax.mail.MessagingException;

import org.apache.commons.logging.Log;
import org.apache.jsieve.mail.Action;
import org.apache.jsieve.mail.ActionFileInto;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Performs the filing of a mail into a specified destination.
* <h4>Thread Safety</h4>
* <p>An instance maybe safe accessed concurrently by multiple threads.</p>
*/
public class FileIntoAction implements MailAction {

private static final Logger LOGGER = LoggerFactory.getLogger(FileIntoAction.class);

private static final char HIERARCHY_DELIMITER = '.';

public void execute(Action action, Mail mail, ActionContext context) throws MessagingException {
Expand Down Expand Up @@ -67,13 +69,11 @@ public void execute(Action action, Mail mail, ActionContext context) throws Mess
* @param context not null
* @throws MessagingException
*/
public void execute(ActionFileInto anAction, Mail aMail, final ActionContext context) throws MessagingException
{
public void execute(ActionFileInto anAction, Mail aMail, final ActionContext context) throws MessagingException {
String destinationMailbox = anAction.getDestination();
MailAddress recipient;
boolean delivered = false;
try
{
try {
recipient = ActionUtils.getSoleRecipient(aMail);

if (!(destinationMailbox.length() > 0
Expand All @@ -87,19 +87,15 @@ public void execute(ActionFileInto anAction, Mail aMail, final ActionContext con
context.post(url, aMail);
delivered = true;
}
catch (MessagingException ex)
{
final Log log = context.getLog();
if (log.isDebugEnabled()) {
log.debug("Error while storing mail into. "+destinationMailbox, ex);
catch (MessagingException ex) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Error while storing mail into. "+destinationMailbox, ex);
}
throw ex;
}
if (delivered)
{
final Log log = context.getLog();
if (log.isDebugEnabled()) {
log.debug("Filed Message ID: "
if (delivered) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Filed Message ID: "
+ aMail.getMessage().getMessageID()
+ " into destination: \""
+ destinationMailbox + "\"");
Expand Down
Expand Up @@ -24,18 +24,20 @@
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.logging.Log;
import org.apache.jsieve.mail.Action;
import org.apache.jsieve.mail.ActionRedirect;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Performs the redirection of a mail.
* <h4>Thread Safety</h4>
* <p>An instance maybe safe accessed concurrently by multiple threads.</p>
*/
public class RedirectAction implements MailAction {
private static final Logger LOGGER = LoggerFactory.getLogger(RedirectAction.class.getName());

public void execute(Action action, Mail mail, ActionContext context)
throws MessagingException {
Expand All @@ -61,9 +63,8 @@ public void execute(ActionRedirect anAction, Mail aMail, ActionContext context)
recipients.add(new MailAddress(new InternetAddress(anAction.getAddress())));
MailAddress sender = aMail.getSender();
context.post(sender, recipients, aMail.getMessage());
Log log = context.getLog();
if (log.isDebugEnabled()) {
log.debug("Redirected Message ID: "
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Redirected Message ID: "
+ aMail.getMessage().getMessageID() + " to \""
+ anAction.getAddress() + "\"");
}
Expand Down
Expand Up @@ -40,13 +40,16 @@
import org.apache.jsieve.mail.ActionReject;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Performs the rejection of a mail, with a reply to the sender.
* <h4>Thread Safety</h4>
* <p>An instance maybe safe accessed concurrently by multiple threads.</p>
*/
public class RejectAction implements MailAction {
private static final Logger LOGGER = LoggerFactory.getLogger(RejectAction.class);

public void execute(Action action, Mail mail, ActionContext context)
throws MessagingException {
Expand Down Expand Up @@ -135,7 +138,7 @@ public void execute(ActionReject anAction, Mail aMail, ActionContext context) th
}
else
{
context.getLog().info("Unable to send reject MDN. Could not determine the recipient.");
LOGGER.info("Unable to send reject MDN. Could not determine the recipient.");
}
}

Expand Down
Expand Up @@ -33,8 +33,6 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.james.mime4j.dom.address.AddressList;
import org.apache.james.mime4j.dom.address.Mailbox;
import org.apache.james.mime4j.dom.address.MailboxList;
Expand All @@ -56,18 +54,14 @@
import org.joda.time.DateTime;

import com.google.common.collect.Lists;

/**
* <p>
* Class <code>SieveMailAdapter</code> implements a <code>MailAdapter</code>
* for use in a Mailet environment.
* </p>
*/
public class SieveMailAdapter implements MailAdapter, EnvelopeAccessors, ActionContext
{
private static final Log LOG = LogFactory.getLog(SieveMailAdapter.class);

private Log log = LOG;

public class SieveMailAdapter implements MailAdapter, EnvelopeAccessors, ActionContext {
/**
* The Mail being adapted.
*/
Expand Down Expand Up @@ -118,10 +112,6 @@ public MailAddress getRecipient() {
return recipient;
}

public void setLog(Log log) {
this.log = log;
}

/**
* Returns the message.
*
Expand Down Expand Up @@ -434,10 +424,6 @@ public Address[] parseAddresses(String arg) throws SieveMailException, InternetA
}
}

public Log getLog() {
return log;
}

public String getServerInfo() {
return getMailetContext().getServerInfo();
}
Expand Down
Expand Up @@ -30,12 +30,15 @@
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.joda.time.Days;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

public class VacationAction implements MailAction {
private static final Logger LOGGER = LoggerFactory.getLogger(VacationAction.class);

@Override
public void execute(Action action, Mail mail, ActionContext context) throws MessagingException {
Expand Down Expand Up @@ -77,7 +80,7 @@ private MailAddress retrieveAddressFromString(String address, ActionContext cont
try {
return new MailAddress(address);
} catch (AddressException e) {
context.getLog().warn("Mail address " + address + " was not well formatted : " + e.getLocalizedMessage());
LOGGER.warn("Mail address " + address + " was not well formatted : " + e.getLocalizedMessage());
return null;
}
}
Expand Down
Expand Up @@ -33,13 +33,15 @@

import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

public class VacationReply {

private static final Logger LOGGER = LoggerFactory.getLogger(VacationReply.class);

public static class Builder {

Expand Down Expand Up @@ -134,7 +136,7 @@ private MailAddress retrieveAddressFromString(String address, ActionContext cont
try {
return new MailAddress(address);
} catch (AddressException e) {
context.getLog().warn("Mail address " + address + " was not well formatted : " + e.getLocalizedMessage());
LOGGER.warn("Mail address " + address + " was not well formatted : " + e.getLocalizedMessage());
return null;
}
}
Expand Down
Expand Up @@ -136,7 +136,6 @@ private void sieveMessageEvaluate(MailAddress recipient, Mail aMail, ResourceLoc
SieveMailAdapter aMailAdapter = new SieveMailAdapter(aMail,
mailetContext, actionDispatcher, sievePoster, userSieveInformation.getScriptActivationDate(),
userSieveInformation.getScriptInterpretationDate(), recipient);
aMailAdapter.setLog(log);
// This logging operation is potentially costly
log.debug("Evaluating " + aMailAdapter.toString() + " against \"" + recipient.asPrettyString() + "\"");
factory.evaluate(aMailAdapter, factory.parse(userSieveInformation.getScriptContent()));
Expand Down

0 comments on commit ecccda0

Please sign in to comment.