001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jcaprops.support;
023:
024: import java.io.PrintWriter;
025: import java.util.Iterator;
026:
027: import javax.resource.ResourceException;
028: import javax.resource.spi.ConnectionEvent;
029: import javax.resource.spi.ConnectionEventListener;
030: import javax.resource.spi.ConnectionRequestInfo;
031: import javax.resource.spi.LocalTransaction;
032: import javax.resource.spi.ManagedConnection;
033: import javax.resource.spi.ManagedConnectionMetaData;
034: import javax.security.auth.Subject;
035: import javax.transaction.xa.XAResource;
036:
037: import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
038: import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
039:
040: /**
041: * A PropertyTestManagedConnection.
042: *
043: * @author <a href="adrian@jboss.com">Adrian Brock</a>
044: * @version $Revision: 57211 $
045: */
046: public class PropertyTestManagedConnection implements
047: ManagedConnection, LocalTransaction {
048: private SynchronizedBoolean destroyed = new SynchronizedBoolean(
049: false);
050: private CopyOnWriteArraySet connections = new CopyOnWriteArraySet();
051: private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet();
052:
053: //private PropertyTestManagedConnectionFactory mcf;
054:
055: public PropertyTestManagedConnection(
056: PropertyTestManagedConnectionFactory mcf) {
057: //this.mcf = mcf;
058: }
059:
060: public void addConnectionEventListener(
061: ConnectionEventListener listener) {
062: checkDestroyed();
063: listeners.add(listener);
064: }
065:
066: public void associateConnection(Object connection)
067: throws ResourceException {
068: checkDestroyed();
069: if (connection instanceof PropertyTestConnectionImpl)
070: throw new ResourceException("Wrong object");
071: ((PropertyTestConnectionImpl) connection)
072: .setManagedConnection(this );
073: connections.add(connection);
074: }
075:
076: public void cleanup() throws ResourceException {
077: for (Iterator i = connections.iterator(); i.hasNext();) {
078: PropertyTestConnectionImpl connection = (PropertyTestConnectionImpl) i
079: .next();
080: connection.setManagedConnection(null);
081: }
082: connections.clear();
083: }
084:
085: public void destroy() throws ResourceException {
086: destroyed.set(true);
087: cleanup();
088: }
089:
090: public Object getConnection(Subject subject,
091: ConnectionRequestInfo cxRequestInfo)
092: throws ResourceException {
093: PropertyTestConnectionImpl connection = new PropertyTestConnectionImpl();
094: connection.setManagedConnection(this );
095: return connection;
096: }
097:
098: public LocalTransaction getLocalTransaction()
099: throws ResourceException {
100: return this ;
101: }
102:
103: public PrintWriter getLogWriter() throws ResourceException {
104: return null;
105: }
106:
107: public ManagedConnectionMetaData getMetaData()
108: throws ResourceException {
109: return null;
110: }
111:
112: public XAResource getXAResource() throws ResourceException {
113: return null;
114: }
115:
116: public void removeConnectionEventListener(
117: ConnectionEventListener listener) {
118: listeners.remove(listener);
119: }
120:
121: public void setLogWriter(PrintWriter out) throws ResourceException {
122: }
123:
124: public void begin() throws ResourceException {
125: }
126:
127: public void commit() throws ResourceException {
128: }
129:
130: public void rollback() throws ResourceException {
131: }
132:
133: protected void checkDestroyed() {
134: if (destroyed.get())
135: throw new IllegalStateException("Destroyed");
136: }
137:
138: void closeHandle(PropertyTestConnectionImpl connection) {
139: if (destroyed.get())
140: return;
141:
142: connections.remove(connection);
143:
144: ConnectionEvent ce = new ConnectionEvent(this ,
145: ConnectionEvent.CONNECTION_CLOSED);
146: ce.setConnectionHandle(connection);
147: for (Iterator i = listeners.iterator(); i.hasNext();) {
148: ConnectionEventListener listener = (ConnectionEventListener) i
149: .next();
150: listener.connectionClosed(ce);
151: }
152: }
153: }
|