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

hudson.plugins.im.AbstractIMConnection Maven / Gradle / Ivy

The newest version!
package hudson.plugins.im;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Abstract implementation of {@link IMConnection}.
 * All concrete implementations are advised to use this as their base class.
 * 
 * @author kutzi
 */
public abstract class AbstractIMConnection implements IMConnection {

    private final Lock connectionLock = new ReentrantLock();
    
    protected AbstractIMConnection(IMPublisherDescriptor desc) {
    }

    /**
     * Acquires a lock on the underlying connection instance.
     * Should be called before any state changing operations are done on the connection.
     * 
     * Make sure to call {@link #unlock()} in a finally block afterwards!
     */
    protected final void lock() {
        this.connectionLock.lock();
    }
    
    /**
     * Tries to acquire a lock on the underlying connection instance or times out.
     * Should be called before any state changing operations are done on the connection.
     * 
     * Make sure to call {@link #unlock()} in a finally block afterwards!
     */
    protected final boolean tryLock(long time, TimeUnit timeUnit) throws InterruptedException {
        return this.connectionLock.tryLock(time, timeUnit);
    }

    /**
     * Releases the lock on the underlying connection instance.
     */
    protected final void unlock() {
        this.connectionLock.unlock();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy