KickJava   Java API By Example, From Geeks To Geeks.

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


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.XAResource JavaDoc;
26 import javax.resource.spi.ConnectionEventListener JavaDoc;
27
28 import com.sun.logging.LogDomains;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 // START OF IASRI 4629815
33
//
34
import javax.security.auth.Subject JavaDoc; // Added by Miriam - ECU
35
// END OF IASRI 4629815
36
//
37

38
39 /**
40  * ResourceHandle encapsulates a resource connection.
41  * Equality on the handle is based on the id field
42  *
43  * @author Tony Ng
44  */

45 public class ResourceHandle {
46
47     // unique ID for resource handles
48
static private long idSequence;
49
50     private long id;
51     private ClientSecurityInfo info;
52     private Object JavaDoc resource; // XAConnection for JDBC 2.0
53
private ResourceSpec spec;
54     private XAResource JavaDoc xares;
55     private Object JavaDoc usercon; // Connection for JDBC 2.0
56
private ResourceAllocator alloc;
57     private Object JavaDoc instance; // the component instance holding this resource
58
private int shareCount; // sharing within a component (XA only)
59
private boolean supportsXAResource=false;
60
61     private Subject JavaDoc subject = null; //Added by Miriam - ECU .
62

63     private ResourceState state = null;
64     private ConnectionEventListener JavaDoc listener = null;
65     private boolean associated_ = false;
66     private long threadId_;
67
68     private static Logger JavaDoc logger =
69     LogDomains.getLogger(LogDomains.RSR_LOGGER);
70     
71     private boolean supportsLazyEnlistment_ = false;
72     private boolean supportsLazyAssoc_ = false;
73
74     //To suspend and enable lazy enlistment - added by Jagadish Ramu
75

76     private boolean enlistmentSuspended = false;
77     public Object JavaDoc lock = new Object JavaDoc();
78     private boolean dirty_;
79     private long lastValidated; //holds the latest time at which the connection was validated.
80

81     static private long getNextId() {
82     synchronized (ResourceHandle.class) {
83         idSequence++;
84             return idSequence;
85         }
86     }
87
88     public ResourceHandle(Object JavaDoc resource,
89                           ResourceSpec spec,
90                           ResourceAllocator alloc,
91                           ClientSecurityInfo info) {
92         this.id = getNextId();
93         this.spec = spec;
94         this.info = info;
95         this.resource = resource;
96         this.alloc = alloc;
97
98     if ( alloc instanceof LocalTxConnectorAllocator)
99         supportsXAResource = false;
100     else
101         supportsXAResource = true;
102
103         if ( resource instanceof
104             javax.resource.spi.LazyEnlistableManagedConnection JavaDoc ) {
105             supportsLazyEnlistment_ = true;
106         }
107
108         if ( resource instanceof
109             javax.resource.spi.DissociatableManagedConnection JavaDoc ) {
110             supportsLazyAssoc_ = true;
111         }
112     }
113     //START OF IASRI 4721130
114
public ResourceHandle(Object JavaDoc resource,
115                           ResourceSpec spec,
116                           ResourceAllocator alloc,
117                           ClientSecurityInfo info,
118               boolean supportsXA) {
119         this.id = getNextId();
120         this.spec = spec;
121         this.info = info;
122         this.resource = resource;
123         this.alloc = alloc;
124
125     supportsXAResource = supportsXA;
126
127         dirty_ = false;
128     }
129     //END OF IASRI 4721130
130
/**
131      * Does this resource need enlistment to transaction manager?
132      */

133     public boolean isTransactional() {
134         return alloc.isTransactional();
135     }
136
137     public boolean supportsXA() {
138         return supportsXAResource;
139     }
140
141     public ResourceAllocator getResourceAllocator() {
142         return alloc;
143     }
144
145     public Object JavaDoc getResource() {
146         return resource;
147     }
148
149     public ClientSecurityInfo getClientSecurityInfo() {
150         return info;
151     }
152
153     public void setResourceSpec(ResourceSpec spec) {
154         this.spec = spec;
155     }
156
157     public ResourceSpec getResourceSpec() {
158         return spec;
159     }
160
161     public XAResource JavaDoc getXAResource() {
162         return xares;
163     }
164
165     public Object JavaDoc getUserConnection() {
166         return usercon;
167     }
168
169     public void setComponentInstance(Object JavaDoc instance) {
170         this.instance = instance;
171     }
172
173     public Object JavaDoc getComponentInstance() {
174         return instance;
175     }
176
177     public void fillInResourceObjects(Object JavaDoc userConnection,
178                                       XAResource JavaDoc xaRes) {
179         if (userConnection != null) usercon = userConnection;
180         if (xaRes !=null) {
181            if(logger.isLoggable(Level.FINEST)){
182              //When Log level is Finest, XAResourceWrapper is used to log
183
//all XA interactions - Don't wrap XAResourceWrapper if it is
184
//already wrapped
185
if ((xaRes instanceof XAResourceWrapper) ||
186                        (xaRes instanceof ConnectorXAResource)) {
187                    this.xares = xaRes;
188                } else {
189                    this.xares = new XAResourceWrapper(xaRes);
190                }
191            } else {
192             this.xares = xaRes;
193            }
194         }
195     }
196
197     // For XA-capable connections, multiple connections within a
198
// component are collapsed into one. shareCount keeps track of
199
// the number of additional shared connections
200
public void incrementCount() {
201         shareCount++;
202     }
203
204     public void decrementCount() {
205         if (shareCount == 0) {
206             throw new IllegalStateException JavaDoc("shareCount cannot be negative");
207         } else {
208             shareCount--;
209         }
210     }
211
212     public int getShareCount() {
213         return shareCount;
214     }
215    // START OF IASRI 4629815
216
//
217

218     //Added by Miriam - ECU
219
public void setSubject(Subject JavaDoc subject){
220         this.subject = subject;
221     }
222     //Added by Miriam - ECU
223
public Subject JavaDoc getSubject(){
224         return subject;
225     }
226
227    // END OF IASRI 4629815
228
//
229

230     public boolean equals(Object JavaDoc other) {
231         if (other == null) return false;
232         if (other instanceof ResourceHandle) {
233             return this.id == (((ResourceHandle) other).id);
234         }
235         return false;
236     }
237
238     public int hashCode() {
239         return (new Long JavaDoc(id)).hashCode();
240     }
241
242     public String JavaDoc toString() {
243         return String.valueOf(id);
244     }
245
246     //GJCINT
247
private boolean connectionErrorOccurred = false;
248
249     public void setConnectionErrorOccurred() {
250     connectionErrorOccurred = true;
251     }
252
253     public boolean hasConnectionErrorOccurred() {
254     return connectionErrorOccurred;
255     }
256
257     public void setResourceState(ResourceState state) {
258         this.state = state;
259     }
260
261     public ResourceState getResourceState() {
262         return state;
263     }
264
265     public void setListener(ConnectionEventListener JavaDoc l) {
266         this.listener = l;
267     }
268     
269     public ConnectionEventListener JavaDoc getListener() {
270         return listener;
271     }
272
273     public boolean isShareable() {
274        return alloc.shareableWithinComponent();
275     }
276
277     public boolean isEnlisted() {
278       if (state != null)
279           return state.isEnlisted();
280       else
281           return false;
282     }
283
284     public boolean isAssociated() {
285         return associated_;
286     }
287
288     public void setAssociated( boolean flag ) {
289         associated_ = flag;
290     }
291
292     public long getThreadId() {
293         return threadId_;
294     }
295
296     public void setThreadId( long threadId ) {
297         threadId_ = threadId;
298     }
299
300     public boolean supportsLazyEnlistment() {
301         return supportsLazyEnlistment_;
302     }
303
304     public boolean supportsLazyAssociation() {
305         return supportsLazyAssoc_;
306     }
307
308     public boolean isDirty() {
309         return dirty_;
310     }
311
312     public void setDirty() {
313         dirty_ = true;
314     }
315
316     /**
317      * To check whether lazy enlistment is suspended or not.<br>
318      * If true, TM will not do enlist/lazy enlist.
319      * @return boolean
320      */

321     public boolean isEnlistmentSuspended()
322     {
323         return enlistmentSuspended;
324     }
325
326     public void setEnlistmentSuspended(boolean enlistmentSuspended)
327     {
328         this.enlistmentSuspended = enlistmentSuspended;
329     }
330
331     public long getLastValidated() {
332      return lastValidated;
333     }
334
335     public void setLastValidated(long lastValidated) {
336      this.lastValidated = lastValidated;
337     }
338
339 }
340
Popular Tags