<commit_before>/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.james.transport;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.MessagingException;
import org.apache.avalon.AbstractLoggable;
import org.apache.avalon.ComponentManager;
import org.apache.avalon.Composer;
import org.apache.avalon.Context;
import org.apache.avalon.Contextualizable;
import org.apache.avalon.DefaultComponentManager;
import org.apache.avalon.DefaultContext;
import org.apache.avalon.Initializable;
import org.apache.avalon.Stoppable;
import org.apache.avalon.configuration.Configurable;
import org.apache.avalon.configuration.Configuration;
import org.apache.avalon.configuration.ConfigurationException;
import org.apache.james.*;
import org.apache.james.core.*;
import org.apache.james.services.*;
import org.apache.mailet.*;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class JamesSpoolManager 
    extends AbstractLoggable
    implements Composer, Configurable, Initializable, Runnable, Stoppable, Contextualizable {

    private DefaultComponentManager compMgr;
    //using implementation as we need put method.
    private Configuration conf;
    private Context context;
    private SpoolRepository spool;
    private MailetContext mailetcontext;
    private HashMap processors;

    public void configure(Configuration conf) throws ConfigurationException {
        this.conf = conf;
    }

    public void contextualize(Context context) {
        this.context = new DefaultContext( context );
    }

    public void compose(ComponentManager comp) {
        compMgr = new DefaultComponentManager(comp);
    }

    public void init() throws Exception {

        getLogger().info("JamesSpoolManager init...");
        spool = (SpoolRepository) compMgr.lookup("org.apache.james.services.SpoolRepository");
        mailetcontext = (MailetContext) compMgr.lookup("org.apache.mailet.MailetContext");
        MailetLoader mailetLoader = new MailetLoader();
        MatchLoader matchLoader = new MatchLoader();
        try {
            mailetLoader.configure(conf.getChild("mailetpackages"));
            matchLoader.configure(conf.getChild("matcherpackages"));
            compMgr.put(Resources.MAILET_LOADER, mailetLoader);
            compMgr.put(Resources.MATCH_LOADER, matchLoader);
        } catch (ConfigurationException ce) {
            final String message = 
                "Unable to configure mailet/matcher Loaders: " + ce.getMessage();
            getLogger().error( message, ce );
            throw new RuntimeException( message );
        }

        //A processor is a Collection of
        processors = new HashMap();

        final Configuration[] processorConfs = conf.getChildren( "processor" );
        for ( int i = 0; i < processorConfs.length; i++ )
        {
            Configuration processorConf = processorConfs[i];
            String processorName = processorConf.getAttribute("name");
            try {
                LinearProcessor processor = new LinearProcessor();
                processor.setSpool(spool);
                processor.setLogger( getLogger() );
                processor.init();
                processors.put(processorName, processor);

                //If this is the root processor, add the PostmasterAlias mailet silently
                //  to the top
                if (processorName.equals("root")) {
                    Matcher matcher = matchLoader.getMatcher("All", mailetcontext);
                    Mailet mailet = 
                        mailetLoader.getMailet("PostmasterAlias", mailetcontext, null);
                    processor.add(matcher, mailet);
                }

                final Configuration[] mailetConfs = processorConf.getChildren( "mailet" );
                for ( int j = 0; j < mailetConfs.length; j++ )
                {
                    Configuration c = mailetConfs[j];
                    String mailetClassName = c.getAttribute("class");
                    String matcherName = c.getAttribute("match");
                    Mailet mailet = null;
                    Matcher matcher = null;
                    try {
                        matcher = matchLoader.getMatcher(matcherName, mailetcontext);
                        //The matcher itself should log that it's been inited.
                        getLogger().info("Matcher " + matcherName + " instantiated");
                    } catch (MessagingException ex) {
                        // **** Do better job printing out exception
                        getLogger().error( "Unable to init matcher " + matcherName + ": " + ex.toString(), ex );
                        throw ex;
                    }
                    try {
                        mailet = mailetLoader.getMailet(mailetClassName, mailetcontext, c);
                        getLogger().info("Mailet " + mailetClassName + " instantiated");
                    } catch (MessagingException ex) {
                        // **** Do better job printing out exception
                        getLogger().error("Unable to init mailet " + mailetClassName + ": " + ex.getMessage());
                        throw ex;
                    }
                    //Add this pair to the proces
                    processor.add(matcher, mailet);
                }

                //Loop through all mailets within processor initializing them

                //Add a Null mailet with All matcher to the processor
                //  this is so if a message gets to the end of a processor, it will always be
                //  ghosted
                Matcher matcher = matchLoader.getMatcher("All", mailetcontext);
                Mailet mailet = mailetLoader.getMailet("Null", mailetcontext, null);
                processor.add(matcher, mailet);

                getLogger().info("processor " + processorName + " instantiated");
            } catch (Exception ex) {
                getLogger().error("Unable to init processor " + processorName + ": " + ex.getMessage());
                throw ex;
            }
        }

    }

    /**
     * This routinely checks the message spool for messages, and processes them as necessary
     */
    public void run() {

        getLogger().info("run JamesSpoolManager");
        while(true) {

            try {
                String key = spool.accept();
                MailImpl mail = spool.retrieve(key);
                getLogger().info("==== Begin processing mail " + mail.getName() + " ====");
                process(mail);
                spool.remove(key);
                getLogger().info("==== Removed from spool mail " + mail.getName() + " ====");
                mail = null;
            } catch (Exception e) {
                e.printStackTrace();
                getLogger().error("Exception in JamesSpoolManager.run " + e.getMessage());
            }
        }
    }

    /**
     * Process this mail message by the appropriate processor as designated
     * in the state of the Mail object.
     */
    protected void process(MailImpl mail) {
        while (true) {
            String processorName = mail.getState();
            try {
                LinearProcessor processor = (LinearProcessor)processors.get(processorName);
                if (processor == null) {
                    throw new MailetException("Unable to find processor " + processorName);
                }
                getLogger().info("Processing " + mail.getName() + " through " + processorName);
                processor.service(mail);
                return;
            } catch (Exception e) {
                //This is a strange error situation that shouldn't ordinarily happen
                System.err.println("Exception in processor <" + processorName + ">");
                e.printStackTrace();
                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 error processor
                    mail.setState("Mail.ERROR");
                    mail.setErrorMessage(e.getMessage());
                }
            }
            getLogger().info("Processed " + mail.getName() + " through " + processorName);
            getLogger().info("Result was " + mail.getState());

        }
    }

    public void stop() {}
}<commit_msg>Fixed minor mistake in code that was causing Mail to have invalid state<commit_after>/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.james.transport;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.mail.MessagingException;
import org.apache.avalon.AbstractLoggable;
import org.apache.avalon.ComponentManager;
import org.apache.avalon.Composer;
import org.apache.avalon.Context;
import org.apache.avalon.Contextualizable;
import org.apache.avalon.DefaultComponentManager;
import org.apache.avalon.DefaultContext;
import org.apache.avalon.Initializable;
import org.apache.avalon.Stoppable;
import org.apache.avalon.configuration.Configurable;
import org.apache.avalon.configuration.Configuration;
import org.apache.avalon.configuration.ConfigurationException;
import org.apache.james.*;
import org.apache.james.core.*;
import org.apache.james.services.*;
import org.apache.mailet.*;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class JamesSpoolManager 
    extends AbstractLoggable
    implements Composer, Configurable, Initializable, Runnable, Stoppable, Contextualizable {

    private DefaultComponentManager compMgr;
    //using implementation as we need put method.
    private Configuration conf;
    private Context context;
    private SpoolRepository spool;
    private MailetContext mailetcontext;
    private HashMap processors;

    public void configure(Configuration conf) throws ConfigurationException {
        this.conf = conf;
    }

    public void contextualize(Context context) {
        this.context = new DefaultContext( context );
    }

    public void compose(ComponentManager comp) {
        compMgr = new DefaultComponentManager(comp);
    }

    public void init() throws Exception {

        getLogger().info("JamesSpoolManager init...");
        spool = (SpoolRepository) compMgr.lookup("org.apache.james.services.SpoolRepository");
        mailetcontext = (MailetContext) compMgr.lookup("org.apache.mailet.MailetContext");
        MailetLoader mailetLoader = new MailetLoader();
        MatchLoader matchLoader = new MatchLoader();
        try {
            mailetLoader.configure(conf.getChild("mailetpackages"));
            matchLoader.configure(conf.getChild("matcherpackages"));
            compMgr.put(Resources.MAILET_LOADER, mailetLoader);
            compMgr.put(Resources.MATCH_LOADER, matchLoader);
        } catch (ConfigurationException ce) {
            final String message = 
                "Unable to configure mailet/matcher Loaders: " + ce.getMessage();
            getLogger().error( message, ce );
            throw new RuntimeException( message );
        }

        //A processor is a Collection of
        processors = new HashMap();

        final Configuration[] processorConfs = conf.getChildren( "processor" );
        for ( int i = 0; i < processorConfs.length; i++ )
        {
            Configuration processorConf = processorConfs[i];
            String processorName = processorConf.getAttribute("name");
            try {
                LinearProcessor processor = new LinearProcessor();
                processor.setSpool(spool);
                processor.setLogger( getLogger() );
                processor.init();
                processors.put(processorName, processor);

                //If this is the root processor, add the PostmasterAlias mailet silently
                //  to the top
                if (processorName.equals("root")) {
                    Matcher matcher = matchLoader.getMatcher("All", mailetcontext);
                    Mailet mailet = 
                        mailetLoader.getMailet("PostmasterAlias", mailetcontext, null);
                    processor.add(matcher, mailet);
                }

                final Configuration[] mailetConfs = processorConf.getChildren( "mailet" );
                for ( int j = 0; j < mailetConfs.length; j++ )
                {
                    Configuration c = mailetConfs[j];
                    String mailetClassName = c.getAttribute("class");
                    String matcherName = c.getAttribute("match");
                    Mailet mailet = null;
                    Matcher matcher = null;
                    try {
                        matcher = matchLoader.getMatcher(matcherName, mailetcontext);
                        //The matcher itself should log that it's been inited.
                        getLogger().info("Matcher " + matcherName + " instantiated");
                    } catch (MessagingException ex) {
                        // **** Do better job printing out exception
                        getLogger().error( "Unable to init matcher " + matcherName + ": " + ex.toString(), ex );
                        throw ex;
                    }
                    try {
                        mailet = mailetLoader.getMailet(mailetClassName, mailetcontext, c);
                        getLogger().info("Mailet " + mailetClassName + " instantiated");
                    } catch (MessagingException ex) {
                        // **** Do better job printing out exception
                        getLogger().error("Unable to init mailet " + mailetClassName + ": " + ex.getMessage());
                        throw ex;
                    }
                    //Add this pair to the proces
                    processor.add(matcher, mailet);
                }

                //Loop through all mailets within processor initializing them

                //Add a Null mailet with All matcher to the processor
                //  this is so if a message gets to the end of a processor, it will always be
                //  ghosted
                Matcher matcher = matchLoader.getMatcher("All", mailetcontext);
                Mailet mailet = mailetLoader.getMailet("Null", mailetcontext, null);
                processor.add(matcher, mailet);

                getLogger().info("processor " + processorName + " instantiated");
            } catch (Exception ex) {
                getLogger().error("Unable to init processor " + processorName + ": " + ex.getMessage());
                throw ex;
            }
        }

    }

    /**
     * This routinely checks the message spool for messages, and processes them as necessary
     */
    public void run() {

        getLogger().info("run JamesSpoolManager");
        while(true) {

            try {
                String key = spool.accept();
                MailImpl mail = spool.retrieve(key);
                getLogger().info("==== Begin processing mail " + mail.getName() + " ====");
                process(mail);
                spool.remove(key);
                getLogger().info("==== Removed from spool mail " + mail.getName() + " ====");
                mail = null;
            } catch (Exception e) {
                e.printStackTrace();
                getLogger().error("Exception in JamesSpoolManager.run " + e.getMessage());
            }
        }
    }

    /**
     * Process this mail message by the appropriate processor as designated
     * in the state of the Mail object.
     */
    protected void process(MailImpl mail) {
        while (true) {
            String processorName = mail.getState();
            try {
                LinearProcessor processor = (LinearProcessor)processors.get(processorName);
                if (processor == null) {
                    throw new MailetException("Unable to find processor " + processorName);
                }
                getLogger().info("Processing " + mail.getName() + " through " + processorName);
                processor.service(mail);
                return;
            } catch (Exception e) {
                //This is a strange error situation that shouldn't ordinarily happen
                System.err.println("Exception in processor <" + processorName + ">");
                e.printStackTrace();
                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 error processor
                    mail.setState(Mail.ERROR);
                    mail.setErrorMessage(e.getMessage());
                }
            }
            getLogger().info("Processed " + mail.getName() + " through " + processorName);
            getLogger().info("Result was " + mail.getState());

        }
    }

    public void stop() {}
}
{'fraction_non_alphanumeric': 0.09909600640805584, 'fraction_numerical': 0.0004577182744021055, 'mean_word_length': 1.8953122411793937, 'pattern_counts': {'":': 6, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '411711', 'n_tokens_mistral': 4401, 'n_tokens_neox': 4177, 'n_words': 1142}