<commit_before>package com.icegreen.greenmail.spring;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;

/**
 * Spring bean for GreenMail server.
 * <p/>
 * By default,
 * <ul>
 *  <li>SMTP, POP3 services are activated</li>
 *  <li>autostart is enabled</li>
 *  <li>port offset is 3000</li>
 * </ul>
 *
 * @author <NAME> (mm)
 */
public class GreenMailBean implements InitializingBean, DisposableBean, BeanNameAware {
    /** New logger. */
    protected final Logger log = LoggerFactory.getLogger(getClass());

    /** Spring bean name. */
    private String name;

    /** The mail server. */
    private GreenMail greenMail;
    /** Automatically start the servers. */
    private boolean autostart = true;
    /** SMTP server */
    private boolean smtpProtocol = true;
    /** SMTPS server */
    private boolean smtpsProtocol = false;
    /** POP3 server */
    private boolean pop3Protocol = true;
    /** POP3S server */
    private boolean pop3sProtocol = false;
    /** IMAP server. */
    private boolean imapProtocol = false;
    /** IMAPS server. */
    private boolean imapsProtocol = false;
    /** Users. */
    private List<String> users;
    /** Port offset (default is 3000) */
    private int portOffset = 3000;
    /** Hostname. Default is null (= localhost). */
    private String hostname;
    /** If the server is started. */
    private boolean started;
    /** Outoing mail server setup. */
    private ServerSetup smtpServerSetup;
    /** Outoing secure mail server setup. */
    private ServerSetup smtpsServerSetup;
    /** Timeout to wait for server startup in millis */
    private long serverStartupTimeout = 1000L;

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied
     * BeanFactoryAware and ApplicationContextAware). <p>This method allows the bean instance to
     * perform initialization only possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     *
     * @throws Exception in the event of misconfiguration (such as failure to set an essential
     *                   property) or if initialization fails.
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        greenMail = new GreenMail(createServerSetup());
        if (null != users) {
            for (String user : users) {
                int posColon = user.indexOf(':');
                int posAt = user.indexOf('@');
                String login = user.substring(0, posColon);
                String pwd = user.substring(posColon + 1, posAt);
                String domain = user.substring(posAt + 1);
                if (log.isDebugEnabled()) {
                    log.debug("Adding user " + login + ':' + pwd + '@' + domain);
                }
                greenMail.setUser(login + '@' + domain, login, pwd);
            }
        }
        if (autostart) {
            greenMail.start();
            started = true;
        }
    }

    /**
     * Creates the server setup, depending on the protocoll flags.
     *
     * @return the configured server setups.
     */
    private ServerSetup[] createServerSetup() {
        List<ServerSetup> setups = new ArrayList<>();
        if (smtpProtocol) {
            smtpServerSetup = createTestServerSetup(ServerSetup.SMTP);
            setups.add(smtpServerSetup);
        }
        if (smtpsProtocol) {
            smtpsServerSetup = createTestServerSetup(ServerSetup.SMTPS);
            setups.add(smtpsServerSetup);
        }
        if (pop3Protocol) {
            setups.add(createTestServerSetup(ServerSetup.POP3));
        }
        if (pop3sProtocol) {
            setups.add(createTestServerSetup(ServerSetup.POP3S));
        }
        if (imapProtocol) {
            setups.add(createTestServerSetup(ServerSetup.IMAP));
        }
        if (imapsProtocol) {
            setups.add(createTestServerSetup(ServerSetup.IMAPS));
        }
        return setups.toArray(new ServerSetup[setups.size()]);
    }

    /** Starts the server. */
    public void start() {
        synchronized (greenMail) {
            if (started) {
                log.warn("Can not start server (already started)");
            } else {
                greenMail.start();
                started = true;
            }
        }
    }

    /** Stops the server. */
    public void stop() {
        synchronized (greenMail) {
            if (started) {
                greenMail.stop();
                started = true;
            } else {
                log.warn("Can not stop server (not started).");
            }
        }
    }

    /**<commit_msg>Fixed stop method.<commit_after>package com.icegreen.greenmail.spring;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;

/**
 * Spring bean for GreenMail server.
 * <p/>
 * By default,
 * <ul>
 *  <li>SMTP, POP3 services are activated</li>
 *  <li>autostart is enabled</li>
 *  <li>port offset is 3000</li>
 * </ul>
 *
 * @author <NAME> (mm)
 */
public class GreenMailBean implements InitializingBean, DisposableBean, BeanNameAware {
    /** New logger. */
    protected final Logger log = LoggerFactory.getLogger(getClass());

    /** Spring bean name. */
    private String name;

    /** The mail server. */
    private GreenMail greenMail;
    /** Automatically start the servers. */
    private boolean autostart = true;
    /** SMTP server */
    private boolean smtpProtocol = true;
    /** SMTPS server */
    private boolean smtpsProtocol = false;
    /** POP3 server */
    private boolean pop3Protocol = true;
    /** POP3S server */
    private boolean pop3sProtocol = false;
    /** IMAP server. */
    private boolean imapProtocol = false;
    /** IMAPS server. */
    private boolean imapsProtocol = false;
    /** Users. */
    private List<String> users;
    /** Port offset (default is 3000) */
    private int portOffset = 3000;
    /** Hostname. Default is null (= localhost). */
    private String hostname;
    /** If the server is started. */
    private boolean started;
    /** Outoing mail server setup. */
    private ServerSetup smtpServerSetup;
    /** Outoing secure mail server setup. */
    private ServerSetup smtpsServerSetup;
    /** Timeout to wait for server startup in millis */
    private long serverStartupTimeout = 1000L;

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied
     * BeanFactoryAware and ApplicationContextAware). <p>This method allows the bean instance to
     * perform initialization only possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     *
     * @throws Exception in the event of misconfiguration (such as failure to set an essential
     *                   property) or if initialization fails.
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        greenMail = new GreenMail(createServerSetup());
        if (null != users) {
            for (String user : users) {
                int posColon = user.indexOf(':');
                int posAt = user.indexOf('@');
                String login = user.substring(0, posColon);
                String pwd = user.substring(posColon + 1, posAt);
                String domain = user.substring(posAt + 1);
                if (log.isDebugEnabled()) {
                    log.debug("Adding user " + login + ':' + pwd + '@' + domain);
                }
                greenMail.setUser(login + '@' + domain, login, pwd);
            }
        }
        if (autostart) {
            greenMail.start();
            started = true;
        }
    }

    /**
     * Creates the server setup, depending on the protocoll flags.
     *
     * @return the configured server setups.
     */
    private ServerSetup[] createServerSetup() {
        List<ServerSetup> setups = new ArrayList<>();
        if (smtpProtocol) {
            smtpServerSetup = createTestServerSetup(ServerSetup.SMTP);
            setups.add(smtpServerSetup);
        }
        if (smtpsProtocol) {
            smtpsServerSetup = createTestServerSetup(ServerSetup.SMTPS);
            setups.add(smtpsServerSetup);
        }
        if (pop3Protocol) {
            setups.add(createTestServerSetup(ServerSetup.POP3));
        }
        if (pop3sProtocol) {
            setups.add(createTestServerSetup(ServerSetup.POP3S));
        }
        if (imapProtocol) {
            setups.add(createTestServerSetup(ServerSetup.IMAP));
        }
        if (imapsProtocol) {
            setups.add(createTestServerSetup(ServerSetup.IMAPS));
        }
        return setups.toArray(new ServerSetup[setups.size()]);
    }

    /** Starts the server. */
    public void start() {
        synchronized (greenMail) {
            if (started) {
                log.warn("Can not start server (already started)");
            } else {
                greenMail.start();
                started = true;
            }
        }
    }

    /** Stops the server. */
    public void stop() {
        synchronized (greenMail) {
            if (started) {
                greenMail.stop();
                started = false;
            } else {
                log.warn("Can not stop server (not started).");
            }
        }
    }

    /**
{'fraction_non_alphanumeric': 0.10676825648129824, 'fraction_numerical': 0.005937067088858104, 'mean_word_length': 2.1663533834586466, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '6964039', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2730, 'n_words': 754}