diff options
author | Roberto E. Escobar | 2014-09-05 01:13:19 +0000 |
---|---|---|
committer | Ryan T. Baldwin | 2014-09-05 01:13:19 +0000 |
commit | 2355b5921e06566c1117d5eb24fbab6c6e1b6f2a (patch) | |
tree | e6e2e03f46c09ff4e840287a9e4dcab7d4554f86 /plugins/org.eclipse.osee.hsqldb | |
parent | b02c87dd2d7b995a5dbde0b2696f1dc92b520e11 (diff) | |
download | org.eclipse.osee-2355b5921e06566c1117d5eb24fbab6c6e1b6f2a.tar.gz org.eclipse.osee-2355b5921e06566c1117d5eb24fbab6c6e1b6f2a.tar.xz org.eclipse.osee-2355b5921e06566c1117d5eb24fbab6c6e1b6f2a.zip |
feature[ats_ATS105410]: Use Slf4j for HsqlDb logging
Change-Id: Ieaf75da596a42c0fe5c962dd0f24f7ab3ca3bf38
Diffstat (limited to 'plugins/org.eclipse.osee.hsqldb')
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/META-INF/MANIFEST.MF | 2 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.client.connection.driver.provider.xml | 7 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.connection.factory.xml | 8 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.server.manager.xml | 8 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HsqlServerManager.java | 33 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlDbServer.java | 106 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlServerMgr.java | 182 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlClientConnection.java (renamed from plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlClientConnection.java) | 31 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlServerManagerImpl.java | 389 |
9 files changed, 460 insertions, 306 deletions
diff --git a/plugins/org.eclipse.osee.hsqldb/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.hsqldb/META-INF/MANIFEST.MF index 82287242e45..85c827d38fa 100644 --- a/plugins/org.eclipse.osee.hsqldb/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.hsqldb/META-INF/MANIFEST.MF @@ -8,7 +8,7 @@ Import-Package: org.eclipse.osee.framework.core.data, org.eclipse.osee.framework.jdk.core.type, org.eclipse.osee.framework.jdk.core.util, org.eclipse.osee.framework.jdk.core.util.network, - org.eclipse.osee.framework.logging, + org.eclipse.osee.logger, org.hsqldb, org.hsqldb.jdbc, org.hsqldb.persist, diff --git a/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.client.connection.driver.provider.xml b/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.client.connection.driver.provider.xml deleted file mode 100644 index e60dbc30f9f..00000000000 --- a/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.client.connection.driver.provider.xml +++ /dev/null @@ -1,7 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.osee.hsqldb.internal.HyperSqlClientConnection"> - <implementation class="org.eclipse.osee.hsqldb.HyperSqlClientConnection"/> - <service> - <provide interface="org.eclipse.osee.framework.database.core.IConnectionFactory"/> - </service> -</scr:component> diff --git a/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.connection.factory.xml b/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.connection.factory.xml new file mode 100644 index 00000000000..db23c5efede --- /dev/null +++ b/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.connection.factory.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" > + <implementation class="org.eclipse.osee.hsqldb.internal.HsqlClientConnection"/> + <service> + <provide interface="org.eclipse.osee.framework.database.core.IConnectionFactory"/> + </service> + <reference bind="setHsqlServerManager" cardinality="1..1" interface="org.eclipse.osee.hsqldb.HsqlServerManager" name="HsqlServerManager" policy="static"/> +</scr:component> diff --git a/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.server.manager.xml b/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.server.manager.xml new file mode 100644 index 00000000000..bb7460e5ede --- /dev/null +++ b/plugins/org.eclipse.osee.hsqldb/OSGI-INF/hsqldb.server.manager.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop"> + <implementation class="org.eclipse.osee.hsqldb.internal.HsqlServerManagerImpl"/> + <service> + <provide interface="org.eclipse.osee.hsqldb.HsqlServerManager"/> + </service> + <reference bind="setLogger" cardinality="1..1" interface="org.eclipse.osee.logger.Log" name="Log" policy="static"/> +</scr:component> diff --git a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HsqlServerManager.java b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HsqlServerManager.java new file mode 100644 index 00000000000..33a04f28597 --- /dev/null +++ b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HsqlServerManager.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2014 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.hsqldb; + +import java.util.Properties; +import org.eclipse.osee.framework.core.data.IDatabaseInfo; + +/** + * @author Roberto E. Escobar + */ +public interface HsqlServerManager { + + String asConnectionUrl(String url, Properties props); + + String startServer(String host, int port, int webPort, IDatabaseInfo dbInfo) throws Exception; + + boolean stopServerWithWait(String dbId); + + Iterable<String> getIds(); + + boolean isRunning(String dbId); + + void printInfo(String dbId); + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlDbServer.java b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlDbServer.java deleted file mode 100644 index 4c7e072dc47..00000000000 --- a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlDbServer.java +++ /dev/null @@ -1,106 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 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.hsqldb; - -import java.net.InetAddress; -import java.util.logging.Level; -import org.eclipse.osee.framework.core.data.IDatabaseInfo; -import org.eclipse.osee.framework.logging.OseeLog; - -/** - * @author Mark Joy - * @author Roberto E. Escobar - */ -public final class HyperSqlDbServer { - private static final int CHECK_ALIVE_NUMBER_OF_TIMES = 5; - - private static HyperSqlServerMgr nwServer = new HyperSqlServerMgr(); - - private HyperSqlDbServer() { - } - - public static void stopServer(String dbId) { - shutdown(dbId); - } - - public static boolean stopServerWithWait(String dbId) { - shutdown(dbId); - return isShutdown(dbId); - - } - - public static String startServer(String host, int port, int webPort, IDatabaseInfo dbInfo) throws Exception { - OseeLog.logf(HyperSqlDbServer.class, Level.INFO, - "Starting HyperSQL Database Server on [%s:%s] with webserver on [%s,%s]....", host, port, host, webPort); - String dbId = ""; - try { - dbId = nwServer.createServerInstance(InetAddress.getByName(host), port, webPort, dbInfo); - - if (isConnectionAvailable(dbId)) { - nwServer.printInfo(dbId); - } else { - OseeLog.log(HyperSqlDbServer.class, Level.INFO, - "Exiting, since unable to connect to HyperSQL Network Server."); - OseeLog.log(HyperSqlDbServer.class, Level.INFO, - "Please try to increase the amount of time to keep trying to connect to the Server."); - } - } catch (Exception ex) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, ex); - } - return dbId; - } - - private static void shutdown(String dbId) { - OseeLog.log(HyperSqlDbServer.class, Level.INFO, "Shutting down HyperSQL Database server..."); - nwServer.shutdown(dbId); - OseeLog.log(HyperSqlDbServer.class, Level.INFO, "Server down."); - } - - private static boolean isConnectionAvailable(String dbId) throws InterruptedException { - boolean knowIfServerUp = false; - int numTimes = CHECK_ALIVE_NUMBER_OF_TIMES; - - while (!knowIfServerUp && numTimes > 0) { - try { - numTimes--; - nwServer.testForConnection(dbId); - knowIfServerUp = true; - } catch (Exception e) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, - "Unable to obtain a connection to network server, trying again after 3000 ms.", e); - Thread.sleep(3000); - } - } - return knowIfServerUp; - } - - private static boolean isShutdown(String dbId) { - boolean isDead = false; - int numTimes = CHECK_ALIVE_NUMBER_OF_TIMES; - while (!isDead && numTimes > 0) { - try { - numTimes--; - nwServer.testNotRunning(dbId); - isDead = true; - } catch (Exception e) { - isDead = false; - try { - Thread.sleep(3000); - } catch (InterruptedException ex) { - // Do nothing - } - } - } - return isDead; - } - -} diff --git a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlServerMgr.java b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlServerMgr.java deleted file mode 100644 index 2e46e6a1cbc..00000000000 --- a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlServerMgr.java +++ /dev/null @@ -1,182 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 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.hsqldb; - -import java.io.IOException; -import java.io.PrintWriter; -import java.net.InetAddress; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; -import org.eclipse.osee.framework.core.data.IDatabaseInfo; -import org.eclipse.osee.framework.jdk.core.type.Pair; -import org.eclipse.osee.framework.logging.OseeLog; -import org.hsqldb.Database; -import org.hsqldb.persist.HsqlProperties; -import org.hsqldb.server.Server; -import org.hsqldb.server.ServerAcl.AclFormatException; - -/** - * @author Mark Joy - */ -public final class HyperSqlServerMgr { - - private static final String sc_key_remote_open_db = "server.remote_open"; - - private final Map<String, Pair<Server, Thread>> serverControls = - new ConcurrentHashMap<String, Pair<Server, Thread>>(); - - public HyperSqlServerMgr() { - } - - public String createServerInstance(InetAddress host, int dbPort, int webPort, IDatabaseInfo dbInfo) throws IOException, AclFormatException { - Server hsqlServer = new Server(); - hsqlServer.setAddress(host.getHostAddress()); - hsqlServer.setPort(dbPort); - - StringBuilder dbCreationUrl = new StringBuilder(); - dbCreationUrl.append(dbInfo.getDatabaseHome()); - - Properties connProps = dbInfo.getConnectionProperties(); - appendProperties(dbCreationUrl, connProps); - - hsqlServer.setDatabaseName(0, dbInfo.getDatabaseName()); - hsqlServer.setDatabasePath(0, dbCreationUrl.toString()); - - HsqlProperties props = new HsqlProperties(); - props.setProperty(sc_key_remote_open_db, true); - hsqlServer.setProperties(props); - - OseeLog.logf(HyperSqlDbServer.class, Level.INFO, "HyperSQL Database Server created on [%s:%s]", host, dbPort); - - hsqlServer.setLogWriter(new PrintWriter(System.out, true)); - try { - hsqlServer.start(); - } catch (Exception e) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, e.getMessage(), e); - } - - Thread shutdownHook = addShutdownHook(dbInfo.getDatabaseHome()); - Pair<Server, Thread> serverPair = new Pair<Server, Thread>(hsqlServer, shutdownHook); - serverControls.put(dbInfo.getDatabaseHome(), serverPair); - - return dbInfo.getDatabaseHome(); - } - - public void testForConnection(String dbId) throws Exception { - Pair<Server, Thread> entry = serverControls.get(dbId); - if (entry != null) { - try { - Server server = entry.getFirst(); - server.checkRunning(true); - } catch (Exception e) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, e.getMessage(), e); - } - } - } - - public void shutdown(String dbId) { - Pair<Server, Thread> entry = serverControls.remove(dbId); - if (entry != null) { - try { - Thread second = entry.getSecond(); - removeShutdownHook(second); - - Server first = entry.getFirst(); - first.shutdownWithCatalogs(Database.CLOSEMODE_NORMAL); - } catch (Exception e) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, e.getMessage(), e); - } - } - } - - private Thread addShutdownHook(final String dbId) { - Thread retThread = new Thread() { - @Override - public void run() { - OseeLog.log(HyperSqlDbServer.class, Level.INFO, "Shutting down"); - shutdown(dbId); - } - }; - Runtime.getRuntime().addShutdownHook(retThread); - return retThread; - } - - private void removeShutdownHook(Thread shutdownHook) { - if (shutdownHook != null) { - Runtime.getRuntime().removeShutdownHook(shutdownHook); - } - } - - public void testNotRunning(String dbId) throws Exception { - Pair<Server, Thread> entry = serverControls.get(dbId); - if (entry != null) { - try { - Server server = entry.getFirst(); - server.checkRunning(false); - } catch (Exception e) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, e.getMessage(), e); - } - } - } - - public void printInfo(String dbId) { - try { - StringBuilder builder = new StringBuilder(); - builder.append("HyperSQL Database: "); - Pair<Server, Thread> entry = serverControls.get(dbId); - if (entry != null) { - Server server = entry.getFirst(); - builder.append("\n\tAddress : "); - builder.append(server.getAddress()); - builder.append("\n\tDatabase Name : "); - builder.append(server.getDatabaseName(0, true)); - builder.append("\n\tDatabasePath : "); - builder.append(server.getDatabasePath(0, true)); - builder.append("\n\tPort : "); - builder.append(server.getPort()); - builder.append("\n\tDefaultWebPage : "); - builder.append(server.getDefaultWebPage()); - builder.append("\n\tProductName : "); - builder.append(server.getProductName()); - builder.append("\n\tProductVersion : "); - builder.append(server.getProductVersion()); - builder.append("\n\tProtocol : "); - builder.append(server.getProtocol()); - builder.append("\n\tServerId : "); - builder.append(server.getServerId()); - builder.append("\n\tState : "); - builder.append(server.getState()); - builder.append("\n\tStateDescriptor : "); - builder.append(server.getStateDescriptor()); - builder.append("\n\tWebRoot : "); - builder.append(server.getWebRoot()); - } else { - builder.append("\n\tDatabase not found for id: "); - builder.append(dbId); - } - OseeLog.log(HyperSqlDbServer.class, Level.INFO, builder.toString()); - } catch (Exception ex) { - OseeLog.log(HyperSqlDbServer.class, Level.SEVERE, "Error getting Server Information", ex); - } - } - - public static void appendProperties(StringBuilder builder, Properties props) { - if (!props.isEmpty()) { - for (Entry<Object, Object> entry : props.entrySet()) { - builder.append(String.format(";%s=%s", entry.getKey(), entry.getValue())); - } - } - } - -}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlClientConnection.java b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlClientConnection.java index 056abfdc019..d2ae2a323db 100644 --- a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/HyperSqlClientConnection.java +++ b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlClientConnection.java @@ -1,4 +1,14 @@ -package org.eclipse.osee.hsqldb; +/******************************************************************************* + * Copyright (c) 2013 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.hsqldb.internal; import java.sql.Connection; import java.sql.DriverManager; @@ -9,16 +19,23 @@ 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; import org.eclipse.osee.framework.jdk.core.util.network.PortUtil; +import org.eclipse.osee.hsqldb.HsqlServerManager; /** * @author Roberto E. Escobar */ -public class HyperSqlClientConnection implements IConnectionFactory { +public class HsqlClientConnection implements IConnectionFactory { private static final String JDBC_DRIVER = "org.hsqldb.jdbc.JDBCDriver"; private final AtomicBoolean firstTime = new AtomicBoolean(true); + private HsqlServerManager dbServer; + + public void setHsqlServerManager(HsqlServerManager dbServer) { + this.dbServer = dbServer; + } + @Override public Connection getConnection(IDatabaseInfo dbInfo) throws Exception { Class.forName(JDBC_DRIVER); @@ -30,17 +47,11 @@ public class HyperSqlClientConnection implements IConnectionFactory { if (webPort < 0) { webPort = PortUtil.getInstance().getValidPort(); } - HyperSqlDbServer.startServer(addressAndPort.getFirst(), addressAndPort.getSecond(), webPort, dbInfo); + dbServer.startServer(addressAndPort.getFirst(), addressAndPort.getSecond(), webPort, dbInfo); } } - Properties props = dbInfo.getConnectionProperties(); - - StringBuilder builder = new StringBuilder(); - builder.append(dbInfo.getConnectionUrl()); - HyperSqlServerMgr.appendProperties(builder, props); - - String url = builder.toString(); + String url = dbServer.asConnectionUrl(dbInfo.getConnectionUrl(), dbInfo.getConnectionProperties()); return DriverManager.getConnection(url, props); } diff --git a/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlServerManagerImpl.java b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlServerManagerImpl.java new file mode 100644 index 00000000000..43731304d14 --- /dev/null +++ b/plugins/org.eclipse.osee.hsqldb/src/org/eclipse/osee/hsqldb/internal/HsqlServerManagerImpl.java @@ -0,0 +1,389 @@ +/******************************************************************************* + * Copyright (c) 2013 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.hsqldb.internal; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.osee.framework.core.data.IDatabaseInfo; +import org.eclipse.osee.framework.jdk.core.type.Pair; +import org.eclipse.osee.hsqldb.HsqlServerManager; +import org.eclipse.osee.logger.Log; +import org.hsqldb.Database; +import org.hsqldb.persist.HsqlProperties; +import org.hsqldb.server.Server; +import org.hsqldb.server.ServerAcl.AclFormatException; + +/** + * @author Mark Joy + */ +public final class HsqlServerManagerImpl implements HsqlServerManager { + + private static final int CHECK_ALIVE_NUMBER_OF_TIMES = 5; + private static final String sc_key_remote_open_db = "server.remote_open"; + + private Map<String, Pair<Server, Thread>> serverControls; + + private Log logger; + + public void setLogger(Log logger) { + this.logger = logger; + } + + public void start() { + serverControls = new ConcurrentHashMap<String, Pair<Server, Thread>>(); + } + + public void stop() { + if (serverControls != null && !serverControls.isEmpty()) { + for (String dbId : serverControls.keySet()) { + try { + shutdown(dbId); + } catch (Exception ex) { + // do nothing + } + } + serverControls.clear(); + serverControls = null; + } + } + + @Override + public Iterable<String> getIds() { + return serverControls.keySet(); + } + + @Override + public String startServer(String host, int port, int webPort, IDatabaseInfo dbInfo) throws Exception { + logger.info("Starting HyperSQL Database Server on [%s:%s] with webserver on [%s,%s]....", host, port, host, + webPort); + String dbId = ""; + try { + dbId = createServerInstance(InetAddress.getByName(host), port, webPort, dbInfo); + + if (isConnectionAvailable(dbId)) { + printInfo(dbId); + } else { + logger.info("Exiting, since unable to connect to HyperSQL Network Server."); + logger.info("Please try to increase the amount of time to keep trying to connect to the Server."); + } + } catch (Exception ex) { + logger.error(ex, "Error starting Hsql server"); + } + return dbId; + } + + @Override + public boolean stopServerWithWait(String dbId) { + logger.info("Shutting down HyperSQL Database server [%s]", dbId); + shutdown(dbId); + boolean dead = isShutdown(dbId); + logger.info("Server [%s] - state[%s]", dbId, dead ? "STOPPED" : "RUNNING"); + return dead; + } + + private String createServerInstance(InetAddress host, int dbPort, int webPort, IDatabaseInfo dbInfo) throws IOException, AclFormatException { + Server hsqlServer = new Server(); + hsqlServer.setErrWriter(newErrorWriter(logger)); + hsqlServer.setLogWriter(newDebugWriter(logger)); + + hsqlServer.setAddress(host.getHostAddress()); + hsqlServer.setPort(dbPort); + + StringBuilder dbCreationUrl = new StringBuilder(); + dbCreationUrl.append(dbInfo.getDatabaseHome()); + + Properties connProps = dbInfo.getConnectionProperties(); + appendProperties(dbCreationUrl, connProps); + + hsqlServer.setDatabaseName(0, dbInfo.getDatabaseName()); + hsqlServer.setDatabasePath(0, dbCreationUrl.toString()); + + HsqlProperties props = new HsqlProperties(); + props.setProperty(sc_key_remote_open_db, true); + hsqlServer.setProperties(props); + + logger.info("HyperSQL Database Server created on [%s:%s]", host, dbPort); + + String dbId = dbInfo.getDatabaseHome(); + try { + hsqlServer.start(); + } catch (Exception ex) { + logger.error(ex, "Error starting hsql server"); + } finally { + ShutdownHook hook = new ShutdownHook(dbId); + Runtime.getRuntime().addShutdownHook(hook); + serverControls.put(dbId, new Pair<Server, Thread>(hsqlServer, hook)); + } + return dbId; + } + + private synchronized void shutdown(String dbId) { + if (serverControls != null) { + Pair<Server, Thread> entry = serverControls.remove(dbId); + if (entry != null) { + Thread hook = entry.getSecond(); + if (hook != null) { + try { + Runtime.getRuntime().removeShutdownHook(hook); + } catch (Exception ex) { + // do nothing; + } + } + + try { + Server first = entry.getFirst(); + first.shutdownWithCatalogs(Database.CLOSEMODE_NORMAL); + } catch (Exception ex) { + logger.error(ex, "Error during shutdown"); + } + } + } + } + + @Override + public void printInfo(String dbId) { + try { + StringBuilder builder = new StringBuilder(); + builder.append("HyperSQL Database: "); + Pair<Server, Thread> entry = serverControls.get(dbId); + if (entry != null) { + Server server = entry.getFirst(); + builder.append("\n\tAddress : "); + builder.append(server.getAddress()); + builder.append("\n\tDatabase Name : "); + builder.append(server.getDatabaseName(0, true)); + builder.append("\n\tDatabasePath : "); + builder.append(server.getDatabasePath(0, true)); + builder.append("\n\tPort : "); + builder.append(server.getPort()); + builder.append("\n\tDefaultWebPage : "); + builder.append(server.getDefaultWebPage()); + builder.append("\n\tProductName : "); + builder.append(server.getProductName()); + builder.append("\n\tProductVersion : "); + builder.append(server.getProductVersion()); + builder.append("\n\tProtocol : "); + builder.append(server.getProtocol()); + builder.append("\n\tServerId : "); + builder.append(server.getServerId()); + builder.append("\n\tState : "); + builder.append(server.getState()); + builder.append("\n\tStateDescriptor : "); + builder.append(server.getStateDescriptor()); + builder.append("\n\tWebRoot : "); + builder.append(server.getWebRoot()); + } else { + builder.append("\n\tDatabase not found for id: "); + builder.append(dbId); + } + logger.info(builder.toString()); + } catch (Exception ex) { + logger.error(ex, "Error getting Server Information"); + } + } + + @Override + public boolean isRunning(String dbId) { + boolean result = false; + if (serverControls != null) { + Pair<Server, Thread> entry = serverControls.get(dbId); + if (entry != null) { + try { + Server server = entry.getFirst(); + server.checkRunning(true); + result = true; + } catch (Exception ex) { + // do nothing; + } + } + } + return result; + } + + private boolean isConnectionAvailable(String dbId) throws InterruptedException { + boolean knowIfServerUp = false; + int numTimes = CHECK_ALIVE_NUMBER_OF_TIMES; + + while (!knowIfServerUp && numTimes > 0) { + try { + numTimes--; + knowIfServerUp = isRunning(dbId); + } catch (Exception e) { + Thread.sleep(3000); + } + } + return knowIfServerUp; + } + + private boolean isShutdown(String dbId) { + boolean isDead = false; + int numTimes = CHECK_ALIVE_NUMBER_OF_TIMES; + while (!isDead && numTimes > 0) { + try { + numTimes--; + isDead = !isRunning(dbId); + } catch (Exception e) { + isDead = false; + try { + Thread.sleep(3000); + } catch (InterruptedException ex) { + // Do nothing + } + } + } + return isDead; + } + + @Override + public String asConnectionUrl(String url, Properties props) { + StringBuilder builder = new StringBuilder(); + builder.append(url); + appendProperties(builder, props); + return builder.toString(); + } + + private static void appendProperties(StringBuilder builder, Properties props) { + if (!props.isEmpty()) { + for (Entry<Object, Object> entry : props.entrySet()) { + builder.append(String.format(";%s=%s", entry.getKey(), entry.getValue())); + } + } + } + + private static PrintWriter newErrorWriter(Log logger) { + PrintWriter toReturn = null; + if (logger != null) { + toReturn = new LogPrintWriter(logger, true); + } + return toReturn; + } + + private static PrintWriter newDebugWriter(Log logger) { + PrintWriter toReturn = null; + if (logger != null) { + toReturn = new LogPrintWriter(logger, false); + } + return toReturn; + } + + private class ShutdownHook extends Thread { + + private final String dbId; + + public ShutdownHook(String dbId) { + this.dbId = dbId; + } + + @Override + public void run() { + try { + shutdown(dbId); + } catch (Exception ex) { + // do nothing + } + } + + } + + private static class LogPrintWriter extends PrintWriter { + + private final StringBuilder builder = new StringBuilder(); + + private final Log log; + private final boolean logToError; + + public LogPrintWriter(Log log, boolean logToError) { + super(new NoOpWriter()); + this.log = log; + this.logToError = logToError; + } + + @Override + public void close() { + flushBuffer(); + super.close(); + } + + @Override + public void flush() { + flushBuffer(); + super.flush(); + } + + @Override + public void write(int c) { + builder.append(c); + } + + @Override + public void write(char cbuf[], int off, int len) { + builder.append(cbuf, off, len); + } + + @Override + public void write(String str, int off, int len) { + builder.append(str.substring(off, off + len)); + } + + @Override + public void println() { + if (log == null) { + builder.append('\n'); + } + flushBuffer(); + } + + private void flushBuffer() { + if (builder.length() == 0) { + return; + } + if (log != null) { + if (logToError) { + log.error(builder.toString()); + } else { + log.debug(builder.toString()); + } + } else { + try { + out.write(builder.toString()); + } catch (IOException e) { + this.setError(); + } + } + builder.setLength(0); + } + + private static class NoOpWriter extends Writer { + + @Override + public void close() { + // do nothing + } + + @Override + public void flush() { + // do nothing + } + + @Override + public void write(char cbuf[], int off, int len) { + // do nothing + } + } + + } +}
\ No newline at end of file |