KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > persistence > builder > PersistenceUnitBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.persistence.builder;
18
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
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 /**
49  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
50  */

51 public class PersistenceUnitBuilder implements NamespaceDrivenBuilder {
52     private static final QName JavaDoc PERSISTENCE_QNAME = PersistenceDocument.type.getDocumentElementName();
53
54     private final Environment defaultEnvironment;
55     private final String JavaDoc defaultPersistenceProviderClassName;
56
57     public PersistenceUnitBuilder(Environment defaultEnvironment, String JavaDoc 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 JavaDoc 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 JavaDoc jtaDataSourceString = persistenceUnit.getJtaDataSource().trim();
91                     try {
92                         AbstractNameQuery jtaDataSourceNameQuery = new AbstractNameQuery(new URI JavaDoc(jtaDataSourceString + "#org.apache.geronimo.connector.outbound.ConnectionFactorySource"));
93                         gbeanData.setReferencePattern("JtaDataSourceWrapper", jtaDataSourceNameQuery);
94                     } catch (URISyntaxException JavaDoc 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 JavaDoc nonJtaDataSourceString = persistenceUnit.getNonJtaDataSource().trim();
101                     try {
102                         AbstractNameQuery nonJtaDataSourceNameQuery = new AbstractNameQuery(new URI JavaDoc(nonJtaDataSourceString + "#org.apache.geronimo.connector.outbound.ConnectionFactorySource"));
103                         gbeanData.setReferencePattern("NonJtaDataSourceWrapper", nonJtaDataSourceNameQuery);
104                     } catch (URISyntaxException JavaDoc e) {
105                         throw new DeploymentException("Could not create non-jta-data-source AbstractNameQuery from string: " + nonJtaDataSourceString, e);
106                     }
107                 }
108
109                 List JavaDoc<String JavaDoc> mappingFileNames = new ArrayList JavaDoc<String JavaDoc>();
110                 String JavaDoc[] mappingFileNameStrings = persistenceUnit.getMappingFileArray();
111                 for (String JavaDoc mappingFileNameString : mappingFileNameStrings) {
112                     mappingFileNames.add(mappingFileNameString.trim());
113                 }
114                 gbeanData.setAttribute("mappingFileNames", mappingFileNames);
115
116                 List JavaDoc<URL JavaDoc> jarFileUrls = new ArrayList JavaDoc<URL JavaDoc>();
117                 String JavaDoc[] jarFileUrlStrings = persistenceUnit.getJarFileArray();
118                 for (String JavaDoc jarFileUrlString1 : jarFileUrlStrings) {
119                     String JavaDoc jarFileUrlString = jarFileUrlString1.trim();
120                     try {
121                         URL JavaDoc jarFileUrl = new URL JavaDoc(jarFileUrlString);
122                         jarFileUrls.add(jarFileUrl);
123                     } catch (MalformedURLException JavaDoc 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 JavaDoc[] managedClassNameStrings = persistenceUnit.getClass1Array();
133                 List JavaDoc<String JavaDoc> managedClassNames = new ArrayList JavaDoc<String JavaDoc>();
134                 for (String JavaDoc 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 JavaDoc properties = new Properties JavaDoc();
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 JavaDoc key = propertyObject.getName().trim();
150                         String JavaDoc 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 JavaDoc.class, true, true);
179
180         infoBuilder.setConstructor(new String JavaDoc[]{
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