KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
26 import java.util.logging.*;
27
28 import javax.transaction.*;
29 import javax.transaction.xa.*;
30
31 import com.sun.logging.*;
32 import com.sun.enterprise.*;
33 import com.sun.enterprise.log.Log;
34
35 /**
36  * Resource Manager for any resource request from a component.
37  *
38  * @author Binod PG
39  */

40 public class ResourceManagerImpl implements ResourceManager {
41     
42     
43     static Logger _logger = null;
44     static {
45         _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER);
46     }
47     
48     /**
49      * Returns the transaction component is participating.
50      *
51      * @return Handle to the <code>Transaction</code> object.
52      * @exception <code>PoolingException<code>
53      */

54     public Transaction getTransaction() throws PoolingException{
55         InvocationManager invmgr =
56         Switch.getSwitch().getInvocationManager();
57         ComponentInvocation inv = invmgr.getCurrentInvocation();
58         if (inv == null) { // To support non component callers
59
try {
60                 return Switch.getSwitch().getTransactionManager().getTransaction();
61             } catch (Exception JavaDoc ex) {
62             return null;
63         }
64         }
65         return inv.getTransaction();
66     }
67     
68     /**
69      * Returns the component invoking resource request.
70      *
71      * @return Handle to the component.
72      */

73     public Object JavaDoc getComponent(){
74         
75         InvocationManager invmgr =
76         Switch.getSwitch().getInvocationManager();
77         ComponentInvocation inv = invmgr.getCurrentInvocation();
78         if (inv == null) {
79             return null;
80         }
81         
82         return inv.getInstance();
83     }
84
85     /**
86      * Enlist the <code>ResourceHandle</code> in the transaction
87      *
88      * @param h <code>ResourceHandle</code> object
89      * @exception <code>PoolingException</code>
90      */

91     public void enlistResource(ResourceHandle h) throws PoolingException{
92         registerResource(h);
93     }
94     
95     /**
96      * Register the <code>ResourceHandle</code> in the transaction
97      *
98      * @param h <code>ResourceHandle</code> object
99      * @exception <code>PoolingException</code>
100      */

101     public void registerResource(ResourceHandle handle)
102             throws PoolingException
103     {
104         try {
105             Transaction tran = null;
106             J2EETransactionManager tm =
107             Switch.getSwitch().getTransactionManager();
108             // enlist if necessary
109
if (handle.isTransactional()) {
110                 InvocationManager invmgr =
111                 Switch.getSwitch().getInvocationManager();
112                 ComponentInvocation inv = invmgr.getCurrentInvocation();
113                               
114                 if (inv == null) {
115                     //throw new InvocationException();
116

117                     //Go to the tm and get the transaction
118
//This is mimicking the current behavior of
119
//the SystemResourceManagerImpl registerResource method
120
//in that, you return the transaction from the TxManager
121
try {
122                         tran = tm.getTransaction();
123                     } catch( Exception JavaDoc e ) {
124                 tran = null;
125             _logger.log(Level.INFO, e.getMessage());
126             }
127                 } else {
128                     tran = inv.getTransaction();
129                     tm.registerComponentResource(handle);
130                 }
131                 
132                 if (tran != null) {
133                     try{
134                         tm.enlistResource(tran, handle);
135                     } catch (Exception JavaDoc ex) {
136                         _logger.fine("Exception whle trying to enlist resource " + ex.getMessage());
137                         //If transactional, remove the connection handle from the
138
//component's resource list as there has been exception attempting
139
//to enlist the resource
140
if(inv != null) {
141                             _logger.fine("Attempting to unregister component resource");
142                             tm.unregisterComponentResource(handle);
143                         }
144                         throw ex;
145                     }
146                 }
147             }
148                         
149         } catch (Exception JavaDoc ex) {
150             _logger.log(Level.SEVERE,"poolmgr.component_register_exception",ex);
151             throw new PoolingException(ex.toString(), ex);
152         }
153     }
154    
155     //Overridden by the LazyEnlistableResourceManager to be a No-Op
156
protected void enlist( J2EETransactionManager tm, Transaction tran,
157         ResourceHandle h ) throws PoolingException {
158         try {
159             tm.enlistResource( tran, h );
160         } catch( Exception JavaDoc e ) {
161             PoolingException pe = new PoolingException( e.getMessage() );
162             pe.initCause( e );
163             throw pe;
164         }
165     }
166     
167     /**
168      * Get's the component's transaction and marks it for rolling back.
169      */

170     public void rollBackTransaction() {
171         InvocationManager invmgr =
172         Switch.getSwitch().getInvocationManager();
173     J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
174     Transaction tran = null;
175         try {
176             ComponentInvocation inv = invmgr.getCurrentInvocation();
177             if (inv == null) {
178                 //throw new InvocationException();
179

180                //Go to the tm and get the transaction
181
//This is mimicking the current behavior of
182
//the SystemResourceManagerImpl registerResource method
183
//in that, you return the transaction from the TxManager
184
try {
185                    tran = tm.getTransaction();
186                } catch( Exception JavaDoc e ) {
187                tran = null;
188                    _logger.log(Level.INFO, e.getMessage());
189                }
190
191             } else {
192                 tran = inv.getTransaction();
193         }
194             if (tran != null) {
195                 tran.setRollbackOnly();
196             }
197         } catch (SystemException ex) {
198             _logger.log(Level.WARNING,"poolmgr.system_exception",ex);
199         } catch (IllegalStateException JavaDoc ex) {
200             // ignore
201
}
202     }
203     
204     /**
205      * delist the <code>ResourceHandle</code> from the transaction
206      *
207      * @param h <code>ResourceHandle</code> object
208      * @param xaresFlag flag indicating transaction success. This can
209      * be XAResource.TMSUCCESS or XAResource.TMFAIL
210      * @exception <code>PoolingException</code>
211      */

212     public void delistResource(ResourceHandle resource, int xaresFlag) {
213         unregisterResource(resource,xaresFlag);
214     }
215     
216     /**
217      * Unregister the <code>ResourceHandle</code> from the transaction
218      *
219      * @param h <code>ResourceHandle</code> object
220      * @param xaresFlag flag indicating transaction success. This can
221      * be XAResource.TMSUCCESS or XAResource.TMFAIL
222      * @exception <code>PoolingException</code>
223      */

224     public void unregisterResource(ResourceHandle resource,
225     int xaresFlag) {
226         
227         J2EETransactionManager tm =
228         Switch.getSwitch().getTransactionManager();
229     Transaction tran = null;
230         
231         try {
232             // delist with TMSUCCESS if necessary
233
if (resource.isTransactional()) {
234                 InvocationManager invmgr =
235                 Switch.getSwitch().getInvocationManager();
236                 ComponentInvocation inv = invmgr.getCurrentInvocation();
237                 if (inv == null) {
238                     //throw new InvocationException();
239

240                     //Go to the tm and get the transaction
241
//This is mimicking the current behavior of
242
//the SystemResourceManagerImpl registerResource method
243
//in that, you return the transaction from the TxManager
244
try {
245                         tran = tm.getTransaction();
246             } catch ( Exception JavaDoc e ) {
247                 tran = null;
248             _logger.log( Level.INFO, e.getMessage() );
249             }
250                 } else {
251                     tran = inv.getTransaction();
252                     tm.unregisterComponentResource(resource);
253         }
254                 if (tran != null && resource.isEnlisted() ) {
255                     tm.delistResource(tran, resource, xaresFlag);
256                 }
257             }
258         } catch (SystemException ex) {
259             _logger.log(Level.WARNING,"poolmgr.system_exception",ex);
260         } catch (IllegalStateException JavaDoc ex) {
261             // transaction aborted. Do nothing
262
} catch (InvocationException ex) {
263             // unregisterResource is called outside of component context
264
// likely to be container-forced destroy. Do nothing
265
}
266         
267     }
268 }
269
Popular Tags