2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
.types
;
13 import java
.io
.IOException
;
15 import javax
.sql
.DataSource
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.hibernate
.dialect
.Dialect
;
19 import org
.hibernate
.dialect
.H2CorrectedDialect
;
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
;
31 public class H2DatabaseType
extends DatabaseTypeBase
{
32 private static final Logger logger
= Logger
.getLogger(H2DatabaseType
.class);
35 private String typeName
= "H2 Database";
38 private String classString
= "org.h2.Driver";
41 private String urlString
= "jdbc:h2:";
44 @SuppressWarnings("unused")
45 private String path
= getDefaultPath();
48 private int defaultPort
= 9092;
51 private Dialect hibernateDialect
= new H2CorrectedDialect();
54 private String initMethod
= "init";
57 private String destroyMethod
= "destroy";
60 public String
getConnectionString(ICdmDataSource ds
, int port
){
61 H2Mode mode
= ds
.getMode();
62 String path
= ds
.getFilePath();
64 path
= getDefaultPath();
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();
73 logger
.warn("Unrecognized mode for Database H2");
82 * @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getServerNameByConnectionString(java.lang.String)
85 public String
getServerNameByConnectionString(String connectionString
) {
87 if (connectionString
.startsWith("file:") || connectionString
.startsWith( urlString
+ "file:")){
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:")){
96 logger
.warn("Unknown conncection string format");
106 * @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getDatabaseNameByConnectionString(java.lang.String)
109 public String
getDatabaseNameByConnectionString(String connectionString
) {
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:")){
121 logger
.warn("Unknown conncection string format");
128 * @see eu.etaxonomy.cdm.database.types.DatabaseTypeBase#getPortByConnectionString(java.lang.String)
131 public int getPortByConnectionString(String connectionString
) {
133 if (connectionString
.startsWith("file:") || connectionString
.startsWith( urlString
+ "file:")){
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:")){
142 logger
.warn("Unknown conncection string format");
149 public H2DatabaseType() {
150 init (typeName
, classString
, urlString
, defaultPort
, hibernateDialect
);
154 public Class
<?
extends DataSource
> getDataSourceClass() {
155 return LocalH2
.class;
159 * @see eu.etaxonomy.cdm.database.types.IDatabaseType#getInitMethod()
162 public String
getInitMethod() {
167 * @see eu.etaxonomy.cdm.database.types.IDatabaseType#getDestroyMethod()
170 public String
getDestroyMethod() {
171 return destroyMethod
;
174 private static final String
getDefaultPath(){
176 File path
= CdmApplicationUtils
.getWritableResourceDir();
177 String subPath
= File
.separator
+ "h2" + File
.separator
+ "LocalH2";
178 return path
+ subPath
;
179 }catch(IOException e
){
181 throw new RuntimeException(e
);