KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > EntityMultiInstanceSynchronizationInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins;
23
24
25 import javax.transaction.Status JavaDoc;
26 import javax.transaction.Synchronization JavaDoc;
27 import javax.transaction.Transaction JavaDoc;
28
29 import org.jboss.ejb.EntityEnterpriseContext;
30 import org.jboss.metadata.ConfigurationMetaData;
31
32 /**
33  * @deprecated this interceptor was used with Instance Per Transaction containers which do not use a global cache
34  * but cache instances per transaction and always passivate instances at commit time (commit option C).
35  * The only difference from the EntityInstanceInterceptor is that it uses specific instance Synchronization implementation
36  * which always passivates the instance at commit time which is equivalent to commit option C in standard container.
37  * Now, the differences between IPT and standard container are:
38  * <ul>
39  * <li>org.jboss.ejb.plugins.PerTxEntityInstanceCache as the cache implementation;</li>
40  * <li>NoLock as the locking policy;</li>
41  * <li>empty container-cache-conf element.</li>
42  * </ul>
43  * (alex@jboss.org)
44  *
45  * The role of this interceptor is to synchronize the state of the cache with
46  * the underlying storage. It does this with the ejbLoad and ejbStore
47  * semantics of the EJB specification. In the presence of a transaction this
48  * is triggered by transaction demarcation. It registers a callback with the
49  * underlying transaction monitor through the JTA interfaces. If there is no
50  * transaction the policy is to store state upon returning from invocation.
51  * The synchronization polices A,B,C of the specification are taken care of
52  * here.
53  *
54  * <p><b>WARNING: critical code</b>, get approval from senior developers
55  * before changing.
56  *
57  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
58  * @version $Revision: 37459 $
59  */

60 public class EntityMultiInstanceSynchronizationInterceptor
61         extends EntitySynchronizationInterceptor
62 {
63    public void create()
64            throws Exception JavaDoc
65    {
66       super.create();
67    }
68
69    public void start()
70    {
71       // This is in here so that EntityMultiInstanceInterceptor can avoid doing a lock.sync().
72
//
73
if (!container.getLockManager().lockClass.equals(org.jboss.ejb.plugins.lock.NoLock.class)
74           && !container.getLockManager().lockClass.equals(org.jboss.ejb.plugins.lock.JDBCOptimisticLock.class)
75           && !container.getLockManager().lockClass.equals(org.jboss.ejb.plugins.lock.MethodOnlyEJBLock.class)
76           )
77       {
78          throw new IllegalStateException JavaDoc("the <locking-policy> must be org.jboss.ejb.plugins.lock.NoLock, JDBCOptimisticLock, or MethodOnlyEJBLock for Instance Per Transaction:"
79                                           + container.getLockManager().lockClass.getName());
80       }
81    }
82
83    protected Synchronization JavaDoc createSynchronization(Transaction JavaDoc tx, EntityEnterpriseContext ctx)
84    {
85       return new MultiInstanceSynchronization(tx, ctx);
86    }
87    // Protected ----------------------------------------------------
88

89    // Inner classes -------------------------------------------------
90

91    protected class MultiInstanceSynchronization implements Synchronization JavaDoc
92    {
93       /**
94        * The transaction we follow.
95        */

96       protected Transaction JavaDoc tx;
97
98       /**
99        * The context we manage.
100        */

101       protected EntityEnterpriseContext ctx;
102
103       /**
104        * Create a new instance synchronization instance.
105        */

106       MultiInstanceSynchronization(Transaction JavaDoc tx, EntityEnterpriseContext ctx)
107       {
108          this.tx = tx;
109          this.ctx = ctx;
110       }
111
112       // Synchronization implementation -----------------------------
113

114       public void beforeCompletion()
115       {
116          //synchronization is handled by GlobalTxEntityMap.
117
}
118
119       public void afterCompletion(int status)
120       {
121          boolean trace = log.isTraceEnabled();
122
123          // This is an independent point of entry. We need to make sure the
124
// thread is associated with the right context class loader
125
ClassLoader JavaDoc oldCl = SecurityActions.getContextClassLoader();
126          SecurityActions.setContextClassLoader(container.getClassLoader());
127
128          ctx.hasTxSynchronization(false);
129          ctx.setTransaction(null);
130          try
131          {
132             try
133             {
134                // If rolled back -> invalidate instance
135
if (status != Status.STATUS_ROLLEDBACK)
136                {
137                   switch (commitOption)
138                   {
139                      // Keep instance cached after tx commit
140
case ConfigurationMetaData.A_COMMIT_OPTION:
141                         throw new IllegalStateException JavaDoc("Commit option A not allowed with this Interceptor");
142                         // Keep instance active, but invalidate state
143
case ConfigurationMetaData.B_COMMIT_OPTION:
144                         break;
145                         // Invalidate everything AND Passivate instance
146
case ConfigurationMetaData.C_COMMIT_OPTION:
147                         break;
148                      case ConfigurationMetaData.D_COMMIT_OPTION:
149                         throw new IllegalStateException JavaDoc("Commit option D not allowed with this Interceptor");
150                   }
151                }
152                try
153                {
154                   if (ctx.getId() != null)
155                      container.getPersistenceManager().passivateEntity(ctx);
156                }
157                catch (Exception JavaDoc ignored)
158                {
159                }
160                container.getInstancePool().free(ctx);
161             }
162             finally
163             {
164                if (trace)
165                   log.trace("afterCompletion, clear tx for ctx=" + ctx + ", tx=" + tx);
166
167             }
168          } // synchronized(lock)
169
finally
170          {
171             SecurityActions.setContextClassLoader(oldCl);
172          }
173       }
174
175    }
176
177 }
178
Popular Tags