001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.resource.jdbc;
018:
019: import java.io.File;
020: import java.sql.SQLException;
021: import java.util.Properties;
022:
023: import javax.sql.DataSource;
024: import javax.transaction.HeuristicMixedException;
025: import javax.transaction.HeuristicRollbackException;
026: import javax.transaction.InvalidTransactionException;
027: import javax.transaction.NotSupportedException;
028: import javax.transaction.RollbackException;
029: import javax.transaction.Synchronization;
030: import javax.transaction.SystemException;
031: import javax.transaction.Transaction;
032: import javax.transaction.TransactionManager;
033: import javax.transaction.xa.XAResource;
034:
035: import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
036: import org.apache.openejb.loader.SystemInstance;
037:
038: public class ManagedDataSourceWithRecovery extends
039: BasicManagedDataSource {
040: private TransactionManager suppliedTransactionManager;
041:
042: @Override
043: public void setTransactionManager(
044: TransactionManager transactionManager) {
045: this .suppliedTransactionManager = transactionManager;
046: }
047:
048: protected void wrapTransactionManager() {
049: if (suppliedTransactionManager != null) {
050: super .setTransactionManager(new TransactionManagerWrapper(
051: suppliedTransactionManager, getUrl()));
052: }
053: }
054:
055: private static class TransactionManagerWrapper implements
056: TransactionManager {
057:
058: private final TransactionManager transactionManager;
059: private final String name;
060:
061: private TransactionManagerWrapper(
062: TransactionManager transactionManager, String name) {
063: this .transactionManager = transactionManager;
064: this .name = name;
065: }
066:
067: public void begin() throws NotSupportedException,
068: SystemException {
069: transactionManager.begin();
070: }
071:
072: public void commit() throws HeuristicMixedException,
073: HeuristicRollbackException, IllegalStateException,
074: RollbackException, SecurityException, SystemException {
075: transactionManager.commit();
076: }
077:
078: public int getStatus() throws SystemException {
079: return transactionManager.getStatus();
080: }
081:
082: public Transaction getTransaction() throws SystemException {
083: Transaction tx = transactionManager.getTransaction();
084: return tx == null ? null : new TransactionWrapper(
085: transactionManager.getTransaction(), name);
086: }
087:
088: public void resume(Transaction transaction)
089: throws IllegalStateException,
090: InvalidTransactionException, SystemException {
091: transactionManager
092: .resume(((TransactionWrapper) transaction).transaction);
093: }
094:
095: public void rollback() throws IllegalStateException,
096: SecurityException, SystemException {
097: transactionManager.rollback();
098: }
099:
100: public void setRollbackOnly() throws IllegalStateException,
101: SystemException {
102: transactionManager.setRollbackOnly();
103: }
104:
105: public void setTransactionTimeout(int i) throws SystemException {
106: transactionManager.setTransactionTimeout(i);
107: }
108:
109: public Transaction suspend() throws SystemException {
110: return new TransactionWrapper(transactionManager.suspend(),
111: name);
112: }
113: }
114:
115: private static class TransactionWrapper implements Transaction {
116:
117: private final Transaction transaction;
118: private final String name;
119:
120: private TransactionWrapper(Transaction transaction, String name) {
121: this .transaction = transaction;
122: this .name = name;
123: }
124:
125: public void commit() throws HeuristicMixedException,
126: HeuristicRollbackException, RollbackException,
127: SecurityException, SystemException {
128: transaction.commit();
129: }
130:
131: public boolean delistResource(XAResource xaResource, int i)
132: throws IllegalStateException, SystemException {
133: XAResource wrapper = new WrapperNamedXAResource(xaResource,
134: name);
135: return transaction.delistResource(wrapper, i);
136: }
137:
138: public boolean enlistResource(XAResource xaResource)
139: throws IllegalStateException, RollbackException,
140: SystemException {
141: XAResource wrapper = new WrapperNamedXAResource(xaResource,
142: name);
143: return transaction.enlistResource(wrapper);
144: }
145:
146: public int getStatus() throws SystemException {
147: return transaction.getStatus();
148: }
149:
150: public void registerSynchronization(
151: Synchronization synchronization)
152: throws IllegalStateException, RollbackException,
153: SystemException {
154: transaction.registerSynchronization(synchronization);
155: }
156:
157: public void rollback() throws IllegalStateException,
158: SystemException {
159: transaction.rollback();
160: }
161:
162: public void setRollbackOnly() throws IllegalStateException,
163: SystemException {
164: transaction.setRollbackOnly();
165: }
166:
167: public boolean equals(Object o) {
168: if (this == o)
169: return true;
170: if (o == null || getClass() != o.getClass())
171: return false;
172:
173: TransactionWrapper that = (TransactionWrapper) o;
174:
175: return transaction.equals(that.transaction);
176: }
177:
178: public int hashCode() {
179: return transaction.hashCode();
180: }
181: }
182: }
|