KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import java.util.logging.Level JavaDoc;
28
29 import javax.transaction.xa.XAResource JavaDoc;
30 import javax.resource.spi.*;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.security.auth.Subject JavaDoc;
33
34 import com.sun.enterprise.deployment.ConnectorDescriptor;
35 import com.sun.enterprise.PoolManager;
36
37
38 /**
39  * @author Tony Ng
40  */

41 public class ConnectorAllocator extends AbstractConnectorAllocator {
42
43     private boolean shareable;
44    
45
46     class ConnectionListenerImpl implements ConnectionEventListener {
47         private ResourceHandle resource;
48         
49         public ConnectionListenerImpl(ResourceHandle resource) {
50             this.resource = resource;
51         }
52
53         public void connectionClosed(ConnectionEvent evt) {
54             if (resource.hasConnectionErrorOccurred()) {
55             return;
56         }
57         
58             resource.decrementCount();
59             if (resource.getShareCount() == 0) {
60                 poolMgr.resourceClosed(resource);
61             }
62             
63         }
64         
65         public void connectionErrorOccurred(ConnectionEvent evt) {
66             resource.setConnectionErrorOccurred();
67
68             ManagedConnection mc = (ManagedConnection) evt.getSource();
69             mc.removeConnectionEventListener(this);
70             poolMgr.resourceErrorOccurred(resource);
71             try {
72                 mc.destroy();
73             } catch (ResourceException JavaDoc ex) {
74                 // ignore exception
75
}
76         }
77
78         public void localTransactionStarted(ConnectionEvent evt) {
79             // no-op
80
}
81
82         public void localTransactionCommitted(ConnectionEvent evt) {
83             // no-op
84
}
85
86         public void localTransactionRolledback(ConnectionEvent evt) {
87             // no-op
88
}
89     }
90
91     public ConnectorAllocator(PoolManager poolMgr,
92                               ManagedConnectionFactory mcf,
93                               ResourceSpec spec,
94                               Subject JavaDoc subject,
95                               ConnectionRequestInfo reqInfo,
96                               ClientSecurityInfo info,
97                               ConnectorDescriptor desc,
98                               boolean shareable) {
99         super(poolMgr, mcf, spec, subject, reqInfo, info, desc);
100         this.shareable = shareable;
101     }
102
103     
104     public ResourceHandle createResource()
105          throws PoolingException
106     {
107         try {
108             ManagedConnection mc =
109                 mcf.createManagedConnection(subject, reqInfo);
110             
111             ResourceHandle resource =
112                 new ResourceHandle(mc, spec, this, info);
113             ConnectionEventListener l =
114                 new ConnectionListenerImpl(resource);
115             mc.addConnectionEventListener(l);
116             return resource;
117         } catch (ResourceException JavaDoc ex) {
118             throw new PoolingException(ex);
119         }
120     }
121
122     public void fillInResourceObjects(ResourceHandle resource)
123         throws PoolingException
124     {
125         try {
126         ManagedConnection mc = (ManagedConnection) resource.getResource();
127         Object JavaDoc con = mc.getConnection( subject, reqInfo );
128         resource.incrementCount();
129             XAResource JavaDoc xares = mc.getXAResource();
130             resource.fillInResourceObjects(con, xares);
131         } catch( ResourceException JavaDoc ex ) {
132         throw new PoolingException( ex );
133     }
134     }
135
136     public void destroyResource(ResourceHandle resource)
137         throws PoolingException {
138
139         try {
140             closeUserConnection(resource);
141         } catch (Exception JavaDoc ex) {
142             // ignore error
143
}
144         
145         try {
146             ManagedConnection mc = (ManagedConnection) resource.getResource();
147             mc.destroy();
148         } catch (Exception JavaDoc ex) {
149             _logger.log(Level.WARNING, ex.getMessage());
150             throw new PoolingException(ex);
151         }
152
153     }
154
155     public boolean shareableWithinComponent() {
156         return shareable;
157     }
158 }
159
Popular Tags