001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins;
023:
024: import javax.transaction.Status;
025: import javax.transaction.Synchronization;
026: import javax.transaction.Transaction;
027:
028: import org.jboss.ejb.EntityEnterpriseContext;
029: import org.jboss.metadata.ConfigurationMetaData;
030:
031: /**
032: * @deprecated this interceptor was used with Instance Per Transaction containers which do not use a global cache
033: * but cache instances per transaction and always passivate instances at commit time (commit option C).
034: * The only difference from the EntityInstanceInterceptor is that it uses specific instance Synchronization implementation
035: * which always passivates the instance at commit time which is equivalent to commit option C in standard container.
036: * Now, the differences between IPT and standard container are:
037: * <ul>
038: * <li>org.jboss.ejb.plugins.PerTxEntityInstanceCache as the cache implementation;</li>
039: * <li>NoLock as the locking policy;</li>
040: * <li>empty container-cache-conf element.</li>
041: * </ul>
042: * (alex@jboss.org)
043: *
044: * The role of this interceptor is to synchronize the state of the cache with
045: * the underlying storage. It does this with the ejbLoad and ejbStore
046: * semantics of the EJB specification. In the presence of a transaction this
047: * is triggered by transaction demarcation. It registers a callback with the
048: * underlying transaction monitor through the JTA interfaces. If there is no
049: * transaction the policy is to store state upon returning from invocation.
050: * The synchronization polices A,B,C of the specification are taken care of
051: * here.
052: *
053: * <p><b>WARNING: critical code</b>, get approval from senior developers
054: * before changing.
055: *
056: * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
057: * @version $Revision: 57209 $
058: */
059: public class EntityMultiInstanceSynchronizationInterceptor extends
060: EntitySynchronizationInterceptor {
061: public void create() throws Exception {
062: super .create();
063: }
064:
065: public void start() {
066: // This is in here so that EntityMultiInstanceInterceptor can avoid doing a lock.sync().
067: //
068: if (!container.getLockManager().lockClass
069: .equals(org.jboss.ejb.plugins.lock.NoLock.class)
070: && !container.getLockManager().lockClass
071: .equals(org.jboss.ejb.plugins.lock.JDBCOptimisticLock.class)
072: && !container.getLockManager().lockClass
073: .equals(org.jboss.ejb.plugins.lock.MethodOnlyEJBLock.class)) {
074: throw new IllegalStateException(
075: "the <locking-policy> must be org.jboss.ejb.plugins.lock.NoLock, JDBCOptimisticLock, or MethodOnlyEJBLock for Instance Per Transaction:"
076: + container.getLockManager().lockClass
077: .getName());
078: }
079: }
080:
081: protected Synchronization createSynchronization(Transaction tx,
082: EntityEnterpriseContext ctx) {
083: return new MultiInstanceSynchronization(tx, ctx);
084: }
085:
086: // Protected ----------------------------------------------------
087:
088: // Inner classes -------------------------------------------------
089:
090: protected class MultiInstanceSynchronization implements
091: Synchronization {
092: /**
093: * The transaction we follow.
094: */
095: protected Transaction tx;
096:
097: /**
098: * The context we manage.
099: */
100: protected EntityEnterpriseContext ctx;
101:
102: /**
103: * Create a new instance synchronization instance.
104: */
105: MultiInstanceSynchronization(Transaction tx,
106: EntityEnterpriseContext ctx) {
107: this .tx = tx;
108: this .ctx = ctx;
109: }
110:
111: // Synchronization implementation -----------------------------
112:
113: public void beforeCompletion() {
114: //synchronization is handled by GlobalTxEntityMap.
115: }
116:
117: public void afterCompletion(int status) {
118: boolean trace = log.isTraceEnabled();
119:
120: // This is an independent point of entry. We need to make sure the
121: // thread is associated with the right context class loader
122: ClassLoader oldCl = SecurityActions.getContextClassLoader();
123: SecurityActions.setContextClassLoader(container
124: .getClassLoader());
125:
126: ctx.hasTxSynchronization(false);
127: ctx.setTransaction(null);
128: try {
129: try {
130: // If rolled back -> invalidate instance
131: if (status != Status.STATUS_ROLLEDBACK) {
132: switch (commitOption) {
133: // Keep instance cached after tx commit
134: case ConfigurationMetaData.A_COMMIT_OPTION:
135: throw new IllegalStateException(
136: "Commit option A not allowed with this Interceptor");
137: // Keep instance active, but invalidate state
138: case ConfigurationMetaData.B_COMMIT_OPTION:
139: break;
140: // Invalidate everything AND Passivate instance
141: case ConfigurationMetaData.C_COMMIT_OPTION:
142: break;
143: case ConfigurationMetaData.D_COMMIT_OPTION:
144: throw new IllegalStateException(
145: "Commit option D not allowed with this Interceptor");
146: }
147: }
148: try {
149: if (ctx.getId() != null)
150: container.getPersistenceManager()
151: .passivateEntity(ctx);
152: } catch (Exception ignored) {
153: }
154: container.getInstancePool().free(ctx);
155: } finally {
156: if (trace)
157: log.trace("afterCompletion, clear tx for ctx="
158: + ctx + ", tx=" + tx);
159:
160: }
161: } // synchronized(lock)
162: finally {
163: SecurityActions.setContextClassLoader(oldCl);
164: }
165: }
166:
167: }
168:
169: }
|