/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.ejb3.pool.strictmax;

import org.jboss.as.ejb3.pool.AbstractPool;
import org.jboss.as.ejb3.pool.StatelessObjectFactory;
import org.jboss.logging.Logger;

import javax.ejb.EJBException;
import java.util.LinkedList;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * A pool with a maximum size.
 *
 * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
 * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
 */
public class StrictMaxPool<T> extends AbstractPool<T> {
    private static final Logger log = Logger.getLogger(StrictMaxPool.class);

    /**
     * A FIFO semaphore that is set when the strict max size behavior is in effect.
     * When set, only maxSize instances may be active and any attempt to get an
     * instance will block until an instance is freed.
     */
    private Semaphore semaphore;
    /**
     * The maximum number of instances allowed in the pool
     */
    private int maxSize = 30;
    /**
     * The time to wait for the semaphore.
     */
    private final long timeout;
    private final TimeUnit timeUnit;
    /**
     * The pool data structure
     * Guarded by the implicit lock for "pool"
     */
    private final LinkedList<T> pool = new LinkedList<T>();

    private int inUse = 0;

    public StrictMaxPool(StatelessObjectFactory<T> factory, int maxSize, long timeout, TimeUnit timeUnit) {
        super(factory);

        this.maxSize = maxSize;
        this.semaphore = new Semaphore(maxSize, true);
        this.timeout = timeout;
        this.timeUnit = timeUnit;
    }

    public void discard(T ctx) {
        if (log.isTraceEnabled()) {
            String msg = "Discard instance:" + this + "#" + ctx;
            log.trace(msg);
        }

        // If we block when maxSize instances are in use, invoke release on strictMaxSize
        semaphore.release();
        --inUse;

        // Let the super do any other remove stuff
        super.doRemove(ctx);
    }

    public int getCurrentSize() {
        return getCreateCount() - getRemoveCount();
    }

    public int getAvailableCount() {
        return maxSize - inUse;
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        this.semaphore = new Semaphore(maxSize, true);
    }

    /**
     * Get an instance without identity.
     * Can be used by finders,create-methods, and activation
     *
     * @return Context /w instance
     */
    public T get() {
        try {
            boolean acquired = semaphore.tryAcquire(timeout, timeUnit);
            if (!acquired)
                throw new EJBException("Failed to acquire a permit within " + timeout + " " + timeUnit);
        } catch (InterruptedException e) {
            throw new EJBException("Acquire semaphore was interrupted");
        }

        synchronized (pool) {
            if (!pool.isEmpty()) {
                return pool.removeFirst();
            }
        }

        T bean = null;
        try {
            // Pool is empty, create an instance
            ++inUse;
            bean = create();
        } finally {
            if (bean == null) {
                --inUse;
                semaphore.release();
            }
        }
        return bean;
    }

    /**
     * Return an instance after invocation.
     * <p/>
     * Called in 2 cases:
     * a) Done with finder method
     * b) Just removed
     *
     * @param obj
     */
    public void release(T obj) {
        if (log.isTraceEnabled()) {
            String msg = pool.size() + "/" + maxSize + " Free instance:" + this;
            log.trace(msg);
        }

        boolean destroyIt = false;
        synchronized (pool) {
            // Add the unused context back into the pool
            if (pool.size() < maxSize)
                pool.add(obj);
            else
                destroyIt = true;
        }
        if (destroyIt)
            destroy(obj);
        // If we block when maxSize instances are in use, invoke release on strictMaxSize
        semaphore.release();
        --inUse;
    }

    @Override
    public void remove(T ctx) {
        if (log.isTraceEnabled()) {
            String msg = "Removing instance:" + this + "#" + ctx;
            log.trace(msg);
        }

        semaphore.release();
        --inUse;
        // let the super do the other remove stuff
        super.doRemove(ctx);
    }

    public void start() {
        // TODO Auto-generated method stub

    }

    public void stop() {
        synchronized (pool) {
            for (T obj : pool) {
                destroy(obj);
            }
            pool.clear();
        }
    }
}

@@ -1894,16 +1894,22 @@
 private 
+final 
 Semaphor
@@ -2004,24 +2004,30 @@
     private 
+final 
 int maxSize 
@@ -2029,13 +2029,8 @@
 Size
- = 30
 ;%0A  
@@ -2470,17 +2470,16 @@
 ctory);%0A
-%0A
         
@@ -3313,82 +3313,50 @@
   th
-is.maxSize = maxSize;%0A        this.semaphore = new Semaphore(maxSize, true
+row new RuntimeException(%22Not implemented%22
 );%0A 

Remove potential Semaphore leak