001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JPersistenceUnitInfoLoader.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.persistence.xml;
025:
026: import static javax.persistence.spi.PersistenceUnitTransactionType.JTA;
027: import static javax.persistence.spi.PersistenceUnitTransactionType.RESOURCE_LOCAL;
028:
029: import java.net.URL;
030: import java.util.ArrayList;
031: import java.util.List;
032: import java.util.Properties;
033:
034: import org.ow2.easybeans.util.xml.DocumentParser;
035: import org.ow2.easybeans.util.xml.DocumentParserException;
036: import org.ow2.easybeans.util.xml.XMLUtils;
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.NodeList;
042:
043: /**
044: * Class used to fill PersistenceUnitInfo implementation class by loading an
045: * XML.
046: * @author Florent Benoit
047: */
048: public final class JPersistenceUnitInfoLoader {
049:
050: /**
051: * Persistence namespace.
052: */
053: private static final String PERSISTENCE_NS = "http://java.sun.com/xml/ns/persistence";
054:
055: /**
056: * <persistence-unit> element.
057: */
058: private static final String PERSISTENCE_UNIT = "persistence-unit";
059:
060: /**
061: * Logger.
062: */
063: private static Log logger = LogFactory
064: .getLog(JPersistenceUnitInfoLoader.class);
065:
066: /**
067: * Validating with schema ?
068: */
069: private static boolean validating = true;
070:
071: /**
072: * Utility class, no public constructor.
073: */
074: private JPersistenceUnitInfoLoader() {
075:
076: }
077:
078: /**
079: * Load the persistence.xml file.
080: * @param url the URL of the the Reader of the XML file.
081: * @throws JPersistenceUnitInfoException if parsing of XML file fails.
082: * @return an application object.
083: */
084: public static JPersistenceUnitInfo[] loadPersistenceUnitInfoImpl(
085: final URL url) throws JPersistenceUnitInfoException {
086:
087: logger.debug("Analyzing url {0}", url);
088:
089: // List of PersistenceUnitInfo objects
090: List<JPersistenceUnitInfo> jPersistenceUnitInfos = new ArrayList<JPersistenceUnitInfo>();
091:
092: // Get document
093: Document document = null;
094: try {
095: document = DocumentParser.getDocument(url, validating,
096: new PersistenceUnitEntityResolver());
097: } catch (DocumentParserException e) {
098: throw new JPersistenceUnitInfoException(
099: "Cannot parse the url", e);
100: }
101:
102: // Root element = <persistence>
103: Element persistenceRootElement = document.getDocumentElement();
104:
105: NodeList persistenceUnitInfoList = persistenceRootElement
106: .getElementsByTagNameNS(PERSISTENCE_NS,
107: PERSISTENCE_UNIT);
108:
109: // Loop on this list
110: for (int i = 0; i < persistenceUnitInfoList.getLength(); i++) {
111: Element pUnitElement = (Element) persistenceUnitInfoList
112: .item(i);
113:
114: // Build instance that is created.
115: JPersistenceUnitInfo persistenceUnitInfo = new JPersistenceUnitInfo();
116:
117: // set the URL of the persistence file.
118: persistenceUnitInfo.setPersistenceXmlFileUrl(url);
119:
120: // Provider
121: String className = XMLUtils.getStringValueElement(
122: PERSISTENCE_NS, pUnitElement, "provider");
123: persistenceUnitInfo
124: .setPersistenceProviderClassName(className);
125:
126: // Jta-data-source
127: String jtaDataSourceName = XMLUtils.getStringValueElement(
128: PERSISTENCE_NS, pUnitElement, "jta-data-source");
129: persistenceUnitInfo.setJtaDataSourceName(jtaDataSourceName);
130:
131: // Non-jta-data-source
132: String nonJtaDataSourceName = XMLUtils
133: .getStringValueElement(PERSISTENCE_NS,
134: pUnitElement, "non-jta-data-source");
135: persistenceUnitInfo
136: .setNonJtaDataSourceName(nonJtaDataSourceName);
137:
138: // mapping-file
139: List<String> mappingFiles = XMLUtils
140: .getStringListValueElement(PERSISTENCE_NS,
141: pUnitElement, "mapping-file");
142: for (String mappingFileName : mappingFiles) {
143: persistenceUnitInfo.addMappingFileName(mappingFileName);
144: }
145:
146: // jar-file
147: List<String> jarFiles = XMLUtils.getStringListValueElement(
148: PERSISTENCE_NS, pUnitElement, "jar-file");
149: for (String jarFileName : jarFiles) {
150: logger
151: .warn(
152: "JarFile found with name {0}. But not yet supported.",
153: jarFileName);
154: //TODO : transform fileName into URL
155: //persistenceUnitInfo.addJarFile(jarFileName);
156: }
157:
158: // class (managed class)
159: List<String> classes = XMLUtils.getStringListValueElement(
160: PERSISTENCE_NS, pUnitElement, "class");
161: for (String managedClassName : classes) {
162: persistenceUnitInfo.addClass(managedClassName);
163: }
164:
165: // exclude-unlisted-classes
166: String excluded = XMLUtils.getStringValueElement(
167: PERSISTENCE_NS, pUnitElement,
168: "exclude-unlisted-classes");
169: persistenceUnitInfo.setExcludeUnlistedClasses("true"
170: .equals(excluded));
171:
172: // properties
173: Properties props = XMLUtils.getPropertiesValueElement(
174: PERSISTENCE_NS, pUnitElement, "properties");
175: persistenceUnitInfo.setProperties(props);
176:
177: // Name attribute
178: String name = XMLUtils.getAttributeValue(pUnitElement,
179: "name");
180: persistenceUnitInfo.setPersistenceUnitName(name);
181:
182: // transaction-type attribute
183: String transactionType = XMLUtils.getAttributeValue(
184: pUnitElement, "transaction-type");
185: if ("JTA".equals(transactionType)) {
186: persistenceUnitInfo.setTransactionType(JTA);
187: } else if ("RESOURCE_LOCAL".equals(transactionType)) {
188: persistenceUnitInfo.setTransactionType(RESOURCE_LOCAL);
189: } else {
190: logger
191: .warn("No transaction-type defined. Set to default JTA transaction-type");
192: persistenceUnitInfo.setTransactionType(JTA);
193: }
194:
195: jPersistenceUnitInfos.add(persistenceUnitInfo);
196:
197: }
198: return jPersistenceUnitInfos
199: .toArray(new JPersistenceUnitInfo[jPersistenceUnitInfos
200: .size()]);
201: }
202:
203: }
|