/*
 * Copyright 2004-2008 IDCA. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and
 *        the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *        and the following disclaimer in the documentation and/or other materials provided with the
 *        distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the authors and
 * should not be interpreted as representing official policies, either expressed or implied, of IDCA.
 */

package com.idcanet.foei.ee.jca.cci;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.resource.NotSupportedException;
import javax.resource.ResourceException;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ConnectionEventListener;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.LocalTransaction;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionMetaData;
import javax.security.auth.Subject;
import javax.transaction.xa.XAResource;

import com.idcanet.foei.core.FoeiContext;

/**
 * 
 *
 * @author Willem Cazander
 * @version 1.0 Apr 19, 2008
 */
public class FoeiManagedConnection implements ManagedConnection {

    /** Log writer */
    private PrintWriter writer;

    /** List of listeners */
    private List<ConnectionEventListener> listeners;

    /** Set of application-level handlers */
    private Set<FoeiConnectionImpl> connectionSet;

    private FoeiContext foeiContext = null;
    
    /**
     * The constructor
     * @param conReqInfo {@link ConnectionRequestInfo}
     * @param writer log writer of the factory that calls this constructor
     * @param repositoryPath path to the repository containing files to retireve
     */
    public FoeiManagedConnection(ConnectionRequestInfo conReqInfo,PrintWriter writer, FoeiContext foeiContext) {
        this.writer = writer;
        this.listeners = new ArrayList<ConnectionEventListener>(10);
        this.connectionSet = new HashSet<FoeiConnectionImpl>(10);
        this.foeiContext = foeiContext;
    }

    /**
     * @see javax.resource.spi.ManagedConnection#getConnection(javax.security.auth.Subject,
     *      javax.resource.spi.ConnectionRequestInfo)
     */
    public Object getConnection(Subject subj, ConnectionRequestInfo conReqInfo) throws ResourceException {
        if (this.foeiContext == null) {
            throw new ResourceException("foeiContext is null");
        }
        
        FoeiConnectionImpl conn = new FoeiConnectionImpl(this);
        addConnection(conn);
        return conn;
    }

    /**
     * @see javax.resource.spi.ManagedConnection#destroy()
     */
    public void destroy() throws ResourceException {
        invalidateAllConnections();
        synchronized (this.listeners) {
            listeners = null;
        }
        foeiContext.destroy();
        foeiContext = null;
    }

    /**
     * @see javax.resource.spi.ManagedConnection#cleanup()
     */
    public void cleanup() throws ResourceException {
        invalidateAllConnections();
    }

    /**
     * @see javax.resource.spi.ManagedConnection#associateConnection(java.lang.Object)
     */
    public void associateConnection(Object conn) throws ResourceException {
        if (!(conn instanceof FoeiConnectionImpl)) {
            throw new ResourceException("Connection has an incorrect type");
        }
        ((FoeiConnectionImpl)conn).associateConnection(this);
    }

    /**
     * @see javax.resource.spi.ManagedConnection#addConnectionEventListener(javax.resource.spi.ConnectionEventListener)
     */
    public void addConnectionEventListener(ConnectionEventListener listener) {
        synchronized (this.listeners) {
            listeners.add(listener);
        }
    }

    /**
     * @see javax.resource.spi.ManagedConnection#removeConnectionEventListener(javax.resource.spi.ConnectionEventListener)
     */
    public void removeConnectionEventListener(ConnectionEventListener listener) {
        synchronized (this.listeners) {
            listeners.remove(listener);
        }
    }

    /**
     * @see javax.resource.spi.ManagedConnection#getXAResource()
     */
    public XAResource getXAResource() throws ResourceException {
        throw new NotSupportedException("XA transactions are not supported");
    }

    /**
     * @see javax.resource.spi.ManagedConnection#getLocalTransaction()
     */
    public LocalTransaction getLocalTransaction() throws ResourceException {
        throw new NotSupportedException("Transactions are not supported");
    }

    /**
     * @see javax.resource.spi.ManagedConnection#getMetaData()
     */
    public ManagedConnectionMetaData getMetaData() throws ResourceException {
        return new FoeiManagedConnectionMetaData();
    }

    /**
     * @see javax.resource.spi.ManagedConnection#setLogWriter(java.io.PrintWriter)
     */
    public void setLogWriter(PrintWriter out) throws ResourceException {
        this.writer = out;
    }

    /**
     * @see javax.resource.spi.ManagedConnection#getLogWriter()
     */
    public PrintWriter getLogWriter() throws ResourceException {
        return writer;
    }

    /**
     * Removes application-level handler from handlers set
     * @param con handler to remove
     * @see FileRetrieverConnectionImpl#associateConnection(FileRetrieverManagedConnection)
     */
    void removeConnection(FoeiConnectionImpl con) {
        synchronized (this.connectionSet) {
            connectionSet.remove(con);
        }
    }

    /**
     * Adds application-level handler to handlers set
     * @param con handler to add
     * @see FileRetrieverConnectionImpl#associateConnection(FileRetrieverManagedConnection)
     */
    void addConnection(FoeiConnectionImpl con) {
        synchronized (this.connectionSet) {
            connectionSet.add(con);
        }
    }

    /**
     * Invalidate all application-level handlers and clears handlers set
     */
    void invalidateAllConnections() {
        synchronized (this.connectionSet) {
            Iterator<FoeiConnectionImpl> itr = connectionSet.iterator();
            while (itr.hasNext()) {
            	FoeiConnectionImpl con = itr.next();
                con.invalidate();
            }
            connectionSet.clear();
        }
    }

    public FoeiContext getFoeiContext() {
    	return foeiContext; 
    }  
    
    
    /**
     * Closes connection
     * @param con connection to close
     */
    public void close(FoeiConnectionImpl con) {
        ConnectionEvent event = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
        event.setConnectionHandle(con);
        synchronized (this.listeners) {
            Iterator<ConnectionEventListener> itr = listeners.iterator();
            while (itr.hasNext()) {
                try {
                    itr.next().connectionClosed(event);
                } catch (Throwable e) {
                }
            }
        }
        con.invalidate();
        removeConnection(con);
    }
}