KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > memory > jca > MapManagedConnection


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/memory/jca/MapManagedConnection.java,v 1.1 2004/11/18 23:27:17 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.1 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.memory.jca;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.resource.ResourceException JavaDoc;
38 import javax.resource.spi.ConnectionEvent JavaDoc;
39 import javax.resource.spi.ConnectionEventListener JavaDoc;
40 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
41 import javax.resource.spi.LocalTransaction JavaDoc;
42 import javax.resource.spi.ManagedConnection JavaDoc;
43 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
44 import javax.security.auth.Subject JavaDoc;
45 import javax.transaction.xa.XAResource JavaDoc;
46
47 import org.apache.commons.transaction.memory.TransactionalMapWrapper;
48
49 /**
50  *
51  * @version $Revision$
52  *
53  */

54 public class MapManagedConnection implements ManagedConnection JavaDoc {
55
56     MapXAResource xares = null;
57     MapLocalTransaction tx = null;
58     String JavaDoc name = null;
59     TransactionalMapWrapper map = null;
60
61     protected MapConnection connection = null;
62     protected List JavaDoc listeners = new ArrayList JavaDoc();
63     protected PrintWriter JavaDoc out;
64
65     public MapManagedConnection(ConnectionRequestInfo JavaDoc cxRequestInfo) {
66         name = ((MapConnectionSpec) cxRequestInfo).getName();
67
68         map = MemoryMapResourceManager.getInstance().lookup(name);
69         xares = new MapXAResource(map);
70         tx = new MapLocalTransaction(map);
71
72     }
73
74     Map JavaDoc getMap() {
75         return map;
76     }
77
78     public void close() {
79         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
80         for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
81             ((ConnectionEventListener JavaDoc) it.next()).connectionClosed(event);
82         }
83         connection.invalidate();
84         connection = null;
85     }
86
87     /**
88      * @see ManagedConnection#getConnection(Subject, ConnectionRequestInfo)
89      */

90     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc {
91
92         if (connection == null) {
93             connection = new MapConnection(this);
94         }
95         return connection;
96     }
97
98     /**
99      * @see ManagedConnection#destroy()
100      */

101     public void destroy() throws ResourceException JavaDoc {
102
103         if (connection != null) {
104             connection.invalidate();
105             connection = null;
106         }
107     
108         listeners = null;
109         name = null;
110         map = null;
111         xares = null;
112         tx = null;
113     }
114
115     /**
116      * @see ManagedConnection#cleanup()
117      */

118     public void cleanup() throws ResourceException JavaDoc {
119
120         if (connection != null) {
121             connection.invalidate();
122         }
123     }
124
125     /**
126      * @see ManagedConnection#associateConnection(Object)
127      */

128     public void associateConnection(Object JavaDoc connection) throws ResourceException JavaDoc {
129         if (!(connection instanceof MapConnection)) {
130             throw new ResourceException JavaDoc("Connection is not of type MapConnection");
131         }
132         
133         this.connection = (MapConnection)connection;
134         this.connection.mc = this;
135     }
136
137     /**
138      * @see ManagedConnection#addConnectionEventListener(ConnectionEventListener)
139      */

140     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
141
142         listeners.add(listener);
143     }
144
145     /**
146      * @see ManagedConnection#removeConnectionEventListener(ConnectionEventListener)
147      */

148     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
149
150         listeners.remove(listener);
151     }
152
153     /**
154      * @see ManagedConnection#getXAResource()
155      */

156     public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
157         return xares;
158     }
159
160     /**
161      * @see ManagedConnection#getLocalTransaction()
162      */

163     public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc {
164         return tx;
165     }
166
167     /**
168      * @see ManagedConnection#getMetaData()
169      */

170     public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc {
171
172         return null;
173     }
174
175     /**
176      * @see ManagedConnection#setLogWriter(PrintWriter)
177      */

178     public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc {
179         this.out = out;
180         xares.setLoggerFacade(out);
181     }
182
183     /**
184      * @see ManagedConnection#getLogWriter()
185      */

186     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
187
188         return out;
189     }
190 }
191
Popular Tags