Project

General

Profile

Revision 2d427fc0

ID2d427fc08875a402ffcd4c3b18e67dac316499da
Parent 37b5850c
Child 0e749c17

Added by Cherian Mathew about 7 years ago

TestDatabase / CdmPersistentDataSourceTest / CdmApplicationUtils : updated as per Cdm Source refactoring
ICdmDataSource : moved generic methods to super interface ICdmSource
CdmPersistentDataSource : refactored by spliting the utility part to CdmPersistentXMLUtils and the jDom elementpart to CdmXMLPersistentSource
CdmDataSourceBase : implementing databse check methods

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationUtils.java
19 19

  
20 20
import eu.etaxonomy.cdm.common.CdmUtils;
21 21
import eu.etaxonomy.cdm.common.FileCopy;
22
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
22 23
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
23 24

  
24 25
public class CdmApplicationUtils {
......
26 27

  
27 28
    //directory of the resources (configfiles etc.)
28 29
    static File fileResourceDir;
29
    static final String MUST_EXIST_FILE = CdmPersistentDataSource.DATASOURCE_PATH + CdmPersistentDataSource.DATASOURCE_FILE_NAME;
30
    static final String MUST_EXIST_FILE = CdmPersistentXMLSource.CDMSOURCE_PATH + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME;
30 31

  
31 32
//	static final String MUST_EXIST_FILE = "persistence.xml";
32 33
//	static final String MUST_EXIST_FILE = "applicationContext.xml";
......
64 65
    }
65 66

  
66 67
    static private void copyResources(File directory){
67
        copyResource(directory, CdmPersistentDataSource.DATASOURCE_FILE_NAME);
68
        copyResource(directory, CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
68 69
    }
69 70

  
70 71
    /**
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmDataSourceBase.java
19 19

  
20 20
import org.apache.log4j.Logger;
21 21

  
22
import eu.etaxonomy.cdm.config.CdmSource;
23
import eu.etaxonomy.cdm.config.CdmSourceException;
22 24
import eu.etaxonomy.cdm.database.types.IDatabaseType;
25
import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
23 26

  
24 27
/**
25 28
 * @author a.mueller
26 29
 * @created 18.12.2008
27 30
 * @veresultSetion 1.0
28 31
 */
29
abstract class CdmDataSourceBase implements ICdmDataSource {
32
abstract class CdmDataSourceBase extends CdmSource implements ICdmDataSource  {
30 33

  
31 34

  
32 35
    private static final Logger logger = Logger.getLogger(CdmDataSourceBase.class);
......
104 107
        return false;
105 108
    }
106 109

  
110
	@Override
111
	public boolean checkConnection() throws CdmSourceException {
112
		try {
113
			return testConnection();
114
		} catch (ClassNotFoundException e) {
115
			throw new CdmSourceException(e.getMessage());			
116
		} catch (SQLException e) {
117
			throw new CdmSourceException(e.getMessage());	
118
		}
119
		
120
	}
121
	
122
	@Override
123
	public String getConnectionMessage() {
124
		String message = "";
125
		if (getDatabaseType().equals(DatabaseTypeEnum.H2)) {
126
			message = " local CDM Store ";
127
		} else {
128
			message = " CDM Community Store ";
129
		}
130
		message += "'" + getName() + "'";
131

  
132
		message = "Connecting to" + message + ".";
133

  
134
		return message;
135
	}
136
	
107 137
    @Override
108 138
    public Object getSingleValue(String query) throws SQLException{
109 139
        String queryString = query == null? "(null)": query;
......
124 154
        return object;
125 155
    }
126 156

  
127

  
157
	@Override
158
	public  String getDbSchemaVersion() throws CdmSourceException  {		
159
		try {
160
			return (String)getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
161
		} catch (SQLException e) {
162
			throw new CdmSourceException(e.getMessage());	
163
		}
164
	}
165
	
166
	@Override
167
	public boolean isDbEmpty() throws CdmSourceException {
168
		// Any CDM DB should have a schema version
169
		String dbSchemaVersion = (String) getDbSchemaVersion();
170
		
171
		return (dbSchemaVersion == null || dbSchemaVersion.equals(""));
172

  
173
	}
128 174
    /**
129 175
     * Executes a query and returns the ResultSet.
130 176
     * @return ResultSet for the query.
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/CdmPersistentDataSource.java
14 14
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;
15 15
import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
16 16

  
17
import java.io.File;
18
import java.io.FileInputStream;
19
import java.io.FileNotFoundException;
20
import java.io.FileOutputStream;
21
import java.io.IOException;
22 17
import java.util.ArrayList;
23 18
import java.util.Enumeration;
24 19
import java.util.Iterator;
......
31 26
import org.hibernate.cache.internal.NoCachingRegionFactory;
32 27
import org.hibernate.cache.spi.RegionFactory;
33 28
import org.jdom.Attribute;
34
import org.jdom.Document;
35 29
import org.jdom.Element;
36
import org.jdom.output.Format;
37 30
import org.springframework.beans.MutablePropertyValues;
38 31
import org.springframework.beans.factory.config.BeanDefinition;
39 32
import org.springframework.beans.factory.config.PropertiesFactoryBean;
......
42 35

  
43 36
import com.mchange.v2.c3p0.ComboPooledDataSource;
44 37

  
45
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
46 38
import eu.etaxonomy.cdm.common.CdmUtils;
47 39
import eu.etaxonomy.cdm.common.XmlHelp;
40
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
41
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
42
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
43
import eu.etaxonomy.cdm.config.ICdmPersistentSource;
48 44
import eu.etaxonomy.cdm.database.types.IDatabaseType;
49 45
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
50 46

  
......
52 48
/**
53 49
 * class to access an CdmDataSource
54 50
 */
55
public class CdmPersistentDataSource extends CdmDataSourceBase{
51
public class CdmPersistentDataSource extends CdmDataSourceBase implements ICdmPersistentSource {
56 52
	private static final Logger logger = Logger.getLogger(CdmPersistentDataSource.class);
57 53
	
58 54
	public static final String DATASOURCE_BEAN_POSTFIX = "DataSource";
59
	public final static String DATASOURCE_FILE_NAME = "cdm.datasources.xml";
60 55
	public final static String DATASOURCE_PATH = "/eu/etaxonomy/cdm/";
61
	
62
	private final static Format format = Format.getPrettyFormat(); 
63

  
64
	public enum DbProperties{
65
		DRIVER_CLASS,
66
		URL,
67
		SERVER, 
68
		DATABASE,
69
		PORT,
70
		MODE,
71
		FILEPATH,
72
		USERNAME,
73
		PASSWORD, 
74
		NOMENCLATURAL_CODE;
75

  
76
		@Override
77
		public String toString(){
78
			switch (this){
79
				case DRIVER_CLASS:
80
					return "driverClassName";
81
				case URL:
82
					return "url";
83
				case SERVER:
84
					return "server";
85
				case DATABASE:
86
					return "database";
87
				case PORT:
88
					return "port";
89
				case MODE:
90
					return "mode";
91
				case FILEPATH:
92
					return "filePath";
93
				case USERNAME:
94
					return "username";
95
				case PASSWORD:
96
					return "password";
97
				case NOMENCLATURAL_CODE:
98
					return "nomenclaturalCode";
99
				default: 
100
					throw new IllegalArgumentException( "Unknown enumeration type" );
101
			}
102
		}
103
	}
56
		
57
	private String beanName;
58
	private String dbUrl;
59
	private Properties cdmSourceProperties;
60
	private Properties dataSourceProperties;
61
	private List<Attribute> cdmSourceAttributes;
62

  
104 63

  
105 64
	/**
106 65
	 * The Datasource class that Spring will use to set up the connection to the database
107 66
	 */
108
	private static String dataSourceClassName = ComboPooledDataSource.class.getName();
109
	// we used dbcps BasicDataSource before
110
//	private static String dataSourceClassName = BasicDataSource.class.getName();
111
	
112
	//name
113
	protected String dataSourceName;
67
	private static String dataSourceClassName = ComboPooledDataSource.class.getName();	
114 68

  
115 69
	
116 70
	/**
......
150 104
	 * @param strDataSource
151 105
	 */
152 106
	private CdmPersistentDataSource(String strDataSource){
153
		dataSourceName = strDataSource;
107
		setName(strDataSource);
108
		loadSource(strDataSource);		
109
	}
110
	
111
	private void loadSource(String strDataSource) {
112
		CdmPersistentXMLSource cdmPersistentXMLSource = CdmPersistentXMLSource.NewInstance(strDataSource, DATASOURCE_BEAN_POSTFIX);
113
		if(cdmPersistentXMLSource.getElement() != null) {
114
			beanName = cdmPersistentXMLSource.getBeanName();
115
			// properties from the persistent xml file 
116
			cdmSourceProperties = cdmPersistentXMLSource.getCdmSourceProperties();
117
			cdmSourceAttributes = cdmPersistentXMLSource.getCdmSourceAttributes();
118
			// properties we must have for a data source
119
			dataSourceProperties = new Properties(cdmSourceProperties);
120
			// added database specific properties if they are null
121
			String url = getCdmSourceProperty(CdmSourceProperties.URL);
122
			DatabaseTypeEnum dbTypeEnum = getDatabaseType();
123
			if (dbTypeEnum != null && url != null){
124
				IDatabaseType dbType = dbTypeEnum.getDatabaseType();				
125
				if (getCdmSourceProperty(CdmSourceProperties.DATABASE) == null){
126
					String database = dbType.getDatabaseNameByConnectionString(url);
127
					if(database != null) {
128
						dataSourceProperties.setProperty(CdmSourceProperties.DATABASE.toString(), database);
129
					}
130
				}
131
				if(getCdmSourceProperty(CdmSourceProperties.SERVER) == null){
132
					String server = dbType.getServerNameByConnectionString(url);
133
					if(server != null) {
134
						dataSourceProperties.setProperty(CdmSourceProperties.SERVER.toString(),server);
135
					}
136
				}
137
				if(getCdmSourceProperty(CdmSourceProperties.PORT) == null){
138
					int port = dbType.getPortByConnectionString(url); 
139
					if(port > 0) {
140
						dataSourceProperties.setProperty(CdmSourceProperties.PORT.toString(),String.valueOf(port));
141
					}
142
				}
143
			}
144
		}					
154 145
	}
155 146
	
156
	@Override
157
	public String getName(){
158
		return dataSourceName;
147
	public String getBeanName() {
148
		return beanName;
159 149
	}
160 150
	
161 151
	
162
	/**
163
	 * Returns the name of the bean Element in the xml config file.
164
	 * @return bean name
165
	 */
166
	private static String getBeanName(String name){
167
		return name == null? null : name + DATASOURCE_BEAN_POSTFIX;
168
	}
169

  
170

  
171
	
172 152
	@Override
173 153
	public String getDatabase() {
174
		return getDatabaseProperty(DbProperties.DATABASE);
154
		return getCdmSourceProperty(CdmSourceProperties.DATABASE);
175 155
	}
176 156

  
177 157

  
178 158
	@Override
179
	public String getFilePath() {
180
		//TODO null
181
		return getDatabaseProperty(DbProperties.FILEPATH);
159
	public String getFilePath() {		
160
		return getCdmSourceProperty(CdmSourceProperties.FILEPATH);
182 161
	}
183 162

  
184 163

  
185 164
	@Override
186
	public H2Mode getMode() {
187
		//TODO null
188
		return H2Mode.fromString(getDatabaseProperty(DbProperties.MODE));
165
	public H2Mode getMode() {		
166
		return H2Mode.fromString(getCdmSourceProperty(CdmSourceProperties.MODE));
167
	}
168
	
169
	@Override
170
	public String getUsername(){
171
		return getCdmSourceProperty(CdmSourceProperties.USERNAME);
189 172
	}
190 173
	
174
	@Override
175
	public String getPassword(){
176
		return getCdmSourceProperty(CdmSourceProperties.PASSWORD);
177
	}
191 178

  
192 179
	@Override
193 180
	public NomenclaturalCode getNomenclaturalCode() {
194 181
		// TODO null
195
		return NomenclaturalCode.fromString(getDatabaseProperty(DbProperties.NOMENCLATURAL_CODE));
182
		return NomenclaturalCode.fromString(getCdmSourceProperty(CdmSourceProperties.NOMENCLATURAL_CODE));
196 183
	}
197 184

  
198 185
	@Override
199 186
	public int getPort() {
200
		String port = CdmUtils.Nz(getDatabaseProperty(DbProperties.PORT));
201
		if ("".equals(port)){
187
		String port = CdmUtils.Nz(getCdmSourceProperty(CdmSourceProperties.PORT));
188
		if (port == null || "".equals(port)){
202 189
			return -1;
203 190
		}else{
204 191
			//TODO exception if non integer
......
209 196

  
210 197
	@Override
211 198
	public String getServer() {
212
		return getDatabaseProperty(DbProperties.SERVER);
199
		return getCdmSourceProperty(CdmSourceProperties.SERVER);
213 200
	}
214 201

  
215 202

  
216 203
	@Override
217 204
	public DatabaseTypeEnum getDatabaseType(){
218
		Element bean = getDatasourceBeanXml(this.dataSourceName);
219
		if (bean == null){
220
			return null;
221
		}else{
222
			Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", "driverClassName");
223
			if (driverProp == null){
224
				logger.warn("Unknown property driverClass");
225
		    	return null;
226
			}else{
227
				String strDriverClass = driverProp.getAttributeValue("value");
228
				DatabaseTypeEnum dbType = DatabaseTypeEnum.getDatabaseEnumByDriverClass(strDriverClass);
229
				return dbType;
230
			}
231
		}
232
	}
233
	
234
	
235
	/**
236
	 * Returns the database type of the data source. 
237
	 * @return the database type of the data source. Null if the bean or the driver class property does not exist or the driver class is unknown.
238
	 */
239
	protected String getDatabaseProperty(DbProperties property){
240
		Element bean = getDatasourceBeanXml(this.dataSourceName);
241
		String url;
242
		String result = null;
243
		if (bean != null){
244
			result = getPropertyValue(bean, property.toString());
245
			if (result == null){  //test if property is database, server or port which are included in the url
246
				url = getPropertyValue(bean, DbProperties.URL.toString());
247
				DatabaseTypeEnum dbTypeEnum = getDatabaseType();
248
				if (dbTypeEnum != null){
249
					IDatabaseType dbType = dbTypeEnum.getDatabaseType();
250
					if (property.equals(DbProperties.DATABASE)){
251
						result = dbType.getDatabaseNameByConnectionString(url);
252
					}else if(property.equals(DbProperties.SERVER)){
253
						result = dbType.getServerNameByConnectionString(url);
254
					}else if(property.equals(DbProperties.PORT)){
255
						result = String.valueOf(dbType.getPortByConnectionString(url));
256
					}else{
257
						logger.debug("Unknown property: " + property);
258
					}
259
				}
260
			}
261
		}
262
		return result;	
205
		String strDriverClass = getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS);
206
		DatabaseTypeEnum dbType = DatabaseTypeEnum.getDatabaseEnumByDriverClass(strDriverClass);
207
		return dbType;
263 208
	}
264 209
	
265
	private String getPropertyValue(Element bean, String property){
266
		Element driverProp = XmlHelp.getFirstAttributedChild(bean, "property", "name", property);
267
		if (driverProp == null){
268
			logger.debug("Unknown property: " + property);
269
	    	return null;
270
		}else{
271
			String strProperty = driverProp.getAttributeValue("value");
272
			return strProperty;
273
		}
274
	}
275 210
	
276 211

  
277

  
278
	/**
279
	 * Returns the list of properties that are defined in the datasource    
280
	 * @return 
281
	 */
282
	@SuppressWarnings("unchecked")
283
	public List<Attribute> getDatasourceAttributes(){
284
		List<Attribute> result = new ArrayList<Attribute>();
285
		Element bean = getDatasourceBeanXml(this.dataSourceName);
286
		if (bean == null){
287
			return null;
288
		}else{
289
			result = bean.getAttributes();
290
		}
291
		return result;
292
	}	
293

  
294
	/**
295
	 * Returns a defined property of the datasource
296
	 * @return the property of the data source. NULL if the datasource bean or the property does not exist.
297
	 */
298
	public String getDatasourceProperty(DbProperties dbProp){
299
		Element bean = getDatasourceBeanXml(this.dataSourceName);
300
		if (bean == null){
301
			return null;
302
		}else{
303
			Element elProperty = XmlHelp.getFirstAttributedChild(bean, "property", "name", dbProp.toString());
304
			if (elProperty == null){
305
				logger.warn("Unknown property: " + dbProp.toString());
306
		    	return null;
307
			}else{
308
				String strValue = elProperty.getAttributeValue("value");
309
				return strValue;
310
			}
311
		}
312
	}
313

  
314 212
	
315
	/**
316
	 * Returns the list of properties that are defined in the datasource    
317
	 * @return 
318
	 */
319
	public Properties getDatasourceProperties(){
320
		Properties result = new Properties();
321
		Element bean = getDatasourceBeanXml(this.dataSourceName);
322
		if (bean == null){
323
			return null;
324
		}else{
325
			List<Element> elProperties = XmlHelp.getAttributedChildList(bean, "property", "name");
326
			Iterator<Element> iterator = elProperties.iterator();
327
			while(iterator.hasNext()){
328
				Element next = iterator.next();
329
				String strName = next.getAttributeValue("name");
330
				String strValue = next.getAttributeValue("value");
331
				result.put(strName, strValue);
332
			}
333
		}
334
		return result;
213
	public String getCdmSourceProperty(CdmSourceProperties property){		
214
		return dataSourceProperties.getProperty(property.toString(),null);
335 215
	}
336
	
337 216
	/**
338 217
	 * Returns a BeanDefinition object of type DataSource that contains
339 218
	 * datsource properties (url, username, password, ...)
......
342 221
	@SuppressWarnings("unchecked")
343 222
	@Override
344 223
	public BeanDefinition getDatasourceBean(){
345
		DatabaseTypeEnum dbtype = DatabaseTypeEnum.getDatabaseEnumByDriverClass(getDatasourceProperty(DbProperties.DRIVER_CLASS));
224
		DatabaseTypeEnum dbtype = 
225
				DatabaseTypeEnum.getDatabaseEnumByDriverClass(getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS));
346 226
		
347 227
		AbstractBeanDefinition bd = new RootBeanDefinition(dbtype.getDataSourceClass());
348 228
		//attributes
349
		Iterator<Attribute> iterator = getDatasourceAttributes().iterator();
229
		Iterator<Attribute> iterator = cdmSourceAttributes.iterator();
350 230
		while(iterator.hasNext()){
351 231
			Attribute attribute = iterator.next();
352 232
			if (attribute.getName().equals("lazy-init")){
......
364 244
		
365 245
		//properties
366 246
		MutablePropertyValues props = new MutablePropertyValues();
367
		Properties persistentProperties = getDatasourceProperties();
368
		Enumeration<String> keys = (Enumeration)persistentProperties.keys(); 
247
		
248
		Enumeration<String> keys = (Enumeration)cdmSourceProperties.keys(); 
369 249
		while (keys.hasMoreElements()){
370 250
			String key = (String)keys.nextElement();
371 251
			
372
			if (key.equals("nomenclaturalCode") && persistentProperties.getProperty(key).equals("ICBN")){
252
			if (key.equals("nomenclaturalCode") && cdmSourceProperties.getProperty(key).equals("ICBN")){
373 253
				//bugfix for old nomenclatural codes, remove if fixed elsewhere, see https://dev.e-taxonomy.eu/trac/ticket/3658
374 254
				props.addPropertyValue(key, NomenclaturalCode.ICNAFP.name());
375 255
			}else{
376
				props.addPropertyValue(key, persistentProperties.getProperty(key));
256
				props.addPropertyValue(key, cdmSourceProperties.getProperty(key));
377 257
			}
378 258
		}
379 259

  
......
443 323
	 * @return true if a datasource with the given name exists in the according datasource config file.
444 324
	 */
445 325
	public static boolean exists(String strDataSourceName){
446
		Element bean = getDatasourceBeanXml(strDataSourceName);
326
		Element bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(strDataSourceName, DATASOURCE_BEAN_POSTFIX);
447 327
		return (bean != null);
448 328
	}
449 329

  
......
488 368
		ICdmDataSource dataSource = new CdmDataSource(databaseTypeEnum, server, database, portNumber, username, password, filePath, mode, code);
489 369
				
490 370
		//root
491
		Element root = getBeansRoot(getDataSourceInputStream());
371
		Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
492 372
		if (root == null){
493 373
			return null;
494 374
		}
495 375
		//bean
496
		Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));
376
		Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", CdmPersistentSourceUtils.getBeanName(strDataSourceName, DATASOURCE_BEAN_POSTFIX));
497 377
		if (bean != null){
498 378
			bean.detach();  //delete old version if necessary
499 379
		}
500
		bean = insertXmlBean(root, getBeanName(strDataSourceName), dataSourceClass.getName());
380
		bean = insertXmlBean(root, CdmPersistentSourceUtils.getBeanName(strDataSourceName, DATASOURCE_BEAN_POSTFIX), dataSourceClass.getName());
501 381
		//attributes
502 382
		bean.setAttribute("lazy-init", "true");
503 383
		if (initMethod != null) {bean.setAttribute("init-method", initMethod);}
......
516 396
		if (code != null) {insertXmlValueProperty(bean, "nomenclaturalCode", code.name());}
517 397
		
518 398
		//save
519
		saveToXml(root.getDocument(), getResourceDirectory(), DATASOURCE_FILE_NAME, format );
399
		saveToXml(root.getDocument(), 
400
				CdmPersistentSourceUtils.getResourceDirectory(), 
401
				CdmPersistentXMLSource.CDMSOURCE_FILE_NAME, 
402
				XmlHelp.prettyFormat );
520 403
		try {
521 404
			return NewInstance(strDataSourceName) ;
522 405
		} catch (DataSourceNotFoundException e) {
......
534 417
	 */
535 418
	public static CdmPersistentDataSource update(String strDataSourceName,
536 419
			ICdmDataSource dataSource) throws DataSourceNotFoundException, IllegalArgumentException{
537
		delete(CdmPersistentDataSource.NewInstance(strDataSourceName));
420
		CdmPersistentSourceUtils.delete(CdmPersistentSourceUtils.getBeanName(strDataSourceName,DATASOURCE_BEAN_POSTFIX));
538 421
		return save(strDataSourceName, dataSource);
539 422
	}
540 423

  
......
555 438
		
556 439
		if(dataSource.getDatabaseType().equals(DatabaseTypeEnum.H2)){
557 440
			Class<? extends DataSource> dataSourceClass =  LocalH2.class;
558
			if(dataSource.getMode() == null){
441
			if(dataSource.getMode() == null) {
559 442
				new IllegalArgumentException("H2 mode not specified");
560
			}
443
			}			
561 444
			return save(
562 445
					strDataSourceName, 
563 446
					dataSource.getDatabaseType(), 
......
600 483
	}
601 484

  
602 485
	private static String getCheckedDataSourceParameter(String parameter) throws IllegalArgumentException{		
603
		if(parameter != null){
486
		if(parameter != null) {
604 487
			return parameter;
605
		}else{
606
			new IllegalArgumentException("Non obsolete paramater was assigned a null value: " + parameter);
607
			return null;
608
		}
609
	}
610

  
611
	/**
612
	 * Deletes a dataSource
613
	 * @param dataSource
614
	 */
615
	public static void delete (CdmPersistentDataSource dataSource){
616
		Element bean = getDatasourceBeanXml(dataSource.getName());
617
		if (bean != null){
618
			Document doc = bean.getDocument();
619
			bean.detach();
620
			saveToXml(doc, getDataSourceOutputStream(), format );
488
		} else {
489
			throw new IllegalArgumentException("Non obsolete paramater was assigned a null value: " + parameter);
621 490
		}
622 491
	}
623 492
	
......
630 499
	static public List<CdmPersistentDataSource> getAllDataSources(){
631 500
		List<CdmPersistentDataSource> dataSources = new ArrayList<CdmPersistentDataSource>();
632 501
		
633
		Element root = getBeansRoot(getDataSourceInputStream());
502
		Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
634 503
		if (root == null){
635 504
			return null;
636 505
		}else{
......
647 516
		return dataSources;
648 517
	}
649 518
	
650
	@Override
651
	public String getUsername(){
652
		return getDatasourceProperty(DbProperties.USERNAME);
653
	}
654
	
655
	@Override
656
	public String getPassword(){
657
		return getDatasourceProperty(DbProperties.PASSWORD);
658
	}
659

  
660

  
661

  
662
	
663
	/**
664
	 * Returns the datasource config file input stream.
665
	 * @return data source config file input stream
666
	 */
667
	static protected FileInputStream getDataSourceInputStream(){
668
		String dir = getResourceDirectory();
669
		File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);
670
		return fileInputStream(file);
671
	}
672
	
673
	
674
	/**
675
	 * Returns the datasource config file outputStream.
676
	 * @return data source config file outputStream
677
	 */
678
	static protected FileOutputStream getDataSourceOutputStream(){
679
		String dir = getResourceDirectory();
680
		File file = new File(dir + File.separator +  DATASOURCE_FILE_NAME);
681
		return fileOutputStream(file);
682
	}
683

  
684
	/**
685
	 * Returns the jdom Element representing the data source bean in the config file.
686
	 * @return
687
	 */
688
	private static Element getDatasourceBeanXml(String strDataSourceName){
689
		FileInputStream inStream = getDataSourceInputStream();
690
		Element root = getBeansRoot(inStream);
691
		if (root == null){
692
			return null;
693
		}else{
694
	    	Element xmlBean = XmlHelp.getFirstAttributedChild(root, "bean", "id", getBeanName(strDataSourceName));
695
			if (xmlBean == null){
696
				//TODO warn or info
697
				logger.debug("Unknown Element 'bean id=" +strDataSourceName + "' ");
698
			}
699
			return xmlBean;
700
		}
701
	}
702
	
703
	// returns the directory containing the resources 
704
	private static String getResourceDirectory(){
705
		try {
706
			File f = CdmApplicationUtils.getWritableResourceDir();
707
			return f.getPath();
708
		} catch (IOException e) {
709
			logger.error(e);
710
			throw new RuntimeException(e);
711
		}
712
	}
713
	
714
	static private FileInputStream fileInputStream(File file){
715
		try {
716
			FileInputStream fis = new FileInputStream(file);
717
			return fis;
718
		} catch (FileNotFoundException e) {
719
			logger.warn("File " + file == null?"null":file.getAbsolutePath() + " does not exist in the file system");
720
			return null;
721
		}
722
	}
723
	
724
	static private FileOutputStream fileOutputStream(File file){
725
		try {
726
			FileOutputStream fos = new FileOutputStream(file);
727
			return fos;
728
		} catch (FileNotFoundException e) {
729
			logger.warn("File " + (file == null?"null":file.getAbsolutePath()) + " does not exist in the file system");
730
			return null;
731
		}
732
	}
733 519
	
734 520
	@Override
735 521
	public boolean equals(Object obj){
......
739 525
			return false;
740 526
		}else{
741 527
			CdmPersistentDataSource dataSource = (CdmPersistentDataSource)obj;
742
			return (this.dataSourceName == dataSource.dataSourceName);
528
			return (getName() == dataSource.getName());
743 529
		}
744 530

  
745 531
	}
746 532
	
747 533
	@Override
748 534
	public String toString(){
749
		if (this.dataSourceName != null){
750
			return dataSourceName;
535
		if (getName() != null){
536
			return getName();
751 537
		}else{
752 538
			return null;
753 539
		}
754 540
	}
755 541

  
542

  
543

  
544

  
756 545
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/ICdmDataSource.java
18 18
import org.hibernate.cache.spi.RegionFactory;
19 19
import org.springframework.beans.factory.config.BeanDefinition;
20 20

  
21
import eu.etaxonomy.cdm.config.ICdmSource;
21 22
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
22 23

  
23
public interface ICdmDataSource extends DataSource {
24
public interface ICdmDataSource  extends DataSource,ICdmSource {
24 25

  
25 26
	/**
26 27
	 * Returns a BeanDefinition object of type  DataSource that contains
......
44 45
	 */
45 46
	public BeanDefinition getHibernatePropertiesBean(DbSchemaValidation hbm2dll, Boolean showSql, Boolean formatSql, Boolean registerSearchListener, Class<? extends RegionFactory> cacheProviderClass);
46 47

  
47
	
48
	/**
49
	 * The name representation of thie Datasource.
50
	 * @return
51
	 */
52
	public String getName();
53
	
54

  
55
	/**
56
	 * @return
57
	 */
58
	public String getServer();
59 48

  
60 49
	/**
61 50
	 * Returns the name of the datasource on the given server.
......
73 62
	/**
74 63
	 * @return
75 64
	 */
76
	public int getPort();
77

  
78
	/**
79
	 * @return
80
	 */
81 65
	public String getFilePath();
82 66

  
83 67
	/**
......
130 114
	public void rollback() throws SQLException;
131 115

  
132 116

  
133
	public NomenclaturalCode getNomenclaturalCode();
134

  
135 117
	/**
136 118
	 * Returns the first value of the first row of a result set.<BR>
137 119
	 * If no row exists in the result set
......
158 140
	 */
159 141
	public DatabaseMetaData getMetaData();
160 142
	
161
	/**
162
	 * 
163
	 */
164
	public void closeOpenConnections();
165 143

  
166
	
167
//
168
//	public void setFilePath(String filePath);
169
//	
170
//	public void setMode(H2Mode mode);
171 144

  
172 145
}
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/database/CdmPersistentDataSourceTest.java
33 33
import org.springframework.beans.PropertyValues;
34 34
import org.springframework.beans.factory.config.BeanDefinition;
35 35

  
36
import eu.etaxonomy.cdm.database.CdmPersistentDataSource.DbProperties;
36
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
37
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
38

  
37 39

  
38 40
/**
39 41
 * @author a.mueller
......
70 72
		String dataSourceString = "tmp";
71 73
		//delete
72 74
		try {
73
			CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
75
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
74 76
		} catch (DataSourceNotFoundException e) {
75 77
			//;
76 78
		}
......
87 89
		String dataSourceString = "tmp";
88 90
		//delete
89 91
		try {
90
			CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
92
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
91 93
		} catch (DataSourceNotFoundException e) {
92 94
			//;
93 95
		}
......
157 159
	 */
158 160
	@Test
159 161
	public void testGetDbProperty() {
160
		assertEquals("com.mysql.jdbc.Driver", dataSource.getDatasourceProperty(DbProperties.DRIVER_CLASS));
162
		assertEquals("com.mysql.jdbc.Driver", dataSource.getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS));
161 163
	}
162 164

  
163 165
	/**
......
272 274
			fail();
273 275
		}
274 276
		assertEquals(databaseType, dataSource.getDatabaseType());
275
		assertEquals(DatabaseTypeEnum.SqlServer2005.getDriverClassName(), loadedDataSource.getDatasourceProperty(DbProperties.DRIVER_CLASS));
276
		assertEquals("jdbc:sqlserver://server:1234;databaseName=database;SelectMethod=cursor", loadedDataSource.getDatasourceProperty(DbProperties.URL));
277
		assertEquals(username, loadedDataSource.getDatasourceProperty(DbProperties.USERNAME));
278
		assertEquals(password, loadedDataSource.getDatasourceProperty(DbProperties.PASSWORD));
277
		assertEquals(DatabaseTypeEnum.SqlServer2005.getDriverClassName(), loadedDataSource.getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS));
278
		assertEquals("jdbc:sqlserver://server:1234;databaseName=database;SelectMethod=cursor", loadedDataSource.getCdmSourceProperty(CdmSourceProperties.URL));
279
		assertEquals(username, loadedDataSource.getCdmSourceProperty(CdmSourceProperties.USERNAME));
280
		assertEquals(password, loadedDataSource.getCdmSourceProperty(CdmSourceProperties.PASSWORD));
279 281
		//delete
280 282
		try {
281
			CdmPersistentDataSource.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
283
			CdmPersistentSourceUtils.delete(CdmPersistentDataSource.NewInstance(dataSourceString));
282 284
		} catch (DataSourceNotFoundException e) {
283 285
			fail();
284 286
		}
......
328 330
	 */
329 331
	@Test
330 332
	public void testGetDataSourceInputStream() {
331
		FileInputStream is = CdmPersistentDataSource.getDataSourceInputStream();
333
		FileInputStream is = CdmPersistentSourceUtils.getCdmSourceInputStream();
332 334
		assertNotNull( is);
333 335
		int firstInput;
334 336
		try {
......
346 348
	@Ignore
347 349
	@Test
348 350
	public void testGetDataSourceOutputStream() {
349
		FileOutputStream os = CdmPersistentDataSource.getDataSourceOutputStream();
351
		FileOutputStream os = CdmPersistentSourceUtils.getCdmSourceOutputStream();
350 352
		assertNotNull(os);
351 353
	}
352 354

  
cdmlib-services/src/test/java/eu/etaxonomy/cdm/test/function/TestDatabase.java
29 29
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
30 30
import eu.etaxonomy.cdm.common.AccountStore;
31 31
import eu.etaxonomy.cdm.common.CdmUtils;
32
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
32 33
import eu.etaxonomy.cdm.database.CdmDataSource;
33 34
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
34 35
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
......
258 259
			NomenclaturalCode loadedCode = loadedDataSource.getNomenclaturalCode();
259 260
			Assert.assertEquals(code, loadedCode);
260 261

  
261
			CdmPersistentDataSource.delete(loadedDataSource);
262
			CdmPersistentSourceUtils.delete(loadedDataSource);
262 263

  
263 264
		} catch (DataSourceNotFoundException e) {
264 265
			// TODO Auto-generated catch block

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)