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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
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 MulTCPManagedConnection implements ManagedConnection {
	private boolean debug;
	private TestConnection con;
	private Socket s;
	private PrintStream ps;
	private BufferedReader br;
	// create
	public MulTCPManagedConnection(String host, int port, boolean debug) throws ResourceException {
		this.debug = debug;
		try {
			s = new Socket(host, port);
			ps = new PrintStream(s.getOutputStream(), true, "UTF-8");
			br = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
		} catch (IOException e) {
			throw new ResourceException(e);
		}
		if(debug) {
			System.out.printf("Managed connection created (%s:%d)\n", host, port);
		}
	}
	// business logic
	public int add(int a, int b) throws ResourceException {
		try {
			ps.println(a + " " + b);
			ps.flush();
			String line = br.readLine();
			int c = Integer.parseInt(line);
			return c;
		} catch (IOException 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 {
		try {
			br.close();
			ps.close();
			s.close();
		} catch (IOException e) {
			throw new ResourceException(e);
		}
		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");
	}
}
