diff options
Diffstat (limited to 'plugins')
15 files changed, 547 insertions, 75 deletions
diff --git a/plugins/org.eclipse.osee.demo.db.connection/support/osee.demo.db.connection.xml b/plugins/org.eclipse.osee.demo.db.connection/support/osee.demo.db.connection.xml index f86f76b5b8c..e4b6d4df76a 100644 --- a/plugins/org.eclipse.osee.demo.db.connection/support/osee.demo.db.connection.xml +++ b/plugins/org.eclipse.osee.demo.db.connection/support/osee.demo.db.connection.xml @@ -1,68 +1,70 @@ -<DbConnection>
-
- <DatabaseInfo id="h2.db.info">
- <DatabaseHome key="#DBHOME#" value="~/h2/"/>
- <DatabaseName key="#DBNAME#" value="osee.h2.db" />
- <DatabaseType key="#TYPE#" value="h2" />
- <Prefix key="#PREFIX#" value="jdbc:h2" />
- <UserName key="#USERNAME#" value="OSEE" />
- <Password key="#PASSWORD#" value="OSEE" />
- <Host key="#HOST#" value="@AvailableDbServices.hostAddress" />
- <Port key="#PORT#" value="@AvailableDbServices.port" />
- </DatabaseInfo>
-
- <ConnectionDescription id="h2.db.connection">
- <Driver>org.h2.Driver</Driver>
- <Url>#PREFIX#:tcp://#HOST#:#PORT#/#DBHOME##DBNAME#;MODE=PostgreSQL;IGNORECASE=TRUE</Url>
- </ConnectionDescription>
-
- <DatabaseInfo id="DerbyServer">
- <DatabaseHome key="#DBHOME#" />
- <DatabaseName key="#DBNAME#" value="DerbyDatabase" />
- <DatabaseType key="#TYPE#" value="derby" />
- <Prefix key="#PREFIX#" value="jdbc:derby" />
- <UserName key="#USERNAME#" value="osee" />
- <Password key="#PASSWORD#" value="oseeadmin" />
- <Host key="#HOST#" value="@AvailableDbServices.hostAddress" />
- <Port key="#PORT#" value="@AvailableDbServices.port" />
- </DatabaseInfo>
-
- <DatabaseInfo id="postgresqlInfo">
- <DatabaseHome key="#DBHOME#" />
- <DatabaseName key="#DBNAME#" value="OSEE" />
- <DatabaseType key="#TYPE#" value="postgresql" />
- <Prefix key="#PREFIX#" value="jdbc:postgresql" />
- <UserName key="#USERNAME#" value="osee" />
- <Password key="#PASSWORD#" value="osee" />
- <Host key="#HOST#" value="@AvailableDbServices.hostAddress" />
- <Port key="#PORT#" value="@AvailableDbServices.port" />
- </DatabaseInfo>
-
- <ConnectionDescription id="postgresql">
- <Driver>org.postgresql.Driver</Driver>
- <Url>#PREFIX#://#HOST#:#PORT#/#DBHOME##DBNAME#</Url>
- </ConnectionDescription>
-
- <ConnectionDescription id="NetDerbyClient">
- <Driver>org.apache.derby.jdbc.ClientDriver</Driver>
- <Url>#PREFIX#://#HOST#:#PORT#/#DBHOME##DBNAME#;</Url>
- <UrlAttributes>
- <Entry>create=true</Entry>
- </UrlAttributes>
- </ConnectionDescription>
-
- <AvailableDbServices>
-
- <Server id="h2" dbInfo="h2.db.info" hostAddress="127.0.0.1" port="1621" connectsWith="h2.db.connection" />
-
- <Server id="derby" dbInfo="DerbyServer" hostAddress="127.0.0.1" port="1621" connectsWith="NetDerbyClient" />
-
- <Server id="osee.microdoc" dbInfo="postgresqlInfo" hostAddress="78.46.71.230"
- port="5432" connectsWith="postgresql" applicationServer="http://osee.microdoc.com:8089/" />
-
- <Server id="postgresqlLocalhost" dbInfo="postgresqlInfo"
- hostAddress="localhost" port="5432" connectsWith="postgresql"
- applicationServer="http://localhost:8089/" />
-
- </AvailableDbServices>
-</DbConnection>
+<DbConnection> + + <DatabaseInfo id="h2.db.info"> + <DatabaseHome key="#DBHOME#" value="~/h2/" /> + <DatabaseName key="#DBNAME#" value="osee.h2.db" /> + <DatabaseType key="#TYPE#" value="h2" /> + <Prefix key="#PREFIX#" value="jdbc:h2" /> + <UserName key="#USERNAME#" value="osee" /> + <Password key="#PASSWORD#" value="oseeadmin" /> + <Host key="#HOST#" value="@AvailableDbServices.hostAddress" /> + <Port key="#PORT#" value="@AvailableDbServices.port" /> + </DatabaseInfo> + + <ConnectionDescription id="h2.db.connection"> + <Driver>org.h2.Driver</Driver> + <Url>#PREFIX#:tcp://#HOST#:#PORT#/#DBHOME##DBNAME#;IGNORECASE=TRUE;SCHEMA_SEARCH_PATH=OSEE, PUBLIC</Url> + </ConnectionDescription> + + <DatabaseInfo id="DerbyServer"> + <DatabaseHome key="#DBHOME#" /> + <DatabaseName key="#DBNAME#" value="DerbyDatabase" /> + <DatabaseType key="#TYPE#" value="derby" /> + <Prefix key="#PREFIX#" value="jdbc:derby" /> + <UserName key="#USERNAME#" value="osee" /> + <Password key="#PASSWORD#" value="oseeadmin" /> + <Host key="#HOST#" value="@AvailableDbServices.hostAddress" /> + <Port key="#PORT#" value="@AvailableDbServices.port" /> + </DatabaseInfo> + + <DatabaseInfo id="postgresqlInfo"> + <DatabaseHome key="#DBHOME#" /> + <DatabaseName key="#DBNAME#" value="OSEE" /> + <DatabaseType key="#TYPE#" value="postgresql" /> + <Prefix key="#PREFIX#" value="jdbc:postgresql" /> + <UserName key="#USERNAME#" value="osee" /> + <Password key="#PASSWORD#" value="osee" /> + <Host key="#HOST#" value="@AvailableDbServices.hostAddress" /> + <Port key="#PORT#" value="@AvailableDbServices.port" /> + </DatabaseInfo> + + <ConnectionDescription id="postgresql"> + <Driver>org.postgresql.Driver</Driver> + <Url>#PREFIX#://#HOST#:#PORT#/#DBHOME##DBNAME#</Url> + </ConnectionDescription> + + <ConnectionDescription id="NetDerbyClient"> + <Driver>org.apache.derby.jdbc.ClientDriver</Driver> + <Url>#PREFIX#://#HOST#:#PORT#/#DBHOME##DBNAME#;</Url> + <UrlAttributes> + <Entry>create=true</Entry> + </UrlAttributes> + </ConnectionDescription> + + <AvailableDbServices> + + <Server id="h2" dbInfo="h2.db.info" hostAddress="127.0.0.1" + port="1621" connectsWith="h2.db.connection" /> + + <Server id="derby" dbInfo="DerbyServer" hostAddress="127.0.0.1" + port="1621" connectsWith="NetDerbyClient" /> + + <Server id="osee.microdoc" dbInfo="postgresqlInfo" hostAddress="78.46.71.230" + port="5432" connectsWith="postgresql" applicationServer="http://osee.microdoc.com:8089/" /> + + <Server id="postgresqlLocalhost" dbInfo="postgresqlInfo" + hostAddress="localhost" port="5432" connectsWith="postgresql" + applicationServer="http://localhost:8089/" /> + + </AvailableDbServices> +</DbConnection> diff --git a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/operations/CreateSchemaOperation.java b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/operations/CreateSchemaOperation.java index 211a1add070..a4d46bb7839 100644 --- a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/operations/CreateSchemaOperation.java +++ b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/operations/CreateSchemaOperation.java @@ -41,18 +41,20 @@ public class CreateSchemaOperation extends AbstractDbTxOperation { @Override protected void doTxWork(IProgressMonitor monitor, OseeConnection connection) throws OseeCoreException { DatabaseMetaData metaData = connection.getMetaData(); + SupportedDatabase dbType = SupportedDatabase.getDatabaseType(metaData); SqlManager sqlManager = SqlFactory.getSqlManager(metaData); SchemaSqlUtil dbInit = new SchemaSqlUtil(sqlManager); Set<String> schemas = userSchema.keySet(); + dbInit.dropIndices(schemas, userSchema, dbSchema); dbInit.dropTables(schemas, userSchema, dbSchema); - if (SupportedDatabase.isDatabaseType(metaData, SupportedDatabase.postgresql)) { + + if (dbType == SupportedDatabase.postgresql || dbType == SupportedDatabase.h2) { dbInit.dropSchema(schemas); dbInit.createSchema(schemas); } dbInit.addTables(schemas, userSchema); dbInit.addIndices(schemas, userSchema); } - } diff --git a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2DataType.java b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2DataType.java new file mode 100644 index 00000000000..d9f74bf1ce5 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2DataType.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.core.datastore.schema.sql; + +/** + * @author Roberto E. Escobar + */ +public class H2DataType extends SqlDataType { + + public H2DataType() { + super(); + } + + @Override + public String getBooleanType() { + return "boolean"; + } + + @Override + public String getBitType() { + return "tinyint"; + } + + @Override + public String getIntegerType() { + return "int"; + } + + @Override + public String getDecimalType() { + return "decimal"; + } + + @Override + public String getFloatType() { + return "double"; + } + + @Override + public String getRealType() { + return "real"; + } + + @Override + public String getDoubleType() { + return "double"; + } + + @Override + public String getDateType() { + return "date"; + } + + @Override + public String getCharType() { + return "char"; + } + + @Override + public String getVarCharType() { + return "varchar"; + } + + @Override + public String getSmallIntType() { + return "smallint"; + } + + @Override + protected String getClobType() { + return "clob"; + } + + @Override + protected String getTimestamp() { + return "timestamp"; + } + + @Override + protected String getTime() { + return "time"; + } + + @Override + protected String getBlobType() { + return "blob"; + } + + @Override + protected String getBigInt() { + return "bigint"; + } + + @Override + protected String getLongVarCharType() { + return "varchar"; + } + + @Override + protected String getNumericType() { + return "numeric"; + } + +} diff --git a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2SqlManager.java b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2SqlManager.java new file mode 100644 index 00000000000..c5505c5d821 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/H2SqlManager.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.core.datastore.schema.sql; + +import org.eclipse.osee.framework.core.datastore.schema.data.TableElement; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.database.core.ConnectionHandler; + +/** + * @author Roberto E. Escobar + */ +public class H2SqlManager extends SqlManagerImpl { + + public H2SqlManager(SqlDataType sqlDataType) { + super(sqlDataType); + } + + @Override + public void createSchema(String schema) throws OseeCoreException { + ConnectionHandler.runPreparedUpdate(String.format("%s SCHEMA IF NOT EXISTS \"%s\"", CREATE_STRING, + schema.toUpperCase())); + } + + @Override + public void dropSchema(String schema) throws OseeCoreException { + ConnectionHandler.runPreparedUpdate(String.format("%s SCHEMA IF EXISTS \"%s\"", DROP_STRING, schema.toUpperCase())); + } + + @Override + public void dropIndex(TableElement tableDef) { + // Do Nothing -- Indexes are dropped during table drop + } + +} diff --git a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/SqlFactory.java b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/SqlFactory.java index 612aac946c3..bbc751a5a85 100644 --- a/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/SqlFactory.java +++ b/plugins/org.eclipse.osee.framework.core.datastore/src/org/eclipse/osee/framework/core/datastore/schema/sql/SqlFactory.java @@ -45,6 +45,9 @@ public class SqlFactory { case postgresql: instance = new PostgreSqlManager(new PostgresqlDataType()); break; + case h2: + instance = new H2SqlManager(new H2DataType()); + break; default: break; } diff --git a/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/ConnectionHandler.java b/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/ConnectionHandler.java index 2c122d64d76..4e37a2d7cc9 100644 --- a/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/ConnectionHandler.java +++ b/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/ConnectionHandler.java @@ -155,11 +155,19 @@ public final class ConnectionHandler { * Cause constraint checking to be deferred until the end of the current transaction. */ public static void deferConstraintChecking(OseeConnection connection) throws OseeCoreException { - if (SupportedDatabase.getDatabaseType(connection.getMetaData()) == SupportedDatabase.derby) { - return; + SupportedDatabase dbType = SupportedDatabase.getDatabaseType(connection.getMetaData()); + switch (dbType) { + case derby: + // Derby Does not support deferring constraints + break; + case h2: + runPreparedUpdate(connection, "SET REFERENTIAL_INTEGRITY = FALSE"); + break; + default: + // NOTE: this must be a PreparedStatement to play correctly with DB Transactions. + runPreparedUpdate(connection, "SET CONSTRAINTS ALL DEFERRED"); + break; } - // NOTE: this must be a PreparedStatement to play correctly with DB Transactions. - runPreparedUpdate(connection, "SET CONSTRAINTS ALL DEFERRED"); } public static DatabaseMetaData getMetaData() throws OseeCoreException { diff --git a/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/SupportedDatabase.java b/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/SupportedDatabase.java index 618f6668608..589b794486a 100644 --- a/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/SupportedDatabase.java +++ b/plugins/org.eclipse.osee.framework.database/src/org/eclipse/osee/framework/database/core/SupportedDatabase.java @@ -17,6 +17,7 @@ import org.eclipse.osee.framework.core.exception.OseeDataStoreException; import org.eclipse.osee.framework.core.exception.OseeExceptions; public enum SupportedDatabase { + h2, oracle, derby, foxpro, @@ -28,7 +29,9 @@ public enum SupportedDatabase { try { String dbName = metaData.getDatabaseProductName(); String lowerCaseName = dbName.toLowerCase(); - if (lowerCaseName.contains(SupportedDatabase.derby.toString())) { + if (lowerCaseName.contains(SupportedDatabase.h2.toString())) { + toReturn = SupportedDatabase.h2; + } else if (lowerCaseName.contains(SupportedDatabase.derby.toString())) { toReturn = SupportedDatabase.derby; } else if (lowerCaseName.contains(SupportedDatabase.oracle.toString())) { toReturn = SupportedDatabase.oracle; diff --git a/plugins/org.eclipse.osee.framework.h2/.classpath b/plugins/org.eclipse.osee.framework.h2/.classpath new file mode 100644 index 00000000000..ad32c83a788 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/plugins/org.eclipse.osee.framework.h2/.project b/plugins/org.eclipse.osee.framework.h2/.project new file mode 100644 index 00000000000..074edb12524 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/.project @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.osee.framework.h2</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/plugins/org.eclipse.osee.framework.h2/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.framework.h2/META-INF/MANIFEST.MF new file mode 100644 index 00000000000..7ea69c5459c --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/META-INF/MANIFEST.MF @@ -0,0 +1,21 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: H2 Db Connector Plug-in (Incubation) +Bundle-SymbolicName: org.eclipse.osee.framework.h2;singleton:=true +Bundle-Version: 0.9.6.qualifier +Bundle-ActivationPolicy: lazy +Import-Package: + org.eclipse.osee.framework.database.core, + org.eclipse.osee.framework.jdk.core.type, + org.eclipse.osee.framework.jdk.core.util, + org.eclipse.osee.framework.logging, + org.h2, + org.h2.jdbc, + org.h2.server, + org.h2.tools, + org.h2.util, + org.osgi.framework +Export-Package: org.eclipse.osee.framework.h2 +Service-Component: OSGI-INF/h2.client.connection.driver.provider.xml +Bundle-Vendor: Eclipse Open System Engineering Environment +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 diff --git a/plugins/org.eclipse.osee.framework.h2/OSGI-INF/h2.client.connection.driver.provider.xml b/plugins/org.eclipse.osee.framework.h2/OSGI-INF/h2.client.connection.driver.provider.xml new file mode 100644 index 00000000000..322f9bad26e --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/OSGI-INF/h2.client.connection.driver.provider.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?>
+<component name="h2.client.connection.driver.provider">
+ <implementation class="org.eclipse.osee.framework.h2.H2ClientConnection"/>
+ <service>
+ <provide interface="org.eclipse.osee.framework.database.core.IConnectionFactory"/>
+ </service>
+</component>
diff --git a/plugins/org.eclipse.osee.framework.h2/build.properties b/plugins/org.eclipse.osee.framework.h2/build.properties new file mode 100644 index 00000000000..c58ea2178c3 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ diff --git a/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2ClientConnection.java b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2ClientConnection.java new file mode 100644 index 00000000000..0d3d2a629b4 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2ClientConnection.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.h2; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Properties; +import org.eclipse.osee.framework.database.core.IConnectionFactory; +import org.eclipse.osee.framework.jdk.core.type.Pair; +import org.eclipse.osee.framework.jdk.core.util.OseeProperties; + +/** + * @author Roberto E. Escobar + */ +public class H2ClientConnection implements IConnectionFactory { + + private static final String driver = "org.h2.Driver"; + private boolean firstTime = true; + + @Override + public Connection getConnection(Properties properties, String connectionURL) throws Exception { + Class.forName(driver); + + if (firstTime) { + firstTime = false; + Pair<String, Integer> addressAndPort = OseeProperties.getDerbyServerAddress(); + if (addressAndPort != null) { + H2DbServer.startServer(addressAndPort.getFirst(), addressAndPort.getSecond()); + } + } + Connection connection = DriverManager.getConnection(connectionURL, properties); + return connection; + } + + @Override + public String getDriver() { + return driver; + } + +} diff --git a/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServer.java b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServer.java new file mode 100644 index 00000000000..0ef17a03a88 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServer.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ + +package org.eclipse.osee.framework.h2; + +import java.net.InetAddress; +import java.util.logging.Level; +import org.eclipse.osee.framework.logging.OseeLog; + +/** + * @author Roberto E. Escobar + */ +public class H2DbServer { + protected static Object keepAlive; + private H2DbServerUtil nwServer; + private static final H2DbServer instance = new H2DbServer(); + + private H2DbServer() { + this.nwServer = null; + } + + public static void startServer(String host, int port) throws Exception { + instance.startServerInternal(host, port); + } + + private void startServerInternal(String host, int port) throws Exception { + try { + OseeLog.log(H2DbServer.class, Level.INFO, "Starting H2 Database Server ...."); + nwServer = new H2DbServerUtil(InetAddress.getByName(host), port); + nwServer.start(); + + if (isConnectionAvailable()) { + nwServer.printInfo(); + addShutdownHook(); + // stayAlive(); + } else { + OseeLog.log(H2DbServer.class, Level.INFO, "Exiting, since unable to connect to Derby Network Server."); + OseeLog.log(H2DbServer.class, Level.INFO, + "Please try to increase the amount of time to keep trying to connect to the Server."); + } + } catch (Exception ex) { + OseeLog.log(H2DbServer.class, Level.SEVERE, ex); + } + } + + // private void stayAlive() { + // keepAlive = new Object(); + // synchronized (keepAlive) { + // try { + // keepAlive.wait(); + // } catch (InterruptedException e) { + // OseeLog.log(H2DbServer.class, Level.SEVERE, e.getMessage(), e); + // } + // } + // } + + // private void commitSuicide() { + // if (keepAlive != null) { + // synchronized (keepAlive) { + // keepAlive.notify(); + // } + // } + // } + + private void shutdown() { + OseeLog.log(H2DbServer.class, Level.INFO, "Shutting down H2 Database server..."); + nwServer.shutdown(); + OseeLog.log(H2DbServer.class, Level.INFO, "Server down."); + // commitSuicide(); + } + + private void addShutdownHook() { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + OseeLog.log(H2DbServer.class, Level.INFO, "Shutting down"); + shutdown(); + } + }); + } + + private boolean isConnectionAvailable() throws InterruptedException { + boolean knowIfServerUp = false; + int numTimes = 5; + + while (!knowIfServerUp && numTimes > 0) { + try { + numTimes--; + nwServer.testForConnection(); + knowIfServerUp = true; + } catch (Exception e) { + OseeLog.log(H2DbServer.class, Level.SEVERE, + "Unable to obtain a connection to network server, trying again after 3000 ms.", e); + Thread.sleep(3000); + } + } + return knowIfServerUp; + } + +} diff --git a/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServerUtil.java b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServerUtil.java new file mode 100644 index 00000000000..3c4f3ab53dd --- /dev/null +++ b/plugins/org.eclipse.osee.framework.h2/src/org/eclipse/osee/framework/h2/H2DbServerUtil.java @@ -0,0 +1,71 @@ +package org.eclipse.osee.framework.h2;
+
+import java.io.PrintStream;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.logging.Level;
+import org.eclipse.osee.framework.logging.OseeLog;
+import org.h2.tools.Server;
+
+public final class H2DbServerUtil {
+
+ private final Collection<Server> serverControls = new ArrayList<Server>();
+
+ public H2DbServerUtil(InetAddress host, int port) {
+ try {
+ String[] webArgs = new String[] {"-webAllowOthers", "true", "-webPort", String.valueOf(port - 1)};
+ serverControls.add(Server.createWebServer(webArgs));
+
+ String[] dbArgs = new String[] {"-tcp", "-tcpAllowOthers", "true", "-tcpPort", String.valueOf(port)};
+ serverControls.add(Server.createTcpServer(dbArgs));
+
+ OseeLog.log(H2DbServer.class, Level.INFO, "H2 Database Server created");
+ } catch (Exception e) {
+ OseeLog.log(H2DbServer.class, Level.SEVERE, "Error Initializing Server Control.", e);
+ }
+
+ for (Server server : serverControls) {
+ server.setOut(new PrintStream(System.out, true));
+ }
+ }
+
+ public void testForConnection() throws Exception {
+ for (Server server : serverControls) {
+ server.isRunning(true);
+ }
+ }
+
+ public void shutdown() {
+ for (Server server : serverControls) {
+ try {
+ server.shutdown();
+ } catch (Exception e) {
+ OseeLog.log(H2DbServer.class, Level.SEVERE, e.getMessage(), e);
+ }
+ }
+ }
+
+ public void start() {
+ for (Server server : serverControls) {
+ try {
+ server.start();
+ } catch (Exception e) {
+ OseeLog.log(H2DbServer.class, Level.SEVERE, e.getMessage(), e);
+ }
+ }
+ }
+
+ public void printInfo() {
+ try {
+ StringBuilder builder = new StringBuilder();
+ builder.append("H2 Database: ");
+ for (Server server : serverControls) {
+ builder.append(server.getStatus());
+ }
+ OseeLog.log(H2DbServer.class, Level.INFO, builder.toString());
+ } catch (Exception ex) {
+ OseeLog.log(H2DbServer.class, Level.SEVERE, "Error getting Server Information", ex);
+ }
+ }
+}
\ No newline at end of file |