001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.metadata;
023:
024: import java.net.URL;
025: import org.jboss.deployment.DeploymentException;
026: import org.jboss.logging.Logger;
027: import org.jboss.metadata.ApplicationMetaData;
028: import org.jboss.metadata.XmlFileLoader;
029: import org.w3c.dom.Element;
030:
031: /**
032: * Immutable class which loads the JDBC application meta data from the jbosscmp-jdbc.xml files.
033: *
034: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
035: * @author <a href="sebastien.alborini@m4x.org">Sebastien Alborini</a>
036: * @version $Revision: 57209 $
037: */
038: public final class JDBCXmlFileLoader {
039: private final ApplicationMetaData application;
040: private final ClassLoader classLoader;
041: private final ClassLoader localClassLoader;
042: private final Logger log;
043:
044: /**
045: * Constructs a JDBC XML file loader, which loads the JDBC application meta data from
046: * the jbosscmp-xml files.
047: *
048: * @param application the application meta data loaded from the ejb-jar.xml file
049: * @param classLoader the classLoader used to load all classes in the application
050: * @param localClassLoader the classLoader used to load the jbosscmp-jdbc.xml file from the jar
051: * @param log the log for this application
052: */
053: public JDBCXmlFileLoader(ApplicationMetaData application,
054: ClassLoader classLoader, ClassLoader localClassLoader,
055: Logger log) {
056: this .application = application;
057: this .classLoader = classLoader;
058: this .localClassLoader = localClassLoader;
059: this .log = log;
060: }
061:
062: /**
063: * Loads the application meta data from the jbosscmp-jdbc.xml file
064: *
065: * @return the jdbc application meta data loaded from the jbosscmp-jdbc.xml files
066: */
067: public JDBCApplicationMetaData load() throws DeploymentException {
068: JDBCApplicationMetaData jamd = new JDBCApplicationMetaData(
069: application, classLoader);
070:
071: // Load standardjbosscmp-jdbc.xml from the default classLoader
072: // we always load defaults first
073: URL stdJDBCUrl = classLoader
074: .getResource("standardjbosscmp-jdbc.xml");
075: if (stdJDBCUrl == null) {
076: throw new DeploymentException(
077: "No standardjbosscmp-jdbc.xml found");
078: }
079:
080: boolean debug = log.isDebugEnabled();
081: if (debug)
082: log.debug("Loading standardjbosscmp-jdbc.xml : "
083: + stdJDBCUrl.toString());
084: Element stdJDBCElement = XmlFileLoader.getDocument(stdJDBCUrl,
085: true).getDocumentElement();
086:
087: // first create the metadata
088: jamd = new JDBCApplicationMetaData(stdJDBCElement, jamd);
089:
090: // Load jbosscmp-jdbc.xml if provided
091: URL jdbcUrl = localClassLoader
092: .getResource("META-INF/jbosscmp-jdbc.xml");
093: if (jdbcUrl != null) {
094: if (debug)
095: log.debug(jdbcUrl.toString()
096: + " found. Overriding defaults");
097: Element jdbcElement = XmlFileLoader.getDocument(jdbcUrl,
098: true).getDocumentElement();
099: jamd = new JDBCApplicationMetaData(jdbcElement, jamd);
100: }
101:
102: return jamd;
103: }
104: }
|