KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > xml > JPersistenceUnitInfoLoader


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JPersistenceUnitInfoLoader.java 572 2006-06-04 21:23:27Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence.xml;
27
28 import static javax.persistence.spi.PersistenceUnitTransactionType.JTA;
29 import static javax.persistence.spi.PersistenceUnitTransactionType.RESOURCE_LOCAL;
30
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38 import org.objectweb.easybeans.util.xml.DocumentParser;
39 import org.objectweb.easybeans.util.xml.DocumentParserException;
40 import org.objectweb.easybeans.util.xml.XMLUtils;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44
45 /**
46  * Class used to fill PersistenceUnitInfo implementation class by loading an
47  * XML.
48  * @author Florent Benoit
49  */

50 public final class JPersistenceUnitInfoLoader {
51
52     /**
53      * Persistence namespace.
54      */

55     private static final String JavaDoc PERSISTENCE_NS = "http://java.sun.com/xml/ns/persistence";
56
57     /**
58      * <persistence-unit> element.
59      */

60     private static final String JavaDoc PERSISTENCE_UNIT = "persistence-unit";
61
62     /**
63      * Logger.
64      */

65     private static JLog logger = JLogFactory.getLog(JPersistenceUnitInfoLoader.class);
66
67     /**
68      * Validating with schema ?
69      */

70     private static boolean validating = true;
71
72     /**
73      * Utility class, no public constructor.
74      */

75     private JPersistenceUnitInfoLoader() {
76
77     }
78
79     /**
80      * Load the persistence.xml file.
81      * @param url the URL of the the Reader of the XML file.
82      * @throws JPersistenceUnitInfoException if parsing of XML file fails.
83      * @return an application object.
84      */

85     public static JPersistenceUnitInfo[] loadPersistenceUnitInfoImpl(final URL JavaDoc url)
86             throws JPersistenceUnitInfoException {
87
88
89         logger.debug("Analyzing url {0}", url);
90
91         // List of PersistenceUnitInfo objects
92
List JavaDoc<JPersistenceUnitInfo> jPersistenceUnitInfos = new ArrayList JavaDoc<JPersistenceUnitInfo>();
93
94         // Get document
95
Document JavaDoc document = null;
96         try {
97             document = DocumentParser.getDocument(url, validating, new PersistenceUnitEntityResolver());
98         } catch (DocumentParserException e) {
99            throw new JPersistenceUnitInfoException("Cannot parse the url", e);
100         }
101
102         // Root element = <persistence>
103
Element JavaDoc persistenceRootElement = document.getDocumentElement();
104
105         NodeList JavaDoc persistenceUnitInfoList = persistenceRootElement.getElementsByTagNameNS(PERSISTENCE_NS, PERSISTENCE_UNIT);
106
107         // Loop on this list
108
for (int i = 0; i < persistenceUnitInfoList.getLength(); i++) {
109             Element JavaDoc pUnitElement = (Element JavaDoc) persistenceUnitInfoList.item(i);
110
111             // Build instance that is created.
112
JPersistenceUnitInfo persistenceUnitInfo = new JPersistenceUnitInfo();
113
114             // set the URL of the persistence file.
115
persistenceUnitInfo.setPersistenceXmlFileUrl(url);
116
117             // Provider
118
String JavaDoc className = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "provider");
119             persistenceUnitInfo.setPersistenceProviderClassName(className);
120
121             // Jta-data-source
122
String JavaDoc jtaDataSourceName = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "jta-data-source");
123             persistenceUnitInfo.setJtaDataSourceName(jtaDataSourceName);
124
125             // Non-jta-data-source
126
String JavaDoc nonJtaDataSourceName = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "non-jta-data-source");
127             persistenceUnitInfo.setNonJtaDataSourceName(nonJtaDataSourceName);
128
129             // mapping-file
130
List JavaDoc<String JavaDoc> mappingFiles = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "mapping-file");
131             for (String JavaDoc mappingFileName : mappingFiles) {
132                 persistenceUnitInfo.addMappingFileName(mappingFileName);
133             }
134
135             // jar-file
136
List JavaDoc<String JavaDoc> jarFiles = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "jar-file");
137             for (String JavaDoc jarFileName : jarFiles) {
138                 logger.warn("JarFile found with name {0}. But not yet supported.", jarFileName);
139                 //TODO : transform fileName into URL
140
//persistenceUnitInfo.addJarFile(jarFileName);
141
}
142
143
144             // class (managed class)
145
List JavaDoc<String JavaDoc> classes = XMLUtils.getStringListValueElement(PERSISTENCE_NS, pUnitElement, "class");
146             for (String JavaDoc managedClassName : classes) {
147                 persistenceUnitInfo.addClass(managedClassName);
148             }
149
150             // exclude-unlisted-classes
151
String JavaDoc excluded = XMLUtils.getStringValueElement(PERSISTENCE_NS, pUnitElement, "exclude-unlisted-classes");
152             persistenceUnitInfo.setExcludeUnlistedClasses("true".equals(excluded));
153
154             // properties
155
Properties JavaDoc props = XMLUtils.getPropertiesValueElement(PERSISTENCE_NS, pUnitElement, "properties");
156             persistenceUnitInfo.setProperties(props);
157
158             // Name attribute
159
String JavaDoc name = XMLUtils.getAttributeValue(pUnitElement, "name");
160             persistenceUnitInfo.setPersistenceUnitName(name);
161
162             // transaction-type attribute
163
String JavaDoc transactionType = XMLUtils.getAttributeValue(pUnitElement, "transaction-type");
164             if ("JTA".equals(transactionType)) {
165                 persistenceUnitInfo.setTransactionType(JTA);
166             } else if ("RESOURCE_LOCAL".equals(transactionType)) {
167                 persistenceUnitInfo.setTransactionType(RESOURCE_LOCAL);
168             } else {
169                 logger.info("No transaction-type defined. Set to default JTA transaction-type");
170                 persistenceUnitInfo.setTransactionType(JTA);
171             }
172
173             jPersistenceUnitInfos.add(persistenceUnitInfo);
174
175         }
176         return jPersistenceUnitInfos.toArray(new JPersistenceUnitInfo[jPersistenceUnitInfos.size()]);
177     }
178
179 }
180
Popular Tags