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 final String typeName = "H2 Database";
|
36
|
|
37
|
//class
|
38
|
private final String classString = "org.h2.Driver";
|
39
|
|
40
|
//url
|
41
|
private final String urlString = "jdbc:h2:";
|
42
|
|
43
|
//port
|
44
|
private final int defaultPort = 9092;
|
45
|
|
46
|
//hibernate dialect
|
47
|
private final Dialect hibernateDialect = new H2CorrectedDialect();
|
48
|
|
49
|
//init method
|
50
|
private final String initMethod = "init";
|
51
|
|
52
|
//destroy method
|
53
|
private final String destroyMethod = "destroy";
|
54
|
|
55
|
//connection String
|
56
|
@Override
|
57
|
public String getConnectionString(ICdmDataSource ds, int port){
|
58
|
H2Mode mode = ds.getMode();
|
59
|
String path = ds.getFilePath();
|
60
|
if (path == null){
|
61
|
path = getDefaultPath();
|
62
|
}
|
63
|
if (mode.equals(H2Mode.IN_MEMORY)){
|
64
|
return urlString + "mem:";
|
65
|
}else if (mode.equals(H2Mode.EMBEDDED)){
|
66
|
return urlString + "file:" + path + "/" + ds.getDatabase();
|
67
|
}else if (mode.equals(H2Mode.TCP)){
|
68
|
return urlString + "tcp://" + ds.getServer() + ":" + port + "/" + path + "/" + ds.getDatabase();
|
69
|
}else{
|
70
|
logger.warn("Unrecognized mode for Database H2");
|
71
|
return null;
|
72
|
}
|
73
|
}
|
74
|
|
75
|
|
76
|
@Override
|
77
|
public String getServerNameByConnectionString(String connectionString) {
|
78
|
String result;
|
79
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
80
|
result = null;
|
81
|
}else if (connectionString.startsWith("tcp://")){
|
82
|
String prefix = "tcp://";
|
83
|
String dbSeparator = "/";
|
84
|
result = getServerNameByConnectionString(connectionString, prefix, dbSeparator);
|
85
|
}else if (connectionString.startsWith("mem:")){
|
86
|
result = null;
|
87
|
}else{
|
88
|
logger.warn("Unknown conncection string format");
|
89
|
result = null;
|
90
|
}
|
91
|
return result;
|
92
|
}
|
93
|
|
94
|
|
95
|
@Override
|
96
|
public String getDatabaseNameByConnectionString(String connectionString) {
|
97
|
int pos = -1;
|
98
|
String result;
|
99
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
100
|
pos = connectionString.lastIndexOf("/");
|
101
|
result = connectionString.substring(pos + 1);
|
102
|
}else if (connectionString.startsWith("tcp://")){
|
103
|
pos = connectionString.lastIndexOf("/");
|
104
|
result = connectionString.substring(pos + 1);
|
105
|
}else if (connectionString.startsWith("mem:")){
|
106
|
return null;
|
107
|
}else{
|
108
|
logger.warn("Unknown conncection string format");
|
109
|
return null;
|
110
|
}
|
111
|
return result;
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public int getPortByConnectionString(String connectionString) {
|
116
|
int result;
|
117
|
if (connectionString.startsWith("file:") || connectionString.startsWith( urlString + "file:")){
|
118
|
result = -1;
|
119
|
}else if (connectionString.startsWith("tcp://")){
|
120
|
String prefix = "tcp://";
|
121
|
String dbSeparator = "/";
|
122
|
result = getPortByConnectionString(connectionString, prefix, dbSeparator);
|
123
|
}else if (connectionString.startsWith("mem:")){
|
124
|
result = -1;
|
125
|
}else{
|
126
|
logger.warn("Unknown conncection string format");
|
127
|
result = -1;
|
128
|
}
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
|
133
|
public H2DatabaseType() {
|
134
|
init (typeName, classString, urlString, defaultPort, hibernateDialect );
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
public Class<? extends DataSource> getDataSourceClass() {
|
139
|
return LocalH2.class;
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public String getInitMethod() {
|
144
|
return initMethod;
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public String getDestroyMethod() {
|
149
|
return destroyMethod;
|
150
|
}
|
151
|
|
152
|
private static final String getDefaultPath(){
|
153
|
try{
|
154
|
File path = CdmApplicationUtils.getWritableResourceDir();
|
155
|
String subPath = File.separator + "h2" + File.separator + "LocalH2";
|
156
|
return path + subPath;
|
157
|
}catch(IOException e){
|
158
|
logger.error(e);
|
159
|
throw new RuntimeException(e);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
|
164
|
}
|