KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.ejb.EntityContainer;
25 import org.jboss.ejb.EntityPersistenceManager;
26 import org.jboss.ejb.EntityEnterpriseContext;
27 import org.jboss.ejb.InstancePool;
28 import org.jboss.ejb.AllowedOperationsAssociation;
29 import org.jboss.invocation.Invocation;
30 import org.jboss.invocation.InvocationType;
31
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.TimedObject JavaDoc;
34 import javax.ejb.Timer JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.rmi.RemoteException JavaDoc;
37
38 /**
39  * @deprecated this interceptor was used with Instance Per Transaction containers which do not use a global cache
40  * but cache instances per transaction and always passivate instances at commit time (commit option C).
41  * This interceptor used org.jboss.ejb.TxEntityMap to compensate the absence of a real per transaction cache implementation.
42  * Now, the differences between IPT and standard container are:
43  * <ul>
44  * <li>org.jboss.ejb.plugins.PerTxEntityInstanceCache as the cache implementation;</li>
45  * <li>NoLock as the locking policy;</li>
46  * <li>empty container-cache-conf element.</li>
47  * </ul>
48  * (alex@jboss.org)
49  *
50  * The instance interceptors role is to acquire a context representing
51  * the target object from the cache.
52  *
53  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
54  * @version $Revision: 37459 $
55  */

56 public class EntityMultiInstanceInterceptor
57    extends AbstractInterceptor
58 {
59    // Constants -----------------------------------------------------
60

61    // Attributes ----------------------------------------------------
62

63    // Static --------------------------------------------------------
64

65    /** A reference to {@link javax.ejb.TimedObject#ejbTimeout}. */
66    protected static final Method JavaDoc ejbTimeout;
67    static
68    {
69       try
70       {
71          ejbTimeout = TimedObject JavaDoc.class.getMethod("ejbTimeout", new Class JavaDoc[]{Timer JavaDoc.class});
72       }
73       catch (Exception JavaDoc e)
74       {
75          throw new ExceptionInInitializerError JavaDoc(e);
76       }
77    }
78
79    // Constructors --------------------------------------------------
80

81     // Public --------------------------------------------------------
82

83    // Interceptor implementation --------------------------------------
84

85    public Object JavaDoc invokeHome(Invocation mi)
86       throws Exception JavaDoc
87    {
88       // Get context
89
EntityContainer ec = (EntityContainer) getContainer();
90       EntityEnterpriseContext ctx = (EntityEnterpriseContext) ec.getInstancePool().get();
91
92         // Pass it to the method invocation
93
mi.setEnterpriseContext(ctx);
94
95       // Give it the transaction
96
ctx.setTransaction(mi.getTransaction());
97
98       // Set the current security information
99
ctx.setPrincipal(mi.getPrincipal());
100
101       AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_HOME);
102
103       Object JavaDoc result;
104       try
105       {
106          // Invoke through interceptors
107
result = getNext().invokeHome(mi);
108       }
109       finally
110       {
111          AllowedOperationsAssociation.popInMethodFlag();
112       }
113       
114       // No id, means we can put the context back in the pool
115
if (ctx.getId() == null)
116       {
117          ctx.setTransaction(null);
118          ec.getInstancePool().free(ctx);
119       }
120       
121       // We are done
122
return result;
123    }
124
125    public Object JavaDoc invoke(Invocation mi)
126       throws Exception JavaDoc
127    {
128
129       // The key
130
Object JavaDoc key = mi.getId();
131
132       EntityEnterpriseContext ctx = null;
133       EntityContainer ec = (EntityContainer) container;
134       if (mi.getTransaction() != null)
135       {
136          //ctx = ec.getTxEntityMap().getCtx(mi.getTransaction(), key);
137
}
138       if (ctx == null)
139       {
140          InstancePool pool = ec.getInstancePool();
141          try
142          {
143             ctx = (EntityEnterpriseContext) pool.get();
144          }
145          catch (EJBException JavaDoc e)
146          {
147             throw e;
148          }
149          catch (RemoteException JavaDoc e)
150          {
151             throw e;
152          }
153          catch (Exception JavaDoc e)
154          {
155             InvocationType type = mi.getType();
156             boolean isLocal = (type == InvocationType.LOCAL || type == InvocationType.LOCALHOME);
157             if (isLocal)
158                throw new EJBException JavaDoc("Unable to get an instance from the pool", e);
159             else
160                throw new RemoteException JavaDoc("Unable to get an intance from the pool", e);
161          }
162          ctx.setCacheKey(key);
163          ctx.setId(key);
164          EntityPersistenceManager pm = ec.getPersistenceManager();
165          pm.activateEntity(ctx);
166       }
167
168       boolean trace = log.isTraceEnabled();
169       if( trace ) log.trace("Begin invoke, key="+key);
170
171       // Associate transaction, in the new design the lock already has the transaction from the
172
// previous interceptor
173
ctx.setTransaction(mi.getTransaction());
174
175       // Set the current security information
176
ctx.setPrincipal(mi.getPrincipal());
177       // Set the JACC EnterpriseBean PolicyContextHandler data
178
EnterpriseBeanPolicyContextHandler.setEnterpriseBean(ctx.getInstance());
179
180       // Set context on the method invocation
181
mi.setEnterpriseContext(ctx);
182
183       if (ejbTimeout.equals(mi.getMethod()))
184          AllowedOperationsAssociation.pushInMethodFlag(IN_EJB_TIMEOUT);
185       else
186          AllowedOperationsAssociation.pushInMethodFlag(IN_BUSINESS_METHOD);
187
188       try
189       {
190          Object JavaDoc ret = getNext().invoke(mi);
191          return ret;
192       }
193       finally
194       {
195          AllowedOperationsAssociation.popInMethodFlag();
196       }
197    }
198 }
199
Popular Tags