001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.core.mdb;
017:
018: import java.rmi.RemoteException;
019: import javax.transaction.Status;
020:
021: import org.apache.openejb.ApplicationException;
022: import org.apache.openejb.SystemException;
023: import org.apache.openejb.ContainerType;
024: import org.apache.openejb.core.transaction.TransactionContainer;
025: import org.apache.openejb.core.transaction.TransactionContext;
026: import org.apache.openejb.core.transaction.TransactionPolicy;
027:
028: public class MessageDrivenBeanManagedTxPolicy extends TransactionPolicy {
029:
030: public MessageDrivenBeanManagedTxPolicy(
031: TransactionContainer container) {
032: super (Type.BeanManaged, container);
033: if (container.getContainerType() != ContainerType.MESSAGE_DRIVEN) {
034: throw new IllegalArgumentException();
035: }
036: }
037:
038: public void beforeInvoke(Object instance, TransactionContext context)
039: throws SystemException, ApplicationException {
040: context.clientTx = suspendTransaction(context);
041: }
042:
043: @SuppressWarnings({"EmptyCatchBlock"})
044: public void afterInvoke(Object instance, TransactionContext context)
045: throws ApplicationException, SystemException {
046:
047: try {
048: /*
049: * The Container must detect the case in which a transaction was started, but
050: * not completed, in the business method, and handle it as follows:
051: */
052: context.currentTx = context.getTransactionManager()
053: .getTransaction();
054:
055: if (context.currentTx == null)
056: return;
057:
058: if (context.currentTx.getStatus() != Status.STATUS_ROLLEDBACK
059: && context.currentTx.getStatus() != Status.STATUS_COMMITTED) {
060: String message = "The message driven bean started a transaction but did not complete it.";
061:
062: /* [1] Log this as an application error ********/
063: logger.error(message);
064:
065: /* [2] Roll back the started transaction *******/
066: try {
067: rollbackTransaction(context, context.currentTx);
068: } catch (Throwable ignore) {
069:
070: }
071:
072: /* [3] Throw the RemoteException to the client */
073: throwAppExceptionToServer(new RemoteException(message));
074: }
075:
076: } catch (javax.transaction.SystemException e) {
077: throw new SystemException(e);
078: } finally {
079: resumeTransaction(context, context.clientTx);
080: }
081: }
082:
083: public void handleApplicationException(Throwable appException,
084: boolean rollback, TransactionContext context)
085: throws ApplicationException, SystemException {
086: if (rollback && context.currentTx != null)
087: markTxRollbackOnly(context.currentTx);
088:
089: throw new ApplicationException(appException);
090: }
091:
092: public void handleSystemException(Throwable sysException,
093: Object instance, TransactionContext context)
094: throws ApplicationException, SystemException {
095: try {
096: context.currentTx = context.getTransactionManager()
097: .getTransaction();
098: } catch (javax.transaction.SystemException e) {
099: context.currentTx = null;
100: }
101:
102: logSystemException(sysException, context);
103:
104: if (context.currentTx != null)
105: markTxRollbackOnly(context.currentTx);
106:
107: discardBeanInstance(instance, context.callContext);
108:
109: throwExceptionToServer(sysException);
110: }
111:
112: }
|