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.stateless;
017:
018: import org.apache.openejb.ApplicationException;
019: import org.apache.openejb.ContainerType;
020: import org.apache.openejb.SystemException;
021: import org.apache.openejb.core.transaction.TransactionContainer;
022: import org.apache.openejb.core.transaction.TransactionContext;
023: import org.apache.openejb.core.transaction.TransactionPolicy;
024:
025: import javax.transaction.Status;
026: import java.rmi.RemoteException;
027:
028: public class StatelessBeanManagedTxPolicy extends TransactionPolicy {
029:
030: public StatelessBeanManagedTxPolicy(TransactionContainer container) {
031: super (Type.BeanManaged, container);
032: if (container.getContainerType() != ContainerType.STATELESS) {
033: throw new IllegalArgumentException();
034: }
035: }
036:
037: public String policyToString() {
038: return "TX_BeanManaged: ";
039: }
040:
041: public void beforeInvoke(Object instance, TransactionContext context)
042: throws SystemException, ApplicationException {
043:
044: context.clientTx = suspendTransaction(context);
045: }
046:
047: public void afterInvoke(Object instance, TransactionContext context)
048: throws ApplicationException, SystemException {
049: try {
050: /*
051: * The Container must detect the case in which a transaction was started, but
052: * not completed, in the business method, and handle it as follows:
053: */
054: context.currentTx = context.getTransactionManager()
055: .getTransaction();
056:
057: if (context.currentTx == null)
058: return;
059:
060: if (context.currentTx.getStatus() != Status.STATUS_ROLLEDBACK
061: && context.currentTx.getStatus() != Status.STATUS_COMMITTED) {
062: String message = "The stateless session bean started a transaction but did not complete it.";
063:
064: /* [1] Log this as an application error ********/
065: logger.error(message);
066:
067: /* [2] Roll back the started transaction *******/
068: try {
069: rollbackTransaction(context, context.currentTx);
070: } catch (Throwable t) {
071:
072: }
073:
074: /* [3] Throw the RemoteException to the client */
075: throwAppExceptionToServer(new RemoteException(message));
076: }
077:
078: } catch (javax.transaction.SystemException e) {
079: throw new SystemException(e);
080: } finally {
081: resumeTransaction(context, context.clientTx);
082: }
083: }
084:
085: public void handleApplicationException(Throwable appException,
086: boolean rollback, TransactionContext context)
087: throws ApplicationException, SystemException {
088: if (rollback && context.currentTx != null)
089: markTxRollbackOnly(context.currentTx);
090:
091: throw new ApplicationException(appException);
092: }
093:
094: public void handleSystemException(Throwable sysException,
095: Object instance, TransactionContext context)
096: throws ApplicationException, SystemException {
097: try {
098: context.currentTx = context.getTransactionManager()
099: .getTransaction();
100: } catch (javax.transaction.SystemException e) {
101: context.currentTx = null;
102: }
103:
104: logSystemException(sysException, context);
105:
106: if (context.currentTx != null)
107: markTxRollbackOnly(context.currentTx);
108:
109: discardBeanInstance(instance, context.callContext);
110:
111: throwExceptionToServer(sysException);
112:
113: }
114:
115: }
|