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