1 17 package org.apache.geronimo.persistence.builder; 18 19 import java.util.Collections ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import javax.xml.namespace.QName ; 24 25 import org.apache.geronimo.common.DeploymentException; 26 import org.apache.geronimo.gbean.AbstractNameQuery; 27 import org.apache.geronimo.gbean.GBeanInfo; 28 import org.apache.geronimo.gbean.GBeanInfoBuilder; 29 import org.apache.geronimo.j2ee.deployment.Module; 30 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 31 import org.apache.geronimo.kernel.GBeanNotFoundException; 32 import org.apache.geronimo.kernel.config.Configuration; 33 import org.apache.geronimo.kernel.repository.Environment; 34 import org.apache.geronimo.naming.deployment.AbstractNamingBuilder; 35 import org.apache.geronimo.naming.reference.EntityManagerFactoryReference; 36 import org.apache.geronimo.schema.NamespaceElementConverter; 37 import org.apache.geronimo.schema.SchemaConversionUtils; 38 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefDocument; 39 import org.apache.geronimo.xbeans.geronimo.naming.GerEntityManagerFactoryRefType; 40 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType; 41 import org.apache.xmlbeans.QNameSet; 42 import org.apache.xmlbeans.XmlObject; 43 44 47 public class EntityManagerFactoryRefBuilder extends AbstractNamingBuilder { 48 private static final QName ENTITY_MANAGER_FACTORY_REF_QNAME = GerEntityManagerFactoryRefDocument.type.getDocumentElementName(); 49 private static final QNameSet ENTITY_MANAGER_FACTORY_REF_QNAME_SET = QNameSet.singleton(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME); 50 51 52 public EntityManagerFactoryRefBuilder(Environment defaultEnvironment) { 53 super(defaultEnvironment); 54 } 55 56 protected boolean willMergeEnvironment(XmlObject specDD, XmlObject plan) throws DeploymentException { 57 return getEntityManagerFactoryRefs(plan).length > 0; 58 } 59 60 public void initContext(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module) throws DeploymentException { 61 } 62 63 public void buildNaming(XmlObject specDD, XmlObject plan, Configuration localConfiguration, Configuration remoteConfiguration, Module module, Map componentContext) throws DeploymentException { 64 XmlObject[] EntityManagerFactoryRefsUntyped = getEntityManagerFactoryRefs(plan); 65 for (int i = 0; i < EntityManagerFactoryRefsUntyped.length; i++) { 66 GerEntityManagerFactoryRefType EntityManagerFactoryRef = (GerEntityManagerFactoryRefType) EntityManagerFactoryRefsUntyped[i]; 67 if (EntityManagerFactoryRef == null) { 68 throw new DeploymentException("Could not read EntityManagerFactoryRef number " + i + " as the correct xml type"); 69 } 70 String EntityManagerFactoryRefName = EntityManagerFactoryRef.getEntityManagerFactoryRefName(); 71 72 Set interfaceTypes = Collections.singleton("org.apache.geronimo.persistence.PersistenceUnitGBean"); 73 AbstractNameQuery persistenceUnitNameQuery; 74 if (EntityManagerFactoryRef.isSetPersistenceUnitName()) { 75 String persistenceUnitName = EntityManagerFactoryRef.getPersistenceUnitName(); 76 persistenceUnitNameQuery = new AbstractNameQuery(null, Collections.singletonMap("name", persistenceUnitName), interfaceTypes); 77 } else { 78 GerPatternType gbeanLocator = EntityManagerFactoryRef.getPattern(); 79 80 persistenceUnitNameQuery = buildAbstractNameQuery(gbeanLocator, null, null, interfaceTypes); 81 } 82 83 try { 84 localConfiguration.findGBeanData(persistenceUnitNameQuery); 85 } catch (GBeanNotFoundException e) { 86 throw new DeploymentException("Could not resolve reference at deploy time for query " + persistenceUnitNameQuery, e); 87 } 88 89 EntityManagerFactoryReference reference = new EntityManagerFactoryReference(localConfiguration.getId(), persistenceUnitNameQuery); 90 91 ((Map )componentContext.get(JNDI_KEY)).put(ENV + EntityManagerFactoryRefName, reference); 92 93 } 94 } 95 96 public QNameSet getSpecQNameSet() { 97 SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getLocalPart(), new NamespaceElementConverter(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME.getNamespaceURI()))); 98 return QNameSet.EMPTY; 99 } 100 101 public QNameSet getPlanQNameSet() { 102 return EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET; 103 } 104 105 private XmlObject[] getEntityManagerFactoryRefs(XmlObject plan) throws DeploymentException { 106 return plan == null? NO_REFS: convert(plan.selectChildren(EntityManagerFactoryRefBuilder.ENTITY_MANAGER_FACTORY_REF_QNAME_SET), NAMING_CONVERTER, GerEntityManagerFactoryRefType.type); 107 } 108 109 public static final GBeanInfo GBEAN_INFO; 110 111 static { 112 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(EntityManagerFactoryRefBuilder.class, NameFactory.MODULE_BUILDER); 113 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 114 115 infoBuilder.setConstructor(new String [] {"defaultEnvironment"}); 116 GBEAN_INFO = infoBuilder.getBeanInfo(); 117 } 118 119 public static GBeanInfo getGBeanInfo() { 120 return EntityManagerFactoryRefBuilder.GBEAN_INFO; 121 } 122 } 123 | Popular Tags |