KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > ConnectorXAResource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.resource;
24
25 import javax.transaction.xa.*;
26 import javax.resource.spi.*;
27 import com.sun.enterprise.log.Log;
28 import com.sun.enterprise.*;
29 import com.sun.enterprise.distributedtx.*;
30 import java.util.*;
31 import java.util.logging.*;
32 import com.sun.logging.*;
33 import javax.resource.ResourceException JavaDoc;
34 import com.sun.jts.CosTransactions.Configuration;
35
36 /**
37  * @author Tony Ng
38  *
39  */

40 public class ConnectorXAResource implements XAResource {
41
42     private Object JavaDoc userHandle;
43     private ResourceSpec spec;
44     private String JavaDoc poolName;
45     private ResourceAllocator alloc;
46     private PoolManager poolMgr;
47     private ManagedConnection localConnection;
48     private ClientSecurityInfo info;
49     private ConnectionEventListener listener;
50     private ResourceHandle localHandle_;
51
52     private static Hashtable listenerTable = new Hashtable();
53
54     private static ThreadLocal JavaDoc sharedConnections = new ThreadLocal JavaDoc();
55
56
57     // Create logger object per Java SDK 1.4 to log messages
58
// introduced Santanu De, Sun Microsystems, March 2002
59

60     static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
61
62
63
64     public ConnectorXAResource(ResourceHandle handle,
65                                ResourceSpec spec,
66                                ResourceAllocator alloc,
67                                ClientSecurityInfo info ) {
68
69         // initially userHandle is associated with mc
70
this.poolMgr = Switch.getSwitch().getPoolManager();
71         this.userHandle = null;
72         this.spec = spec;
73     this.poolName = spec.getConnectionPoolName();
74         this.alloc = alloc;
75         this.info = info;
76         localConnection = (ManagedConnection) handle.getResource();
77     localHandle_ = handle;
78     }
79
80     public void setUserHandle(Object JavaDoc userHandle) {
81         this.userHandle = userHandle;
82     }
83
84     private void handleResourceException(Exception JavaDoc ex)
85         throws XAException {
86         _logger.log(Level.SEVERE,"poolmgr.system_exception",ex);
87         XAException xae = new XAException(ex.toString());
88         xae.errorCode = XAException.XAER_RMERR;
89         throw xae;
90     }
91
92     public void commit(Xid xid, boolean onePhase) throws XAException {
93         try {
94         ResourceHandle handle = getResourceHandle();
95         ManagedConnection mc = (ManagedConnection) handle.getResource();
96             mc.getLocalTransaction().commit();
97         } catch (Exception JavaDoc ex) {
98             handleResourceException(ex);
99         }
100         resetAssociation();
101     }
102
103     public void start(Xid xid, int flags) throws XAException {
104         try {
105         ResourceHandle handle = getResourceHandle();
106         if ( ! localHandle_.equals( handle) ) {
107             ManagedConnection mc = (ManagedConnection) handle.getResource();
108                 mc.associateConnection(userHandle);
109                 LocalTxConnectionEventListener l =
110                      (LocalTxConnectionEventListener) handle.getListener();
111                 _logger.log(Level.FINEST, "connection_sharing_start", userHandle);
112                 l.associateHandle(userHandle, localHandle_);
113         }
114         } catch (Exception JavaDoc ex) {
115             handleResourceException(ex);
116         }
117     }
118
119     public void end(Xid xid, int flags) throws XAException {
120         //no -op
121
_logger.log(Level.FINEST, "connection_sharing_end");
122     }
123    
124     private void resetAssociation() throws XAException{
125         try {
126         ResourceHandle handle = getResourceHandle();
127
128             LocalTxConnectionEventListener l = (LocalTxConnectionEventListener)handle.getListener();
129             //Get all associated Handles and reset their ManagedConnection association.
130
Map associatedHandles = l.getAssociatedHandles();
131             if(associatedHandles != null ){
132                 Set userHandles = associatedHandles.keySet();
133                 for(Object JavaDoc userHandleObject : userHandles ){
134                     ResourceHandle associatedHandle = (ResourceHandle)associatedHandles.get(userHandleObject);
135                     ManagedConnection associatedConnection = (ManagedConnection)associatedHandle.getResource();
136                     associatedConnection.associateConnection(userHandleObject);
137                     _logger.log(Level.FINEST, "connection_sharing_reset_association", userHandleObject);
138                 }
139                 //all associated handles are mapped back to their actual Managed Connection. Clear the associations.
140
associatedHandles.clear();
141             }
142
143         } catch (Exception JavaDoc ex) {
144             handleResourceException(ex);
145         }
146     }
147
148     
149     public void forget(Xid xid) throws XAException {
150     _logger.fine("Well, forget is called for xid :"+xid);
151         // no-op
152
}
153
154     public int getTransactionTimeout() throws XAException {
155         return 0;
156     }
157     
158     public boolean isSameRM(XAResource other) throws XAException {
159         if (this == other) return true;
160         if (other == null) return false;
161         if (other instanceof ConnectorXAResource) {
162             ConnectorXAResource obj = (ConnectorXAResource) other;
163             return (this.spec.equals(obj.spec) &&
164                     this.info.equals(obj.info));
165         } else {
166             return false;
167         }
168     }
169
170     public int prepare(Xid xid) throws XAException {
171     return Configuration.LAO_PREPARE_OK;
172     }
173     
174     public Xid[] recover(int flag) throws XAException {
175         return new Xid[0];
176     }
177     
178     public void rollback(Xid xid) throws XAException {
179         try {
180         ResourceHandle handle = getResourceHandle();
181         ManagedConnection mc = (ManagedConnection) handle.getResource();
182             mc.getLocalTransaction().rollback();
183         } catch (Exception JavaDoc ex) {
184             handleResourceException(ex);
185         }
186         resetAssociation();
187     }
188
189     public boolean setTransactionTimeout(int seconds) throws XAException {
190         return false;
191     }
192
193     public static void freeListener(ManagedConnection mc) {
194         listenerTable.remove(mc);
195     }
196
197     public static void addListener(ManagedConnection mc, ConnectionEventListener l) {
198         listenerTable.put(mc,l);
199     }
200
201     private ResourceHandle getResourceHandle() throws PoolingException {
202         try {
203             ResourceHandle h = null;
204             J2EETransactionManager txMgr = Switch.getSwitch().getTransactionManager();
205             J2EETransaction j2eetran = (J2EETransaction) txMgr.getTransaction();
206             if (j2eetran == null) { //Only if some thing is wrong with tx manager.
207
h = localHandle_; //Just return the local handle.
208
} else {
209                 h = j2eetran.getNonXAResource();
210             }
211         if (h.getResourceState().isUnenlisted()) {
212             ManagedConnection mc = (ManagedConnection) h.getResource();
213                 // begin the local transaction if first time
214
// this ManagedConnection is used in this JTA transaction
215
mc.getLocalTransaction().begin();
216             }
217             return h;
218         } catch (Exception JavaDoc ex) {
219             _logger.log(Level.SEVERE,"poolmgr.system_exception",ex);
220             throw new PoolingException(ex.toString(), ex);
221     }
222     }
223
224 }
225
Popular Tags