KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jcaprops > support > PropertyTestManagedConnection


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jcaprops.support;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.resource.ResourceException JavaDoc;
28 import javax.resource.spi.ConnectionEvent JavaDoc;
29 import javax.resource.spi.ConnectionEventListener JavaDoc;
30 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
31 import javax.resource.spi.LocalTransaction JavaDoc;
32 import javax.resource.spi.ManagedConnection JavaDoc;
33 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35 import javax.transaction.xa.XAResource JavaDoc;
36
37 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet;
38 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
39
40 /**
41  * A PropertyTestManagedConnection.
42  *
43  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
44  * @version $Revision: 37406 $
45  */

46 public class PropertyTestManagedConnection implements ManagedConnection JavaDoc, LocalTransaction JavaDoc
47 {
48    private SynchronizedBoolean destroyed = new SynchronizedBoolean(false);
49    private CopyOnWriteArraySet connections = new CopyOnWriteArraySet();
50    private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet();
51    //private PropertyTestManagedConnectionFactory mcf;
52

53    public PropertyTestManagedConnection(PropertyTestManagedConnectionFactory mcf)
54    {
55       //this.mcf = mcf;
56
}
57    
58    public void addConnectionEventListener(ConnectionEventListener JavaDoc listener)
59    {
60       checkDestroyed();
61       listeners.add(listener);
62    }
63
64    public void associateConnection(Object JavaDoc connection) throws ResourceException JavaDoc
65    {
66       checkDestroyed();
67       if (connection instanceof PropertyTestConnectionImpl)
68          throw new ResourceException JavaDoc("Wrong object");
69       ((PropertyTestConnectionImpl) connection).setManagedConnection(this);
70       connections.add(connection);
71    }
72
73    public void cleanup() throws ResourceException JavaDoc
74    {
75       for (Iterator JavaDoc i = connections.iterator(); i.hasNext();)
76       {
77          PropertyTestConnectionImpl connection = (PropertyTestConnectionImpl) i.next();
78          connection.setManagedConnection(null);
79       }
80       connections.clear();
81    }
82
83    public void destroy() throws ResourceException JavaDoc
84    {
85       destroyed.set(true);
86       cleanup();
87    }
88
89    public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc
90    {
91       PropertyTestConnectionImpl connection = new PropertyTestConnectionImpl();
92       connection.setManagedConnection(this);
93       return connection;
94    }
95
96    public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc
97    {
98       return this;
99    }
100
101    public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc
102    {
103       return null;
104    }
105
106    public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc
107    {
108       return null;
109    }
110
111    public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc
112    {
113       return null;
114    }
115
116    public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener)
117    {
118       listeners.remove(listener);
119    }
120
121    public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc
122    {
123    }
124
125    public void begin() throws ResourceException JavaDoc
126    {
127    }
128
129    public void commit() throws ResourceException JavaDoc
130    {
131    }
132
133    public void rollback() throws ResourceException JavaDoc
134    {
135    }
136
137    protected void checkDestroyed()
138    {
139       if (destroyed.get())
140          throw new IllegalStateException JavaDoc("Destroyed");
141    }
142    
143    void closeHandle(PropertyTestConnectionImpl connection)
144    {
145       if (destroyed.get())
146          return;
147       
148       connections.remove(connection);
149       
150       ConnectionEvent JavaDoc ce = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
151       ce.setConnectionHandle(connection);
152       for (Iterator JavaDoc i = listeners.iterator(); i.hasNext();)
153       {
154          ConnectionEventListener JavaDoc listener = (ConnectionEventListener JavaDoc) i.next();
155          listener.connectionClosed(ce);
156       }
157    }
158 }
159
Popular Tags