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.deployers.rar.mcf1;
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: public class MCF1ManagedConnection implements ManagedConnection,
041: LocalTransaction {
042: private SynchronizedBoolean destroyed = new SynchronizedBoolean(
043: false);
044: private CopyOnWriteArraySet connections = new CopyOnWriteArraySet();
045: private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet();
046:
047: //private MCF1ManagedConnectionFactory mcf;
048:
049: public MCF1ManagedConnection(MCF1ManagedConnectionFactory mcf) {
050: //this.mcf = mcf;
051: }
052:
053: public void addConnectionEventListener(
054: ConnectionEventListener listener) {
055: checkDestroyed();
056: listeners.add(listener);
057: }
058:
059: public void associateConnection(Object connection)
060: throws ResourceException {
061: checkDestroyed();
062: if (connection instanceof MCF1ConnectionImpl)
063: throw new ResourceException("Wrong object");
064: ((MCF1ConnectionImpl) connection).setManagedConnection(this );
065: connections.add(connection);
066: }
067:
068: public void cleanup() throws ResourceException {
069: for (Iterator i = connections.iterator(); i.hasNext();) {
070: MCF1ConnectionImpl connection = (MCF1ConnectionImpl) i
071: .next();
072: connection.setManagedConnection(null);
073: }
074: connections.clear();
075: }
076:
077: public void destroy() throws ResourceException {
078: destroyed.set(true);
079: cleanup();
080: }
081:
082: public Object getConnection(Subject subject,
083: ConnectionRequestInfo cxRequestInfo)
084: throws ResourceException {
085: MCF1ConnectionImpl connection = new MCF1ConnectionImpl();
086: connection.setManagedConnection(this );
087: return connection;
088: }
089:
090: public LocalTransaction getLocalTransaction()
091: throws ResourceException {
092: return this ;
093: }
094:
095: public PrintWriter getLogWriter() throws ResourceException {
096: return null;
097: }
098:
099: public ManagedConnectionMetaData getMetaData()
100: throws ResourceException {
101: return null;
102: }
103:
104: public XAResource getXAResource() throws ResourceException {
105: return null;
106: }
107:
108: public void removeConnectionEventListener(
109: ConnectionEventListener listener) {
110: listeners.remove(listener);
111: }
112:
113: public void setLogWriter(PrintWriter out) throws ResourceException {
114: }
115:
116: public void begin() throws ResourceException {
117: }
118:
119: public void commit() throws ResourceException {
120: }
121:
122: public void rollback() throws ResourceException {
123: }
124:
125: protected void checkDestroyed() {
126: if (destroyed.get())
127: throw new IllegalStateException("Destroyed");
128: }
129:
130: void closeHandle(MCF1ConnectionImpl connection) {
131: if (destroyed.get())
132: return;
133:
134: connections.remove(connection);
135:
136: ConnectionEvent ce = new ConnectionEvent(this ,
137: ConnectionEvent.CONNECTION_CLOSED);
138: ce.setConnectionHandle(connection);
139: for (Iterator i = listeners.iterator(); i.hasNext();) {
140: ConnectionEventListener listener = (ConnectionEventListener) i
141: .next();
142: listener.connectionClosed(ce);
143: }
144: }
145: }
|