KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > context > AbstractTransactionContext


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.context;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.geronimo.transaction.ConnectionReleaser;
28 import org.apache.geronimo.transaction.DoubleKeyedHashMap;
29 import org.apache.geronimo.transaction.InstanceContext;
30 import org.tranql.cache.InTxCache;
31
32
33 /**
34  * @version $Rev: 155376 $ $Date: 2005-02-25 15:10:24 -0800 (Fri, 25 Feb 2005) $
35  */

36 abstract class AbstractTransactionContext implements TransactionContext {
37     protected static final Log log = LogFactory.getLog(AbstractTransactionContext.class);
38     protected Map JavaDoc managedConnections;
39
40     private InstanceContext currentContext;
41     private final DoubleKeyedHashMap associatedContexts = new DoubleKeyedHashMap();
42     private final DoubleKeyedHashMap dirtyContexts = new DoubleKeyedHashMap();
43     private InTxCache inTxCache;
44
45     public final void associate(InstanceContext context) throws Throwable JavaDoc {
46         if (associatedContexts.put(context.getContainerId(), context.getId(), context) == null) {
47             context.associate();
48         }
49     }
50
51     public final void unassociate(InstanceContext context) throws Throwable JavaDoc {
52         associatedContexts.remove(context.getContainerId(), context.getId());
53         context.unassociate();
54     }
55
56     public final void unassociate(Object JavaDoc containerId, Object JavaDoc id) throws Throwable JavaDoc {
57         InstanceContext context = (InstanceContext) associatedContexts.remove(containerId, id);
58         if (context != null) {
59             context.unassociate();
60         }
61     }
62
63     public final InstanceContext getContext(Object JavaDoc containerId, Object JavaDoc id) {
64         return (InstanceContext) associatedContexts.get(containerId, id);
65     }
66
67     protected final ArrayList JavaDoc getAssociatedContexts() {
68         return new ArrayList JavaDoc(associatedContexts.values());
69     }
70
71     protected final void unassociateAll() {
72         ArrayList JavaDoc toFlush = getAssociatedContexts();
73         for (Iterator JavaDoc i = toFlush.iterator(); i.hasNext();) {
74             InstanceContext context = (InstanceContext) i.next();
75             try {
76                 context.unassociate();
77             } catch (Throwable JavaDoc throwable) {
78                 log.warn("Error while unassociating instance from transaction context: " + context, throwable);
79             }
80         }
81     }
82
83     public final InstanceContext beginInvocation(InstanceContext context) throws Throwable JavaDoc {
84         if (context.getId() != null) {
85             associate(context);
86             dirtyContexts.put(context.getContainerId(), context.getId(), context);
87         }
88         context.enter();
89         InstanceContext caller = currentContext;
90         currentContext = context;
91         return caller;
92     }
93
94     public final void endInvocation(InstanceContext caller) {
95         if (currentContext != null) {
96             currentContext.exit();
97         }
98         currentContext = caller;
99     }
100
101     public final void flushState() throws Throwable JavaDoc {
102         while (dirtyContexts.isEmpty() == false) {
103             ArrayList JavaDoc toFlush = new ArrayList JavaDoc(dirtyContexts.values());
104             dirtyContexts.clear();
105             for (Iterator JavaDoc i = toFlush.iterator(); i.hasNext();) {
106                 InstanceContext context = (InstanceContext) i.next();
107                 if (!context.isDead()) {
108                     context.flush();
109                 }
110             }
111         }
112         if (currentContext != null && currentContext.getId() != null) {
113             dirtyContexts.put(currentContext.getContainerId(), currentContext.getId(), currentContext);
114         }
115         if(inTxCache != null) {
116             inTxCache.flush();
117         }
118     }
119
120     public final void setInTxCache(InTxCache inTxCache) {
121         this.inTxCache = inTxCache;
122     }
123     
124     public final InTxCache getInTxCache() {
125         return inTxCache;
126     }
127
128     public void setManagedConnectionInfo(ConnectionReleaser key, Object JavaDoc info) {
129         if (managedConnections == null) {
130             managedConnections = new HashMap JavaDoc();
131         }
132         managedConnections.put(key, info);
133     }
134
135     public Object JavaDoc getManagedConnectionInfo(ConnectionReleaser key) {
136         if (managedConnections == null) {
137             return null;
138         }
139         return managedConnections.get(key);
140     }
141 }
142
Popular Tags