/* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.ejb.plugins; import org.jboss.ejb.Container; import org.jboss.ejb.EntityCache; import org.jboss.ejb.EntityContainer; import org.jboss.ejb.EntityEnterpriseContext; import org.jboss.ejb.EntityPersistenceManager; import org.jboss.ejb.AllowedOperationsAssociation; import org.jboss.ejb.GenericEntityObjectFactory; import org.jboss.logging.Logger; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.EntityBean; import javax.ejb.FinderException; import javax.ejb.RemoveException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; /** * Persistence manager for BMP entites. All calls are simply deligated * to the entity implementation class. * * @author Rickard Öberg * @author Marc Fleury * @author Andreas Schaefer * @author Dain Sundstrom * @version $Revision: 57209 $ */ public class BMPPersistenceManager implements EntityPersistenceManager { // Constants ----------------------------------------------------- private final static Object[] EMPTY_OBJECT_ARRAY = new Object[0]; // Attributes ---------------------------------------------------- Logger log = Logger.getLogger(BMPPersistenceManager.class); EntityContainer con; Method ejbLoad; Method ejbStore; Method ejbActivate; Method ejbPassivate; Method ejbRemove; /** * Optional isModified method used by storeEntity */ Method isModified; HashMap createMethods = new HashMap(); HashMap postCreateMethods = new HashMap(); HashMap finderMethods = new HashMap(); // Static -------------------------------------------------------- // Constructors -------------------------------------------------- // Public -------------------------------------------------------- public void setContainer(Container c) { con = (EntityContainer)c; } public void create() throws Exception { 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]); // Create cache of create methods if (con.getHomeClass() != null) { Method[] methods = con.getHomeClass().getMethods(); createMethodCache( methods ); } if (con.getLocalHomeClass() != null) { Method[] methods = con.getLocalHomeClass().getMethods(); createMethodCache( methods ); } try { isModified = con.getBeanClass().getMethod("isModified", new Class[0]); if (!isModified.getReturnType().equals(Boolean.TYPE)) isModified = null; // Has to have "boolean" as return type! } catch (NoSuchMethodException ignored) {} } /** * Returns a new instance of the bean class or a subclass of the bean class. * * @return the new instance */ public Object createBeanClassInstance() throws Exception { return con.getBeanClass().newInstance(); } private void createMethodCache( Method[] methods ) throws NoSuchMethodException { for (int i = 0; i < methods.length; i++) { String name = methods[i].getName(); if (name.startsWith("create")) { String nameSuffix = name.substring(0, 1).toUpperCase() + name.substring(1); try { createMethods.put(methods[i], con.getBeanClass().getMethod("ejb" + nameSuffix, methods[i].getParameterTypes())); } catch (NoSuchMethodException e) { log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejb" + nameSuffix); throw e; } try { postCreateMethods.put(methods[i], con.getBeanClass().getMethod("ejbPost" + nameSuffix, methods[i].getParameterTypes())); } catch (NoSuchMethodException e) { log.error("Home Method " + methods[i] + " not implemented in bean class " + con.getBeanClass() + " looking for method named: ejbPost" + nameSuffix); throw e; } } } // Create cache of finder methods for (int i = 0; i < methods.length; i++) { if (methods[i].getName().startsWith("find")) { try { finderMethods.put(methods[i], con.getBeanClass().getMethod("ejbF" + methods[i].getName().substring(1), methods[i].getParameterTypes())); } catch (NoSuchMethodException e) { log.error("Home Method " + methods[i] + " not implemented in bean class"); throw e; } } } } public void start() { } public void stop() { } public void destroy() { } public void createEntity( Method m, Object[] args, EntityEnterpriseContext ctx) throws Exception { Object id = null; try { // Call ejbCreate