diff --git a/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/ConnectionProvider.java b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/ConnectionProvider.java index f39517d3d6..a6ca6b49c8 100644 --- a/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/ConnectionProvider.java +++ b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/ConnectionProvider.java @@ -41,7 +41,6 @@ import com.orientechnologies.orient.core.db.document.ODatabaseDocumentPool; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; import com.orientechnologies.orient.server.OServer; import com.orientechnologies.orient.server.OServerMain; -import com.orientechnologies.orient.server.config.OServerConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,7 +52,6 @@ import sonia.scm.SCMContext; import java.io.Closeable; import java.io.File; -import java.io.IOException; import java.net.URL; @@ -130,7 +128,8 @@ public class ConnectionProvider directory); } - OServer server = OServerMain.create(); + server = OServerMain.create(); + URL configUrl = Resources.getResource(EMBEDDED_CONFIGURATION); String config = Resources.toString(configUrl, Charset.defaultCharset()); @@ -192,7 +191,26 @@ public class ConnectionProvider { if (connectionPool != null) { - connectionPool.close(); + try + { + connectionPool.close(); + } + catch (Exception ex) + { + logger.error("could not close connection pool", ex); + } + } + + if (server != null) + { + try + { + server.shutdown(); + } + catch (Exception ex) + { + logger.error("shutdown of orientdb server failed", ex); + } } } @@ -241,4 +259,7 @@ public class ConnectionProvider /** Field description */ private ODatabaseDocumentPool connectionPool; + + /** Field description */ + private OServer server; } diff --git a/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBModule.java b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBModule.java index 47882cb48b..a5fc9b6c26 100644 --- a/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBModule.java +++ b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBModule.java @@ -34,9 +34,14 @@ package sonia.scm.orientdb; //~--- non-JDK imports -------------------------------------------------------- import com.google.inject.AbstractModule; +import com.google.inject.multibindings.Multibinder; import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; +//~--- JDK imports ------------------------------------------------------------ + +import javax.servlet.ServletContextListener; + /** * * @author Sebastian Sdorra @@ -52,5 +57,11 @@ public class OrientDBModule extends AbstractModule protected void configure() { bind(ODatabaseDocumentTx.class).toProvider(ConnectionProvider.class); + + Multibinder servletContextListenerBinder = + Multibinder.newSetBinder(binder(), ServletContextListener.class); + + servletContextListenerBinder.addBinding().to( + OrientDBServletContextListener.class); } } diff --git a/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBServletContextListener.java b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBServletContextListener.java new file mode 100644 index 0000000000..63dbbd6051 --- /dev/null +++ b/scm-dao-orientdb/src/main/java/sonia/scm/orientdb/OrientDBServletContextListener.java @@ -0,0 +1,113 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. 2. Redistributions in + * binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. 3. Neither the name of SCM-Manager; + * nor the names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.orientdb; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +//~--- JDK imports ------------------------------------------------------------ + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * + * @author Sebastian Sdorra + */ +@Singleton +public class OrientDBServletContextListener implements ServletContextListener +{ + + /** + * the logger for OrientDBServletContextListener + */ + private static final Logger logger = + LoggerFactory.getLogger(OrientDBServletContextListener.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + * + * @param connectionProvider + */ + @Inject + public OrientDBServletContextListener(ConnectionProvider connectionProvider) + { + this.connectionProvider = connectionProvider; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param sce + */ + @Override + public void contextDestroyed(ServletContextEvent sce) + { + connectionProvider.close(); + + if (logger.isInfoEnabled()) + { + logger.info("orientdb context listener destroyed"); + } + } + + /** + * Method description + * + * + * @param sce + */ + @Override + public void contextInitialized(ServletContextEvent sce) + { + if (logger.isInfoEnabled()) + { + logger.info("orientdb context listener initialized"); + } + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private ConnectionProvider connectionProvider; +}