提交 d497e7d0 authored 作者: Thomas Mueller's avatar Thomas Mueller

Newline

上级 56ccd6ea
/* /*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0 * 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group * (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/ */
package org.h2.util; package org.h2.util;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException; import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties; import java.util.Properties;
import javax.sql.ConnectionPoolDataSource; import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource; import javax.sql.DataSource;
import javax.sql.XADataSource; import javax.sql.XADataSource;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.jdbcx.JdbcDataSource; import org.h2.jdbcx.JdbcDataSource;
import org.osgi.service.jdbc.DataSourceFactory; import org.osgi.service.jdbc.DataSourceFactory;
/** /**
* This class implements the OSGi DataSourceFactory interface for the H2 JDBC * This class implements the OSGi DataSourceFactory interface for the H2 JDBC
* driver. The following standard configuration properties are supported: * driver. The following standard configuration properties are supported:
* {@link #JDBC_USER}, {@link #JDBC_PASSWORD}, {@link #JDBC_DESCRIPTION}, * {@link #JDBC_USER}, {@link #JDBC_PASSWORD}, {@link #JDBC_DESCRIPTION},
* {@link #JDBC_DATASOURCE_NAME}, {@link #JDBC_NETWORK_PROTOCOL}, * {@link #JDBC_DATASOURCE_NAME}, {@link #JDBC_NETWORK_PROTOCOL},
* {@link #JDBC_URL}, {@link #JDBC_SERVER_NAME}, {@link #JDBC_PORT_NUMBER}. The * {@link #JDBC_URL}, {@link #JDBC_SERVER_NAME}, {@link #JDBC_PORT_NUMBER}. The
* following standard configuration properties are not supported: * following standard configuration properties are not supported:
* {@link #JDBC_ROLE_NAME}, {@link #JDBC_DATABASE_NAME}, * {@link #JDBC_ROLE_NAME}, {@link #JDBC_DATABASE_NAME},
* {@link #JDBC_INITIAL_POOL_SIZE}, {@link #JDBC_MAX_POOL_SIZE}, * {@link #JDBC_INITIAL_POOL_SIZE}, {@link #JDBC_MAX_POOL_SIZE},
* {@link #JDBC_MIN_POOL_SIZE}, {@link #JDBC_MAX_IDLE_TIME}, * {@link #JDBC_MIN_POOL_SIZE}, {@link #JDBC_MAX_IDLE_TIME},
* {@link #JDBC_MAX_STATEMENTS}, {@link #JDBC_PROPERTY_CYCLE}. Any other * {@link #JDBC_MAX_STATEMENTS}, {@link #JDBC_PROPERTY_CYCLE}. Any other
* property will be treated as a H2 specific option. If the {@link #JDBC_URL} * property will be treated as a H2 specific option. If the {@link #JDBC_URL}
* property is passed to any of the DataSource factories, the following * property is passed to any of the DataSource factories, the following
* properties will be ignored: {@link #JDBC_DATASOURCE_NAME}, * properties will be ignored: {@link #JDBC_DATASOURCE_NAME},
* {@link #JDBC_NETWORK_PROTOCOL}, {@link #JDBC_SERVER_NAME}, * {@link #JDBC_NETWORK_PROTOCOL}, {@link #JDBC_SERVER_NAME},
* {@link #JDBC_PORT_NUMBER}. * {@link #JDBC_PORT_NUMBER}.
* *
* @author Per Otterstrom * @author Per Otterstrom
*/ */
public class OsgiDataSourceFactory implements DataSourceFactory { public class OsgiDataSourceFactory implements DataSourceFactory {
private org.h2.Driver driver; private org.h2.Driver driver;
public OsgiDataSourceFactory(org.h2.Driver driver) { public OsgiDataSourceFactory(org.h2.Driver driver) {
this.driver = driver; this.driver = driver;
} }
/** /**
* Creates a basic data source. * Creates a basic data source.
* *
* @param properties the properties for the data source. * @param properties the properties for the data source.
* @throws SQLException if unsupported properties are supplied, or if data * @throws SQLException if unsupported properties are supplied, or if data
* source can not be created. * source can not be created.
* @return a new data source. * @return a new data source.
*/ */
public DataSource createDataSource(Properties properties) throws SQLException { public DataSource createDataSource(Properties properties) throws SQLException {
// Make copy of properties // Make copy of properties
Properties propertiesCopy = new Properties(); Properties propertiesCopy = new Properties();
if (properties != null) { if (properties != null) {
propertiesCopy.putAll(properties); propertiesCopy.putAll(properties);
} }
// Verify that no unsupported standard options are used // Verify that no unsupported standard options are used
rejectUnsupportedOptions(propertiesCopy); rejectUnsupportedOptions(propertiesCopy);
// Standard pool properties in OSGi not applicable here // Standard pool properties in OSGi not applicable here
rejectPoolingOptions(propertiesCopy); rejectPoolingOptions(propertiesCopy);
JdbcDataSource dataSource = new JdbcDataSource(); JdbcDataSource dataSource = new JdbcDataSource();
setupH2DataSource(dataSource, propertiesCopy); setupH2DataSource(dataSource, propertiesCopy);
return dataSource; return dataSource;
} }
/** /**
* Creates a pooled data source. * Creates a pooled data source.
* *
* @param properties the properties for the data source. * @param properties the properties for the data source.
* @throws SQLException if unsupported properties are supplied, or if data * @throws SQLException if unsupported properties are supplied, or if data
* source can not be created. * source can not be created.
* @return a new data source. * @return a new data source.
*/ */
public ConnectionPoolDataSource createConnectionPoolDataSource(Properties properties) throws SQLException { public ConnectionPoolDataSource createConnectionPoolDataSource(Properties properties) throws SQLException {
// Make copy of properties // Make copy of properties
Properties propertiesCopy = new Properties(); Properties propertiesCopy = new Properties();
if (properties != null) { if (properties != null) {
propertiesCopy.putAll(properties); propertiesCopy.putAll(properties);
} }
// Verify that no unsupported standard options are used // Verify that no unsupported standard options are used
rejectUnsupportedOptions(propertiesCopy); rejectUnsupportedOptions(propertiesCopy);
// The integrated connection pool is H2 is not configurable // The integrated connection pool is H2 is not configurable
rejectPoolingOptions(propertiesCopy); rejectPoolingOptions(propertiesCopy);
JdbcDataSource dataSource = new JdbcDataSource(); JdbcDataSource dataSource = new JdbcDataSource();
setupH2DataSource(dataSource, propertiesCopy); setupH2DataSource(dataSource, propertiesCopy);
return dataSource; return dataSource;
} }
/** /**
* Creates a pooled XA data source. * Creates a pooled XA data source.
* *
* @param properties the properties for the data source. * @param properties the properties for the data source.
* @throws SQLException if unsupported properties are supplied, or if data * @throws SQLException if unsupported properties are supplied, or if data
* source can not be created. * source can not be created.
* @return a new data source. * @return a new data source.
*/ */
public XADataSource createXADataSource(Properties properties) throws SQLException { public XADataSource createXADataSource(Properties properties) throws SQLException {
// Make copy of properties // Make copy of properties
Properties propertiesCopy = new Properties(); Properties propertiesCopy = new Properties();
if (properties != null) { if (properties != null) {
propertiesCopy.putAll(properties); propertiesCopy.putAll(properties);
} }
// Verify that no unsupported standard options are used // Verify that no unsupported standard options are used
rejectUnsupportedOptions(propertiesCopy); rejectUnsupportedOptions(propertiesCopy);
// The integrated connection pool is H2 is not configurable // The integrated connection pool is H2 is not configurable
rejectPoolingOptions(propertiesCopy); rejectPoolingOptions(propertiesCopy);
JdbcDataSource dataSource = new JdbcDataSource(); JdbcDataSource dataSource = new JdbcDataSource();
setupH2DataSource(dataSource, propertiesCopy); setupH2DataSource(dataSource, propertiesCopy);
return dataSource; return dataSource;
} }
/** /**
* Returns a driver. The H2 driver does not support any properties. * Returns a driver. The H2 driver does not support any properties.
* *
* @param properties must be null or empty list. * @param properties must be null or empty list.
* @throws SQLException if any property is supplied. * @throws SQLException if any property is supplied.
* @return a driver. * @return a driver.
*/ */
public java.sql.Driver createDriver(Properties properties) throws SQLException { public java.sql.Driver createDriver(Properties properties) throws SQLException {
if (properties != null && !properties.isEmpty()) { if (properties != null && !properties.isEmpty()) {
// No properties supported // No properties supported
throw new SQLException(); throw new SQLException();
} }
return driver; return driver;
} }
/** /**
* Checker method that will throw if any unsupported standard OSGi options * Checker method that will throw if any unsupported standard OSGi options
* is present. * is present.
* *
* @param properties the properties to check * @param properties the properties to check
* @throws SQLFeatureNotSupportedException if unsupported properties are * @throws SQLFeatureNotSupportedException if unsupported properties are
* present * present
*/ */
private static void rejectUnsupportedOptions(Properties properties) throws SQLFeatureNotSupportedException { private static void rejectUnsupportedOptions(Properties properties) throws SQLFeatureNotSupportedException {
// Unsupported standard properties in OSGi // Unsupported standard properties in OSGi
if (properties.containsKey(DataSourceFactory.JDBC_ROLE_NAME)) { if (properties.containsKey(DataSourceFactory.JDBC_ROLE_NAME)) {
throw new SQLFeatureNotSupportedException("The " + DataSourceFactory.JDBC_ROLE_NAME throw new SQLFeatureNotSupportedException("The " + DataSourceFactory.JDBC_ROLE_NAME
+ " property is not supported by H2"); + " property is not supported by H2");
} }
if (properties.containsKey(DataSourceFactory.JDBC_DATASOURCE_NAME)) { if (properties.containsKey(DataSourceFactory.JDBC_DATASOURCE_NAME)) {
throw new SQLFeatureNotSupportedException("The " + DataSourceFactory.JDBC_DATASOURCE_NAME throw new SQLFeatureNotSupportedException("The " + DataSourceFactory.JDBC_DATASOURCE_NAME
+ " property is not supported by H2"); + " property is not supported by H2");
} }
} }
/** /**
* Applies common OSGi properties to a H2 data source. Non standard * Applies common OSGi properties to a H2 data source. Non standard
* properties will be applied as H2 options. * properties will be applied as H2 options.
* *
* @param dataSource the data source to configure * @param dataSource the data source to configure
* @param properties the properties to apply to the data source * @param properties the properties to apply to the data source
*/ */
private static void setupH2DataSource(JdbcDataSource dataSource, Properties properties) { private static void setupH2DataSource(JdbcDataSource dataSource, Properties properties) {
// Setting user and password // Setting user and password
if (properties.containsKey(DataSourceFactory.JDBC_USER)) { if (properties.containsKey(DataSourceFactory.JDBC_USER)) {
dataSource.setUser((String) properties.remove(DataSourceFactory.JDBC_USER)); dataSource.setUser((String) properties.remove(DataSourceFactory.JDBC_USER));
} }
if (properties.containsKey(DataSourceFactory.JDBC_PASSWORD)) { if (properties.containsKey(DataSourceFactory.JDBC_PASSWORD)) {
dataSource.setPassword((String) properties.remove(DataSourceFactory.JDBC_PASSWORD)); dataSource.setPassword((String) properties.remove(DataSourceFactory.JDBC_PASSWORD));
} }
// Setting description // Setting description
if (properties.containsKey(DataSourceFactory.JDBC_DESCRIPTION)) { if (properties.containsKey(DataSourceFactory.JDBC_DESCRIPTION)) {
dataSource.setDescription((String) properties.remove(DataSourceFactory.JDBC_DESCRIPTION)); dataSource.setDescription((String) properties.remove(DataSourceFactory.JDBC_DESCRIPTION));
} }
// Setting URL // Setting URL
StringBuffer connectionUrl = new StringBuffer(); StringBuffer connectionUrl = new StringBuffer();
if (properties.containsKey(DataSourceFactory.JDBC_URL)) { if (properties.containsKey(DataSourceFactory.JDBC_URL)) {
// Use URL if specified // Use URL if specified
connectionUrl.append(properties.remove(DataSourceFactory.JDBC_URL)); connectionUrl.append(properties.remove(DataSourceFactory.JDBC_URL));
// Remove individual properties // Remove individual properties
properties.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL); properties.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL);
properties.remove(DataSourceFactory.JDBC_SERVER_NAME); properties.remove(DataSourceFactory.JDBC_SERVER_NAME);
properties.remove(DataSourceFactory.JDBC_PORT_NUMBER); properties.remove(DataSourceFactory.JDBC_PORT_NUMBER);
properties.remove(DataSourceFactory.JDBC_DATABASE_NAME); properties.remove(DataSourceFactory.JDBC_DATABASE_NAME);
} else { } else {
// Creating URL from individual properties // Creating URL from individual properties
connectionUrl.append(Constants.START_URL); connectionUrl.append(Constants.START_URL);
// Set network protocol (tcp/ssl) or DB type (mem/file) // Set network protocol (tcp/ssl) or DB type (mem/file)
String protocol = ""; String protocol = "";
if (properties.containsKey(DataSourceFactory.JDBC_NETWORK_PROTOCOL)) { if (properties.containsKey(DataSourceFactory.JDBC_NETWORK_PROTOCOL)) {
protocol = (String) properties.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL); protocol = (String) properties.remove(DataSourceFactory.JDBC_NETWORK_PROTOCOL);
connectionUrl.append(protocol).append(":"); connectionUrl.append(protocol).append(":");
} }
// Host name and/or port // Host name and/or port
if (properties.containsKey(DataSourceFactory.JDBC_SERVER_NAME)) { if (properties.containsKey(DataSourceFactory.JDBC_SERVER_NAME)) {
connectionUrl.append("//").append(properties.remove(DataSourceFactory.JDBC_SERVER_NAME)); connectionUrl.append("//").append(properties.remove(DataSourceFactory.JDBC_SERVER_NAME));
if (properties.containsKey(DataSourceFactory.JDBC_PORT_NUMBER)) { if (properties.containsKey(DataSourceFactory.JDBC_PORT_NUMBER)) {
connectionUrl.append(":").append(properties.remove(DataSourceFactory.JDBC_PORT_NUMBER)); connectionUrl.append(":").append(properties.remove(DataSourceFactory.JDBC_PORT_NUMBER));
} }
connectionUrl.append("/"); connectionUrl.append("/");
} else if (properties.containsKey(DataSourceFactory.JDBC_PORT_NUMBER)) { } else if (properties.containsKey(DataSourceFactory.JDBC_PORT_NUMBER)) {
// Assume local host if only port was set // Assume local host if only port was set
connectionUrl.append("//localhost:").append(properties.remove(DataSourceFactory.JDBC_PORT_NUMBER)) connectionUrl.append("//localhost:").append(properties.remove(DataSourceFactory.JDBC_PORT_NUMBER))
.append("/"); .append("/");
} else if (protocol.equals("tcp") || protocol.equals("ssl")) { } else if (protocol.equals("tcp") || protocol.equals("ssl")) {
// Assume local host if network protocol is set, but no host or // Assume local host if network protocol is set, but no host or
// port is set // port is set
connectionUrl.append("//localhost/"); connectionUrl.append("//localhost/");
} }
// DB path and name // DB path and name
if (properties.containsKey(DataSourceFactory.JDBC_DATABASE_NAME)) { if (properties.containsKey(DataSourceFactory.JDBC_DATABASE_NAME)) {
connectionUrl.append(properties.remove(DataSourceFactory.JDBC_DATABASE_NAME)); connectionUrl.append(properties.remove(DataSourceFactory.JDBC_DATABASE_NAME));
} }
} }
// Add remaining properties as options // Add remaining properties as options
for (Object option : properties.keySet()) { for (Object option : properties.keySet()) {
connectionUrl.append(";").append(option).append("=").append(properties.get(option)); connectionUrl.append(";").append(option).append("=").append(properties.get(option));
} }
if (connectionUrl.length() > Constants.START_URL.length()) { if (connectionUrl.length() > Constants.START_URL.length()) {
dataSource.setURL(connectionUrl.toString()); dataSource.setURL(connectionUrl.toString());
} }
} }
/** /**
* Checker method that will throw if any pooling related standard OSGi * Checker method that will throw if any pooling related standard OSGi
* options are present. * options are present.
* *
* @param properties the properties to check * @param properties the properties to check
* @throws SQLFeatureNotSupportedException if unsupported properties are * @throws SQLFeatureNotSupportedException if unsupported properties are
* present * present
*/ */
private static void rejectPoolingOptions(Properties properties) throws SQLFeatureNotSupportedException { private static void rejectPoolingOptions(Properties properties) throws SQLFeatureNotSupportedException {
if (properties.containsKey(DataSourceFactory.JDBC_INITIAL_POOL_SIZE) if (properties.containsKey(DataSourceFactory.JDBC_INITIAL_POOL_SIZE)
|| properties.containsKey(DataSourceFactory.JDBC_MAX_IDLE_TIME) || properties.containsKey(DataSourceFactory.JDBC_MAX_IDLE_TIME)
|| properties.containsKey(DataSourceFactory.JDBC_MAX_POOL_SIZE) || properties.containsKey(DataSourceFactory.JDBC_MAX_POOL_SIZE)
|| properties.containsKey(DataSourceFactory.JDBC_MAX_STATEMENTS) || properties.containsKey(DataSourceFactory.JDBC_MAX_STATEMENTS)
|| properties.containsKey(DataSourceFactory.JDBC_MIN_POOL_SIZE) || properties.containsKey(DataSourceFactory.JDBC_MIN_POOL_SIZE)
|| properties.containsKey(DataSourceFactory.JDBC_PROPERTY_CYCLE)) { || properties.containsKey(DataSourceFactory.JDBC_PROPERTY_CYCLE)) {
throw new SQLFeatureNotSupportedException("Pooling properties are not supported by H2"); throw new SQLFeatureNotSupportedException("Pooling properties are not supported by H2");
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论