1 9 package org.jboss.portal.setup.impl.config; 10 11 import org.jboss.portal.setup.config.HibernateConfig; 12 import org.jboss.portal.setup.config.DbAccessConfig; 13 import org.jboss.portal.setup.config.Configuration; 14 import org.jboss.portal.setup.PortalSetupConstants; 15 import org.jboss.portal.setup.PortalSetupException; 16 import org.jboss.portal.setup.util.hibernate.HibernareDialectResolver; 17 import org.jboss.portal.common.util.XML; 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.NodeList ; 20 21 import java.util.Properties ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 27 28 29 30 38 public class HibernateConfigImpl extends ConfigurationImpl implements HibernateConfig, PortalSetupConstants 39 { 40 41 private DbAccessConfig m_dbAccessConfig = null; 42 private List m_SchemaClassesList = new ArrayList (); 43 private Properties m_HibernateProperties = new Properties (); 44 private boolean m_validate = true; 45 46 47 48 54 public void setDbAccessConfig(DbAccessConfig dbConfig) 55 { 56 m_dbAccessConfig = (DbAccessConfig)dbConfig.getConfiguration(); 57 58 resolveHibernateDbProperies(); 59 } 60 61 67 public DbAccessConfig getDbAccessConfig() 68 { 69 return m_dbAccessConfig; 70 } 71 72 73 78 public String getHibernateDialect() throws PortalSetupException 79 { 80 String dialect = (String )m_HibernateProperties.get(HIBERNATE_DIALECT); 81 if (null == dialect || 82 AUTO_RESOLVE.equals(dialect)) { 83 dialect = resolveHbDialect(); 84 m_HibernateProperties.put(HIBERNATE_DIALECT, dialect); 85 } 86 return dialect; 87 } 88 89 94 public void setHibernateDialect(String dialectClassName) 95 { 96 97 m_HibernateProperties.put(HIBERNATE_DIALECT, dialectClassName); 98 } 99 100 101 106 public void setCacheProvider(String cacheProvider) 107 { 108 m_HibernateProperties.put(HIBERNATE_CACHE_PROVIDER, cacheProvider); 109 } 110 111 116 public String getCacheProvider() 117 { 118 return (String )m_HibernateProperties.get(HIBERNATE_CACHE_PROVIDER); 119 } 120 121 122 128 public void setSchemaClasses(Element classesElmt) 129 { 130 List nodes = XML.getChildren(classesElmt, "db-schema-class-name"); 131 if (null != nodes) 132 { 133 for (int i = 0, size = nodes.size(); i < size; i++) 134 { 135 Element classElmt = (Element )nodes.get(i); 136 if (classElmt != null) 137 { 138 String className = XML.asString(classElmt); 139 try 140 { 141 Class clazz = Class.forName(className); 142 m_SchemaClassesList.add(clazz); 143 } 144 catch (ClassNotFoundException cnfe) 145 { 146 throw new IllegalArgumentException ("Fail to load class: " + className + "due to" + cnfe.getMessage()); 147 } 148 } 149 } 150 } 151 } 152 153 159 public Element getSchemaClasses() 160 { 161 Element classes = null; 162 return classes; 164 } 165 166 167 171 public Properties getHibernateProperties() throws PortalSetupException 172 { 173 String dialect = getHibernateDialect(); 174 return m_HibernateProperties; 175 } 176 177 178 181 public List getSchemaClassesList() 182 { 183 return m_SchemaClassesList; 184 } 185 186 190 public void setHibernateProperties(Properties props) 191 { 192 if (props != null) 193 { 194 m_HibernateProperties.putAll(props); 195 } 196 } 197 198 203 public void addPropery(String key, String value) 204 { 205 m_HibernateProperties.put(key, value); 206 } 207 208 209 public void setSchemaClasses(List classes) 210 { 211 m_SchemaClassesList.addAll(classes); 212 } 213 214 public void addSchemaClass(Class schemaClass) 215 { 216 m_SchemaClassesList.add(schemaClass); 217 } 218 219 220 private void resolveHibernateDbProperies() 221 { 222 223 if (m_dbAccessConfig.getDataSourceName() != null) 224 { 225 m_HibernateProperties.put(DATA_SOURCE_NAME, m_dbAccessConfig.getDataSourceName()); 227 228 } 229 else 230 { 231 m_HibernateProperties.put(DB_CONN_URL, m_dbAccessConfig.getConnUrl()); 233 m_HibernateProperties.put(DB_CONN_DRIVER_CLASS, m_dbAccessConfig.getDriverClassName()); 234 m_HibernateProperties.put(DB_USER_NAME, m_dbAccessConfig.getUserName()); 235 m_HibernateProperties.put(DB_USER_PASSWORD, m_dbAccessConfig.getPassword()); 236 237 } 238 } 239 240 private String resolveHbDialect() throws PortalSetupException { 241 String dialect = null; 242 String str =m_dbAccessConfig.getDataSourceName(); 243 if(null != str) { 244 dialect = HibernareDialectResolver.getDialectFromDataSource(str); 245 } else { 246 str = m_dbAccessConfig.getConnUrl(); 247 dialect = HibernareDialectResolver.getDialectFromConnURL(str); 248 } 249 return dialect; 250 251 } 252 253 } 254 | Popular Tags |