Author: thomasm
Date: Thu Nov 29 02:43:13 2007
New Revision: 599370
URL: http://svn.apache.org/viewvc?rev=599370&view=rev
Log:
JCR-948 Support for JNDI configuration of BundleDbPersistenceManager and DbDataStore
Added:
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java
(with props)
Modified:
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java?rev=599370&r1=599369&r2=599370&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleDbPersistenceManager.java
Thu Nov 29 02:43:13 2007
@@ -197,7 +197,12 @@
}
/**
- * Sets the JDBC connection url.
+ * Sets the JDBC connection URL.
+ * The connection can be created using a JNDI Data Source as well.
+ * To do that, the driver class name must reference a javax.naming.Context class
+ * (for example javax.naming.InitialContext), and the URL must be the JNDI URL
+ * (for example java:comp/env/jdbc/Test).
+ *
* @param url the url to set.
*/
public void setUrl(String url) {
@@ -247,6 +252,7 @@
/**
* Sets the class name of the JDBC driver. The driver class will be loaded
* during {@link #init(PMContext) init} in order to assure the existence.
+ * If no driver is specified, the default driver for the database is used.
*
* @param driver the class name of the driver
*/
Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java?rev=599370&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java
Thu Nov 29 02:43:13 2007
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.persistence.bundle.util;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import javax.jcr.RepositoryException;
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+/**
+ * A factory for new database connections.
+ * Supported are regular JDBC drivers, as well as
+ * JNDI resources.
+ */
+public class ConnectionFactory {
+
+ /**
+ * Open a connection using the specified properties.
+ * The connection can be created using a JNDI Data Source as well. To do that,
+ * the driver class name must reference a javax.naming.Context class
+ * (for example javax.naming.InitialContext), and the URL must be the JNDI URL
+ * (for example java:comp/env/jdbc/Test).
+ *
+ * @param driver the JDBC driver or the Context class
+ * @param url the database URL
+ * @param user the user name
+ * @param password the password
+ * @return the connection
+ * @throws RepositoryException if the driver could not be loaded
+ * @throws SQLException if the connection could not be established
+ */
+ public static Connection getConnection(String driver, String url,
+ String user, String password) throws RepositoryException,
+ SQLException {
+ if (driver != null || driver.length() > 0) {
+ try {
+ Class d = Class.forName(driver);
+ if (javax.naming.Context.class.isAssignableFrom(d)) {
+ // JNDI context
+ Context context = (Context) d.newInstance();
+ DataSource ds = (DataSource) context.lookup(url);
+ return ds.getConnection(user, password);
+ } else {
+ try {
+ // Workaround for Apache Derby:
+ // The JDBC specification recommends the Class.forName method without
the .newInstance() method call,
+ // but it is required after a Derby 'shutdown'.
+ d.newInstance();
+ } catch (Throwable e) {
+ // Ignore exceptions
+ // There's no requirement that a JDBC driver class has a public default
constructor
+ }
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RepositoryException("Could not load class " + driver, e);
+ } catch (InstantiationException e) {
+ throw new RepositoryException("Could not instantiate context " + driver,
e);
+ } catch (IllegalAccessException e) {
+ throw new RepositoryException("Could not instantiate context " + driver,
e);
+ } catch (NamingException e) {
+ throw new RepositoryException("Naming exception using " + driver + " url:
" + url, e);
+ }
+ }
+ return DriverManager.getConnection(url, user, password);
+ }
+
+}
Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java?rev=599370&r1=599369&r2=599370&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
Thu Nov 29 02:43:13 2007
@@ -19,7 +19,6 @@
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
-import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -326,18 +325,7 @@
*/
private void setupConnection() throws SQLException, RepositoryException {
try {
- if (driver != null && driver.length() > 0) {
- Class driverClass = Class.forName(driver);
- // Workaround for Apache Derby:
- // The JDBC specification recommends the Class.ForName method without the
.newInstance() method call,
- // but adding the newInstance() guarantees that Derby will be booted on any
Java Virtual Machine.
- driverClass.newInstance();
- }
- } catch (Throwable e) {
- throw new RepositoryException("Could not load or initialize the database driver
class " + driver, e);
- }
- try {
- connection = DriverManager.getConnection(url, user, password);
+ connection = ConnectionFactory.getConnection(driver, url, user, password);
} catch (SQLException e) {
log.warn("Could not connect; driver: " + driver + " url: " + url + " user: "
+ user + " error: " + e.toString(), e);
throw e;
|