KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > deployers > rar > mcf1 > MCF1ManagedConnection


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.deployers.rar.mcf1;
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 public class MCF1ManagedConnection implements ManagedConnection JavaDoc, LocalTransaction JavaDoc
41 {
42    private SynchronizedBoolean destroyed = new SynchronizedBoolean(false);
43    private CopyOnWriteArraySet connections = new CopyOnWriteArraySet();
44    private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet();
45    //private MCF1ManagedConnectionFactory mcf;
46

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