From e0bb411332dfd2a4a586e089343b636dfe125284 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 9 Mar 2012 19:39:37 +0100 Subject: [PATCH] added connection provider for the jdbc backend --- scm-backend-jdbc/pom.xml | 14 ++ .../jdbc/DataSourceConnectionProvider.java | 129 ++++++++++++++++++ .../DataSourceConnectionProviderTest.java | 102 ++++++++++++++ .../scm/jdbc/TestIntialContextFactory.java | 84 ++++++++++++ .../main/java/sonia/scm/BackendException.java | 91 ++++++++++++ 5 files changed, 420 insertions(+) create mode 100644 scm-backend-jdbc/src/main/java/sonia/scm/jdbc/DataSourceConnectionProvider.java create mode 100644 scm-backend-jdbc/src/test/java/sonia/scm/jdbc/DataSourceConnectionProviderTest.java create mode 100644 scm-backend-jdbc/src/test/java/sonia/scm/jdbc/TestIntialContextFactory.java create mode 100644 scm-core/src/main/java/sonia/scm/BackendException.java diff --git a/scm-backend-jdbc/pom.xml b/scm-backend-jdbc/pom.xml index 6c251492bd..2668730a8b 100644 --- a/scm-backend-jdbc/pom.xml +++ b/scm-backend-jdbc/pom.xml @@ -39,5 +39,19 @@ test + + org.apache.derby + derby + 10.8.2.2 + test + + + + tomcat + catalina + 5.0.28 + test + + diff --git a/scm-backend-jdbc/src/main/java/sonia/scm/jdbc/DataSourceConnectionProvider.java b/scm-backend-jdbc/src/main/java/sonia/scm/jdbc/DataSourceConnectionProvider.java new file mode 100644 index 0000000000..f80967961a --- /dev/null +++ b/scm-backend-jdbc/src/main/java/sonia/scm/jdbc/DataSourceConnectionProvider.java @@ -0,0 +1,129 @@ +/** + * 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.jdbc; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Provider; +import com.google.inject.Singleton; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sonia.scm.BackendException; + +//~--- JDK imports ------------------------------------------------------------ + +import java.sql.Connection; +import java.sql.SQLException; + + +import javax.naming.InitialContext; + +import javax.sql.DataSource; + +/** + * Provider for jdbc database connections. This provider uses jndi to get the + * {@link DataSource} from the application server. + * + * @author Sebastian Sdorra + */ +@Singleton +public class DataSourceConnectionProvider implements Provider +{ + + /** jndi resource for the database connection */ + public static final String JNDI_DATASOURCE = + "java:comp/env/jdbc/scm-mananger"; + + /** + * the logger for DataSourceConnectionProvider + */ + private static final Logger logger = + LoggerFactory.getLogger(DataSourceConnectionProvider.class); + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public DataSourceConnectionProvider() + { + try + { + InitialContext ic = new InitialContext(); + + dataSource = (DataSource) ic.lookup(JNDI_DATASOURCE); + } + catch (Exception ex) + { + throw new BackendException("could not fetch datasource", ex); + } + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @Override + public Connection get() + { + if (logger.isTraceEnabled()) + { + logger.trace("return new database connection from datasource"); + } + + Connection connection = null; + + try + { + connection = dataSource.getConnection(); + } + catch (SQLException ex) + { + throw new BackendException( + "could not get database connection from datasource", ex); + } + + return connection; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private DataSource dataSource; +} diff --git a/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/DataSourceConnectionProviderTest.java b/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/DataSourceConnectionProviderTest.java new file mode 100644 index 0000000000..f743542fdf --- /dev/null +++ b/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/DataSourceConnectionProviderTest.java @@ -0,0 +1,102 @@ +/** + * 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.jdbc; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.naming.Context; + +/** + * + * @author Sebastian Sdorra + */ +public class DataSourceConnectionProviderTest +{ + + /** + * Method description + * + * + * @throws Exception + */ + @BeforeClass + public static void setUpClass() throws Exception + { + System.setProperty("derby.stream.error.field", "java.lang.System.err"); + + // Create initial context + System.setProperty(Context.INITIAL_CONTEXT_FACTORY, + TestIntialContextFactory.class.getName()); + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + */ + @Test + public void testGet() + { + DataSourceConnectionProvider provider = new DataSourceConnectionProvider(); + + assertNotNull(provider.get()); + } + + /** + * Method description + * + * + * @throws SQLException + */ + @Test + public void testGetAndClose() throws SQLException + { + DataSourceConnectionProvider provider = new DataSourceConnectionProvider(); + Connection connection = provider.get(); + + assertNotNull(connection); + connection.close(); + connection = provider.get(); + assertNotNull(connection); + } +} diff --git a/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/TestIntialContextFactory.java b/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/TestIntialContextFactory.java new file mode 100644 index 0000000000..f7bdba9726 --- /dev/null +++ b/scm-backend-jdbc/src/test/java/sonia/scm/jdbc/TestIntialContextFactory.java @@ -0,0 +1,84 @@ +/** + * 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.jdbc; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40; + +import static org.mockito.Mockito.*; + +//~--- JDK imports ------------------------------------------------------------ + +import java.util.Hashtable; + +import javax.naming.Context; +import javax.naming.NamingException; +import javax.naming.spi.InitialContextFactory; + +/** + * + * @author Sebastian Sdorra + */ +public class TestIntialContextFactory implements InitialContextFactory +{ + + + + + /** + * Method description + * + * + * @param environment + * + * @return + * + * @throws NamingException + */ + @Override + public Context getInitialContext(Hashtable environment) + throws NamingException + { + EmbeddedConnectionPoolDataSource40 ds = + new EmbeddedConnectionPoolDataSource40(); + + ds.setDatabaseName("memory:scm-manager;create=true"); + + Context context = mock(Context.class); + + when(context.lookup( + DataSourceConnectionProvider.JNDI_DATASOURCE)).thenReturn(ds); + + return context; + } +} diff --git a/scm-core/src/main/java/sonia/scm/BackendException.java b/scm-core/src/main/java/sonia/scm/BackendException.java new file mode 100644 index 0000000000..774087da8e --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/BackendException.java @@ -0,0 +1,91 @@ +/** + * 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; + +/** + * This type of exception is only thrown by scm-manager backend implementation, + * such as the xml or jdbc backend. + * + * @author Sebastian Sdorra + * @since 1.14 + */ +public class BackendException extends RuntimeException +{ + + /** Field description */ + private static final long serialVersionUID = -6885276113482167556L; + + //~--- constructors --------------------------------------------------------- + + /** + * Constructs ... + * + */ + public BackendException() + { + super(); + } + + /** + * Constructs ... + * + * + * @param message + */ + public BackendException(String message) + { + super(message); + } + + /** + * Constructs ... + * + * + * @param cause + */ + public BackendException(Throwable cause) + { + super(cause); + } + + /** + * Constructs ... + * + * + * @param message + * @param cause + */ + public BackendException(String message, Throwable cause) + { + super(message, cause); + } +}