FileDocCategorySizeDatePackage
EntityManagerFactoryWrapper.javaAPI DocGlassfish v2 API7435Fri May 04 22:35:58 BST 2007com.sun.enterprise.util

EntityManagerFactoryWrapper

public class EntityManagerFactoryWrapper extends Object implements Serializable, EntityManagerFactory
Wrapper for application references to entity manager factories. A new instance of this class will be created for each injected EntityManagerFactory reference or each lookup of an EntityManagerFactory reference within the component jndi environment.
author
Kenneth Saks

Fields Summary
static Logger
_logger
private static LocalStringManagerImpl
localStrings
private String
unitName
private transient EntityManagerFactory
entityManagerFactory
Constructors Summary
public EntityManagerFactoryWrapper(com.sun.enterprise.deployment.types.EntityManagerFactoryReference referenceDescriptor)


     
                                        

        unitName = referenceDescriptor.getUnitName();

    
Methods Summary
public voidclose()

        getDelegate().close();
    
public javax.persistence.EntityManagercreateEntityManager()

        return getDelegate().createEntityManager();
    
public javax.persistence.EntityManagercreateEntityManager(java.util.Map map)

        return getDelegate().createEntityManager(map);
    
private javax.persistence.EntityManagerFactorygetDelegate()


        if( entityManagerFactory == null ) {
        
            entityManagerFactory = lookupEntityManagerFactory(unitName);
            
            if( entityManagerFactory == null ) {
                throw new IllegalStateException
                    ("Unable to retrieve EntityManagerFactory for unitName "
                     + unitName);
            }

        }

        return entityManagerFactory;
    
public booleanisOpen()

        return getDelegate().isOpen();
    
static javax.persistence.EntityManagerFactorylookupEntityManagerFactory(java.lang.String emfUnitName)
Lookup physical EntityManagerFactory based on current component invocation.

param
emfUnitName unit name of entity manager factory or null if not specified.
return
EntityManagerFactory or null if no matching factory could be found.


        InvocationManager invMgr = Switch.getSwitch().getInvocationManager();
        ComponentInvocation inv  = invMgr.getCurrentInvocation();

        EntityManagerFactory emf = null;

        if( inv != null ) {

            Object descriptor = 
                Switch.getSwitch().getDescriptorFor(inv.getContainerContext());

            emf = lookupEntityManagerFactory(inv.getInvocationType(),
                    emfUnitName, descriptor);
        }
        
        return emf;
    
public static javax.persistence.EntityManagerFactorylookupEntityManagerFactory(int invType, java.lang.String emfUnitName, java.lang.Object descriptor)


        Application app = null;
        BundleDescriptor module = null;

        EntityManagerFactory emf = null;

        switch (invType) {

        case ComponentInvocation.EJB_INVOCATION:

            EjbDescriptor ejbDesc = (EjbDescriptor) descriptor;
            module = ejbDesc.getEjbBundleDescriptor();
            app = module.getApplication();

            break;

        case ComponentInvocation.SERVLET_INVOCATION:

            module = (WebBundleDescriptor) descriptor;
            app = module.getApplication();

            break;

        case ComponentInvocation.APP_CLIENT_INVOCATION:

            module = (ApplicationClientDescriptor) descriptor;
            app = module.getApplication();

            break;

        default:

            break;
        }

        // First check module-level for a match.
        if (module != null) {
            if (emfUnitName != null) {
                emf = module.getEntityManagerFactory(emfUnitName);
            } else {
                Set<EntityManagerFactory> emFactories = module
                        .getEntityManagerFactories();
                if (emFactories.size() == 1) {
                    emf = emFactories.iterator().next();
                }
            }
        }

        // If we're in an .ear and no module-level persistence unit was
        // found, check for an application-level match.
        if ((app != null) && (emf == null)) {
            if (emfUnitName != null) {

                emf = app.getEntityManagerFactory(emfUnitName, module);

            } else {
                Set<EntityManagerFactory> emFactories = app
                        .getEntityManagerFactories();
                if (emFactories.size() == 1) {
                    emf = emFactories.iterator().next();
                }
            }
        }

        return emf;