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.types;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.IOException;
|
14
|
|
15
|
import javax.sql.DataSource;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.hibernate.dialect.Dialect;
|
19
|
import org.hibernate.dialect.H2CorrectedDialect;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
22
|
import eu.etaxonomy.cdm.database.H2Mode;
|
23
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
24
|
import eu.etaxonomy.cdm.database.LocalH2;
|
25
|
|
26
|
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
*
|
30
|
*/
|
31
|
public class H2DatabaseType extends DatabaseTypeBase {
|
32
|
private static final Logger logger = Logger.getLogger(H2DatabaseType.class);
|
33
|
|
34
|
//typeName
|
35
|
private String typeName = "H2 Database";
|
36
|
|
37
|
//class
|
38
|
private String classString = "org.h2.Driver";
|
39
|
|
40
|
//url
|
41
|
private String urlString = "jdbc:h2:";
|
42
|
|
43
|
//path
|
44
|
@SuppressWarnings("unused")
|
45
|
private String path = getDefaultPath();
|
46
|
|
47
|
//port
|
48
|
private int defaultPort = 9092;
|
49
|
|
50
|
//hibernate dialect
|
51
|
private Dialect hibernateDialect = new H2CorrectedDialect();
|
52
|
|
53
|
//init method
|
54
|
private String initMethod = "init";
|
55
|
|
56
|
//destroy method
|
57
|
private String destroyMethod = "destroy";
|
58
|
|
59
|
//connection String
|
60
|
public String getConnectionString(ICdmDataSource ds, int port){
|
61
|
H2Mode mode = ds.getMode();
|
62
|
String path = ds.getFilePath();
|
63
|
if (path == null){
|
64
|
path = getDefaultPath();
|
65
|
}
|
66
|
if (mode.equals(H2Mode.IN_MEMORY)){
|
67
|
return urlString + "mem:";
|
68
|
}else if (mode.equals(H2Mode.EMBEDDED)){
|
69
|
return urlString + "file:" + path + "/" + ds.getDatabase();
|
70
|
}else if (mode.equals(H2Mode.TCP)){
|
71
|
return urlString + "tcp://" + ds.getServer() + ":" + port + "/" + path + "/" + ds.getDatabase();
|
72
|
}else{
|
73
|
logger.warn("Unrecognized mode for Database H2");
|
74
|
return null;
|
75
|
}
|
76
|
}
|
77
|
|
78
|
|
79
|
|
80
|
|
81
|
/* (non-Javadoc)
|
82
|
* @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getServerNameByConnectionString(java.lang.String)
|
83
|
*/
|
84
|
@Override
|
85
|
public String getServerNameByConnectionString(String connectionString) {
|
86
|
String result;
|
87
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
88
|
result = null;
|
89
|
}else if (connectionString.startsWith("tcp://")){
|
90
|
String prefix = "tcp://";
|
91
|
String dbSeparator = "/";
|
92
|
result = getServerNameByConnectionString(connectionString, prefix, dbSeparator);
|
93
|
}else if (connectionString.startsWith("mem:")){
|
94
|
result = null;
|
95
|
}else{
|
96
|
logger.warn("Unknown conncection string format");
|
97
|
result = null;
|
98
|
}
|
99
|
return result;
|
100
|
}
|
101
|
|
102
|
|
103
|
|
104
|
|
105
|
/* (non-Javadoc)
|
106
|
* @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getDatabaseNameByConnectionString(java.lang.String)
|
107
|
*/
|
108
|
@Override
|
109
|
public String getDatabaseNameByConnectionString(String connectionString) {
|
110
|
int pos = -1;
|
111
|
String result;
|
112
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
113
|
pos = connectionString.lastIndexOf("/");
|
114
|
result = connectionString.substring(pos + 1);
|
115
|
}else if (connectionString.startsWith("tcp://")){
|
116
|
pos = connectionString.lastIndexOf("/");
|
117
|
result = connectionString.substring(pos + 1);
|
118
|
}else if (connectionString.startsWith("mem:")){
|
119
|
return null;
|
120
|
}else{
|
121
|
logger.warn("Unknown conncection string format");
|
122
|
return null;
|
123
|
}
|
124
|
return result;
|
125
|
}
|
126
|
|
127
|
/* (non-Javadoc)
|
128
|
* @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getPortByConnectionString(java.lang.String)
|
129
|
*/
|
130
|
@Override
|
131
|
public int getPortByConnectionString(String connectionString) {
|
132
|
int result;
|
133
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
134
|
result = -1;
|
135
|
}else if (connectionString.startsWith("tcp://")){
|
136
|
String prefix = "tcp://";
|
137
|
String dbSeparator = "/";
|
138
|
result = getPortByConnectionString(connectionString, prefix, dbSeparator);
|
139
|
}else if (connectionString.startsWith("mem:")){
|
140
|
result = -1;
|
141
|
}else{
|
142
|
logger.warn("Unknown conncection string format");
|
143
|
result = -1;
|
144
|
}
|
145
|
return result;
|
146
|
}
|
147
|
|
148
|
|
149
|
public H2DatabaseType() {
|
150
|
init (typeName, classString, urlString, defaultPort, hibernateDialect );
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public Class<? extends DataSource> getDataSourceClass() {
|
155
|
return LocalH2.class;
|
156
|
}
|
157
|
|
158
|
/* (non-Javadoc)
|
159
|
* @see eu.etaxonomy.cdm.database.types.IDatabaseType#getInitMethod()
|
160
|
*/
|
161
|
@Override
|
162
|
public String getInitMethod() {
|
163
|
return initMethod;
|
164
|
}
|
165
|
|
166
|
/* (non-Javadoc)
|
167
|
* @see eu.etaxonomy.cdm.database.types.IDatabaseType#getDestroyMethod()
|
168
|
*/
|
169
|
@Override
|
170
|
public String getDestroyMethod() {
|
171
|
return destroyMethod;
|
172
|
}
|
173
|
|
174
|
private static final String getDefaultPath(){
|
175
|
try{
|
176
|
File path = CdmApplicationUtils.getWritableResourceDir();
|
177
|
String subPath = File.separator + "h2" + File.separator + "LocalH2";
|
178
|
return path + subPath;
|
179
|
}catch(IOException e){
|
180
|
logger.error(e);
|
181
|
throw new RuntimeException(e);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
}
|