All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nlab.smtp.transport.DefaultClosableSmtpConnection Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package org.nlab.smtp.transport;

import org.apache.commons.pool2.ObjectPool;
import org.nlab.smtp.pool.ObjectPoolAware;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.event.TransportListener;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by nlabrot on 30/04/15.
 */
public class DefaultClosableSmtpConnection implements ClosableSmtpConnection, ObjectPoolAware {

    private final Transport delegate;
    private ObjectPool objectPool;

    private final LinkedBlockingQueue transportListeners = new LinkedBlockingQueue<>();

    public DefaultClosableSmtpConnection(Transport delegate) {
        this.delegate = delegate;
    }

    public void sendMessage(Message msg, Address[] addresses)
            throws MessagingException{
        delegate.sendMessage(msg, addresses);
    }

    public void addTransportListener(TransportListener l){
        transportListeners.add(l);
        delegate.addTransportListener(l);
    }

    public void removeTransportListener(TransportListener l){
        transportListeners.remove(l);
        delegate.removeTransportListener(l);
    }

    public void clearListeners(){
        for (TransportListener transportListener : transportListeners) {
            delegate.removeTransportListener(transportListener);
        }
        transportListeners.clear();
    }

    public boolean isConnected(){
        return delegate.isConnected();
    }


    @Override
    public void close() throws Exception {
        objectPool.returnObject(this);
    }

    @Override
    public ObjectPool getObjectPool() {
        return objectPool;
    }

    @Override
    public void setObjectPool(ObjectPool objectPool) {
        this.objectPool = objectPool;
    }

    @Override
    public Transport getDelegate() {
        return delegate;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy