test connection and ICdmDatasource getters
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / CdmDataSource.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.database;
11
12 import java.util.Enumeration;
13 import java.util.Properties;
14
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;
23
24 import eu.etaxonomy.cdm.common.CdmUtils;
25 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
26 import eu.etaxonomy.cdm.database.DbSchemaValidation;
27
28 /**
29 * @author a.mueller
30 *
31 */
32 public class CdmDataSource extends CdmDataSourceBase {
33 @SuppressWarnings("unused")
34 private static final Logger logger = Logger.getLogger(CdmDataSource.class);
35
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;
42
43 private String filePath;
44 private H2Mode mode;
45
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;;
53
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);
56 }
57
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);
60 }
61
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);
64 }
65
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);
68 }
69
70 /** in work */
71 static public CdmDataSource NewH2EmbeddedInstance(String database, String username, String password){
72 //FIXME in work
73 int port = -1;
74 H2Mode mode = H2Mode.EMBEDDED;
75 CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, database, port, username, password, null, mode);
76 return dataSource;
77 }
78
79 /** in work */
80 static public CdmDataSource NewH2InMemoryInstance(){
81 //FIXME in work
82 int port = -1;
83 H2Mode mode = H2Mode.IN_MEMORY;
84 String username = "sa";
85 String password = "";
86 CdmDataSource dataSource = new CdmDataSource(DatabaseTypeEnum.H2, null, null, port, username, password, null, mode);
87 return dataSource;
88 }
89
90
91 /**
92 * @param server
93 * @param database
94 * @param port
95 */
96 protected CdmDataSource(DatabaseTypeEnum dbType, String server, String database, int port, String username, String password, String filePath, H2Mode mode) {
97 super();
98 this.dbType = dbType;
99 this.server = server;
100 this.database = database;
101 this.port = port;
102 this.username = username;
103 this.password = password;
104 this.initMethodName = dbType.getInitMethod();
105 this.destroyMethodName = dbType.getDestroyMethod();
106 this.filePath = filePath;
107 this.mode = mode;
108 }
109
110
111 /* (non-Javadoc)
112 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getName()
113 */
114 public String getName() {
115 return database;
116 }
117
118 /* (non-Javadoc)
119 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getDatasourceBean()
120 */
121 @SuppressWarnings("unchecked")
122 public BeanDefinition getDatasourceBean(){
123 AbstractBeanDefinition bd = new RootBeanDefinition(dbType.getDriverManagerDataSourceClass());
124 //attributes
125 bd.setLazyInit(isLazy);
126 if (! CdmUtils.Nz(initMethodName).trim().equals("") ){
127 bd.setInitMethodName(initMethodName);
128 }
129 if (! CdmUtils.Nz(destroyMethodName).trim().equals("") ){
130 bd.setInitMethodName(destroyMethodName);
131 }
132
133 //properties
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));
140 }
141
142 bd.setPropertyValues(props);
143 return bd;
144 }
145
146 /**
147 * Returns the list of properties that are defined in the datasource
148 * @return
149 */
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);
157 return result;
158 }
159
160
161 /* (non-Javadoc)
162 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL)
163 */
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);
169 }
170
171 /* (non-Javadoc)
172 * @see eu.etaxonomy.cdm.api.application.ICdmDataSource#getHibernatePropertiesBean(eu.etaxonomy.cdm.database.CdmPersistentDataSource.HBM2DDL, java.lang.Boolean, java.lang.Boolean, java.lang.Class)
173 */
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;
178 }
179 if (showSql == null){
180 showSql = this.showSql;
181 }
182 if (formatSql == null){
183 formatSql = this.formatSql;
184 }
185 if (cacheProviderClass == null){
186 cacheProviderClass = this.cacheProviderClass;
187 }
188
189 DatabaseTypeEnum dbtype = dbType;
190 AbstractBeanDefinition bd = new RootBeanDefinition(PropertiesFactoryBean.class);
191 MutablePropertyValues hibernateProps = new MutablePropertyValues();
192
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));
199
200 hibernateProps.addPropertyValue("properties",props);
201 bd.setPropertyValues(hibernateProps);
202 return bd;
203 }
204
205 public String getInitMethodName() {
206 return initMethodName;
207 }
208
209 public void setInitMethodName(String initMethodName) {
210 this.initMethodName = initMethodName;
211 }
212
213 public String getDestroyMethodName() {
214 return destroyMethodName;
215 }
216
217 public void setDestroyMethodName(String destroyMethodName) {
218 this.destroyMethodName = destroyMethodName;
219 }
220
221 public String getDatabase() {
222 return database;
223 }
224
225 public DatabaseTypeEnum getDatabaseType() {
226 return dbType;
227 }
228
229 public String getFilePath() {
230 return filePath;
231 }
232
233
234 public int getPort() {
235 return port;
236 }
237
238 public String getServer() {
239 return server;
240 }
241
242 public H2Mode getMode() {
243 return mode;
244 }
245
246 /* (non-Javadoc)
247 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getPassword()
248 */
249 public String getPassword() {
250 return password;
251 }
252
253 /* (non-Javadoc)
254 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getUserName()
255 */
256 public String getUserName() {
257 return username;
258 }
259
260
261
262
263 }
264