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.app.common;
|
11
|
|
12
|
import java.lang.reflect.InvocationTargetException;
|
13
|
import java.lang.reflect.Method;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.AccountStore;
|
18
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
19
|
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
|
20
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
21
|
|
22
|
public class CdmDestinations {
|
23
|
@SuppressWarnings("unused")
|
24
|
private static Logger logger = Logger.getLogger(CdmDestinations.class);
|
25
|
|
26
|
|
27
|
public static ICdmDataSource cdm_test_local_mysql(){
|
28
|
DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
|
29
|
String cdmServer = "127.0.0.1";
|
30
|
String cdmDB = "cdm_test";
|
31
|
String cdmUserName = "edit";
|
32
|
return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
|
33
|
}
|
34
|
|
35
|
public static ICdmDataSource cdm_test_local_xper(){
|
36
|
DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
|
37
|
String cdmServer = "127.0.0.1";
|
38
|
String cdmDB = "xper";
|
39
|
String cdmUserName = "edit";
|
40
|
return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
|
41
|
}
|
42
|
|
43
|
|
44
|
|
45
|
public static ICdmDataSource cdm_local_postgres_CdmTest(){
|
46
|
DatabaseTypeEnum dbType = DatabaseTypeEnum.PostgreSQL;
|
47
|
String cdmServer = "127.0.0.1";
|
48
|
String cdmDB = "CdmTest";
|
49
|
String cdmUserName = "edit";
|
50
|
return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
|
51
|
}
|
52
|
|
53
|
public static ICdmDataSource NULL(){
|
54
|
return null;
|
55
|
}
|
56
|
|
57
|
|
58
|
public static ICdmDataSource localH2(){
|
59
|
return CdmDataSource.NewH2EmbeddedInstance("cdm", "sa", "");
|
60
|
}
|
61
|
|
62
|
public static ICdmDataSource localH2Xper(){
|
63
|
return CdmDataSource.NewH2EmbeddedInstance("xper", "sa", "");
|
64
|
}
|
65
|
|
66
|
public static ICdmDataSource localH2(String database, String username, String filePath){
|
67
|
return CdmDataSource.NewH2EmbeddedInstance(database, "sa", "", filePath, null);
|
68
|
}
|
69
|
|
70
|
|
71
|
/**
|
72
|
* initializes source
|
73
|
* TODO only supports MySQL and PostgreSQL
|
74
|
*
|
75
|
* @param dbType
|
76
|
* @param cdmServer
|
77
|
* @param cdmDB
|
78
|
* @param port
|
79
|
* @param cdmUserName
|
80
|
* @param pwd
|
81
|
* @return
|
82
|
*/
|
83
|
private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType, String cdmServer, String cdmDB, int port, String cdmUserName, String pwd ){
|
84
|
//establish connection
|
85
|
pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName, pwd);
|
86
|
ICdmDataSource destination;
|
87
|
if(dbType.equals(DatabaseTypeEnum.MySQL)){
|
88
|
destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB, port, cdmUserName, pwd, null);
|
89
|
} else if(dbType.equals(DatabaseTypeEnum.PostgreSQL)){
|
90
|
destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB, port, cdmUserName, pwd, null);
|
91
|
} else {
|
92
|
//TODO others
|
93
|
throw new RuntimeException("Unsupported DatabaseType");
|
94
|
}
|
95
|
return destination;
|
96
|
|
97
|
}
|
98
|
|
99
|
|
100
|
/**
|
101
|
* Accepts a string array and tries to find a method returning an ICdmDataSource with
|
102
|
* the name of the given first string in the array
|
103
|
*
|
104
|
* @param args
|
105
|
* @return
|
106
|
*/
|
107
|
public static ICdmDataSource chooseDestination(String[] args) {
|
108
|
if(args == null)
|
109
|
return null;
|
110
|
|
111
|
if(args.length != 1)
|
112
|
return null;
|
113
|
|
114
|
String possibleDestination = args[0];
|
115
|
|
116
|
Method[] methods = CdmDestinations.class.getMethods();
|
117
|
|
118
|
for (Method method : methods){
|
119
|
if(method.getName().equals(possibleDestination)){
|
120
|
try {
|
121
|
return (ICdmDataSource) method.invoke(null, null);
|
122
|
} catch (IllegalArgumentException e) {
|
123
|
// TODO Auto-generated catch block
|
124
|
e.printStackTrace();
|
125
|
} catch (IllegalAccessException e) {
|
126
|
// TODO Auto-generated catch block
|
127
|
e.printStackTrace();
|
128
|
} catch (InvocationTargetException e) {
|
129
|
// TODO Auto-generated catch block
|
130
|
e.printStackTrace();
|
131
|
}
|
132
|
}
|
133
|
}
|
134
|
return null;
|
135
|
}
|
136
|
|
137
|
}
|
138
|
|