KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 package com.sun.enterprise.resource;
25
26 import com.sun.enterprise.log.Log;
27 import com.sun.enterprise.PoolManager;
28 import com.sun.enterprise.deployment.ConnectorDescriptor;
29
30 import javax.resource.spi.*;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33 import java.util.logging.*;
34 import java.lang.IllegalStateException JavaDoc;
35
36 /**
37  * @author Tony Ng
38  */

39 public class NoTxConnectorAllocator extends AbstractConnectorAllocator {
40     
41     class ConnectionListenerImpl implements ConnectionEventListener {
42         private ResourceHandle resource;
43         
44         public ConnectionListenerImpl(ResourceHandle resource) {
45             this.resource = resource;
46         }
47         
48         public void connectionClosed(ConnectionEvent evt) {
49             poolMgr.putbackResourceToPool(resource, false);
50         }
51         
52         public void connectionErrorOccurred(ConnectionEvent evt) {
53             ManagedConnection mc = (ManagedConnection) evt.getSource();
54             mc.removeConnectionEventListener(this);
55             poolMgr.putbackResourceToPool(resource, true);
56             try {
57                 mc.destroy();
58             } catch (ResourceException JavaDoc ex) {
59                 // ignore exception
60
}
61             //GJCINT
62
resource.setConnectionErrorOccurred();
63         }
64         
65         public void localTransactionStarted(ConnectionEvent evt) {
66             throw new IllegalStateException JavaDoc("local transaction not supported");
67         }
68         
69         public void localTransactionCommitted(ConnectionEvent evt) {
70             throw new IllegalStateException JavaDoc("local transaction not supported");
71         }
72         
73         public void localTransactionRolledback(ConnectionEvent evt) {
74             throw new IllegalStateException JavaDoc("local transaction not supported");
75         }
76     }
77     
78     public NoTxConnectorAllocator(PoolManager poolMgr,
79                                     ManagedConnectionFactory mcf,
80                                     ResourceSpec spec,
81                                     Subject JavaDoc subject,
82                                     ConnectionRequestInfo reqInfo,
83                                     ClientSecurityInfo info,
84                                     ConnectorDescriptor desc) {
85         super(poolMgr, mcf, spec, subject, reqInfo, info, desc);
86     }
87     
88     
89     public ResourceHandle createResource()
90     throws PoolingException {
91         try {
92             ManagedConnection mc =
93             mcf.createManagedConnection(subject, reqInfo);
94             ResourceHandle resource =
95             new ResourceHandle(mc, spec, this, info);
96             ConnectionEventListener l =
97             new ConnectionListenerImpl(resource);
98             mc.addConnectionEventListener(l);
99             return resource;
100         } catch (ResourceException JavaDoc ex) {
101             _logger.log(Level.SEVERE,"poolmgr.create_resource_error",ex);
102             
103             if (ex.getLinkedException() != null) {
104                 _logger.log(Level.SEVERE,"poolmgr.create_resource_error",ex.getLinkedException());
105             }
106             Log.err.flush();
107             throw new PoolingException(ex);
108         }
109     }
110     
111     public void fillInResourceObjects(ResourceHandle resource)
112     throws PoolingException {
113         try {
114             ManagedConnection mc = (ManagedConnection) resource.getResource();
115             Object JavaDoc con = mc.getConnection(subject, reqInfo);
116             resource.fillInResourceObjects(con, null);
117         } catch (ResourceException JavaDoc ex) {
118             throw new PoolingException(ex);
119         }
120     }
121     
122     public void destroyResource(ResourceHandle resource)
123     throws PoolingException {
124         
125         try {
126             ManagedConnection mc = (ManagedConnection) resource.getResource();
127             mc.destroy();
128         } catch (Exception JavaDoc ex) {
129             _logger.log(Level.WARNING, ex.getMessage());
130             throw new PoolingException(ex);
131         }
132     }
133     
134     public boolean isTransactional() {
135         return false;
136     }
137 }
138
Popular Tags