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
.commons
.lang
.StringUtils
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.hibernate
.cache
.internal
.NoCachingRegionFactory
;
18 import org
.hibernate
.cache
.spi
.RegionFactory
;
19 import org
.hibernate
.cfg
.Environment
;
20 import org
.springframework
.beans
.MutablePropertyValues
;
21 import org
.springframework
.beans
.factory
.config
.BeanDefinition
;
22 import org
.springframework
.beans
.factory
.config
.PropertiesFactoryBean
;
23 import org
.springframework
.beans
.factory
.support
.AbstractBeanDefinition
;
24 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
33 public class CdmDataSource
extends CdmDataSourceBase
{
34 @SuppressWarnings("unused")
35 private static final Logger logger
= Logger
.getLogger(CdmDataSource
.class);
37 private DatabaseTypeEnum dbType
;
38 private String server
;
39 private String database
;
40 private int port
= -1;
41 private String username
;
42 private String password
;
43 private NomenclaturalCode nomenclaturalCode
;
45 private String filePath
;
48 private boolean isLazy
= true;
49 private String initMethodName
= null;
50 private String destroyMethodName
= null;
51 private DbSchemaValidation hbm2dll
= DbSchemaValidation
.VALIDATE
;
52 private boolean showSql
= false;
53 private boolean formatSql
= false;
54 private boolean registerSearchListener
= false;
55 private Class
<?
extends RegionFactory
> cacheProviderClass
= NoCachingRegionFactory
.class;
57 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
){
58 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, null);
61 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
){
62 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, null);
65 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, String username
, String password
, NomenclaturalCode code
){
66 return new CdmDataSource(dbType
, server
, database
, -1, username
, password
, null, null, code
);
69 public static CdmDataSource
NewInstance(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
70 return new CdmDataSource(dbType
, server
, database
, port
, username
, password
, null, null, code
);
74 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
){
75 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, null);
78 static public CdmDataSource
NewMySqlInstance(String server
, String database
, String username
, String password
, NomenclaturalCode code
){
79 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, -1, username
, password
, null, null, code
);
82 static public CdmDataSource
NewMySqlInstance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
83 return new CdmDataSource(DatabaseTypeEnum
.MySQL
, server
, database
, port
, username
, password
, null, null, code
);
86 static public CdmDataSource
NewPostgreSQLInstance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
87 return new CdmDataSource(DatabaseTypeEnum
.PostgreSQL
, server
, database
, port
, username
, password
, null, null, code
);
90 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
, NomenclaturalCode code
){
91 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null, code
);
94 static public CdmDataSource
NewSqlServer2005Instance(String server
, String database
, int port
, String username
, String password
/*, NomenclaturalCode code*/){
95 return new CdmDataSource(DatabaseTypeEnum
.SqlServer2005
, server
, database
, port
, username
, password
, null, null, null);
101 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
){
102 return NewH2EmbeddedInstance(database
, username
, password
, null, null);
107 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, NomenclaturalCode code
){
108 return NewH2EmbeddedInstance(database
, username
, password
, null, code
);
113 static public CdmDataSource
NewH2EmbeddedInstance(String database
, String username
, String password
, String filePath
, NomenclaturalCode code
){
116 H2Mode mode
= H2Mode
.EMBEDDED
;
117 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, database
, port
, username
, password
, null, mode
, code
);
122 static public CdmDataSource
NewH2InMemoryInstance(){
125 H2Mode mode
= H2Mode
.IN_MEMORY
;
126 String username
= "sa";
127 String password
= "";
128 CdmDataSource dataSource
= new CdmDataSource(DatabaseTypeEnum
.H2
, null, null, port
, username
, password
, null, mode
, null);
138 protected CdmDataSource(DatabaseTypeEnum dbType
, String server
, String database
, int port
, String username
, String password
, String filePath
, H2Mode mode
, NomenclaturalCode code
) {
140 this.dbType
= dbType
;
141 this.server
= server
;
142 this.database
= database
;
144 this.username
= username
;
145 this.password
= password
;
146 this.initMethodName
= dbType
.getInitMethod();
147 this.destroyMethodName
= dbType
.getDestroyMethod();
148 this.filePath
= filePath
;
150 this.nomenclaturalCode
= code
;
155 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
156 * A CdmDataSource does not have a name representation therefor the database name is returned
158 public String
getName() {
163 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
165 public BeanDefinition
getDatasourceBean(){
166 AbstractBeanDefinition bd
= new RootBeanDefinition(dbType
.getDataSourceClass());
168 bd
.setLazyInit(isLazy
);
169 if (! CdmUtils
.Nz(initMethodName
).trim().equals("") ){
170 bd
.setInitMethodName(initMethodName
);
172 if (! CdmUtils
.Nz(destroyMethodName
).trim().equals("") ){
173 bd
.setInitMethodName(destroyMethodName
);
177 MutablePropertyValues props
= new MutablePropertyValues();
178 Properties persistentProperties
= getDatasourceProperties();
179 Enumeration
<Object
> keys
= (Enumeration
<Object
>)persistentProperties
.keys();
180 while (keys
.hasMoreElements()){
181 String key
= (String
)keys
.nextElement();
182 props
.addPropertyValue(key
, persistentProperties
.getProperty(key
));
183 Properties a
= Environment
.getProperties();
186 bd
.setPropertyValues(props
);
191 * Returns the list of properties that are defined in the datasource
194 private Properties
getDatasourceProperties(){
195 Properties result
= new Properties();
196 result
.put("driverClassName", dbType
.getDriverClassName());
197 String connectionString
= dbType
.getConnectionString(this);
198 result
.put("url", connectionString
);
199 result
.put("username", username
);
200 result
.put("password", password
);
206 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
208 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
){
209 boolean showSql
= false;
210 boolean formatSql
= false;
211 boolean registerSearchListener
= false;
212 Class
<?
extends RegionFactory
> cacheProviderClass
= NoCachingRegionFactory
.class;
213 return getHibernatePropertiesBean(hbm2dll
, showSql
, formatSql
, registerSearchListener
, cacheProviderClass
);
217 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
219 public BeanDefinition
getHibernatePropertiesBean(DbSchemaValidation hbm2dll
, Boolean showSql
, Boolean formatSql
, Boolean registerSearchListener
, Class
<?
extends RegionFactory
> cacheProviderClass
){
220 //Hibernate default values
221 if (hbm2dll
== null){
222 hbm2dll
= this.hbm2dll
;
224 if (showSql
== null){
225 showSql
= this.showSql
;
227 if (formatSql
== null){
228 formatSql
= this.formatSql
;
230 if (cacheProviderClass
== null){
231 cacheProviderClass
= this.cacheProviderClass
;
233 if(registerSearchListener
== null){
234 registerSearchListener
= this.registerSearchListener
;
237 DatabaseTypeEnum dbtype
= dbType
;
238 AbstractBeanDefinition bd
= new RootBeanDefinition(PropertiesFactoryBean
.class);
239 MutablePropertyValues hibernateProps
= new MutablePropertyValues();
241 Properties props
= new Properties();
242 props
.setProperty("hibernate.hbm2ddl.auto", hbm2dll
.toString());
243 props
.setProperty("hibernate.dialect", dbtype
.getHibernateDialect());
244 // OLD:props.setProperty("hibernate.cache.provider_class", cacheProviderClass.getName());
245 props
.setProperty("hibernate.cache.region.factory_class", cacheProviderClass
.getName());
246 props
.setProperty("hibernate.show_sql", String
.valueOf(showSql
));
247 props
.setProperty("hibernate.format_sql", String
.valueOf(formatSql
));
248 props
.setProperty("hibernate.search.autoregister_listeners", String
.valueOf(registerSearchListener
));
250 hibernateProps
.addPropertyValue("properties",props
);
251 bd
.setPropertyValues(hibernateProps
);
255 public String
getInitMethodName() {
256 return initMethodName
;
259 public void setInitMethodName(String initMethodName
) {
260 this.initMethodName
= initMethodName
;
263 public String
getDestroyMethodName() {
264 return destroyMethodName
;
267 public void setDestroyMethodName(String destroyMethodName
) {
268 this.destroyMethodName
= destroyMethodName
;
271 public String
getDatabase() {
275 public DatabaseTypeEnum
getDatabaseType() {
279 public String
getFilePath() {
284 public int getPort() {
288 public String
getServer() {
292 public H2Mode
getMode() {
297 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
299 public String
getPassword() {
304 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
306 public String
getUsername() {
311 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getNomenclaturalCode()
313 public NomenclaturalCode
getNomenclaturalCode() {
314 return nomenclaturalCode
;
318 public String
toString() {
319 if (StringUtils
.isBlank(this.database
)){
320 return super.toString();
322 String result
= "DataSource<" + dbType
.getConnectionString(this).replace(CdmUtils
.Nz(password
), "") + ">";