KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.util.i18n.StringManager;
26 import javax.transaction.*;
27 import javax.resource.spi.ManagedConnection JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.List JavaDoc;
30 import com.sun.enterprise.distributedtx.J2EETransactionManagerImpl;
31 import com.sun.enterprise.J2EETransactionManager;
32 import com.sun.enterprise.Switch;
33 import javax.resource.ResourceException JavaDoc;
34 import com.sun.enterprise.PoolManager;
35 import com.sun.enterprise.ComponentInvocation;
36 import java.util.ListIterator JavaDoc;
37
38 /**
39  * This class is used for lazy enlistment of a resource
40  *
41  * @author Aditya Gore
42  */

43 public class LazyEnlistableResourceManagerImpl extends ResourceManagerImpl {
44     
45     private static StringManager sm =
46         StringManager.getManager(LazyEnlistableResourceManagerImpl.class);
47
48     protected void enlist( J2EETransactionManager tm, Transaction tran,
49         ResourceHandle h )
50     {
51         //do nothing
52
}
53
54     /**
55      * Overridden to suspend lazyenlistment.
56      * @param handle
57      * @throws PoolingException
58      */

59       public void registerResource(ResourceHandle handle)
60             throws PoolingException {
61             handle.setEnlistmentSuspended(true);
62             super.registerResource(handle);
63      }
64     /**
65      * This is called by the PoolManager (in turn by the LazyEnlistableConnectionManager)
66      * when a lazy enlistment is sought.
67      */

68     public void lazyEnlist( ManagedConnection JavaDoc mc ) throws ResourceException JavaDoc {
69         if ( _logger.isLoggable(Level.FINE) ) {
70             _logger.fine("@@@@ Entering lazyEnlist");
71         }
72
73         J2EETransactionManager tm = Switch.getSwitch().getTransactionManager();
74                         
75         Transaction tran = null;
76         
77         try {
78             tran = tm.getTransaction();
79             if ( tran == null ) {
80                 if (_logger.isLoggable(Level.FINE)) {
81                     _logger.fine(" Transaction null - not enlisting ");
82                 }
83
84                 return;
85             }
86         } catch( SystemException se ) {
87             ResourceException JavaDoc re = new ResourceException JavaDoc( se.getMessage() );
88             re.initCause( se );
89             throw re;
90         }
91        
92         
93         List JavaDoc invList = Switch.getSwitch().getInvocationManager().getAllInvocations();
94
95         ResourceHandle h = null;
96         for ( int j = invList.size(); j > 0; j-- ) {
97             ComponentInvocation inv = (ComponentInvocation) invList.get( j - 1 );
98             Object JavaDoc comp = inv.getInstance();
99
100             List JavaDoc l = ((J2EETransactionManagerImpl)tm).getResourceList( comp, inv );
101             
102             ListIterator JavaDoc it = l.listIterator();
103             while( it.hasNext()) {
104                 ResourceHandle hand = (ResourceHandle) it.next();
105                 ManagedConnection JavaDoc toEnlist = (ManagedConnection JavaDoc) hand.getResource();
106                 if ( mc.equals( toEnlist ) ) {
107                     h = hand;
108                     break;
109                 }
110             }
111         }
112         
113         //NOTE: Notice that here we are always assuming that the connection we
114
//are trying to enlist was acquired in this component only. This
115
//might be inadequate in situations where component A acquires a connection
116
//and passes it on to a method of component B, and the lazyEnlist is
117
//triggered in B
118
//At this point however, we will only support the straight and narrow
119
//case where a connection is acquired and then used in the same component.
120
//The other case might or might not work
121
if( h != null && tran != null && h.getResourceState().isUnenlisted()) {
122             try {
123                 //Enable the suspended lazyenlistment so as to enlist the resource.
124
h.setEnlistmentSuspended(false);
125                     tm.enlistResource( tran, h );
126                 //Suspend it back
127
h.setEnlistmentSuspended(true);
128             } catch( Exception JavaDoc e ) {
129                 //In the rare cases where enlistResource throws exception, we
130
//should return the resource to the pool
131
if ( h != null ) {
132                     PoolManager mgr = Switch.getSwitch().getPoolManager();
133                     mgr.putbackDirectToPool( h, h.getResourceSpec().getConnectionPoolName());
134                 }
135                 _logger.log(Level.WARNING, "poolmgr.err_enlisting_res_in_getconn");
136                 if (_logger.isLoggable(Level.FINE) ) {
137                     _logger.fine("rm.enlistResource threw Exception. Returning resource to pool");
138                 }
139                 //and rethrow the exception
140
throw new ResourceException JavaDoc( e );
141             }
142         }
143     }
144
145         
146 }
147
Popular Tags