package org.greglanthier.echo;

import java.io.PrintWriter;
import java.util.Set;

import javax.resource.ResourceException;
import javax.resource.spi.ConnectionDefinition;
import javax.resource.spi.ConnectionManager;
import javax.resource.spi.ConnectionRequestInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ManagedConnectionFactory;
import javax.resource.spi.ValidatingManagedConnectionFactory;
import javax.security.auth.Subject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ConnectionDefinition(
		connectionFactory=org.greglanthier.echo.spi.EchoConnectionFactory.class,
		connectionFactoryImpl=org.greglanthier.echo.EchoConnectionFactoryImpl.class,
		connection=org.greglanthier.echo.spi.EchoManagedConnection.class,
		connectionImpl=org.greglanthier.echo.EchoManagedConnectionImpl.class
		)
public class ManagedConnectionFactoryImpl implements ManagedConnectionFactory, ValidatingManagedConnectionFactory {

	private static final transient Logger LOG = LoggerFactory.getLogger( ManagedConnectionFactoryImpl.class );

	private static final long serialVersionUID = 1L;

	ConnectionManager m_connectionManager;

	@Override
	public Object createConnectionFactory(ConnectionManager cxManager)
			throws ResourceException {
		LOG.info( this + "#createConnectionFactory( {} )", cxManager );
		return new EchoConnectionFactoryImpl( this, cxManager );
	}

	@Override
	public Object createConnectionFactory() throws ResourceException {
		LOG.info( this + "#createConnectionFactory( )" );
		return null;
	}

	@Override
	public ManagedConnection createManagedConnection(Subject subject,
			ConnectionRequestInfo cxRequestInfo) throws ResourceException {
		LOG.info( this + "#createManagedConnection( {}, {} )", subject, cxRequestInfo );
		return new EchoManagedConnectionImpl();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public ManagedConnection matchManagedConnections(Set connectionSet,
			Subject subject, ConnectionRequestInfo cxRequestInfo)
			throws ResourceException {
		ManagedConnection answer = (ManagedConnection) connectionSet.iterator().next();
		LOG.info( this + "#matchManagedConnection( {}, {}, {} ): {}", connectionSet, subject, cxRequestInfo, answer );
		return answer;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws ResourceException {

	}

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

	@SuppressWarnings("rawtypes")
	@Override
	public Set getInvalidConnections(Set connectionSet)
			throws ResourceException {
		LOG.info( this + "#getInvalidConnections( {} ): {}", connectionSet, connectionSet );
		return connectionSet;
	}

}

@@ -49,24 +49,51 @@
 rintWriter;%0A
+import java.util.Iterator;%0A
 import java.
@@ -102,16 +102,16 @@
 il.Set;%0A
-
 %0Aimport 
@@ -375,32 +375,129 @@
 nectionFactory;%0A
+import javax.resource.spi.ResourceAdapter;%0Aimport javax.resource.spi.ResourceAdapterAssociation;%0A
 import javax.res
@@ -1035,16 +1035,44 @@
 Factory,
+ ResourceAdapterAssociation,
  Validat
@@ -2169,87 +2169,355 @@
 %7B%0A%09%09
-ManagedConnection answer = (ManagedConnection) connectionSet.iterator().next();
+Iterator iterator = connectionSet.iterator();%0A%09%09if ( ! iterator.hasNext() ) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%0A%09%09ManagedConnection answer = null;%0A%09%09for ( ; iterator.hasNext(); ) %7B%0A%09%09%09Object next = iterator.next();%0A%09%09%09if ( ! ManagedConnection.class.isAssignableFrom( next.getClass() ) ) %7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%09%09%09answer = (ManagedConnection) next;%0A%09%09%09break;%0A%09%09%7D%0A
 %0A%09%09L
@@ -3033,24 +3033,24 @@
 ctionSet );%0A
-
 %09%09return con
@@ -3065,10 +3065,273 @@
 et;%0A%09%7D%0A%0A
+%09@Override%0A%09public ResourceAdapter getResourceAdapter() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09return null;%0A%09%7D%0A%0A%09@Override%0A%09public void setResourceAdapter(ResourceAdapter ra) throws ResourceException %7B%0A%09%09LOG.info( this + %22#setResourceAdapter( %7B%7D )%22, ra );%0A%0A%09%7D%0A%0A
 %7D%0A

Make the ManagedConnectionFactoryImpl implement the ResourceAdapterAssociation interface so the container will give us the ResourceAdapter instance (so we can access the bootstrap context for the WorkManager, for example).