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

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 NatMulTCPManagedConnection implements ManagedConnection {
	private boolean debug;
	private TestConnection con;
	private long sd;
	// create
	public NatMulTCPManagedConnection(String host, int port, boolean debug) throws ResourceException {
		this.debug = debug;
		sd = NatMulTCP.open(host, port);
		if(sd == 0) {
			throw new ResourceException("NatMulTCP.open returned 0");
		}
		if(debug) {
			System.out.printf("Managed connection created (%s:%d)\n", host, port);
		}
	}
	// business logic
	public int add(int a, int b) throws ResourceException {
		if(!NatMulTCP.send(sd, a + " " + b + "\r\n")) { // add \r\n
			throw new ResourceException("NatMulTCP.send returned false");
		}
		String line = NatMulTCP.receive(sd);
		if(line == null) {
			throw new ResourceException("NatMulTCP.receive returned null");
		}
		line = line.substring(0, line.length() - 2); // drop \r\n
		int c = Integer.parseInt(line);
		return c;
	}
	// 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 {
		NatMulTCP.close(sd);
		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");
	}
}
