KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > entity > EntityContainer


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: EntityContainer.java 1976 2005-07-06 02:04:04Z dblevins $
44  */

45 package org.openejb.core.entity;
46
47 import java.lang.reflect.Method JavaDoc;
48 import java.rmi.RemoteException JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 import javax.ejb.EJBHome JavaDoc;
53 import javax.ejb.EJBLocalHome JavaDoc;
54 import javax.ejb.EJBLocalObject JavaDoc;
55 import javax.ejb.EJBObject JavaDoc;
56 import javax.ejb.EnterpriseBean JavaDoc;
57 import javax.ejb.EntityBean JavaDoc;
58 import javax.transaction.Transaction JavaDoc;
59
60 import org.openejb.Container;
61 import org.openejb.DeploymentInfo;
62 import org.openejb.OpenEJB;
63 import org.openejb.OpenEJBException;
64 import org.openejb.ProxyInfo;
65 import org.openejb.SystemException;
66 import org.openejb.core.EnvProps;
67 import org.openejb.core.Operations;
68 import org.openejb.core.ThreadContext;
69 import org.openejb.core.transaction.TransactionContainer;
70 import org.openejb.core.transaction.TransactionContext;
71 import org.openejb.core.transaction.TransactionPolicy;
72 import org.openejb.util.Logger;
73 import org.openejb.util.SafeProperties;
74 import org.openejb.util.SafeToolkit;
75
76 /**
77  * Bean-Managed Persistence EntityBean container
78  *
79  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
80  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
81  * @version $Revision: 1976 $ $Date: 2005-07-05 19:04:04 -0700 (Tue, 05 Jul 2005) $
82  */

83 public class EntityContainer implements org.openejb.RpcContainer, TransactionContainer{
84
85     /**
86      * Managed bean instances; transaction ready and ready pools
87      */

88     protected EntityInstanceManager instanceManager;
89     
90     /**
91      * Contains deployment information for each by deployed to this container
92      */

93     protected HashMap JavaDoc deploymentRegistry;
94     
95     /**
96      * The unique id for this container
97      */

98     protected Object JavaDoc containerID = null;
99
100     // manages the transactional scope according to the bean's transaction attributes
101
//EntityTransactionScopeHandler txScopeHandle;
102
public Logger logger = Logger.getInstance( "OpenEJB", "org.openejb.util.resources" );
103
104
105     /**
106      * Construct this container with the specified container id, deployments,
107      * container manager and properties. The properties can include the class
108      * name of the preferred InstanceManager,
109      * org.openejb.core.entity.EntityInstanceManager is the default. The
110      * properties should also include the properties for the instance manager.
111      *
112      * @param id the unique id to identify this container in the ContainerSystem
113      * @param registry a hashMap of bean delpoyments that this container will be responsible for
114      * @param properties the properties this container needs to initialize and run
115      * @exception OpenEJBException
116      * if there is a problem constructing the container
117      * @exception org.openejb.OpenEJBException
118      * @see org.openejb.Container
119      */

120     public void init(Object JavaDoc id, HashMap JavaDoc registry, Properties JavaDoc properties)
121     throws org.openejb.OpenEJBException{
122         containerID = id;
123         deploymentRegistry = registry;
124
125         if(properties == null)properties = new Properties JavaDoc();
126
127
128         SafeToolkit toolkit = SafeToolkit.getToolkit("EntityContainer");
129         SafeProperties safeProps = toolkit.getSafeProperties(properties);
130         try{
131         String JavaDoc className = safeProps.getProperty(EnvProps.IM_CLASS_NAME, "org.openejb.core.entity.EntityInstanceManager");
132         ClassLoader JavaDoc cl = OpenEJB.getContextClassLoader();
133         instanceManager =(EntityInstanceManager)Class.forName(className, true, cl).newInstance();
134         }catch(Exception JavaDoc e){
135         throw new org.openejb.SystemException("Initialization of InstanceManager for the \""+containerID+"\" entity container failed",e);
136         }
137         instanceManager.init(this, registry, properties);
138
139
140         //txScopeHandle = new EntityTransactionScopeHandler(this,instanceManager);
141

142         /*
143         * This block of code is necessary to avoid a chicken and egg problem. The DeploymentInfo
144         * objects must have a reference to their container during this assembly process, but the
145         * container is created after the DeploymentInfo necessitating this loop to assign all
146         * deployment info object's their containers.
147         */

148         org.openejb.DeploymentInfo [] deploys = this.deployments();
149         for(int x = 0; x < deploys.length; x++){
150             org.openejb.core.DeploymentInfo di = (org.openejb.core.DeploymentInfo)deploys[x];
151             di.setContainer(this);
152         }
153
154     }
155     //===============================
156
// begin Container Implementation
157
//
158

159     /**
160      * Gets the <code>DeploymentInfo</code> objects for all the beans deployed in
161      * this container.
162      *
163      * @return an array of DeploymentInfo objects
164      * @see org.openejb.DeploymentInfo
165      */

166     public DeploymentInfo [] deployments(){
167         return (DeploymentInfo [])deploymentRegistry.values().toArray(new DeploymentInfo[deploymentRegistry.size()]);
168     }
169
170     /**
171      * Gets the <code>DeploymentInfo</code> object for the bean with the specified
172      * deployment id.
173      *
174      * @param deploymentID
175      * @return the DeploymentInfo object associated with the bean.
176      * @see org.openejb.DeploymentInfo
177      * @see org.openejb.DeploymentInfo#getDeploymentID()
178      */

179     public DeploymentInfo getDeploymentInfo(Object JavaDoc deploymentID){
180         return (DeploymentInfo)deploymentRegistry.get(deploymentID);
181     }
182     /**
183      * Gets the type of container (STATELESS, STATEFUL, ENTITY, or MESSAGE_DRIVEN
184      *
185      * @return id type bean container
186      */

187     public int getContainerType( ){
188         return Container.ENTITY;
189     }
190
191     /**
192      * Gets the id of this container.
193      *
194      * @return the id of this container.
195      */

196     public Object JavaDoc getContainerID(){
197         return containerID;
198     }
199
200     /**
201      * Adds a bean to this container.
202      * @param deploymentID the deployment id of the bean to deploy.
203      * @param info the DeploymentInfo object associated with the bean.
204      * @throws org.openejb.OpenEJBException
205      * Occurs when the container is not able to deploy the bean for some
206      * reason.
207      */

208     public void deploy(Object JavaDoc deploymentID, DeploymentInfo info) throws OpenEJBException {
209         HashMap JavaDoc registry = (HashMap JavaDoc)deploymentRegistry.clone();
210         registry.put(deploymentID, info);
211         deploymentRegistry = registry;
212     }
213
214     /**
215      * Invokes a method on an instance of the specified bean deployment.
216      *
217      * @param deployID the dployment id of the bean deployment
218      * @param callMethod the method to be called on the bean instance
219      * @param args the arguments to use when invoking the specified method
220      * @param primKey the primary key class of the bean or null if the bean does not need a primary key
221      * @param securityIdentity identity
222      * @return the result of invoking the specified method on the bean instance
223      * @throws org.openejb.OpenEJBException
224      * @see org.openejb.core.stateful.StatefulContainer#invoke StatefulContainer.invoke
225      */

226     public Object JavaDoc invoke(Object JavaDoc deployID, Method JavaDoc callMethod,Object JavaDoc [] args,Object JavaDoc primKey, Object JavaDoc securityIdentity) throws org.openejb.OpenEJBException{
227         try{
228
229         org.openejb.core.DeploymentInfo deployInfo = (org.openejb.core.DeploymentInfo)this.getDeploymentInfo(deployID);
230
231         ThreadContext callContext = ThreadContext.getThreadContext();
232         callContext.set(deployInfo, primKey, securityIdentity);
233
234         // check authorization to invoke
235

236         boolean authorized = OpenEJB.getSecurityService().isCallerAuthorized(securityIdentity, deployInfo.getAuthorizedRoles(callMethod));
237         if(!authorized)
238             throw new org.openejb.ApplicationException(new RemoteException JavaDoc("Unauthorized Access by Principal Denied"));
239
240         Class JavaDoc declaringClass = callMethod.getDeclaringClass();
241         String JavaDoc methodName = callMethod.getName();
242         
243         // process home interface methods
244
if(EJBHome JavaDoc.class.isAssignableFrom(declaringClass) || EJBLocalHome JavaDoc.class.isAssignableFrom(declaringClass) ){
245             if(declaringClass != EJBHome JavaDoc.class && declaringClass != EJBLocalHome JavaDoc.class){
246             // Its a home interface method, which is declared by the bean provider, but not a EJBHome method.
247
// only create(), find<METHOD>( ) and home business methods are declared by the bean provider.
248
if(methodName.equals("create")){
249                     // create( ) method called, execute ejbCreate() method
250
return createEJBObject(callMethod, args, callContext);
251                 }else if(methodName.startsWith("find")){
252                     // find<METHOD> called, execute ejbFind<METHOD>
253
return findMethod(callMethod, args, callContext);
254                 }else{
255                     // home method called, execute ejbHome method
256
return homeMethod(callMethod, args, callContext);
257                 }
258             }else if(methodName.equals("remove")){
259                 removeEJBObject(callMethod, args, callContext);
260                 return null;
261             }
262         } else if((EJBObject JavaDoc.class == declaringClass || EJBLocalObject JavaDoc.class == declaringClass) && methodName.equals("remove") ) {
263             removeEJBObject(callMethod, args, callContext);
264             return null;
265         }
266
267
268         // retreive instance from instance manager
269
callContext.setCurrentOperation(Operations.OP_BUSINESS);
270         Method JavaDoc runMethod = deployInfo.getMatchingBeanMethod(callMethod);
271         Object JavaDoc retValue = invoke(callMethod, runMethod, args, callContext) ;
272
273         // see comments in org.openejb.core.DeploymentInfo.
274
return deployInfo.convertIfLocalReference(callMethod, retValue);
275
276
277         }finally{
278             /*
279                 The thread context must be stripped from the thread before returning or throwing an exception
280                 so that an object outside the container does not have access to a
281                 bean's JNDI ENC. In addition, its important for the
282                 org.openejb.core.ivm.java.javaURLContextFactory, which determines the context
283                 of a JNDI lookup based on the presence of a ThreadContext object. If no ThreadContext
284                 object is available, then the request is assumed to be made from outside the container
285                 system and is given the global OpenEJB JNDI name space instead. If there is a thread context,
286                 then the request is assumed to be made from within the container system and so the
287                 javaContextFactory must return the JNDI ENC of the current enterprise bean which it
288                 obtains from the DeploymentInfo object associated with the current thread context.
289             */

290             ThreadContext.setThreadContext(null);
291         }
292     }
293     //
294
// end ContainerManager Implementation
295
//====================================
296

297
298     //============================================
299
// begin methods unique to this implementation
300
//
301

302     public EntityInstanceManager getInstanceManager( ){
303         return instanceManager;
304     }
305
306     protected Object JavaDoc invoke(Method JavaDoc callMethod, Method JavaDoc runMethod, Object JavaDoc [] args, ThreadContext callContext)
307     throws org.openejb.OpenEJBException{
308         
309         TransactionPolicy txPolicy = callContext.getDeploymentInfo().getTransactionPolicy( callMethod );
310         TransactionContext txContext = new TransactionContext();
311         txContext.callContext = callContext;
312
313         EntityBean JavaDoc bean = null;
314         txPolicy.beforeInvoke( bean, txContext );
315         
316         Object JavaDoc returnValue = null;
317         
318         try{
319             // this is nessary to ensure that the ejbLoad method is execute in the context of the business method
320
try{
321                 bean = instanceManager.obtainInstance(callContext);
322             }catch(org.openejb.OpenEJBException e){
323                 //TODO: Shouldn't we be throwing a NoSuchEntityException?
324
throw e.getRootCause();
325             }
326             
327             ejbLoad_If_No_Transaction(callContext,bean);
328             returnValue = runMethod.invoke(bean, args);
329             ejbStore_If_No_Transaction(callContext, bean);
330             instanceManager.poolInstance(callContext,bean);
331         }catch(java.lang.reflect.InvocationTargetException JavaDoc ite){// handle enterprise bean exceptions
332
if ( ite.getTargetException() instanceof RuntimeException JavaDoc ) {
333                 /* System Exception ****************************/
334                 // don't pool after system exception
335
txPolicy.handleSystemException( ite.getTargetException(), bean, txContext );
336             } else {
337                 /* Application Exception ***********************/
338                 instanceManager.poolInstance(callContext,bean);
339                 txPolicy.handleApplicationException( ite.getTargetException(), txContext );
340             }
341         }catch(org.openejb.SystemException se){
342             txPolicy.handleSystemException( se.getRootCause(), bean, txContext );
343         }catch(Throwable JavaDoc iae){// handle reflection exception
344
/*
345               Any exception thrown by reflection; not by the enterprise bean. Possible
346               Exceptions are:
347                 IllegalAccessException - if the underlying method is inaccessible.
348                 IllegalArgumentException - if the number of actual and formal parameters differ, or if an unwrapping conversion fails.
349                 NullPointerException - if the specified object is null and the method is an instance method.
350                 ExceptionInInitializerError - if the initialization provoked by this method fails.
351             */

352             txPolicy.handleSystemException( iae, bean, txContext );
353         }finally{
354             txPolicy.afterInvoke( bean, txContext );
355         }
356
357         return returnValue;
358     }
359     
360     /**
361      * If a business method or remove method is called without a transaction
362      * context the ejbLoad method must be invoked before the call is serviced.
363      * This provides a bean instance that is not in a transaction an opportunity
364      * to load its state.
365      *
366      * @param callContext
367      * @param bean
368      * @exception org.openejb.SystemException
369      * @exception Exception
370      */

371     public void ejbLoad_If_No_Transaction(ThreadContext callContext, EntityBean JavaDoc bean)
372     throws org.openejb.SystemException, Exception JavaDoc{
373         byte orginalOperation = callContext.getCurrentOperation();
374         if(orginalOperation == Operations.OP_BUSINESS || orginalOperation == Operations.OP_REMOVE){
375         
376             Transaction JavaDoc currentTx = null;
377             try{
378             currentTx = org.openejb.OpenEJB.getTransactionManager().getTransaction();
379             }catch(javax.transaction.SystemException JavaDoc se){
380                 throw new org.openejb.SystemException("Transaction Manager failure",se);
381             }
382             
383             if(currentTx ==null){
384                 callContext.setCurrentOperation(org.openejb.core.Operations.OP_LOAD);
385                 try{
386                     ((javax.ejb.EntityBean JavaDoc)bean).ejbLoad();
387                 }catch(Exception JavaDoc e){
388                     // this will always be handled by the invoke( ) method
389
instanceManager.discardInstance(callContext,(EntityBean JavaDoc)bean);
390                     throw e;
391                 }finally{
392                     callContext.setCurrentOperation(orginalOperation);
393                 }
394             }
395                 
396         }
397     }
398     
399     /**
400      * If a business method is called without a transaction context the ejbStore
401      * method must be invoked after the call is serviced. This provides a bean
402      * instance that is not in a transaction an opportunity to store its state.
403      *
404      * @param callContext
405      * @param bean
406      * @exception Exception
407      */

408     public void ejbStore_If_No_Transaction(ThreadContext callContext, EntityBean JavaDoc bean)
409     throws Exception JavaDoc{
410         
411         byte currentOp = callContext.getCurrentOperation();
412         if (currentOp == Operations.OP_BUSINESS){
413             
414             Transaction JavaDoc currentTx = null;
415             try{
416             currentTx = org.openejb.OpenEJB.getTransactionManager().getTransaction();
417             }catch(javax.transaction.SystemException JavaDoc se){
418                 throw new org.openejb.SystemException("Transaction Manager failure",se);
419             }
420             
421             if (currentTx == null){
422                 callContext.setCurrentOperation(org.openejb.core.Operations.OP_STORE);
423                 try{
424                     ((javax.ejb.EntityBean JavaDoc)bean).ejbStore();
425                 }catch(Exception JavaDoc e){
426                     // this will always be handled by the invoke( ) method
427
instanceManager.discardInstance(callContext,(EntityBean JavaDoc)bean);
428                     throw e;
429                 }finally{
430                     callContext.setCurrentOperation(currentOp);
431                 }
432             }
433         }
434     }
435         
436     protected void didCreateBean(ThreadContext callContext, EntityBean JavaDoc bean) throws org.openejb.OpenEJBException{
437     }
438
439     // create methods from home interface
440
protected ProxyInfo createEJBObject(Method JavaDoc callMethod, Object JavaDoc [] args, ThreadContext callContext)
441     throws org.openejb.OpenEJBException {
442
443         org.openejb.core.DeploymentInfo deploymentInfo = (org.openejb.core.DeploymentInfo)callContext.getDeploymentInfo();
444         
445         callContext.setCurrentOperation(Operations.OP_CREATE);
446         EntityBean JavaDoc bean = null;
447         Object JavaDoc primaryKey = null;
448         
449         TransactionPolicy txPolicy = callContext.getDeploymentInfo().getTransactionPolicy( callMethod );
450         TransactionContext txContext = new TransactionContext();
451         txContext.callContext = callContext;
452
453         /*
454         * According to section 9.1.5.1 of the EJB 1.1 specification, the "ejbPostCreate(...)
455         * method executes in the same transaction context as the previous ejbCreate(...) method."
456         *
457         * For this reason the TransactionScopeHandler methods usally preformed by the invoke( )
458         * operation must be handled here along with the call explicitly.
459         * This ensures that the afterInvoke() is not processed between the ejbCreate and ejbPostCreate methods to
460         * ensure that the ejbPostCreate executes in the same transaction context of the ejbCreate.
461         * This would otherwise not be possible if container-managed transactions were used because
462         * the TransactionScopeManager would attempt to commit the transaction immediately after the ejbCreate
463         * and before the ejbPostCreate had a chance to execute. Once the ejbPostCreate method execute the
464         * super classes afterInvoke( ) method will be executed committing the transaction if its a CMT.
465         */

466         
467         txPolicy.beforeInvoke( bean, txContext );
468         
469         try{
470
471             bean = instanceManager.obtainInstance(callContext);
472             Method JavaDoc ejbCreateMethod = deploymentInfo.getMatchingBeanMethod(callMethod);
473             
474             // invoke ejbCreateX
475
primaryKey = ejbCreateMethod.invoke(bean, args);
476                         
477             callContext.setPrimaryKey(primaryKey);
478             didCreateBean(callContext, bean);
479             callContext.setCurrentOperation(Operations.OP_POST_CREATE);
480
481             // invoke ejbPostCreateX
482
Method JavaDoc ejbPostCreateMethod = deploymentInfo.getMatchingPostCreateMethod(ejbCreateMethod);
483             
484             ejbPostCreateMethod.invoke(bean, args);
485             // might have change in didCreateBean e.g. in the castor container
486
primaryKey = callContext.getPrimaryKey();
487             callContext.setPrimaryKey(null);
488             instanceManager.poolInstance(callContext,bean);
489         }catch(java.lang.reflect.InvocationTargetException JavaDoc ite){// handle enterprise bean exceptions
490
if ( ite.getTargetException() instanceof RuntimeException JavaDoc ) {
491                 /* System Exception ****************************/
492                 txPolicy.handleSystemException( ite.getTargetException(), bean, txContext);
493             } else {
494                 /* Application Exception ***********************/
495                 instanceManager.poolInstance(callContext,bean);
496                 txPolicy.handleApplicationException( ite.getTargetException(), txContext);
497             }
498         }catch(OpenEJBException e){
499             txPolicy.handleSystemException( e.getRootCause(), bean, txContext);
500         }catch(Throwable JavaDoc e){// handle reflection exception
501
/*
502               Any exception thrown by reflection; not by the enterprise bean. Possible
503               Exceptions are:
504                 IllegalAccessException - if the underlying method is inaccessible.
505                 IllegalArgumentException - if the number of actual and formal parameters differ, or if an unwrapping conversion fails.
506                 NullPointerException - if the specified object is null and the method is an instance method.
507                 ExceptionInInitializerError - if the initialization provoked by this method fails.
508             */

509             txPolicy.handleSystemException( e, bean, txContext);
510         }finally{
511             txPolicy.afterInvoke( bean, txContext );
512         }
513         
514         Class JavaDoc callingClass = callMethod.getDeclaringClass();
515         boolean isLocalInterface = EJBLocalHome JavaDoc.class.isAssignableFrom(callingClass);
516         return new ProxyInfo(deploymentInfo, primaryKey, isLocalInterface, this);
517         
518         
519     }
520     /**
521      * This method is used to execute the find methods which are considered global
522      * in scope.
523      *
524      * Global methods use bean instances from the MethodReady pool and are not
525      * specific to on bean identity.
526      *
527      * The return value will be either a single ProxyInfo object or collection of
528      * ProxyInfo objects representing one or more remote references.
529      *
530      * @param callMethod
531      * @param args
532      * @param callContext
533      * @return Object
534      * @exception org.openejb.OpenEJBException
535      */

536     protected Object JavaDoc findMethod(Method JavaDoc callMethod, Object JavaDoc [] args, ThreadContext callContext)
537     throws org.openejb.OpenEJBException {
538         org.openejb.core.DeploymentInfo deploymentInfo = (org.openejb.core.DeploymentInfo)callContext.getDeploymentInfo();
539         callContext.setCurrentOperation(Operations.OP_FIND);
540         Method JavaDoc runMethod = deploymentInfo.getMatchingBeanMethod(callMethod);
541         Object JavaDoc returnValue = invoke(callMethod,runMethod, args, callContext);
542
543         Class JavaDoc callingClass = callMethod.getDeclaringClass();
544         boolean isLocalInterface = EJBLocalHome JavaDoc.class.isAssignableFrom(callingClass);
545
546         /*
547         * Find operations return either a single primary key or a collection of primary keys.
548         * The primary keys are converted to ProxyInfo objects.
549         */

550         if(returnValue instanceof java.util.Collection JavaDoc){
551             java.util.Iterator JavaDoc keys = ((java.util.Collection JavaDoc)returnValue).iterator();
552             java.util.Vector JavaDoc proxies = new java.util.Vector JavaDoc();
553             while(keys.hasNext()){
554                 Object JavaDoc primaryKey = keys.next();
555                 proxies.addElement(new ProxyInfo(deploymentInfo, primaryKey, isLocalInterface, this));
556             }
557             returnValue = proxies;
558         }else if(returnValue instanceof java.util.Enumeration JavaDoc){
559             java.util.Enumeration JavaDoc keys = (java.util.Enumeration JavaDoc)returnValue;
560             java.util.Vector JavaDoc proxies = new java.util.Vector JavaDoc();
561             while(keys.hasMoreElements()){
562                 Object JavaDoc primaryKey = keys.nextElement();
563                 proxies.addElement(new ProxyInfo(deploymentInfo, primaryKey, isLocalInterface, this));
564             }
565             returnValue = new org.openejb.util.ArrayEnumeration(proxies);
566         }else
567             returnValue = new ProxyInfo(deploymentInfo, returnValue, isLocalInterface, this);
568
569         return returnValue;
570     }
571
572     /**
573      * This method is used to execute the home methods which are considered global
574      * in scope.
575      *
576      * Global methods use bean instances from the MethodReady pool and are not
577      * specific to on bean identity.
578      *
579      * @param callMethod
580      * @param args
581      * @param callContext
582      * @return Object
583      * @exception org.openejb.OpenEJBException
584      */

585     protected Object JavaDoc homeMethod(Method JavaDoc callMethod, Object JavaDoc [] args, ThreadContext callContext)
586     throws org.openejb.OpenEJBException {
587         org.openejb.core.DeploymentInfo deploymentInfo = (org.openejb.core.DeploymentInfo)callContext.getDeploymentInfo();
588         callContext.setCurrentOperation(Operations.OP_HOME);
589         Method JavaDoc runMethod = deploymentInfo.getMatchingBeanMethod(callMethod);
590         return invoke(callMethod,runMethod, args, callContext);
591     }
592
593     protected void didRemove(EntityBean JavaDoc bean, ThreadContext callContext) throws OpenEJBException{
594     }
595     
596     protected void removeEJBObject(Method JavaDoc callMethod, Object JavaDoc [] args, ThreadContext callContext)
597     throws org.openejb.OpenEJBException {
598         callContext.setCurrentOperation(Operations.OP_REMOVE);
599
600         TransactionPolicy txPolicy = callContext.getDeploymentInfo().getTransactionPolicy( callMethod );
601         TransactionContext txContext = new TransactionContext();
602         txContext.callContext = callContext;
603
604         EntityBean JavaDoc bean = null;
605         txPolicy.beforeInvoke( bean, txContext );
606
607         try{
608             // this is nessary to ensure that the ejbLoad method is execute in the context of the business method
609
bean = instanceManager.obtainInstance(callContext);
610
611             ejbLoad_If_No_Transaction(callContext,bean);
612             bean.ejbRemove();
613             didRemove(bean, callContext);
614             instanceManager.poolInstance(callContext,bean);
615         }catch(org.openejb.SystemException se){
616             txPolicy.handleSystemException( se.getRootCause(), bean, txContext );
617         }catch(Exception JavaDoc e){// handle reflection exception
618
if ( e instanceof RuntimeException JavaDoc ) {
619                 /* System Exception ****************************/
620                 txPolicy.handleSystemException( e, bean, txContext );
621             } else {
622                 /* Application Exception ***********************/
623                 instanceManager.poolInstance(callContext,bean);
624                 txPolicy.handleApplicationException( e, txContext );
625             }
626         }finally{
627             txPolicy.afterInvoke( bean, txContext );
628         }
629     }
630
631
632     /**
633      * @param bean
634      * @param threadContext
635      */

636     public void discardInstance(EnterpriseBean JavaDoc bean, ThreadContext threadContext) {
637         if ( bean != null ) {
638             try{
639                 instanceManager.discardInstance(threadContext,(EntityBean JavaDoc)bean);
640             } catch (SystemException e){
641                 logger.error("The instance manager encountered an unkown system exception while trying to discard the entity instance with primary key "+threadContext.getPrimaryKey());
642             }
643         }
644     }
645     
646
647 }
648
Popular Tags