1 | /* |
2 | |
3 | Derby - Class org.apache.derby.jdbc.ResourceAdapterImpl |
4 | |
5 | Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.jdbc; |
22 | |
23 | import org.apache.derby.iapi.services.info.JVMInfo; |
24 | |
25 | import org.apache.derby.iapi.services.context.ContextService; |
26 | import org.apache.derby.iapi.services.monitor.ModuleControl; |
27 | import org.apache.derby.iapi.services.monitor.Monitor; |
28 | import org.apache.derby.iapi.services.sanity.SanityManager; |
29 | |
30 | import org.apache.derby.iapi.jdbc.ResourceAdapter; |
31 | //import org.apache.derby.iapi.jdbc.XATransactionResource; |
32 | |
33 | import org.apache.derby.iapi.error.StandardException; |
34 | import org.apache.derby.iapi.store.access.AccessFactory; |
35 | import org.apache.derby.iapi.store.access.xa.XAResourceManager; |
36 | import org.apache.derby.iapi.store.access.xa.XAXactId; |
37 | |
38 | |
39 | import java.util.Properties; |
40 | import java.util.Hashtable; |
41 | import java.util.Enumeration; |
42 | |
43 | |
44 | public class ResourceAdapterImpl |
45 | implements ResourceAdapter, ModuleControl |
46 | { |
47 | private boolean active; |
48 | |
49 | // the real resource manager |
50 | private XAResourceManager rm; |
51 | |
52 | // maps Xid to XATransationResource for run time transactions |
53 | private Hashtable connectionTable; |
54 | |
55 | /* |
56 | * Module control |
57 | */ |
58 | |
59 | public void boot(boolean create, Properties properties) |
60 | throws StandardException |
61 | { |
62 | // we can only run on jdk1.2 or beyond with JTA and JAVA 20 extension |
63 | // loaded. |
64 | |
65 | connectionTable = new Hashtable(); |
66 | |
67 | AccessFactory af = |
68 | (AccessFactory)Monitor.findServiceModule(this, AccessFactory.MODULE); |
69 | |
70 | rm = (XAResourceManager) af.getXAResourceManager(); |
71 | |
72 | active = true; |
73 | } |
74 | |
75 | public void stop() |
76 | { |
77 | active = false; |
78 | |
79 | for (Enumeration e = connectionTable.elements(); e.hasMoreElements(); ) { |
80 | |
81 | XATransactionState tranState = (XATransactionState) e.nextElement(); |
82 | |
83 | try { |
84 | tranState.conn.close(); |
85 | } catch (java.sql.SQLException sqle) { |
86 | } |
87 | } |
88 | |
89 | active = false; |
90 | } |
91 | |
92 | public boolean isActive() |
93 | { |
94 | return active; |
95 | } |
96 | |
97 | /* |
98 | * Resource Adapter methods |
99 | */ |
100 | |
101 | public synchronized Object findConnection(XAXactId xid) { |
102 | |
103 | return connectionTable.get(xid); |
104 | } |
105 | |
106 | public synchronized boolean addConnection(XAXactId xid, Object conn) { |
107 | if (connectionTable.get(xid) != null) |
108 | return false; |
109 | |
110 | // put this into the transaction table, if the xid is already |
111 | // present as an in-doubt transaction, we need to remove it from |
112 | // the run time list |
113 | connectionTable.put(xid, conn); |
114 | return true; |
115 | } |
116 | |
117 | public synchronized Object removeConnection(XAXactId xid) { |
118 | |
119 | return connectionTable.remove(xid); |
120 | |
121 | } |
122 | |
123 | |
124 | /** |
125 | Return the XA Resource manager to the XA Connection |
126 | */ |
127 | public XAResourceManager getXAResourceManager() |
128 | { |
129 | return rm; |
130 | } |
131 | } |