KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > util > hibernate > HibernareDialectResolver


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.util.hibernate;
10
11
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Element JavaDoc;
14 import org.jboss.portal.common.util.XML;
15 import org.jboss.portal.setup.PortalSetupException;
16 import org.xml.sax.SAXException JavaDoc;
17
18 import javax.sql.DataSource JavaDoc;
19 import javax.xml.parsers.ParserConfigurationException JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
32  * Date: May 6, 2005; Time: 1:03:09 PM
33  * @since JBoss portal 2.0
34  * Class org.jboss.portal.setup.util.hibernate.HibernareDialectResolver
35  */

36 public class HibernareDialectResolver
37 {
38    private static List JavaDoc DB_TO_HB_DIALECT = null;
39    private static final String JavaDoc DIALECT_MAPPINGS_URI = "org/jboss/portal/setup/util/hibernate/hb-driver-mappings.xml";
40    private static final String JavaDoc HB_MAPPING_ELMT_NAME = "hb-driver-mapping";
41    private static final String JavaDoc ERROR_MSG = "Failed to load hibernate mapping file resource from '" +
42       DIALECT_MAPPINGS_URI + "'";
43    private static final String JavaDoc CON_DRIVER = "con-driver";
44    private static final String JavaDoc CON_URL_PREFIX = "con-url-prefix";
45    private static final String JavaDoc HB_DIALECT = "hb-dialect";
46
47
48     public static String JavaDoc getDialectFromDataSource(String JavaDoc dsName) throws PortalSetupException
49    {
50       DataSource JavaDoc ds = locateDataSource(dsName);
51       Connection JavaDoc con = null;
52       String JavaDoc conUrl = null;
53       try
54       {
55          con = con = ds.getConnection();
56          conUrl = con.getMetaData().getURL();
57       }
58       catch (SQLException JavaDoc e)
59       {
60          throw new PortalSetupException("Failed to retrive database metadata.", e);
61       }
62        if(null == conUrl) {
63           throw new PortalSetupException("Failed to locate datanase connection url info");
64       }
65       return getDialectFromConnURL(conUrl);
66
67    }
68
69    synchronized public static String JavaDoc getDialectFromConnURL(String JavaDoc conUrl) throws PortalSetupException
70    {
71      String JavaDoc dialect = null;
72       if(null == conUrl) {
73           throw new PortalSetupException("Database connection url info cannot be null");
74       }
75       List JavaDoc hbMappings = getHibernateMappings();
76       Iterator JavaDoc iter = hbMappings.iterator();
77       while(iter.hasNext()) {
78         DialectMapping mapping = (DialectMapping)iter.next();
79         if(conUrl.startsWith(mapping.getConUrlPrefix())) {
80            dialect = mapping.getHbDialectClass();
81            break;
82         }
83
84       }
85       if(null == dialect) {
86          throw new PortalSetupException ("failed to locate hb dialect class mapping for db connection url:" + conUrl);
87       }
88
89       return dialect;
90
91    }
92
93    synchronized private static List JavaDoc getHibernateMappings() throws PortalSetupException
94    {
95       if (null == DB_TO_HB_DIALECT)
96       {
97          DB_TO_HB_DIALECT = new ArrayList JavaDoc();
98          {
99             Element JavaDoc docElmt;
100             InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(DIALECT_MAPPINGS_URI);
101             if (is == null)
102             {
103                throw new PortalSetupException(ERROR_MSG);
104             }
105             Document JavaDoc dmt = null;
106             try
107             {
108                dmt = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(is);
109                List JavaDoc mappings = XML.getChildren(dmt.getDocumentElement(), HB_MAPPING_ELMT_NAME);
110                if (null == mappings)
111                {
112                   throw new IOException JavaDoc("No hibernate dialect to db vendor mappings have been defined in '" +
113                      DIALECT_MAPPINGS_URI + "'");
114                }
115                Iterator JavaDoc iter = mappings.iterator();
116                while (iter.hasNext())
117                {
118                   Element JavaDoc map = (Element JavaDoc)iter.next();
119                   String JavaDoc conUrlPr = XML.asString(XML.getUniqueChild(map, CON_URL_PREFIX, true));
120                   String JavaDoc conDrv = XML.asString(XML.getUniqueChild(map, CON_DRIVER, true));
121                   String JavaDoc hbDlct = XML.asString(XML.getUniqueChild(map, HB_DIALECT, true));
122                   DB_TO_HB_DIALECT.add(new DialectMapping(conUrlPr, conDrv, hbDlct));
123                }
124             }
125             catch (SAXException JavaDoc e)
126             {
127
128                throw new PortalSetupException(ERROR_MSG, e);
129             }
130             catch (IOException JavaDoc e)
131             {
132                throw new PortalSetupException(ERROR_MSG, e);
133             }
134             catch (ParserConfigurationException JavaDoc e)
135             {
136                throw new PortalSetupException(ERROR_MSG, e);
137             }
138
139
140          }
141       }
142       return DB_TO_HB_DIALECT;
143    }
144
145
146    private static DataSource JavaDoc locateDataSource(String JavaDoc dsName) throws PortalSetupException
147    {
148       DataSource JavaDoc ds = null;
149
150
151       try
152       {
153          ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup(dsName);
154       }
155       catch (NamingException JavaDoc e)
156       {
157          throw new PortalSetupException("Failed to locate data source: " + dsName, e);
158       }
159
160
161       return ds;
162    }
163
164
165 }
166
Popular Tags