001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.persistence.builder;
018
019 import java.net.MalformedURLException;
020 import java.net.URL;
021 import java.net.URI;
022 import java.net.URISyntaxException;
023 import java.util.ArrayList;
024 import java.util.List;
025 import java.util.Properties;
026
027 import javax.xml.namespace.QName;
028
029 import org.apache.geronimo.common.DeploymentException;
030 import org.apache.geronimo.deployment.DeploymentContext;
031 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
032 import org.apache.geronimo.deployment.xbeans.ServiceDocument;
033 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
034 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
035 import org.apache.geronimo.gbean.AbstractNameQuery;
036 import org.apache.geronimo.gbean.GBeanData;
037 import org.apache.geronimo.gbean.GBeanInfo;
038 import org.apache.geronimo.gbean.GBeanInfoBuilder;
039 import org.apache.geronimo.j2ee.deployment.EARContext;
040 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
041 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
042 import org.apache.geronimo.kernel.repository.Environment;
043 import org.apache.geronimo.persistence.PersistenceUnitGBean;
044 import org.apache.geronimo.xbeans.persistence.PersistenceDocument;
045 import org.apache.xmlbeans.XmlObject;
046 import org.apache.xmlbeans.QNameSet;
047
048 /**
049 * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
050 */
051 public class PersistenceUnitBuilder implements NamespaceDrivenBuilder {
052 private static final QName PERSISTENCE_QNAME = PersistenceDocument.type.getDocumentElementName();
053
054 private final Environment defaultEnvironment;
055 private final String defaultPersistenceProviderClassName;
056
057 public PersistenceUnitBuilder(Environment defaultEnvironment, String defaultPersistenceProviderClassName) {
058 this.defaultEnvironment = defaultEnvironment;
059 this.defaultPersistenceProviderClassName = defaultPersistenceProviderClassName;
060 }
061
062 public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
063 XmlObject[] raws = container.selectChildren(PERSISTENCE_QNAME);
064 if (raws.length > 0) {
065 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
066 }
067 }
068
069 public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
070 XmlObject[] raws = container.selectChildren(PERSISTENCE_QNAME);
071 for (XmlObject raw : raws) {
072 PersistenceDocument.Persistence persistence = (PersistenceDocument.Persistence) raw.copy().changeType(PersistenceDocument.Persistence.type);
073 PersistenceDocument.Persistence.PersistenceUnit[] persistenceUnits = persistence.getPersistenceUnitArray();
074 for (PersistenceDocument.Persistence.PersistenceUnit persistenceUnit : persistenceUnits) {
075 String persistenceUnitName = persistenceUnit.getName().trim();
076 GBeanData gbeanData;
077 try {
078 gbeanData = moduleContext.addGBean(persistenceUnitName, PersistenceUnitGBean.GBEAN_INFO);
079 } catch (GBeanAlreadyExistsException e) {
080 throw new DeploymentException("Duplicate persistenceUnit name " + persistenceUnitName, e);
081 }
082 gbeanData.setAttribute("persistenceUnitName", persistenceUnitName);
083 if (persistenceUnit.isSetProvider()) {
084 gbeanData.setAttribute("persistenceProviderClassName", persistenceUnit.getProvider().trim());
085 } else {
086 gbeanData.setAttribute("persistenceProviderClassName", defaultPersistenceProviderClassName);
087 }
088 gbeanData.setAttribute("persistenceUnitTransactionType", persistenceUnit.getTransactionType().toString());
089 if (persistenceUnit.isSetJtaDataSource()) {
090 String jtaDataSourceString = persistenceUnit.getJtaDataSource().trim();
091 try {
092 AbstractNameQuery jtaDataSourceNameQuery = new AbstractNameQuery(new URI(jtaDataSourceString + "#org.apache.geronimo.connector.outbound.ConnectionFactorySource"));
093 gbeanData.setReferencePattern("JtaDataSourceWrapper", jtaDataSourceNameQuery);
094 } catch (URISyntaxException e) {
095 throw new DeploymentException("Could not create jta-data-source AbstractNameQuery from string: " + jtaDataSourceString, e);
096 }
097 }
098
099 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 //TODO what is this from??
129 // URL persistenceUnitRootUrl = new URL(persistenceUnit.get)
130 // gbeanData.setAttribute("persistenceUnitRootUrl", persistenceUnitRootUrl);
131
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 }