Back to 35, see Bug 328667
diff --git a/.project b/.project
new file mode 100644
index 0000000..3c1a94a
--- /dev/null
+++ b/.project
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.gemini.dbaccess</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ </natures>
+</projectDescription>
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/.classpath b/derby/org.eclipse.gemini.dbaccess.derby/.classpath
index f280071..335ba65 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/.classpath
+++ b/derby/org.eclipse.gemini.dbaccess.derby/.classpath
@@ -3,6 +3,5 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry excluding="**/.svn/*" kind="src" path="src"/>
- <classpathentry excluding="**/.svn/*" kind="src" path="common"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/.project b/derby/org.eclipse.gemini.dbaccess.derby/.project
index caeb204..d641622 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/.project
+++ b/derby/org.eclipse.gemini.dbaccess.derby/.project
@@ -25,11 +25,4 @@
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
- <linkedResources>
- <link>
- <name>common</name>
- <type>2</type>
- <locationURI>DBACCESS_COMMON</locationURI>
- </link>
- </linkedResources>
</projectDescription>
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/META-INF/MANIFEST.MF b/derby/org.eclipse.gemini.dbaccess.derby/META-INF/MANIFEST.MF
index 9f29f4e..ed0cc3d 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/META-INF/MANIFEST.MF
+++ b/derby/org.eclipse.gemini.dbaccess.derby/META-INF/MANIFEST.MF
@@ -11,5 +11,5 @@
org.apache.derby.jdbc,
org.osgi.framework;version="[1.3,2)",
org.osgi.service.jdbc;version="[1.0,2.0)"
-Export-Package: org.eclipse.gemini.dbaccess.derby.service,
- org.osgi.service.jdbc
+
+
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/AbstractDataSourceFactory.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/AbstractDataSourceFactory.java
new file mode 100644
index 0000000..d920f13
--- /dev/null
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/AbstractDataSourceFactory.java
@@ -0,0 +1,224 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Oracle.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Apache License v2.0 which accompanies this distribution.
+ * The Eclipse Public License is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * and the Apache License v2.0 is available at
+ * http://www.opensource.org/licenses/apache2.0.php.
+ * You may elect to redistribute this code under either of these licenses.
+ *
+ * Contributors:
+ * JJ Snyder - Embedded Derby JDBC support
+ ******************************************************************************/
+
+package org.eclipse.gemini.dbaccess.derby;
+
+import java.lang.reflect.Method;
+import java.sql.Driver;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Properties;
+
+import javax.sql.ConnectionPoolDataSource;
+import javax.sql.DataSource;
+import javax.sql.XADataSource;
+
+import org.osgi.service.jdbc.DataSourceFactory;
+
+/**
+ * Abstract behavior for Derby data source factories.
+ */
+public abstract class AbstractDataSourceFactory implements DataSourceFactory {
+
+ /** Option to indicate whether to use JDBC 4.0 flavor of the driver */
+ boolean jdbc4 = true;
+
+ /***************************************************
+ * Abstract methods must be implemented by subclass
+ ***************************************************/
+
+ public abstract DataSource createDataSource(Properties props) throws SQLException;
+
+ public abstract ConnectionPoolDataSource createConnectionPoolDataSource(Properties props) throws SQLException;
+
+ public abstract XADataSource createXADataSource(Properties props) throws SQLException;
+
+ public abstract Driver createDriver(Properties props) throws SQLException;
+
+ /************************************
+ * Methods inherited by subclasses
+ ***********************************/
+
+ protected void setDataSourceProperties(Object object, Properties props)
+ throws SQLException {
+
+ if (props == null) return;
+
+ Enumeration<?> enumeration = props.keys();
+ while (enumeration.hasMoreElements()) {
+ String name = (String) enumeration.nextElement();
+ setProperty(object, name, props.getProperty(name));
+ }
+ }
+
+ protected void throwSQLException(Exception cause, String theType, String value)
+ throws SQLException {
+ SQLException sqlException = new SQLException("Invalid " + theType
+ + " value: " + value);
+ sqlException.initCause(cause);
+ throw sqlException;
+ }
+
+ protected Object toBasicType(String value, String type) throws SQLException {
+ if (value == null) {
+ return null;
+ }
+ else
+ if (type == null || type.equals(String.class.getName())) {
+ return value;
+ }
+ else
+ if (type.equals(Integer.class.getName())
+ || type.equals(int.class.getName())) {
+ try {
+ return Integer.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Integer", value);
+ }
+ }
+ else
+ if (type.equals(Float.class.getName())
+ || type.equals(float.class.getName())) {
+ try {
+ return Float.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Float", value);
+ }
+ }
+ else
+ if (type.equals(Long.class.getName())
+ || type.equals(long.class.getName())) {
+ try {
+ return Long.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Long", value);
+ }
+ }
+ else
+ if (type.equals(Double.class.getName())
+ || type.equals(double.class.getName())) {
+ try {
+ return Double.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Double", value);
+ }
+ }
+ else
+ if (type.equals(Character.class.getName())
+ || type.equals(char.class.getName())) {
+ if (value.length() != 1) {
+ throw new SQLException(
+ "Invalid Character value: "
+ + value);
+ }
+
+ return Character.valueOf(value.charAt(0));
+ }
+ else
+ if (type.equals(Byte.class.getName())
+ || type
+ .equals(byte.class
+ .getName())) {
+ try {
+ return Byte.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Byte", value);
+ }
+ }
+ else
+ if (type.equals(Short.class.getName())
+ || type.equals(short.class
+ .getName())) {
+ try {
+ return Short.valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e, "Short",
+ value);
+ }
+ }
+ else
+ if (type.equals(Boolean.class
+ .getName())
+ || type
+ .equals(boolean.class
+ .getName())) {
+ try {
+ return Boolean
+ .valueOf(value);
+ }
+ catch (NumberFormatException e) {
+ throwSQLException(e,
+ "Boolean", value);
+ }
+ }
+ else {
+ throw new SQLException(
+ "Invalid property type: "
+ + type);
+ }
+ return null;
+ }
+
+ protected void setProperty(Object object, String name, String value)
+ throws SQLException {
+ Class<?> type = object.getClass();
+
+ java.beans.PropertyDescriptor[] descriptors;
+ try {
+ descriptors = java.beans.Introspector.getBeanInfo(type)
+ .getPropertyDescriptors();
+ }
+ catch (Exception exc) {
+ SQLException sqlException = new SQLException();
+ sqlException.initCause(exc);
+ throw sqlException;
+ }
+ List<String> names = new ArrayList<String>();
+
+ for (int i = 0; i < descriptors.length; i++) {
+ if (descriptors[i].getWriteMethod() == null) {
+ continue;
+ }
+
+ if (descriptors[i].getName().equals(name)) {
+ Method method = descriptors[i].getWriteMethod();
+ Class<?> paramType = method.getParameterTypes()[0];
+ Object param = toBasicType(value, paramType.getName());
+
+ try {
+ method.invoke(object, new Object[] {param});
+ }
+ catch (Exception exc) {
+ SQLException sqlException = new SQLException();
+ sqlException.initCause(exc);
+ throw sqlException;
+ }
+ return;
+ }
+
+ names.add(descriptors[i].getName());
+ }
+ throw new SQLException("No such property: " + name
+ + ", exists. Writable properties are: " + names);
+ }
+}
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/Activator.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/Activator.java
index 086c185..7a251ac 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/Activator.java
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/Activator.java
@@ -24,8 +24,6 @@
import org.osgi.service.jdbc.DataSourceFactory;
-import org.eclipse.gemini.dbaccess.derby.service.DerbyServiceProperties;
-
/**
* Creates a {@link DataSourceFactory} for each of the available Derby JDBC driver types.
*/
@@ -34,52 +32,39 @@
private ServiceRegistration embeddedService, clientService, embeddedService4, clientService4;
public void start(BundleContext context) throws Exception {
- System.out.println("Gemini DBAccess - Derby JDBC starting");
+ System.out.println("Gemini DBAccess Derby starting");
Hashtable<String,String> props = new Hashtable<String,String>();
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME,
- DerbyServiceProperties.DERBY_DRIVER_NAME);
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, DataSourceFactoryConstants.DERBY_DRIVER_NAME);
- /*******************************/
- /* Register the JDBC 3 drivers */
- /*******************************/
-
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION,
- DerbyServiceProperties.JDBC_3_DRIVER_VERSION);
+ /*=== Register the JDBC 3 drivers ===*/
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, DataSourceFactoryConstants.JDBC_3_DRIVER_VERSION);
// Register the embedded driver
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS,
- DerbyServiceProperties.DERBY_EMBEDDED_DRIVER_CLASS);
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, DataSourceFactoryConstants.DERBY_EMBEDDED_DRIVER_CLASS);
embeddedService = context.registerService(
DataSourceFactory.class.getName(),
new EmbeddedDataSourceFactory(false),
props);
// Register the client driver
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS,
- DerbyServiceProperties.DERBY_CLIENT_DRIVER_CLASS);
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, DataSourceFactoryConstants.DERBY_CLIENT_DRIVER_CLASS);
clientService = context.registerService(
DataSourceFactory.class.getName(),
new ClientDataSourceFactory(false),
props);
- /*******************************/
- /* Register the JDBC 4 drivers */
- /*******************************/
-
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION,
- DerbyServiceProperties.JDBC_4_DRIVER_VERSION);
+ /*=== Register the JDBC 4 drivers ===*/
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, DataSourceFactoryConstants.JDBC_4_DRIVER_VERSION);
// Register the embedded driver
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS,
- DerbyServiceProperties.DERBY_EMBEDDED_DRIVER_CLASS);
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, DataSourceFactoryConstants.DERBY_EMBEDDED_DRIVER_CLASS);
embeddedService4 = context.registerService(
DataSourceFactory.class.getName(),
new EmbeddedDataSourceFactory(true),
props);
// Register the client driver
- props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS,
- DerbyServiceProperties.DERBY_CLIENT_DRIVER_CLASS);
+ props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, DataSourceFactoryConstants.DERBY_CLIENT_DRIVER_CLASS);
clientService4 = context.registerService(
DataSourceFactory.class.getName(),
new ClientDataSourceFactory(true),
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/ClientDataSourceFactory.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/ClientDataSourceFactory.java
index 694156c..e5095d1 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/ClientDataSourceFactory.java
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/ClientDataSourceFactory.java
@@ -10,11 +10,13 @@
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
- * mkeith - Client/Server Derby JDBC support
+ * mkeith - CLient/Server Derby JDBC support
******************************************************************************/
package org.eclipse.gemini.dbaccess.derby;
+import java.util.Properties;
+
import java.sql.Driver;
import java.sql.SQLException;
@@ -22,8 +24,6 @@
import javax.sql.ConnectionPoolDataSource;
import javax.sql.XADataSource;
-import org.eclipse.gemini.dbaccess.AbstractDataSourceFactory;
-
import org.apache.derby.jdbc.ClientConnectionPoolDataSource;
import org.apache.derby.jdbc.ClientConnectionPoolDataSource40;
import org.apache.derby.jdbc.ClientDriver;
@@ -32,6 +32,8 @@
import org.apache.derby.jdbc.ClientXADataSource;
import org.apache.derby.jdbc.ClientXADataSource40;
+import org.osgi.service.jdbc.DataSourceFactory;
+
/**
* A factory for creating Derby network data sources. The properties specified
* in the create methods determine how the created object is configured.
@@ -59,38 +61,84 @@
*/
public class ClientDataSourceFactory extends AbstractDataSourceFactory {
- /** Option to indicate whether to use JDBC 4.0 flavor of the driver */
- boolean jdbc4 = true;
-
public ClientDataSourceFactory() {}
public ClientDataSourceFactory(boolean jdbc4) {
this.jdbc4 = jdbc4;
}
-
- @Override
- public Driver newJdbcDriver() throws SQLException {
- return new ClientDriver();
+
+ /**
+ * Create a Derby DataSource object.
+ *
+ * @param props The properties that define the DataSource implementation to
+ * create and how the DataSource is configured.
+ * @return The configured DataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createDataSource(java.util.Properties)
+ */
+ public DataSource createDataSource(Properties props) throws SQLException {
+ if (props == null) props = new Properties();
+ if (props.get(DataSourceFactory.JDBC_URL) != null) {
+ return new UrlBasedDriverDataSource(props, false);
+ } else {
+ DataSource dataSource = (jdbc4)
+ ? new ClientDataSource40()
+ : new ClientDataSource();
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
+ }
}
- @Override
- public DataSource newDataSource() throws SQLException {
- return jdbc4
- ? new ClientDataSource40()
- : new ClientDataSource();
- }
-
- @Override
- public ConnectionPoolDataSource newConnectionPoolDataSource()
- throws SQLException {
- return jdbc4
+ /**
+ * Create a Derby ConnectionPoolDataSource object.
+ *
+ * @param props The properties that define the ConnectionPoolDataSource
+ * implementation to create and how the ConnectionPoolDataSource is
+ * configured.
+ * @return The configured ConnectionPoolDataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createConnectionPoolDataSource(java.util.Properties)
+ */
+ public ConnectionPoolDataSource createConnectionPoolDataSource(Properties props) throws SQLException {
+ if (props == null) props = new Properties();
+ ConnectionPoolDataSource dataSource = (jdbc4)
? new ClientConnectionPoolDataSource40()
: new ClientConnectionPoolDataSource();
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
}
- @Override
- public XADataSource newXADataSource() throws SQLException {
- return jdbc4
+ /**
+ * Create a Derby XADataSource object.
+ *
+ * @param props The properties that define the XADataSource implementation
+ * to create and how the XADataSource is configured.
+ * @return The configured XADataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createXADataSource(java.util.Properties)
+ */
+ public XADataSource createXADataSource(Properties props) throws SQLException {
+ if (props == null) props = new Properties();
+ XADataSource dataSource = (jdbc4)
? new ClientXADataSource40()
: new ClientXADataSource();
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
}
+
+ /**
+ * Create a new org.apache.derby.jdbc.EmbeddedDriver.
+ *
+ * @param props The properties used to configure the Driver. Null
+ * indicates no properties.
+ * If the property cannot be set on the Driver being
+ * created then a SQLException must be thrown.
+ * @return A configured org.apache.derby.jdbc.EmbeddedDriver.
+ * @throws SQLException If the org.apache.derby.jdbc.ClientDriver cannot be created.
+ */
+ public Driver createDriver(Properties props) throws SQLException {
+ // Properties not used when accessing the raw driver.
+ Driver driver = new ClientDriver();
+ setDataSourceProperties(driver, props);
+ return driver;
+ }
}
\ No newline at end of file
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/DataSourceFactoryConstants.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/DataSourceFactoryConstants.java
new file mode 100644
index 0000000..f9d6849
--- /dev/null
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/DataSourceFactoryConstants.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Oracle.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Apache License v2.0 which accompanies this distribution.
+ * The Eclipse Public License is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * and the Apache License v2.0 is available at
+ * http://www.opensource.org/licenses/apache2.0.php.
+ * You may elect to redistribute this code under either of these licenses.
+ *
+ * Contributors:
+ * mkeith - Constants for Derby JDBC support
+ ******************************************************************************/
+
+package org.eclipse.gemini.dbaccess.derby;
+
+/**
+ * Contants for Derby data source factory registration.
+ */
+public class DataSourceFactoryConstants {
+
+ // Register a service under each of the following driver class names
+ public static final String DERBY_EMBEDDED_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
+ public static final String DERBY_CLIENT_DRIVER_CLASS = "org.apache.derby.jdbc.ClientDriver";
+
+ // Register all Derby factory services under this driver name
+ public static final String DERBY_DRIVER_NAME = "Derby";
+
+ // Register under the JDBC version the driver supports
+ public static final String JDBC_3_DRIVER_VERSION = "3.0";
+ public static final String JDBC_4_DRIVER_VERSION = "4.0";
+}
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/EmbeddedDataSourceFactory.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/EmbeddedDataSourceFactory.java
index da33298..9c40644 100644
--- a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/EmbeddedDataSourceFactory.java
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/EmbeddedDataSourceFactory.java
@@ -11,11 +11,12 @@
*
* Contributors:
* JJ Snyder - Embedded Derby JDBC support
- * mkeith - Inherit from abstract class
******************************************************************************/
package org.eclipse.gemini.dbaccess.derby;
+import java.util.Properties;
+
import java.sql.Driver;
import java.sql.SQLException;
@@ -30,8 +31,7 @@
import org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40;
import org.apache.derby.jdbc.EmbeddedXADataSource;
import org.apache.derby.jdbc.EmbeddedXADataSource40;
-
-import org.eclipse.gemini.dbaccess.AbstractDataSourceFactory;
+import org.osgi.service.jdbc.DataSourceFactory;
/**
* A factory for creating Derby embedded data sources. The properties specified
@@ -55,39 +55,96 @@
* props.put(DataSourceFactory.JDBC_URL, "jdbc:derby:myDB");
*/
public class EmbeddedDataSourceFactory extends AbstractDataSourceFactory {
-
- /** Option to indicate whether to use JDBC 4.0 flavor of the driver */
- boolean jdbc4 = true;
public EmbeddedDataSourceFactory() {}
public EmbeddedDataSourceFactory(boolean jdbc4) {
this.jdbc4 = jdbc4;
}
- @Override
- public Driver newJdbcDriver() throws SQLException {
- return new EmbeddedDriver();
- }
+ /**
+ * Create a Derby DataSource object.
+ *
+ * @param props The properties that define the DataSource implementation to
+ * create and how the DataSource is configured.
+ * @return The configured DataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createDataSource(java.util.Properties)
+ */
+ public DataSource createDataSource(Properties props) throws SQLException {
+ if (props == null) {
+ props = new Properties();
+ }
+ if (props.get(DataSourceFactory.JDBC_URL) != null) {
+ return new UrlBasedDriverDataSource(props, true);
+ } else {
+ DataSource dataSource = (jdbc4)
+ ? new EmbeddedDataSource40()
+ : new EmbeddedDataSource();
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
+ }
+ }
- @Override
- public DataSource newDataSource() throws SQLException {
- return jdbc4
- ? new EmbeddedDataSource40()
- : new EmbeddedDataSource();
- }
+ /**
+ * Create a Derby ConnectionPoolDataSource object.
+ *
+ * @param props The properties that define the ConnectionPoolDataSource
+ * implementation to create and how the ConnectionPoolDataSource is
+ * configured.
+ * @return The configured ConnectionPoolDataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createConnectionPoolDataSource(java.util.Properties)
+ */
+ public ConnectionPoolDataSource createConnectionPoolDataSource(
+ Properties props) throws SQLException {
+ if (props == null) {
+ props = new Properties();
+ }
+ ConnectionPoolDataSource dataSource = (jdbc4)
+ ? new EmbeddedConnectionPoolDataSource40()
+ : new EmbeddedConnectionPoolDataSource();
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
+ }
- @Override
- public ConnectionPoolDataSource newConnectionPoolDataSource()
- throws SQLException {
- return jdbc4
- ? new EmbeddedConnectionPoolDataSource40()
- : new EmbeddedConnectionPoolDataSource();
- }
-
- @Override
- public XADataSource newXADataSource() throws SQLException {
- return jdbc4
+ /**
+ * Create a Derby XADataSource object.
+ *
+ * @param props The properties that define the XADataSource implementation
+ * to create and how the XADataSource is configured.
+ * @return The configured XADataSource.
+ * @throws SQLException
+ * @see org.osgi.service.jdbc.DataSourceFactory#createXADataSource(java.util.Properties)
+ */
+ public XADataSource createXADataSource(Properties props)
+ throws SQLException {
+ if (props == null) {
+ props = new Properties();
+ }
+ XADataSource dataSource = (jdbc4)
? new EmbeddedXADataSource40()
: new EmbeddedXADataSource();
- }
+ setDataSourceProperties(dataSource, props);
+ return dataSource;
+ }
+
+ /**
+ * Create a new org.apache.derby.jdbc.EmbeddedDriver.
+ *
+ * @param props The properties used to configure the Driver. Null
+ * indicates no properties.
+ * If the property cannot be set on the Driver being
+ * created then a SQLException must be thrown.
+ * @return A configured org.apache.derby.jdbc.EmbeddedDriver.
+ * @throws SQLException If the org.apache.derby.jdbc.EmbeddedDriver cannot be created.
+ */
+ public Driver createDriver(Properties props) throws SQLException {
+ if (props == null) {
+ props = new Properties();
+ }
+ EmbeddedDriver driver = new EmbeddedDriver();
+ setDataSourceProperties(driver, props);
+ return driver;
+ }
+
}
diff --git a/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/UrlBasedDriverDataSource.java b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/UrlBasedDriverDataSource.java
new file mode 100644
index 0000000..c951290
--- /dev/null
+++ b/derby/org.eclipse.gemini.dbaccess.derby/src/org/eclipse/gemini/dbaccess/derby/UrlBasedDriverDataSource.java
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Oracle.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and Apache License v2.0 which accompanies this distribution.
+ * The Eclipse Public License is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * and the Apache License v2.0 is available at
+ * http://www.opensource.org/licenses/apache2.0.php.
+ * You may elect to redistribute this code under either of these licenses.
+ *
+ * Contributors:
+ * mkeith - CLient/Server Derby JDBC support
+ ******************************************************************************/
+
+package org.eclipse.gemini.dbaccess.derby;
+
+import java.io.PrintWriter;
+import java.util.Properties;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Driver;
+
+import org.apache.derby.jdbc.ClientDriver;
+import org.apache.derby.jdbc.EmbeddedDriver;
+
+import static org.osgi.service.jdbc.DataSourceFactory.*;
+
+/**
+ * An abbreviated/simplified DataSource impl that takes a URL from the client
+ * and just returns a thin data source wrapper around the basic JDBC driver.
+ */
+class UrlBasedDriverDataSource implements javax.sql.DataSource {
+
+ boolean embedded;
+ Driver driver;
+ Properties properties = null;
+ String url = null;
+
+ /**
+ * @param properties The properties to use for operations on the driver
+ * @param embedded Whether to wrap an embedded or a client driver
+ */
+ public UrlBasedDriverDataSource(Properties properties, boolean embedded) {
+ this.embedded = embedded;
+ this.driver = embedded ? new EmbeddedDriver() : new ClientDriver();
+ this.properties = (Properties) properties.clone();
+ this.url = properties.getProperty(JDBC_URL);
+ }
+
+ public UrlBasedDriverDataSource(Properties properties) {
+ this(properties, true);
+ }
+
+ public Connection getConnection() throws java.sql.SQLException {
+ return driver.connect(url, properties);
+ }
+
+ public Connection getConnection(String user, String password) throws java.sql.SQLException {
+ Properties localProps = (Properties) properties.clone();
+ localProps.put(JDBC_USER, user);
+ localProps.put(JDBC_PASSWORD, password);
+ return driver.connect(url, localProps);
+ }
+
+ public boolean isWrapperFor(Class<?> cls) {
+ return embedded
+ ? (cls == EmbeddedDriver.class)
+ : (cls == ClientDriver.class);
+ }
+
+ public <T> T unwrap(Class<T> cls) {
+ return (isWrapperFor(cls))
+ ? (T) driver
+ : null;
+ }
+
+ public PrintWriter getLogWriter() throws SQLException {
+ return DriverManager.getLogWriter();
+ }
+
+ public int getLoginTimeout() throws SQLException {
+ return DriverManager.getLoginTimeout();
+ }
+
+ // Don't support setting log writer or timeout
+
+ public void setLogWriter(PrintWriter writer) throws SQLException {
+ throw new SQLException("Can't set Log Writer on URL data source");
+ }
+
+ public void setLoginTimeout(int timeout) throws SQLException {
+ throw new SQLException("Can't set Login Timeout on URL data source");
+ }
+}
\ No newline at end of file