// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package jcademo.raoutbound.permtcp;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

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;

public class PermTCPManagedConnection implements ManagedConnection {
	private boolean debug;
	private String id;
	private BlockingQueue<String> q;
	private TestConnection con;
	// create
	public PermTCPManagedConnection(boolean debug) throws ResourceException {
		this.debug = debug;
		id = UUID.randomUUID().toString();
		q = new ArrayBlockingQueue<String>(10);
		TCPMultiPlexer.getInstance().register(id, q);
		if(debug) {
			System.out.println("Managed connection created");
		}
	}
	// business logic
	public int add(int a, int b) throws ResourceException {
		try {
			TCPMultiPlexer.getInstance().send(id, a + " " + b);
			String line = q.take();
			int c = Integer.parseInt(line);
			return c;
		} catch (NumberFormatException e) {
			throw new ResourceException(e);
		} catch (InterruptedException e) {
			throw new ResourceException(e);
		}
	}
	// called by Connection.close => informs connection manager that connection is closed
	public void close() {
		for(ConnectionEventListener cel : cellst) {
			ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_CLOSED);
			ce.setConnectionHandle(con);
			cel.connectionClosed(ce);
		}
	}
	// called by connection manager to destroy this instance when adapter is shutting down or pool size is decreased
	@Override
	public void destroy() throws ResourceException {
		TCPMultiPlexer.getInstance().deregister(id);
		if(debug) {
			System.out.println("Managed connection destroyed");
		}
	}
	// called by connection manager when it is informed that connection is closed
	@Override
	public void cleanup() {
		con = null;
	}
	// get connection and save connection reference
	@Override
	public TestConnection getConnection(Subject subj, ConnectionRequestInfo cri) {
		con = new TestConnectionImpl(this, debug);
		return con;
	}
	// change connection reference
	@Override
	public void associateConnection(Object con) {
		this.con = (TestConnection)con;
	}
	// manage listeners (used by connection manager)
	private List<ConnectionEventListener> cellst = new ArrayList<>();
	@Override
	public void addConnectionEventListener(ConnectionEventListener cel) {
		cellst.add(cel);
	}
	@Override
	public void removeConnectionEventListener(ConnectionEventListener cel) {
		cellst.remove(cel);
	}
	// log support:
	private PrintWriter logWriter;
	@Override
	public PrintWriter getLogWriter() throws ResourceException {
		return logWriter;
	}
	@Override
	public void setLogWriter(PrintWriter logWriter) {
		this.logWriter = logWriter;
	}
	// transaction support
	@Override
	public LocalTransaction getLocalTransaction() throws ResourceException {
		throw new ResourceException("Transaction not supported");
	}
	@Override
	public XAResource getXAResource() throws ResourceException {
		throw new ResourceException("Transaction not supported");
	}
	// meta data support
	@Override
	public ManagedConnectionMetaData getMetaData() throws ResourceException {
		throw new ResourceException("Meta data not supported");
	}
}
