KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCXmlFileLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import java.net.URL JavaDoc;
25 import org.jboss.deployment.DeploymentException;
26 import org.jboss.logging.Logger;
27 import org.jboss.metadata.XmlFileLoader;
28 import org.jboss.ejb.Container;
29 import org.jboss.virtual.VirtualFile;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Immutable class which loads the JDBC application meta data from the jbosscmp-jdbc.xml files.
34  *
35  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
36  * @author <a HREF="sebastien.alborini@m4x.org">Sebastien Alborini</a>
37  * @author <a HREF="mailto:alex@jboss.com">Alexey Loubyansky</a>
38  * @version $Revision: 58204 $
39  */

40 public final class JDBCXmlFileLoader {
41    private final Container container;
42    private final Logger log;
43    
44    /**
45     * Constructs a JDBC XML file loader, which loads the JDBC application meta data from
46     * the jbosscmp-xml files.
47     *
48     * @param con the container
49     * @param log the log for this application
50     */

51    public JDBCXmlFileLoader(Container con, Logger log)
52    {
53       this.container = con;
54       this.log = log;
55    }
56
57    /**
58     * Loads the application meta data from the jbosscmp-jdbc.xml file
59     *
60     * @return the jdbc application meta data loaded from the jbosscmp-jdbc.xml files
61     */

62    public JDBCApplicationMetaData load() throws DeploymentException {
63       JDBCApplicationMetaData jamd = new JDBCApplicationMetaData(
64          container.getBeanMetaData().getApplicationMetaData(), container.getClassLoader()
65       );
66       
67       // Load standardjbosscmp-jdbc.xml from the default classLoader
68
// we always load defaults first
69
URL JavaDoc stdJDBCUrl = container.getClassLoader().getResource("standardjbosscmp-jdbc.xml");
70       if(stdJDBCUrl == null) {
71          throw new DeploymentException("No standardjbosscmp-jdbc.xml found");
72       }
73
74       boolean debug = log.isDebugEnabled();
75       if (debug)
76          log.debug("Loading standardjbosscmp-jdbc.xml : " + stdJDBCUrl.toString());
77       Element JavaDoc stdJDBCElement = XmlFileLoader.getDocument(stdJDBCUrl, true).getDocumentElement();
78
79       // first create the metadata
80
jamd = new JDBCApplicationMetaData(stdJDBCElement, jamd);
81
82       // Load jbosscmp-jdbc.xml if provided
83
URL JavaDoc jdbcUrl = null;
84       VirtualFile dd = container.getDeploymentUnit().getMetaDataFile("jbosscmp-jdbc.xml");
85       if(dd != null)
86       {
87          try
88          {
89             jdbcUrl = dd.toURL();
90          }
91          catch(Exception JavaDoc e)
92          {
93             throw new IllegalStateException JavaDoc("Failed to create URL for " + dd.getPathName(), e);
94          }
95       }
96
97       if(jdbcUrl != null)
98       {
99          if (debug)
100             log.debug(jdbcUrl.toString() + " found. Overriding defaults");
101          Element JavaDoc jdbcElement = XmlFileLoader.getDocument(jdbcUrl, true).getDocumentElement();
102          jamd = new JDBCApplicationMetaData(jdbcElement, jamd);
103       }
104
105       return jamd;
106    }
107 }
108
Popular Tags