Example #1
0
 /**
  * This is a helper method that updates the state of the mail object to Mail.ERROR as well as
  * recording the exception to the log
  *
  * @param me the exception to be handled
  * @param mail the mail being processed when the exception was generated
  * @param offendersName the matcher or mailet than generated the exception
  * @param nextState the next state to set
  * @throws MessagingException thrown always, rethrowing the passed in exception
  */
 public static void handleException(
     MessagingException me, Mail mail, String offendersName, String nextState, Logger logger)
     throws MessagingException {
   mail.setState(nextState);
   StringWriter sout = new StringWriter();
   PrintWriter out = new PrintWriter(sout, true);
   StringBuffer exceptionBuffer =
       new StringBuffer(128)
           .append("Exception calling ")
           .append(offendersName)
           .append(": ")
           .append(me.getMessage());
   out.println(exceptionBuffer.toString());
   Exception e = me;
   while (e != null) {
     e.printStackTrace(out);
     if (e instanceof MessagingException) {
       e = ((MessagingException) e).getNextException();
     } else {
       e = null;
     }
   }
   String errorString = sout.toString();
   mail.setErrorMessage(errorString);
   logger.error(errorString);
   throw me;
 }
 /**
  * Process this mail message by the appropriate processor as designated in the state of the Mail
  * object.
  *
  * @param mail the mail message to be processed
  */
 protected void process(Mail mail) {
   while (true) {
     String processorName = mail.getState();
     if (processorName.equals(Mail.GHOST)) {
       // This message should disappear
       return;
     }
     try {
       LinearProcessor processor = (LinearProcessor) processors.get(processorName);
       if (processor == null) {
         StringBuffer exceptionMessageBuffer =
             new StringBuffer(128)
                 .append("Unable to find processor ")
                 .append(processorName)
                 .append(" requested for processing of ")
                 .append(mail.getName());
         String exceptionMessage = exceptionMessageBuffer.toString();
         getLogger().debug(exceptionMessage);
         mail.setState(Mail.ERROR);
         throw new MailetException(exceptionMessage);
       }
       StringBuffer logMessageBuffer = null;
       if (getLogger().isDebugEnabled()) {
         logMessageBuffer =
             new StringBuffer(64)
                 .append("Processing ")
                 .append(mail.getName())
                 .append(" through ")
                 .append(processorName);
         getLogger().debug(logMessageBuffer.toString());
       }
       processor.service(mail);
       if (getLogger().isDebugEnabled()) {
         logMessageBuffer =
             new StringBuffer(128)
                 .append("Processed ")
                 .append(mail.getName())
                 .append(" through ")
                 .append(processorName);
         getLogger().debug(logMessageBuffer.toString());
         getLogger().debug("Result was " + mail.getState());
       }
       return;
     } catch (Throwable e) {
       // This is a strange error situation that shouldn't ordinarily
       // happen
       StringBuffer exceptionBuffer =
           new StringBuffer(64)
               .append("Exception in processor <")
               .append(processorName)
               .append(">");
       getLogger().error(exceptionBuffer.toString(), e);
       if (processorName.equals(Mail.ERROR)) {
         // We got an error on the error processor...
         // kill the message
         mail.setState(Mail.GHOST);
         mail.setErrorMessage(e.getMessage());
       } else {
         // We got an error... send it to the requested processor
         if (!(e instanceof MessagingException)) {
           // We got an error... send it to the error processor
           mail.setState(Mail.ERROR);
         }
         mail.setErrorMessage(e.getMessage());
       }
     }
     if (getLogger().isErrorEnabled()) {
       StringBuffer logMessageBuffer =
           new StringBuffer(128)
               .append("An error occurred processing ")
               .append(mail.getName())
               .append(" through ")
               .append(processorName);
       getLogger().error(logMessageBuffer.toString());
       getLogger().error("Result was " + mail.getState());
     }
   }
 }