<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 org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.james.core.MatcherConfigImpl;
import org.apache.mailet.MailetContext;
import org.apache.mailet.MailetException;
import org.apache.mailet.Matcher;

import javax.mail.MessagingException;
import java.util.Vector;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class MatchLoader implements Component, Configurable {

    private Configuration conf;
    private Vector matcherPackages;

    public void configure(Configuration conf) throws ConfigurationException {
        matcherPackages = new Vector();
        matcherPackages.addElement("");
        final Configuration[] pkgConfs = conf.getChildren( "matcherpackage" );
        for ( int i = 0; i < pkgConfs.length; i++ )
        {
            Configuration c = pkgConfs[i];
            String packageName = c.getValue();
            if (!packageName.endsWith(".")) {
                packageName += ".";
            }
            matcherPackages.addElement(packageName);
        }
    }

    public Matcher getMatcher(String matchName, MailetContext context)
        throws MessagingException {
        try {
            String condition = (String) null;
            int i = matchName.indexOf('=');
            if (i != -1) {
                condition = matchName.substring(i + 1);
                matchName = matchName.substring(0, i);
            }
            for (i = 0; i < matcherPackages.size(); i++) {
                String className = (String)matcherPackages.elementAt(i) + matchName;
                try {
                    MatcherConfigImpl configImpl = new MatcherConfigImpl();
                    configImpl.setCondition(condition);
                    configImpl.setMailetContext(context);

                    Matcher matcher = (Matcher) Class.forName(className).newInstance();
                    matcher.init(configImpl);
                    return matcher;
                } catch (ClassNotFoundException cnfe) {
                    //do this so we loop through all the packages
                }
            }
            throw new ClassNotFoundException("Requested matcher not found: " + matchName + ".  looked in " + matcherPackages.toString());
        } catch (MessagingException me) {
            throw me;
        } catch (Exception e) {
            throw new MailetException("Could not load matcher (" + matchName + ")", e);
        }
    }
}
<commit_msg>Sets the matcher name on the MatcherConfig impl object.<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 org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.james.core.MatcherConfigImpl;
import org.apache.mailet.MailetContext;
import org.apache.mailet.MailetException;
import org.apache.mailet.Matcher;

import javax.mail.MessagingException;
import java.util.Vector;

/**
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class MatchLoader implements Component, Configurable {

    private Configuration conf;
    private Vector matcherPackages;

    public void configure(Configuration conf) throws ConfigurationException {
        matcherPackages = new Vector();
        matcherPackages.addElement("");
        final Configuration[] pkgConfs = conf.getChildren( "matcherpackage" );
        for ( int i = 0; i < pkgConfs.length; i++ )
        {
            Configuration c = pkgConfs[i];
            String packageName = c.getValue();
            if (!packageName.endsWith(".")) {
                packageName += ".";
            }
            matcherPackages.addElement(packageName);
        }
    }

    public Matcher getMatcher(String matchName, MailetContext context)
        throws MessagingException {
        try {
            String condition = (String) null;
            int i = matchName.indexOf('=');
            if (i != -1) {
                condition = matchName.substring(i + 1);
                matchName = matchName.substring(0, i);
            }
            for (i = 0; i < matcherPackages.size(); i++) {
                String className = (String)matcherPackages.elementAt(i) + matchName;
                try {
                    MatcherConfigImpl configImpl = new MatcherConfigImpl();
                    configImpl.setMatcherName(matchName);
                    configImpl.setCondition(condition);
                    configImpl.setMailetContext(context);

                    Matcher matcher = (Matcher) Class.forName(className).newInstance();
                    matcher.init(configImpl);
                    return matcher;
                } catch (ClassNotFoundException cnfe) {
                    //do this so we loop through all the packages
                }
            }
            throw new ClassNotFoundException("Requested matcher not found: " + matchName + ".  looked in " + matcherPackages.toString());
        } catch (MessagingException me) {
            throw me;
        } catch (Exception e) {
            throw new MailetException("Could not load matcher (" + matchName + ")", e);
        }
    }
}

{'fraction_non_alphanumeric': 0.09789156626506024, 'fraction_numerical': 0.0023427041499330657, 'mean_word_length': 2.3205555555555555, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, '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': '3134026', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1503, 'n_words': 403}