/*
 * JBoss, the OpenSource J2EE webOS
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.deployers.rar.mcf1;

import java.io.PrintWriter;
import java.util.Iterator;

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 EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;

public class MCF1ManagedConnection implements ManagedConnection, LocalTransaction
{
   private SynchronizedBoolean destroyed = new SynchronizedBoolean(false); 
   private CopyOnWriteArraySet connections = new CopyOnWriteArraySet();
   private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet();
   //private MCF1ManagedConnectionFactory mcf;
   
   public MCF1ManagedConnection(MCF1ManagedConnectionFactory mcf)
   {
      //this.mcf = mcf;
   }
   
   public void addConnectionEventListener(ConnectionEventListener listener)
   {
      checkDestroyed();
      listeners.add(listener);
   }

   public void associateConnection(Object connection) throws ResourceException
   {
      checkDestroyed();
      if (connection instanceof MCF1ConnectionImpl)
         throw new ResourceException("Wrong object");
      ((MCF1ConnectionImpl) connection).setManagedConnection(this);
      connections.add(connection);
   }

   public void cleanup() throws ResourceException
   {
      for (Iterator i = connections.iterator(); i.hasNext();)
      {
         MCF1ConnectionImpl connection = (MCF1ConnectionImpl) i.next();
         connection.setManagedConnection(null);
      }
      connections.clear();
   }

   public void destroy() throws ResourceException
   {
      destroyed.set(true);
      cleanup();
   }

   public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException
   {
      MCF1ConnectionImpl connection = new MCF1ConnectionImpl();
      connection.setManagedConnection(this);
      return connection;
   }

   public LocalTransaction getLocalTransaction() throws ResourceException
   {
      return this;
   }

   public PrintWriter getLogWriter() throws ResourceException
   {
      return null;
   }

   public ManagedConnectionMetaData getMetaData() throws ResourceException
   {
      return null;
   }

   public XAResource getXAResource() throws ResourceException
   {
      return null;
   }

   public void removeConnectionEventListener(ConnectionEventListener listener)
   {
      listeners.remove(listener);
   }

   public void setLogWriter(PrintWriter out) throws ResourceException
   {
   }

   public void begin() throws ResourceException
   {
   }

   public void commit() throws ResourceException
   {
   }

   public void rollback() throws ResourceException
   {
   }

   protected void checkDestroyed()
   {
      if (destroyed.get())
         throw new IllegalStateException("Destroyed");
   }
   
   void closeHandle(MCF1ConnectionImpl connection)
   {
      if (destroyed.get())
         return;
      
      connections.remove(connection);
      
      ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
      ce.setConnectionHandle(connection);
      for (Iterator i = listeners.iterator(); i.hasNext();)
      {
         ConnectionEventListener listener = (ConnectionEventListener) i.next();
         listener.connectionClosed(ce);
      }
   }
}
