Index: common/container-common/src/main/java/com/sun/enterprise/container/common/impl/EntityManagerFactoryWrapper.java =================================================================== --- common/container-common/src/main/java/com/sun/enterprise/container/common/impl/EntityManagerFactoryWrapper.java (revision 44769) +++ common/container-common/src/main/java/com/sun/enterprise/container/common/impl/EntityManagerFactoryWrapper.java (working copy) @@ -40,6 +40,7 @@ package com.sun.enterprise.container.common.impl; +import com.sun.enterprise.container.common.spi.EMFFinder; import com.sun.enterprise.container.common.spi.util.ComponentEnvManager; import com.sun.enterprise.deployment.*; import com.sun.logging.LogDomains; @@ -60,6 +61,7 @@ import java.io.Serializable; import java.util.Map; import java.util.Set; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -69,6 +71,7 @@ * reference within the component jndi environment. * * @author Kenneth Saks + * @author Sanjeeb Sahoo */ public class EntityManagerFactoryWrapper implements EntityManagerFactory, Serializable { @@ -239,9 +242,24 @@ } } + // now lookup using any custom EMFFinders + if (emf == null) { + lookupEntityManagerFactoryUsingEMFFinders(invType, emfUnitName, descriptor); + } return emf; } + private static EntityManagerFactory lookupEntityManagerFactoryUsingEMFFinders(ComponentInvocationType invType, String puName, Object dd) { + for (EMFFinder emfFinder : Globals.getDefaultHabitat().getAllByContract(EMFFinder.class)) { + _logger.log(Level.FINE, "looking up EMF using {0} ", emfFinder); + EntityManagerFactory emf = emfFinder.find(invType, puName, dd); + if (emf != null) { + return emf; + } + } + return null; + } + private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); Index: common/container-common/src/main/java/com/sun/enterprise/container/common/spi/EMFFinder.java =================================================================== --- common/container-common/src/main/java/com/sun/enterprise/container/common/spi/EMFFinder.java (revision 0) +++ common/container-common/src/main/java/com/sun/enterprise/container/common/spi/EMFFinder.java (revision 0) @@ -0,0 +1,75 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html + * or packager/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at packager/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * Oracle designates this particular file as subject to the "Classpath" + * exception as provided by Oracle in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ + + +package com.sun.enterprise.container.common.spi; + +import com.sun.enterprise.container.common.spi.util.ComponentEnvManager; +import org.glassfish.api.invocation.ComponentInvocation; +import org.glassfish.api.invocation.InvocationManager; + +import javax.persistence.EntityManagerFactory; + +/** + * This provides an extension point to our {@link com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper}. + * It allows users to package their persistence-unit outside their Java EE application + * yet reference it using standard Java EE mechanismms like + * PersistenceContext or PersistenceUnit annotation or JNDI lookup. + * This class comes into picture after the usual Java EE logic fails to find a suitable EMF. + * + * @author Sanjeeb.Sahoo@Sun.COM + */ +public interface EMFFinder { + /** + * Find a suitable EntityManagerFactory for the given component invocation. This method is called + * after the usual Java EE logic fails to find a suitable EMF. In other words, there is no point in + * trying to locate an EMF inside the ear file or the war/jar, because + * {@link com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper#lookupEntityManagerFactory} would have + * already searched in those places. + * + * @param invType invocation type of the current invocation context + * @param puName name of the persistence-unit (may be null) + * @param dd DeploymentDescriptor of the current component (it's EjbDescriptor for an EJB, + * WebBundleDescriptor for a web component and ApplicationClientDescriptor for appclient) + * @return a matched EntityManagerFactory or null if nothing matches + */ + EntityManagerFactory find(ComponentInvocation.ComponentInvocationType invType, + String puName, Object dd); +}