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
extends CdmDataSourceBase
{
33 @SuppressWarnings("unused")
34 private static final Logger logger
= Logger
.getLogger(CdmDataSource
.class);
36 private DatabaseTypeEnum dbType
;
37 private String server
;
38 private String database
;
39 private int port
= -1;
40 private String username
;
41 private String password
;
43 private String filePath
;
46 private boolean isLazy
= true;
47 private String initMethodName
= null;
48 private String destroyMethodName
= null;
49 private DbSchemaValidation hbm2dll
= DbSchemaValidation
.VALIDATE
;
50 private boolean showSql
= false;
51 private boolean formatSql
= false;
52 private Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;;
54 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
55 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null);
58 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
){
59 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null);
62 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, String username
, String password
){
63 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, -1, username
, password
, null, null);
66 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
){
67 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null);
71 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
74 H2Mode mode
= H2Mode
.EMBEDDED
;
75 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
);
80 static public CdmDataSource
NewH2InMemoryInstance(){
83 H2Mode mode
= H2Mode
.IN_MEMORY
;
84 String username
= "sa";
86 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
);
96 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
) {
100 this.database
= database
;
102 this.username
= username
;
103 this.password
= password
;
104 this.initMethodName
= dbType
.getInitMethod();
105 this.destroyMethodName
= dbType
.getDestroyMethod();
106 this.filePath
= filePath
;
112 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
114 public String
getName() {
119 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
121 @SuppressWarnings("unchecked")
122 public BeanDefinition
getDatasourceBean(){
123 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDriverManagerDataSourceClass());
125 bd
.setLazyInit(isLazy
);
126 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
127 bd
.setInitMethodName(initMethodName
);
129 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
130 bd
.setInitMethodName(destroyMethodName
);
134 MutablePropertyValues props
= new MutablePropertyValues();
135 Properties persistentProperties
= getDatasourceProperties();
136 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
137 while (keys
.hasMoreElements()){
138 String key
= (String
)keys
.nextElement();
139 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
142 bd
.setPropertyValues(props
);
147 * Returns the list of properties that are defined in the datasource
150 private Properties
getDatasourceProperties(){
151 Properties result
= new Properties();
152 result
.put("driverClassName", dbType
.getDriverClassName());
153 String connectionString
= dbType
.getConnectionString(this);
154 result
.put("url", connectionString
);
155 result
.put("username", username
);
156 result
.put("password", password
);
162 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
164 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
165 boolean showSql
= false;
166 boolean formatSql
= false;
167 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
168 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, cacheProviderClass
);
172 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
174 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Class
<?
extends CacheProvider
> cacheProviderClass
){
175 //Hibernate default values
176 if (hbm2dll
== null){
177 hbm2dll
= this.hbm2dll
;
179 if (showSql
== null){
180 showSql
= this.showSql
;
182 if (formatSql
== null){
183 formatSql
= this.formatSql
;
185 if (cacheProviderClass
== null){
186 cacheProviderClass
= this.cacheProviderClass
;
189 DatabaseTypeEnum dbtype
= dbType
;
190 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
191 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
193 Properties props
= new Properties();
194 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
195 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
196 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
197 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
198 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
200 hibernateProps
.addPropertyValue("properties",props
);
201 bd
.setPropertyValues(hibernateProps
);
205 public String
getInitMethodName() {
206 return initMethodName
;
209 public void setInitMethodName(String initMethodName
) {
210 this.initMethodName
= initMethodName
;
213 public String
getDestroyMethodName() {
214 return destroyMethodName
;
217 public void setDestroyMethodName(String destroyMethodName
) {
218 this.destroyMethodName
= destroyMethodName
;
221 public String
getDatabase() {
225 public DatabaseTypeEnum
getDatabaseType() {
229 public String
getFilePath() {
234 public int getPort() {
238 public String
getServer() {
242 public H2Mode
getMode() {
247 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
249 public String
getPassword() {
254 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
256 public String
getUserName() {