2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
;
12 import java
.util
.Enumeration
;
13 import java
.util
.Properties
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.hibernate
.cache
.CacheProvider
;
17 import org
.hibernate
.cache
.NoCacheProvider
;
18 import org
.springframework
.beans
.MutablePropertyValues
;
19 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
20 import org
.springframework
.beans
.factory
.config
.PropertiesFactoryBean
;
21 import org
.springframework
.beans
.factory
.support
.AbstractBeanDefinition
;
22 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.database
.DatabaseTypeEnum
;
26 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
32 public class CdmDataSource
implements ICdmDataSource
{
33 private static final Logger logger
= Logger
.getLogger(CdmDataSource
.class);
35 private DatabaseTypeEnum dbType
;
36 private String server
;
37 private String database
;
38 private int port
= -1;
39 private String username
;
40 private String password
;
42 private String filePath
;
45 private boolean isLazy
= true;
46 private String initMethodName
= null;
47 private String destroyMethodName
= null;
48 private DbSchemaValidation hbm2dll
= DbSchemaValidation
.VALIDATE
;
49 private boolean showSql
= false;
50 private boolean formatSql
= false;
51 private Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;;
53 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
54 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null);
57 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
){
58 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null);
61 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, String username
, String password
){
62 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, -1, username
, password
, null, null);
65 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
){
66 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null);
70 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
73 H2Mode mode
= H2Mode
.EMBEDDED
;
74 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
);
79 static public CdmDataSource
NewH2InMemoryInstance(){
82 H2Mode mode
= H2Mode
.IN_MEMORY
;
83 String username
= "sa";
85 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
);
95 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
) {
99 this.database
= database
;
101 this.username
= username
;
102 this.password
= password
;
103 this.initMethodName
= dbType
.getInitMethod();
104 this.destroyMethodName
= dbType
.getDestroyMethod();
105 this.filePath
= filePath
;
111 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
113 public String
getName() {
118 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
120 public BeanDefinition
getDatasourceBean(){
121 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDriverManagerDataSourceClass());
123 bd
.setLazyInit(isLazy
);
124 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
125 bd
.setInitMethodName(initMethodName
);
127 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
128 bd
.setInitMethodName(destroyMethodName
);
132 MutablePropertyValues props
= new MutablePropertyValues();
133 Properties persistentProperties
= getDatasourceProperties();
134 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
135 while (keys
.hasMoreElements()){
136 String key
= (String
)keys
.nextElement();
137 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
140 bd
.setPropertyValues(props
);
145 * Returns the list of properties that are defined in the datasource
148 private Properties
getDatasourceProperties(){
149 Properties result
= new Properties();
150 result
.put("driverClassName", dbType
.getDriverClassName());
151 String connectionString
= dbType
.getConnectionString(this);
152 result
.put("url", connectionString
);
153 result
.put("username", username
);
154 result
.put("password", password
);
160 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
162 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
163 boolean showSql
= false;
164 boolean formatSql
= false;
165 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
166 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, cacheProviderClass
);
170 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
172 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Class
<?
extends CacheProvider
> cacheProviderClass
){
173 //Hibernate default values
174 if (hbm2dll
== null){
175 hbm2dll
= this.hbm2dll
;
177 if (showSql
== null){
178 showSql
= this.showSql
;
180 if (formatSql
== null){
181 formatSql
= this.formatSql
;
183 if (cacheProviderClass
== null){
184 cacheProviderClass
= this.cacheProviderClass
;
187 DatabaseTypeEnum dbtype
= dbType
;
188 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
189 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
191 Properties props
= new Properties();
192 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
193 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
194 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
195 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
196 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
198 hibernateProps
.addPropertyValue("properties",props
);
199 bd
.setPropertyValues(hibernateProps
);
203 public String
getInitMethodName() {
204 return initMethodName
;
207 public void setInitMethodName(String initMethodName
) {
208 this.initMethodName
= initMethodName
;
211 public String
getDestroyMethodName() {
212 return destroyMethodName
;
215 public void setDestroyMethodName(String destroyMethodName
) {
216 this.destroyMethodName
= destroyMethodName
;
219 public String
getDatabase() {
223 public String
getFilePath() {
228 public int getPort() {
232 public String
getServer() {
236 public H2Mode
getMode() {