// Path: src/main/java/com/github/sleroy/fakesmtp/core/exception/BindPortException.java
// public final class BindPortException extends AbstractPortException {
// 
//     private static final long serialVersionUID = -4019988153141714187L;
// 
//     public BindPortException(final Exception e, final int port) {
// 	super(e, port);
//     }
// }
// 
// Path: src/main/java/com/github/sleroy/fakesmtp/core/exception/OutOfRangePortException.java
// public final class OutOfRangePortException extends AbstractPortException {
//     private static final long serialVersionUID = -8357518994968551990L;
// 
//     public OutOfRangePortException(final Exception e, final int port) {
// 	super(e, port);
//     }
// }


import java.net.InetAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.smtp.AuthenticationHandlerFactory;
import org.subethamail.smtp.helper.SimpleMessageListener;
import org.subethamail.smtp.helper.SimpleMessageListenerAdapter;
import org.subethamail.smtp.server.SMTPServer;
import com.github.sleroy.fakesmtp.core.exception.BindPortException;
import com.github.sleroy.fakesmtp.core.exception.OutOfRangePortException;
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.github.sleroy.junit.mail.server;




/**
 * Starts and stops the SMTP server.
 *
 * @author Nilhcem
 * @since 1.0
 */
public class SMTPServerHandler {

    private static final Logger	LOGGER = LoggerFactory.getLogger(SMTPServerHandler.class);
    private final SMTPServer	smtpServer;

    /**
     * Instantiates a new SMTP server handler.
     *
     * @param mailListener
     *            the mail listener
     * @param smtpAuthHandlerFactory
     *            the smtp auth handler factory
     */
    public SMTPServerHandler(final SimpleMessageListener mailListener,
            final AuthenticationHandlerFactory smtpAuthHandlerFactory) {

	smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(mailListener), smtpAuthHandlerFactory);
    }

    /**
     * Checks whether the server is running
     *
     * @return true, whether the server is running
     */
    public boolean isRunning() {
	return smtpServer.isRunning();
    }

    /**
     * Starts the server on the port and address specified in parameters.
     *
     * @param port
     *            the SMTP port to be opened.
     * @param bindAddress
     *            the address to bind to. null means bind to all.
     * @throws BindPortException
     *             when the port can't be opened.
     * @throws OutOfRangePortException
     *             when port is out of range.
     * @throws IllegalArgumentException
     *             when port is out of range.
     */
    public void startServer(final int port, final InetAddress bindAddress)
// Path: src/main/java/com/github/sleroy/fakesmtp/core/exception/BindPortException.java
// public final class BindPortException extends AbstractPortException {
// 
//     private static final long serialVersionUID = -4019988153141714187L;
// 
//     public BindPortException(final Exception e, final int port) {
// 	super(e, port);
//     }
// }
// 
// Path: src/main/java/com/github/sleroy/fakesmtp/core/exception/OutOfRangePortException.java
// public final class OutOfRangePortException extends AbstractPortException {
//     private static final long serialVersionUID = -8357518994968551990L;
// 
//     public OutOfRangePortException(final Exception e, final int port) {
// 	super(e, port);
//     }
// }


// Path: src/main/java/com/github/sleroy/junit/mail/server/SMTPServerHandler.java
import java.net.InetAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.smtp.AuthenticationHandlerFactory;
import org.subethamail.smtp.helper.SimpleMessageListener;
import org.subethamail.smtp.helper.SimpleMessageListenerAdapter;
import org.subethamail.smtp.server.SMTPServer;
import com.github.sleroy.fakesmtp.core.exception.BindPortException;
import com.github.sleroy.fakesmtp.core.exception.OutOfRangePortException;

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.github.sleroy.junit.mail.server;




/**
 * Starts and stops the SMTP server.
 *
 * @author Nilhcem
 * @since 1.0
 */
public class SMTPServerHandler {

    private static final Logger	LOGGER = LoggerFactory.getLogger(SMTPServerHandler.class);
    private final SMTPServer	smtpServer;

    /**
     * Instantiates a new SMTP server handler.
     *
     * @param mailListener
     *            the mail listener
     * @param smtpAuthHandlerFactory
     *            the smtp auth handler factory
     */
    public SMTPServerHandler(final SimpleMessageListener mailListener,
            final AuthenticationHandlerFactory smtpAuthHandlerFactory) {

	smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(mailListener), smtpAuthHandlerFactory);
    }

    /**
     * Checks whether the server is running
     *
     * @return true, whether the server is running
     */
    public boolean isRunning() {
	return smtpServer.isRunning();
    }

    /**
     * Starts the server on the port and address specified in parameters.
     *
     * @param port
     *            the SMTP port to be opened.
     * @param bindAddress
     *            the address to bind to. null means bind to all.
     * @throws BindPortException
     *             when the port can't be opened.
     * @throws OutOfRangePortException
     *             when port is out of range.
     * @throws IllegalArgumentException
     *             when port is out of range.
     */
    public void startServer(final int port, final InetAddress bindAddress)
            throws BindPortException, OutOfRangePortException {