package org.jboss.test.jca.adapter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
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.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.jboss.logging.Logger;
public class TestManagedConnection implements ManagedConnection, XAResource
{
private final int id;
private Logger log = Logger.getLogger(getClass());
private List handles = new LinkedList();
private List listeners = new LinkedList();
private Subject subject;
private TestConnectionRequestInfo cri;
private Xid xid;
private boolean destroyed = false;
private boolean failInPrepare = false;
private boolean failInCommit = false;
private int xaCode;
public TestManagedConnection (final Subject subject, final TestConnectionRequestInfo cri, final int id)
{
this.subject = subject;
this.cri = cri;
this.id = id;
}
void setFailInPrepare(final boolean fail, final int xaCode)
{
this.failInPrepare = fail;
this.xaCode = xaCode;
}
void setFailInCommit(final boolean fail, final int xaCode)
{
this.failInCommit = fail;
this.xaCode = xaCode;
}
public void destroy() throws ResourceException
{
log.info("Destroying connection");
cleanup();
destroyed = true;
}
public void cleanup() throws ResourceException
{
for (Iterator i = handles.iterator(); i.hasNext(); )
{
TestConnection c = (TestConnection)i.next();
c.setMc(null);
i.remove();
}
}
public Object getConnection(Subject param1, ConnectionRequestInfo param2) throws ResourceException
{
if (param2 != null && ((TestConnectionRequestInfo) param2).failure.equals("getConnectionResource"))
throw new ResourceException("");
if (param2 != null && ((TestConnectionRequestInfo) param2).failure.equals("getConnectionRuntime"))
throw new RuntimeException("");
TestConnection c = new TestConnection(this);
handles.add(c);
return c;
}
public void associateConnection(Object p) throws ResourceException
{
if (p instanceof TestConnection)
{
((TestConnection)p).setMc(this);
handles.add(p);
} else
{
throw new ResourceException("wrong kind of Connection");
}
}
public void addConnectionEventListener(ConnectionEventListener cel)
{
log.info("adding 1 cel");
listeners.add(cel);
}
public void removeConnectionEventListener(ConnectionEventListener cel)
{
log.info("removing 1 cel");
listeners.remove(cel);
}
public XAResource getXAResource() throws ResourceException
{
return this;
}
public LocalTransaction getLocalTransaction() throws ResourceException
{
return null;
}
public ManagedConnectionMetaData getMetaData() throws ResourceException
{
return null;
}
public void setLogWriter(PrintWriter param1) throws ResourceException
{
}
public PrintWriter getLogWriter() throws ResourceException
{
return null;
}
public void start(Xid xid, int flags) throws XAException
{
log.info("start with xid " + xid + ", flags " + flags);
this.xid = xid;
}
public void end(final Xid xid, final int flags) throws XAException
{
log.info("end with xid " + xid + ", flags " + flags);
if (!xid.equals(this.xid))
{
log.info("wrong xid ended: have " + this.xid + ", got: " + xid);
} this.xid = null;
}
public void commit(Xid xid, boolean onePhase) throws XAException
{
log.info("commit with xid " + xid);
if (failInCommit)
{
throw new XAException(xaCode);
} }
public void rollback(Xid xid) throws XAException
{
log.info("rollback with xid " + xid);
}
public int prepare(Xid xid) throws XAException
{
log.info("prepare with xid " + xid);
if (failInPrepare)
{
throw new XAException(xaCode);
}
return 0;
}
public void forget(Xid xid) throws XAException
{
log.info("forget with xid " + xid);
}
public Xid[] recover(int param1) throws XAException
{
return null;
}
public boolean isSameRM(XAResource xar) throws XAException
{
return this == xar;
}
public int getTransactionTimeout() throws XAException
{
return 0;
}
public boolean setTransactionTimeout(int param1) throws XAException
{
return false;
}
boolean isInTx()
{
log.info("isInTx, xid: " + xid);
return xid != null;
}
void connectionClosed(TestConnection handle)
{
if (destroyed)
return;
ConnectionEvent ce = new ConnectionEvent(this ,ConnectionEvent.CONNECTION_CLOSED);
ce.setConnectionHandle(handle);
Collection copy = new ArrayList(listeners);
for (Iterator i = copy.iterator(); i.hasNext(); )
{
log.info("notifying 1 cel connectionClosed");
ConnectionEventListener cel = (ConnectionEventListener)i.next();
try
{
cel.connectionClosed(ce);
}
catch (Throwable ignored)
{
ignored.printStackTrace();
}
} handles.remove(handle);
}
void connectionError(TestConnection handle, Exception e)
{
ConnectionEvent ce = new ConnectionEvent(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, e);
ce.setConnectionHandle(handle);
Collection copy = new ArrayList(listeners);
for (Iterator i = copy.iterator(); i.hasNext(); )
{
ConnectionEventListener cel = (ConnectionEventListener)i.next();
try
{
cel.connectionErrorOccurred(ce);
}
catch (Throwable ignored)
{
}
} }
public String toString()
{
return "tmc: " + id;
}
}