01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.core.stateful;
17:
18: import org.apache.openejb.ApplicationException;
19: import org.apache.openejb.InvalidateReferenceException;
20: import org.apache.openejb.ContainerType;
21: import org.apache.openejb.SystemException;
22: import org.apache.openejb.core.transaction.TransactionContext;
23: import org.apache.openejb.core.transaction.TransactionPolicy;
24: import org.apache.openejb.core.Operation;
25:
26: import javax.ejb.SessionSynchronization;
27:
28: public class SessionSynchronizationTxPolicy extends TransactionPolicy {
29:
30: protected TransactionPolicy policy;
31:
32: public SessionSynchronizationTxPolicy(TransactionPolicy policy) {
33: super (policy.getPolicyType(), policy.getContainer());
34: this .policy = policy;
35: if (container.getContainerType() != ContainerType.STATEFUL
36: || getPolicyType() == TransactionPolicy.Type.Never
37: || getPolicyType() == TransactionPolicy.Type.NotSupported) {
38: throw new IllegalArgumentException();
39: }
40:
41: }
42:
43: public void beforeInvoke(Object instance, TransactionContext context)
44: throws SystemException, ApplicationException {
45: policy.beforeInvoke(instance, context);
46:
47: if (context.currentTx == null)
48: return;
49:
50: if (context.callContext.getCurrentOperation() == Operation.CREATE)
51: return;
52:
53: try {
54: StatefulInstanceManager.Instance instance2 = (StatefulInstanceManager.Instance) instance;
55: SessionSynchronizationCoordinator
56: .registerSessionSynchronization(instance2, context);
57: } catch (javax.transaction.RollbackException e) {
58: logger
59: .error("Cannot register the SessionSynchronization bean with the transaction, the transaction has been rolled back");
60: handleSystemException(e, instance, context);
61: } catch (javax.transaction.SystemException e) {
62: logger
63: .error("Cannot register the SessionSynchronization bean with the transaction, received an unknown system exception from the transaction manager: "
64: + e.getMessage());
65: handleSystemException(e, instance, context);
66: } catch (Throwable e) {
67: logger
68: .error("Cannot register the SessionSynchronization bean with the transaction, received an unknown exception: "
69: + e.getClass().getName()
70: + " "
71: + e.getMessage());
72: handleSystemException(e, instance, context);
73: }
74: }
75:
76: public void afterInvoke(Object instance, TransactionContext context)
77: throws ApplicationException, SystemException {
78: policy.afterInvoke(instance, context);
79: }
80:
81: public void handleApplicationException(Throwable appException,
82: boolean rollback, TransactionContext context)
83: throws ApplicationException, SystemException {
84: policy.handleApplicationException(appException, rollback,
85: context);
86: }
87:
88: public void handleSystemException(Throwable sysException,
89: Object instance, TransactionContext context)
90: throws ApplicationException, SystemException {
91: try {
92: policy.handleSystemException(sysException, instance,
93: context);
94: } catch (ApplicationException e) {
95: throw new InvalidateReferenceException(e.getRootCause());
96: }
97: }
98:
99: }
|