1 9 package org.jboss.portal.setup.util.hibernate; 10 11 12 import org.w3c.dom.Document ; 13 import org.w3c.dom.Element ; 14 import org.jboss.portal.common.util.XML; 15 import org.jboss.portal.setup.PortalSetupException; 16 import org.xml.sax.SAXException ; 17 18 import javax.sql.DataSource ; 19 import javax.xml.parsers.ParserConfigurationException ; 20 import javax.naming.InitialContext ; 21 import javax.naming.NamingException ; 22 import java.util.List ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 30 36 public class HibernareDialectResolver 37 { 38 private static List DB_TO_HB_DIALECT = null; 39 private static final String DIALECT_MAPPINGS_URI = "org/jboss/portal/setup/util/hibernate/hb-driver-mappings.xml"; 40 private static final String HB_MAPPING_ELMT_NAME = "hb-driver-mapping"; 41 private static final String ERROR_MSG = "Failed to load hibernate mapping file resource from '" + 42 DIALECT_MAPPINGS_URI + "'"; 43 private static final String CON_DRIVER = "con-driver"; 44 private static final String CON_URL_PREFIX = "con-url-prefix"; 45 private static final String HB_DIALECT = "hb-dialect"; 46 47 48 public static String getDialectFromDataSource(String dsName) throws PortalSetupException 49 { 50 DataSource ds = locateDataSource(dsName); 51 Connection con = null; 52 String conUrl = null; 53 try 54 { 55 con = con = ds.getConnection(); 56 conUrl = con.getMetaData().getURL(); 57 } 58 catch (SQLException 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 getDialectFromConnURL(String conUrl) throws PortalSetupException 70 { 71 String dialect = null; 72 if(null == conUrl) { 73 throw new PortalSetupException("Database connection url info cannot be null"); 74 } 75 List hbMappings = getHibernateMappings(); 76 Iterator 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 getHibernateMappings() throws PortalSetupException 94 { 95 if (null == DB_TO_HB_DIALECT) 96 { 97 DB_TO_HB_DIALECT = new ArrayList (); 98 { 99 Element docElmt; 100 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(DIALECT_MAPPINGS_URI); 101 if (is == null) 102 { 103 throw new PortalSetupException(ERROR_MSG); 104 } 105 Document dmt = null; 106 try 107 { 108 dmt = XML.getDocumentBuilderFactory().newDocumentBuilder().parse(is); 109 List mappings = XML.getChildren(dmt.getDocumentElement(), HB_MAPPING_ELMT_NAME); 110 if (null == mappings) 111 { 112 throw new IOException ("No hibernate dialect to db vendor mappings have been defined in '" + 113 DIALECT_MAPPINGS_URI + "'"); 114 } 115 Iterator iter = mappings.iterator(); 116 while (iter.hasNext()) 117 { 118 Element map = (Element )iter.next(); 119 String conUrlPr = XML.asString(XML.getUniqueChild(map, CON_URL_PREFIX, true)); 120 String conDrv = XML.asString(XML.getUniqueChild(map, CON_DRIVER, true)); 121 String 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 e) 126 { 127 128 throw new PortalSetupException(ERROR_MSG, e); 129 } 130 catch (IOException e) 131 { 132 throw new PortalSetupException(ERROR_MSG, e); 133 } 134 catch (ParserConfigurationException 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 locateDataSource(String dsName) throws PortalSetupException 147 { 148 DataSource ds = null; 149 150 151 try 152 { 153 ds = (DataSource )new InitialContext ().lookup(dsName); 154 } 155 catch (NamingException 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 |