1 17 package org.apache.geronimo.persistence.builder; 18 19 import java.net.MalformedURLException ; 20 import java.net.URL ; 21 import java.net.URI ; 22 import java.net.URISyntaxException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.Properties ; 26 27 import javax.xml.namespace.QName ; 28 29 import org.apache.geronimo.common.DeploymentException; 30 import org.apache.geronimo.deployment.DeploymentContext; 31 import org.apache.geronimo.deployment.NamespaceDrivenBuilder; 32 import org.apache.geronimo.deployment.xbeans.ServiceDocument; 33 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil; 34 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 35 import org.apache.geronimo.gbean.AbstractNameQuery; 36 import org.apache.geronimo.gbean.GBeanData; 37 import org.apache.geronimo.gbean.GBeanInfo; 38 import org.apache.geronimo.gbean.GBeanInfoBuilder; 39 import org.apache.geronimo.j2ee.deployment.EARContext; 40 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 41 import org.apache.geronimo.kernel.GBeanAlreadyExistsException; 42 import org.apache.geronimo.kernel.repository.Environment; 43 import org.apache.geronimo.persistence.PersistenceUnitGBean; 44 import org.apache.geronimo.xbeans.persistence.PersistenceDocument; 45 import org.apache.xmlbeans.XmlObject; 46 import org.apache.xmlbeans.QNameSet; 47 48 51 public class PersistenceUnitBuilder implements NamespaceDrivenBuilder { 52 private static final QName PERSISTENCE_QNAME = PersistenceDocument.type.getDocumentElementName(); 53 54 private final Environment defaultEnvironment; 55 private final String defaultPersistenceProviderClassName; 56 57 public PersistenceUnitBuilder(Environment defaultEnvironment, String defaultPersistenceProviderClassName) { 58 this.defaultEnvironment = defaultEnvironment; 59 this.defaultPersistenceProviderClassName = defaultPersistenceProviderClassName; 60 } 61 62 public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException { 63 XmlObject[] raws = container.selectChildren(PERSISTENCE_QNAME); 64 if (raws.length > 0) { 65 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 66 } 67 } 68 69 public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException { 70 XmlObject[] raws = container.selectChildren(PERSISTENCE_QNAME); 71 for (XmlObject raw : raws) { 72 PersistenceDocument.Persistence persistence = (PersistenceDocument.Persistence) raw.copy().changeType(PersistenceDocument.Persistence.type); 73 PersistenceDocument.Persistence.PersistenceUnit[] persistenceUnits = persistence.getPersistenceUnitArray(); 74 for (PersistenceDocument.Persistence.PersistenceUnit persistenceUnit : persistenceUnits) { 75 String persistenceUnitName = persistenceUnit.getName().trim(); 76 GBeanData gbeanData; 77 try { 78 gbeanData = moduleContext.addGBean(persistenceUnitName, PersistenceUnitGBean.GBEAN_INFO); 79 } catch (GBeanAlreadyExistsException e) { 80 throw new DeploymentException("Duplicate persistenceUnit name " + persistenceUnitName, e); 81 } 82 gbeanData.setAttribute("persistenceUnitName", persistenceUnitName); 83 if (persistenceUnit.isSetProvider()) { 84 gbeanData.setAttribute("persistenceProviderClassName", persistenceUnit.getProvider().trim()); 85 } else { 86 gbeanData.setAttribute("persistenceProviderClassName", defaultPersistenceProviderClassName); 87 } 88 gbeanData.setAttribute("persistenceUnitTransactionType", persistenceUnit.getTransactionType().toString()); 89 if (persistenceUnit.isSetJtaDataSource()) { 90 String jtaDataSourceString = persistenceUnit.getJtaDataSource().trim(); 91 try { 92 AbstractNameQuery jtaDataSourceNameQuery = new AbstractNameQuery(new URI (jtaDataSourceString + "#org.apache.geronimo.connector.outbound.ConnectionFactorySource")); 93 gbeanData.setReferencePattern("JtaDataSourceWrapper", jtaDataSourceNameQuery); 94 } catch (URISyntaxException e) { 95 throw new DeploymentException("Could not create jta-data-source AbstractNameQuery from string: " + jtaDataSourceString, e); 96 } 97 } 98 99 if (persistenceUnit.isSetNonJtaDataSource()) { 100 String nonJtaDataSourceString = persistenceUnit.getNonJtaDataSource().trim(); 101 try { 102 AbstractNameQuery nonJtaDataSourceNameQuery = new AbstractNameQuery(new URI (nonJtaDataSourceString + "#org.apache.geronimo.connector.outbound.ConnectionFactorySource")); 103 gbeanData.setReferencePattern("NonJtaDataSourceWrapper", nonJtaDataSourceNameQuery); 104 } catch (URISyntaxException e) { 105 throw new DeploymentException("Could not create non-jta-data-source AbstractNameQuery from string: " + nonJtaDataSourceString, e); 106 } 107 } 108 109 List <String > mappingFileNames = new ArrayList <String >(); 110 String [] mappingFileNameStrings = persistenceUnit.getMappingFileArray(); 111 for (String mappingFileNameString : mappingFileNameStrings) { 112 mappingFileNames.add(mappingFileNameString.trim()); 113 } 114 gbeanData.setAttribute("mappingFileNames", mappingFileNames); 115 116 List <URL > jarFileUrls = new ArrayList <URL >(); 117 String [] jarFileUrlStrings = persistenceUnit.getJarFileArray(); 118 for (String jarFileUrlString1 : jarFileUrlStrings) { 119 String jarFileUrlString = jarFileUrlString1.trim(); 120 try { 121 URL jarFileUrl = new URL (jarFileUrlString); 122 jarFileUrls.add(jarFileUrl); 123 } catch (MalformedURLException e) { 124 throw new DeploymentException("could not create URL for jarFileURL", e); 125 } 126 } 127 gbeanData.setAttribute("jarFileUrls", jarFileUrls); 128 132 String [] managedClassNameStrings = persistenceUnit.getClass1Array(); 133 List <String > managedClassNames = new ArrayList <String >(); 134 for (String managedClassNameString : managedClassNameStrings) { 135 managedClassNames.add(managedClassNameString.trim()); 136 } 137 gbeanData.setAttribute("managedClassNames", managedClassNames); 138 if (persistenceUnit.isSetExcludeUnlistedClasses()) { 139 gbeanData.setAttribute("excludeUnlistedClasses", persistenceUnit.getExcludeUnlistedClasses()); 140 } else { 141 gbeanData.setAttribute("excludeUnlistedClassesValue", false); 142 } 143 144 Properties properties = new Properties (); 145 if (persistenceUnit.isSetProperties()) { 146 PersistenceDocument.Persistence.PersistenceUnit.Properties.Property[] propertyObjects = persistenceUnit.getProperties().getPropertyArray(); 147 for (PersistenceDocument.Persistence.PersistenceUnit.Properties.Property propertyObject : propertyObjects) 148 { 149 String key = propertyObject.getName().trim(); 150 String value = propertyObject.getValue().trim(); 151 properties.setProperty(key, value); 152 } 153 } 154 155 gbeanData.setAttribute("properties", properties); 156 if (moduleContext instanceof EARContext) { 157 AbstractNameQuery transactionManagerName = ((EARContext) moduleContext).getTransactionManagerName(); 158 gbeanData.setReferencePattern("TransactionManager", transactionManagerName); 159 } 160 } 161 } 162 } 163 164 public QNameSet getSpecQNameSet() { 165 return QNameSet.EMPTY; 166 } 167 168 public QNameSet getPlanQNameSet() { 169 return QNameSet.singleton(PERSISTENCE_QNAME); 170 } 171 172 public static final GBeanInfo GBEAN_INFO; 173 174 static { 175 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(PersistenceUnitBuilder.class, NameFactory.MODULE_BUILDER); 176 177 infoBuilder.addAttribute("defaultEnvironment", Environment.class, true, true); 178 infoBuilder.addAttribute("defaultPersistenceProviderClassName", String .class, true, true); 179 180 infoBuilder.setConstructor(new String []{ 181 "defaultEnvironment", 182 "defaultPersistenceProviderClassName" 183 }); 184 185 GBEAN_INFO = infoBuilder.getBeanInfo(); 186 187 } 188 189 public static GBeanInfo getGBeanInfo() { 190 return GBEAN_INFO; 191 } 192 193 194 } 195 | Popular Tags |