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()
113 * A CdmDataSource does not have a name representation therefor the database name is returned
115 public String
getName() {
120 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
122 @SuppressWarnings("unchecked")
123 public BeanDefinition
getDatasourceBean(){
124 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDriverManagerDataSourceClass());
126 bd
.setLazyInit(isLazy
);
127 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
128 bd
.setInitMethodName(initMethodName
);
130 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
131 bd
.setInitMethodName(destroyMethodName
);
135 MutablePropertyValues props
= new MutablePropertyValues();
136 Properties persistentProperties
= getDatasourceProperties();
137 Enumeration
<String
> keys
= (Enumeration
)persistentProperties
.keys();
138 while (keys
.hasMoreElements()){
139 String key
= (String
)keys
.nextElement();
140 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
143 bd
.setPropertyValues(props
);
148 * Returns the list of properties that are defined in the datasource
151 private Properties
getDatasourceProperties(){
152 Properties result
= new Properties();
153 result
.put("driverClassName", dbType
.getDriverClassName());
154 String connectionString
= dbType
.getConnectionString(this);
155 result
.put("url", connectionString
);
156 result
.put("username", username
);
157 result
.put("password", password
);
163 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
165 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
166 boolean showSql
= false;
167 boolean formatSql
= false;
168 Class
<?
extends CacheProvider
> cacheProviderClass
= NoCacheProvider
.class;
169 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, cacheProviderClass
);
173 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
175 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Class
<?
extends CacheProvider
> cacheProviderClass
){
176 //Hibernate default values
177 if (hbm2dll
== null){
178 hbm2dll
= this.hbm2dll
;
180 if (showSql
== null){
181 showSql
= this.showSql
;
183 if (formatSql
== null){
184 formatSql
= this.formatSql
;
186 if (cacheProviderClass
== null){
187 cacheProviderClass
= this.cacheProviderClass
;
190 DatabaseTypeEnum dbtype
= dbType
;
191 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
192 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
194 Properties props
= new Properties();
195 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
196 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
197 props
.setProperty("hibernate.cache.provider_class", cacheProviderClass
.getName());
198 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
199 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
201 hibernateProps
.addPropertyValue("properties",props
);
202 bd
.setPropertyValues(hibernateProps
);
206 public String
getInitMethodName() {
207 return initMethodName
;
210 public void setInitMethodName(String initMethodName
) {
211 this.initMethodName
= initMethodName
;
214 public String
getDestroyMethodName() {
215 return destroyMethodName
;
218 public void setDestroyMethodName(String destroyMethodName
) {
219 this.destroyMethodName
= destroyMethodName
;
222 public String
getDatabase() {
226 public DatabaseTypeEnum
getDatabaseType() {
230 public String
getFilePath() {
235 public int getPort() {
239 public String
getServer() {
243 public H2Mode
getMode() {
248 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
250 public String
getPassword() {
255 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
257 public String
getUsername() {