User: starksm Date: 01/11/02 18:37:23 Modified: src/main/org/jboss/ejb/plugins CMPPersistenceManager.java Log: Cleanup formatting Revision Changes Path 1.34 +354 -259 jboss/src/main/org/jboss/ejb/plugins/CMPPersistenceManager.java Index: CMPPersistenceManager.java =================================================================== RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/CMPPersistenceManager.java,v retrieving revision 1.33 retrieving revision 1.34 diff -u -r1.33 -r1.34 --- CMPPersistenceManager.java 2001/11/03 02:07:41 1.33 +++ CMPPersistenceManager.java 2001/11/03 02:37:23 1.34 @@ -1,9 +1,9 @@ /* -* JBoss, the OpenSource J2EE webOS -* -* Distributable under LGPL license. -* See terms of license at gnu.org. -*/ + * JBoss, the OpenSource J2EE webOS + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ package org.jboss.ejb.plugins; import java.lang.reflect.Method; @@ -42,27 +42,28 @@ import org.jboss.management.j2ee.TimeStatistic; /** -* The CMP Persistence Manager implements the semantics of the CMP -* EJB 1.1 call back specification. -* -* This Manager works with a "EntityPersistenceStore" that takes care of the -* physical storing of instances (JAWS, JDBC O/R, FILE, Object). -* -* @see <related> -* @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a> -* @author <a href="mailto:[EMAIL PROTECTED]">Dan Christopherson</a> -* @author <a href="mailto:[EMAIL PROTECTED]">Bill Burke</a> -* @author <a href="mailto:[EMAIL PROTECTED]">Andreas Schaefer</a> -* @version $Revision: 1.33 $ -* -* Revisions: -* 20010621 Bill Burke: removed loadEntities call because CMP read-ahead is now -* done directly by the finder. -* 20010709 Andreas Schaefer: added statistics gathering -* -*/ + * The CMP Persistence Manager implements the semantics of the CMP + * EJB 1.1 call back specification. + * + * This Manager works with a "EntityPersistenceStore" that takes care of the + * physical storing of instances (JAWS, JDBC O/R, FILE, Object). + * + * @see <related> + * @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Dan Christopherson</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Bill Burke</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Andreas Schaefer</a> + * @version $Revision: 1.34 $ + * + * Revisions: + * 20010621 Bill Burke: removed loadEntities call because CMP read-ahead is now + * done directly by the finder. + * 20010709 Andreas Schaefer: added statistics gathering + * + */ public class CMPPersistenceManager - implements EntityPersistenceManager { + implements EntityPersistenceManager +{ // Constants ----------------------------------------------------- // Attributes ---------------------------------------------------- @@ -89,39 +90,43 @@ private TimeStatistic mStore = new TimeStatistic( "Store", "ms", "Load Time" ); // Static -------------------------------------------------------- - - // Constructors -------------------------------------------------- - - // Public -------------------------------------------------------- - public void setContainer(Container c) { + + // Constructors -------------------------------------------------- + + // Public -------------------------------------------------------- + public void setContainer(Container c) + { con = (EntityContainer)c; - if (store != null) store.setContainer(c); + if (store != null) + store.setContainer(c); } - /** - * Gets the entity persistence store. - */ - public EntityPersistenceStore getPersistenceStore() { - return store; - } + /** + * Gets the entity persistence store. + */ + public EntityPersistenceStore getPersistenceStore() + { + return store; + } - public void setPersistenceStore(EntityPersistenceStore store) { + public void setPersistenceStore(EntityPersistenceStore store) + { this.store= store; - + //Give it the container if (con!= null) store.setContainer(con); } public void init() - throws Exception { - + throws Exception + { // The common EJB methods ejbLoad = EntityBean.class.getMethod("ejbLoad", new Class[0]); ejbStore = EntityBean.class.getMethod("ejbStore", new Class[0]); ejbActivate = EntityBean.class.getMethod("ejbActivate", new Class[0]); ejbPassivate = EntityBean.class.getMethod("ejbPassivate", new Class[0]); ejbRemove = EntityBean.class.getMethod("ejbRemove", new Class[0]); - + if (con.getHomeClass() != null) { Method[] methods = con.getHomeClass().getMethods(); @@ -131,139 +136,178 @@ { Method[] methods = con.getLocalHomeClass().getMethods(); createMethodCache( methods ); - } - + } + store.init(); } - - /** - * Returns a new instance of the bean class or a subclass of the bean class. - * - * @return the new instance - */ - public Object createBeanClassInstance() throws Exception { - if(store instanceof EntityPersistenceStore2) { - return ((EntityPersistenceStore2)store).createBeanClassInstance(); - } - return con.getBeanClass().newInstance(); - } + /** + * Returns a new instance of the bean class or a subclass of the bean class. + * + * @return the new instance + */ + public Object createBeanClassInstance() throws Exception + { + if(store instanceof EntityPersistenceStore2) + { + return ((EntityPersistenceStore2)store).createBeanClassInstance(); + } + return con.getBeanClass().newInstance(); + } + private void createMethodCache( Method[] methods ) throws NoSuchMethodException { // Create cache of create methods + Class beanClass = con.getBeanClass(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals("create")) { - try{ - createMethods.put(methods[i], con.getBeanClass().getMethod("ejbCreate", methods[i].getParameterTypes())); - postCreateMethods.put(methods[i], con.getBeanClass().getMethod("ejbPostCreate", methods[i].getParameterTypes())); - } - catch (NoSuchMethodException nsme){ - throw new NoSuchMethodException("Can't find ejb[Post]Create in "+con.getBeanClass().getName()); - } + Class[] types = methods[i].getParameterTypes(); + try + { + Method beanMethod = beanClass.getMethod("ejbCreate", types); + createMethods.put(methods[i], beanMethod); + beanMethod = beanClass.getMethod("ejbPostCreate", types); + postCreateMethods.put(methods[i], beanMethod); + } + catch (NoSuchMethodException nsme) + { + throw new NoSuchMethodException("Can't find ejb[Post]Create in "+beanClass.getName()); + } } } - } public void start() - throws Exception { - + throws Exception + { store.start(); } - - public void stop() { + + public void stop() + { store.stop(); } - - public void destroy() { + + public void destroy() + { store.destroy(); } + + public void createEntity(Method m, Object[] args, EntityEnterpriseContext ctx) + throws Exception + { + // Get methods + Method createMethod = (Method)createMethods.get(m); + Method postCreateMethod = (Method)postCreateMethods.get(m); + + // Deligate initialization of bean to persistence store + // if the store can handle initialization. + if(store instanceof EntityPersistenceStore2) + { + ((EntityPersistenceStore2)store).initEntity(ctx); + } + else + { + // for backwards compatibility + initEntity(ctx); + } - public void createEntity(Method m, Object[] args, EntityEnterpriseContext ctx) - throws Exception { + // Call ejbCreate on the target bean + try + { + createMethod.invoke(ctx.getInstance(), args); + } + catch (IllegalAccessException e) + { + // Throw this as a bean exception...(?) + throw new EJBException(e); + } + catch (InvocationTargetException ite) + { + Throwable e = ite.getTargetException(); + if(e instanceof EJBException) + { + // Rethrow exception + throw (EJBException)e; + } + else if (e instanceof RuntimeException) + { + // Wrap runtime exceptions + throw new EJBException((Exception)e); + } + else if(e instanceof Exception) + { + // Remote, Create, or custom app. exception + throw (Exception)e; + } + else + { + throw (Error)e; + } + } - // Get methods - Method createMethod = (Method)createMethods.get(m); - Method postCreateMethod = (Method)postCreateMethods.get(m); - - // Deligate initialization of bean to persistence store - // if the store can handle initialization. - if(store instanceof EntityPersistenceStore2) { - ((EntityPersistenceStore2)store).initEntity(ctx); - } else { - // for backwards compatibility - initEntity(ctx); - } - - // Call ejbCreate on the target bean - try { - createMethod.invoke(ctx.getInstance(), args); - } catch (IllegalAccessException e){ - // Throw this as a bean exception...(?) - throw new EJBException(e); - } catch (InvocationTargetException ite) { - Throwable e = ite.getTargetException(); - if(e instanceof EJBException) { - // Rethrow exception - throw (EJBException)e; - } else if (e instanceof RuntimeException) { - // Wrap runtime exceptions - throw new EJBException((Exception)e); - } else if(e instanceof Exception) { + // Have the store persist the new instance, the return is the key + Object id = store.createEntity(m, args, ctx); + + // Set the key on the target context + ctx.setId(id); + + // Create a new CacheKey + Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id ); + + // Give it to the context + ctx.setCacheKey(cacheKey); + + // Create EJBObject + if (con.getContainerInvoker() != null) + { + ctx.setEJBObject(con.getContainerInvoker().getEntityEJBObject(cacheKey)); + } + if (con.getLocalHomeClass() != null) + { + ctx.setEJBLocalObject(con.getLocalContainerInvoker().getEntityEJBLocalObject(cacheKey)); + } + + try + { + postCreateMethod.invoke(ctx.getInstance(), args); + } + catch (IllegalAccessException e) + { + // Throw this as a bean exception...(?) + throw new EJBException(e); + } + catch (InvocationTargetException ite) + { + Throwable e = ite.getTargetException(); + if (e instanceof EJBException) + { + // Rethrow exception + throw (EJBException)e; + } + else if (e instanceof RuntimeException) + { + // Wrap runtime exceptions + throw new EJBException((Exception)e); + } + else if (e instanceof Exception) + { // Remote, Create, or custom app. exception - throw (Exception)e; - } else { - throw (Error)e; - } - } - - // Have the store persist the new instance, the return is the key - Object id = store.createEntity(m, args, ctx); - - // Set the key on the target context - ctx.setId(id); - - // Create a new CacheKey - Object cacheKey = ((EntityCache) con.getInstanceCache()).createCacheKey( id ); - - // Give it to the context - ctx.setCacheKey(cacheKey); - - // Create EJBObject - if (con.getContainerInvoker() != null) { - ctx.setEJBObject(con.getContainerInvoker().getEntityEJBObject(cacheKey)); - } - if (con.getLocalHomeClass() != null) { - ctx.setEJBLocalObject(con.getLocalContainerInvoker().getEntityEJBLocalObject(cacheKey)); - } - - try { - postCreateMethod.invoke(ctx.getInstance(), args); - } catch (IllegalAccessException e) { - // Throw this as a bean exception...(?) - throw new EJBException(e); - } catch (InvocationTargetException ite) { - Throwable e = ite.getTargetException(); - if (e instanceof EJBException) { - // Rethrow exception - throw (EJBException)e; - } else if (e instanceof RuntimeException) { - // Wrap runtime exceptions - throw new EJBException((Exception)e); - } else if (e instanceof Exception) { - // Remote, Create, or custom app. exception - throw (Exception)e; - } else { - throw (Error)e; - } - } - } + throw (Exception)e; + } + else + { + throw (Error)e; + } + } + } public Object findEntity(Method finderMethod, Object[] args, EntityEnterpriseContext ctx) - throws Exception { + throws Exception + { // For now only optimize fBPK if (finderMethod.getName().equals("findByPrimaryKey")) { @@ -284,13 +328,13 @@ // We return the cache key return ((EntityCache) con.getInstanceCache()).createCacheKey(id); } - + /** find multiple entities */ public Collection findEntities(Method finderMethod, Object[] args, EntityEnterpriseContext ctx) throws Exception { - // return the finderResults so that the invoker layer can extend this back - // giving the client an OO 'cursor' + // return the finderResults so that the invoker layer can extend this back + // giving the client an OO 'cursor' return store.findEntities(finderMethod, args, ctx); } @@ -306,111 +350,128 @@ * @see activateEntity on EntityPersistenceStore.java */ public void activateEntity(EntityEnterpriseContext ctx) - throws RemoteException { - + throws RemoteException + { + // Call bean try { ejbActivate.invoke(ctx.getInstance(), new Object[0]); - } catch (IllegalAccessException e) + } + catch (IllegalAccessException e) { // Throw this as a bean exception...(?) throw new EJBException(e); - } catch (InvocationTargetException ite) + } + catch (InvocationTargetException ite) { Throwable e = ite.getTargetException(); if (e instanceof RemoteException) { // Rethrow exception throw (RemoteException)e; - } else if (e instanceof EJBException) + } + else if (e instanceof EJBException) { // Rethrow exception throw (EJBException)e; - } else if (e instanceof RuntimeException) + } + else if (e instanceof RuntimeException) { // Wrap runtime exceptions throw new EJBException((Exception)e); } } - + long lStart = System.currentTimeMillis(); // The implementation of the call can be left absolutely empty, the propagation of the call - // is just a notification for stores that would need to know that an instance is being activated + // is just a notification for stores that would need to know that an instance is being activated store.activateEntity(ctx); mActivation.add( System.currentTimeMillis() - lStart ); } public void loadEntity(EntityEnterpriseContext ctx) - throws RemoteException { - + throws RemoteException + { + long lStart = System.currentTimeMillis(); // Have the store load the fields of the instance store.loadEntity(ctx); mLoad.add( System.currentTimeMillis() - lStart ); - + invokeLoad(ctx); } - + public void storeEntity(EntityEnterpriseContext ctx) - throws RemoteException { + throws RemoteException + { // Logger.debug("Store entity"); - try { - + try + { + // Prepare the instance for storage ejbStore.invoke(ctx.getInstance(), new Object[0]); - } catch (IllegalAccessException e) + } + catch (IllegalAccessException e) { // Throw this as a bean exception...(?) throw new EJBException(e); - } catch (InvocationTargetException ite) + } + catch (InvocationTargetException ite) { Throwable e = ite.getTargetException(); if (e instanceof RemoteException) { // Rethrow exception throw (RemoteException)e; - } else if (e instanceof EJBException) + } + else if (e instanceof EJBException) { // Rethrow exception throw (EJBException)e; - } else if (e instanceof RuntimeException) + } + else if (e instanceof RuntimeException) { // Wrap runtime exceptions throw new EJBException((Exception)e); } } - + long lStart = System.currentTimeMillis(); // Have the store deal with storing the fields of the instance store.storeEntity(ctx); mStore.add( System.currentTimeMillis() - lStart ); - + } - + public void passivateEntity(EntityEnterpriseContext ctx) - throws RemoteException { - - try { - + throws RemoteException + { + + try + { // Prepare the instance for passivation ejbPassivate.invoke(ctx.getInstance(), new Object[0]); - } catch (IllegalAccessException e) + } + catch (IllegalAccessException e) { // Throw this as a bean exception...(?) throw new EJBException(e); - } catch (InvocationTargetException ite) + } + catch (InvocationTargetException ite) { Throwable e = ite.getTargetException(); if (e instanceof RemoteException) { // Rethrow exception throw (RemoteException)e; - } else if (e instanceof EJBException) + } + else if (e instanceof EJBException) { // Rethrow exception throw (EJBException)e; - } else if (e instanceof RuntimeException) + } + else if (e instanceof RuntimeException) { // Wrap runtime exceptions throw new EJBException((Exception)e); @@ -423,17 +484,20 @@ } public void removeEntity(EntityEnterpriseContext ctx) - throws RemoteException, RemoveException { - - try { - + throws RemoteException, RemoveException + { + try + { + // Call ejbRemove ejbRemove.invoke(ctx.getInstance(), new Object[0]); - } catch (IllegalAccessException e) + } + catch (IllegalAccessException e) { // Throw this as a bean exception...(?) throw new EJBException(e); - } catch (InvocationTargetException ite) + } + catch (InvocationTargetException ite) { Throwable e = ite.getTargetException(); if (e instanceof RemoveException) @@ -445,44 +509,51 @@ { // Rethrow exception throw (RemoteException)e; - } else if (e instanceof EJBException) + } + else if (e instanceof EJBException) { // Rethrow exception throw (EJBException)e; - } else if (e instanceof RuntimeException) + } + else if (e instanceof RuntimeException) { // Wrap runtime exceptions throw new EJBException((Exception)e); } } - + long lStart = System.currentTimeMillis(); store.removeEntity(ctx); mRemove.add(); } - - protected void invokeLoad(EntityEnterpriseContext ctx) throws RemoteException { - try { + protected void invokeLoad(EntityEnterpriseContext ctx) throws RemoteException + { + try + { // Call ejbLoad on bean instance, wake up! ejbLoad.invoke(ctx.getInstance(), new Object[0]); - - } catch (IllegalAccessException e) + + } + catch (IllegalAccessException e) { // Throw this as a bean exception...(?) throw new EJBException(e); - } catch (InvocationTargetException ite) + } + catch (InvocationTargetException ite) { Throwable e = ite.getTargetException(); if (e instanceof RemoteException) { // Rethrow exception throw (RemoteException)e; - } else if (e instanceof EJBException) + } + else if (e instanceof EJBException) { // Rethrow exception throw (EJBException)e; - } else if (e instanceof RuntimeException) + } + else if (e instanceof RuntimeException) { // Wrap runtime exceptions throw new EJBException((Exception)e); @@ -490,62 +561,87 @@ } } - /** - * Reset all attributes to default value - * - * This method is supplied for backwards compatibility. - * New versions of the PersistenceStore handle this for us. - * - * The EJB 1.1 specification is not entirely clear about this, - * the EJB 2.0 spec is, see page 169. - * Robustness is more important than raw speed for most server - * applications, and not resetting atrribute values result in - * *very* weird errors (old states re-appear in different instances and the - * developer thinks he's on drugs). - */ - protected void initEntity(EntityEnterpriseContext ctx) { - // first get cmp metadata of this entity - Object instance = ctx.getInstance(); - Class ejbClass = instance.getClass(); - Field cmpField; - Class cmpFieldType; - Iterator i= ((EntityMetaData)ctx.getContainer().getBeanMetaData()).getCMPFields(); - while(i.hasNext()) { - try { - // get the field declaration - try{ - cmpField = ejbClass.getField((String)i.next()); - cmpFieldType = cmpField.getType(); - // find the type of the field and reset it - // to the default value - if (cmpFieldType.equals(boolean.class)) { - cmpField.setBoolean(instance,false); - } else if (cmpFieldType.equals(byte.class)) { - cmpField.setByte(instance,(byte)0); - } else if (cmpFieldType.equals(int.class)) { - cmpField.setInt(instance,0); - } else if (cmpFieldType.equals(long.class)) { - cmpField.setLong(instance,0L); - } else if (cmpFieldType.equals(short.class)) { - cmpField.setShort(instance,(short)0); - } else if (cmpFieldType.equals(char.class)) { - cmpField.setChar(instance,'\u0000'); - } else if (cmpFieldType.equals(double.class)) { - cmpField.setDouble(instance,0d); - } else if (cmpFieldType.equals(float.class)) { - cmpField.setFloat(instance,0f); - } else { - cmpField.set(instance,null); - } - } catch (NoSuchFieldException e){ - // will be here with dependant value object's private attributes - // should not be a problem - } - } catch (Exception e) { - throw new EJBException(e); - } - } - } + /** + * Reset all attributes to default value + * + * This method is supplied for backwards compatibility. + * New versions of the PersistenceStore handle this for us. + * + * The EJB 1.1 specification is not entirely clear about this, + * the EJB 2.0 spec is, see page 169. + * Robustness is more important than raw speed for most server + * applications, and not resetting atrribute values result in + * *very* weird errors (old states re-appear in different instances and the + * developer thinks he's on drugs). + */ + protected void initEntity(EntityEnterpriseContext ctx) + { + // first get cmp metadata of this entity + Object instance = ctx.getInstance(); + Class ejbClass = instance.getClass(); + Field cmpField; + Class cmpFieldType; + Iterator i= ((EntityMetaData)ctx.getContainer().getBeanMetaData()).getCMPFields(); + while(i.hasNext()) + { + try + { + // get the field declaration + try + { + cmpField = ejbClass.getField((String)i.next()); + cmpFieldType = cmpField.getType(); + // find the type of the field and reset it + // to the default value + if (cmpFieldType.equals(boolean.class)) + { + cmpField.setBoolean(instance,false); + } + else if (cmpFieldType.equals(byte.class)) + { + cmpField.setByte(instance,(byte)0); + } + else if (cmpFieldType.equals(int.class)) + { + cmpField.setInt(instance,0); + } + else if (cmpFieldType.equals(long.class)) + { + cmpField.setLong(instance,0L); + } + else if (cmpFieldType.equals(short.class)) + { + cmpField.setShort(instance,(short)0); + } + else if (cmpFieldType.equals(char.class)) + { + cmpField.setChar(instance,'\u0000'); + } + else if (cmpFieldType.equals(double.class)) + { + cmpField.setDouble(instance,0d); + } + else if (cmpFieldType.equals(float.class)) + { + cmpField.setFloat(instance,0f); + } + else + { + cmpField.set(instance,null); + } + } + catch (NoSuchFieldException e) + { + // will be here with dependant value object's private attributes + // should not be a problem + } + } + catch (Exception e) + { + throw new EJBException(e); + } + } + } public Map retrieveStatistic() { @@ -572,13 +668,12 @@ } // Z implementation ---------------------------------------------- - + // Package protected --------------------------------------------- - + // Protected ----------------------------------------------------- - + // Private ------------------------------------------------------- - + // Inner classes ------------------------------------------------- } -
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development