1
|
/**
|
2
|
* Copyright (C) 2015 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
|
package eu.etaxonomy.taxeditor.remoting.server;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.IOException;
|
13
|
|
14
|
import org.apache.commons.io.FileUtils;
|
15
|
import org.apache.commons.lang.StringEscapeUtils;
|
16
|
|
17
|
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
18
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
|
19
|
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
|
20
|
|
21
|
/**
|
22
|
* @author cmathew
|
23
|
* @date 11 Nov 2015
|
24
|
*
|
25
|
*/
|
26
|
public class CDMServerUtils {
|
27
|
|
28
|
|
29
|
public static String convertEditorToServerConfig() {
|
30
|
StringBuilder configStringBuilder = new StringBuilder();
|
31
|
configStringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + System.lineSeparator());
|
32
|
configStringBuilder.append("<!-- DO NOT EDIT THIS FILE MANUALLY. -->");
|
33
|
configStringBuilder.append("<!-- It is created by the Taxonomic Editor for launching a managed CDM Server. -->");
|
34
|
configStringBuilder.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"" + System.lineSeparator());
|
35
|
configStringBuilder.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + System.lineSeparator());
|
36
|
configStringBuilder.append(" xmlns:tx=\"http://www.springframework.org/schema/tx\"" + System.lineSeparator());
|
37
|
configStringBuilder.append(" xmlns:context=\"http://www.springframework.org/schema/context\"" + System.lineSeparator());
|
38
|
configStringBuilder.append(" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" + System.lineSeparator());
|
39
|
configStringBuilder.append(" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" + System.lineSeparator());
|
40
|
configStringBuilder.append(" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" + System.lineSeparator());
|
41
|
configStringBuilder.append("\">" + System.lineSeparator());
|
42
|
configStringBuilder.append(" <bean id=\"dataSourceProperties\" class=\"eu.etaxonomy.cdm.remote.config.DataSourceProperties\">" + System.lineSeparator());
|
43
|
configStringBuilder.append(" <property name=\"propsMap\">" + System.lineSeparator());
|
44
|
configStringBuilder.append(" <map/>" + System.lineSeparator());
|
45
|
configStringBuilder.append(" </property>" + System.lineSeparator());
|
46
|
configStringBuilder.append(" </bean>" + System.lineSeparator());
|
47
|
|
48
|
for(CdmPersistentDataSource dataSource : CdmPersistentDataSource.getAllDataSources()) {
|
49
|
String beanId = xmlNCNamefrom(dataSource.getName());
|
50
|
String user = dataSource.getUsername();
|
51
|
String password = dataSource.getPassword();
|
52
|
String driverClass = dataSource.getCdmSourceProperty(CdmSourceProperties.DRIVER_CLASS);
|
53
|
String jdbcUrl = StringEscapeUtils.escapeXml(dataSource.getCdmSourceProperty(CdmSourceProperties.URL));
|
54
|
configStringBuilder.append(" <bean id=\"" + beanId + "\" lazy-init=\"true\" class=\"com.mchange.v2.c3p0.ComboPooledDataSource\">" + System.lineSeparator());
|
55
|
configStringBuilder.append(" <property name=\"driverClass\" value=\"" + driverClass + "\"/>" + System.lineSeparator());
|
56
|
configStringBuilder.append(" <property name=\"user\" value=\"" + user + "\"/>" + System.lineSeparator());
|
57
|
configStringBuilder.append(" <property name=\"password\" value=\"" + password + "\"/>" + System.lineSeparator());
|
58
|
configStringBuilder.append(" <property name=\"jdbcUrl\" value=\"" + jdbcUrl + "\"/>" + System.lineSeparator());
|
59
|
configStringBuilder.append(" </bean>" + System.lineSeparator());
|
60
|
|
61
|
}
|
62
|
configStringBuilder.append("</beans>" + System.lineSeparator());
|
63
|
|
64
|
return configStringBuilder.toString();
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Assures that the datasource name is converted into a valid XML type NCName.
|
69
|
*
|
70
|
* Prefixes the string and replaces all forbidden characters by '_'.
|
71
|
*
|
72
|
* @param dataSource
|
73
|
* @return
|
74
|
*/
|
75
|
public static String xmlNCNamefrom(String beanName) {
|
76
|
return "MGD_" + beanName.replaceAll("[^a-zA-Z0-9]", "_");
|
77
|
}
|
78
|
|
79
|
public static File writeManagedServerConfig(String config, String fileName) throws IOException {
|
80
|
File managedConfigFile = new File(CdmApplicationUtils.getWritableResourceDir(), fileName);
|
81
|
if(managedConfigFile.exists()) {
|
82
|
managedConfigFile.delete();
|
83
|
}
|
84
|
if(managedConfigFile.createNewFile()) {
|
85
|
FileUtils.writeStringToFile(managedConfigFile, config);
|
86
|
}
|
87
|
return managedConfigFile;
|
88
|
}
|
89
|
|
90
|
}
|