org.postgresql.osgi.PGDataSourceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of postgresql Show documentation
Show all versions of postgresql Show documentation
Java JDBC 4.0 (JRE 6+) driver for PostgreSQL database
/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2015, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.osgi;
import java.sql.SQLException;
import java.util.Map.Entry;
import java.util.Properties;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.XADataSource;
import org.osgi.service.jdbc.DataSourceFactory;
import org.postgresql.ds.common.BaseDataSource;
import org.postgresql.jdbc2.optional.ConnectionPool;
import org.postgresql.jdbc2.optional.PoolingDataSource;
import org.postgresql.jdbc2.optional.SimpleDataSource;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.postgresql.xa.PGXADataSource;
/**
* This factory service is designed to be used in OSGi Enterprise environments
* to create and configure JDBC data-sources.
*/
public class PGDataSourceFactory implements DataSourceFactory
{
/**
* A class that removes properties as they are used (without modifying the supplied initial Properties)
*/
private static class SingleUseProperties extends Properties
{
private static final long serialVersionUID = 1L;
public SingleUseProperties(Properties initialProperties)
{
super();
if (initialProperties != null)
{
putAll(initialProperties);
}
}
@Override
public String getProperty(String key)
{
String value = super.getProperty(key);
remove(key);
return value;
}
}
private void configureBaseDataSource(BaseDataSource ds, Properties props)
throws SQLException
{
if (props.containsKey(JDBC_URL))
{
ds.setUrl(props.getProperty(JDBC_URL));
}
if (props.containsKey(JDBC_SERVER_NAME))
{
ds.setServerName(props.getProperty(JDBC_SERVER_NAME));
}
if (props.containsKey(JDBC_PORT_NUMBER))
{
ds.setPortNumber(Integer.parseInt(props.getProperty(JDBC_PORT_NUMBER)));
}
if (props.containsKey(JDBC_DATABASE_NAME))
{
ds.setDatabaseName(props.getProperty(JDBC_DATABASE_NAME));
}
if (props.containsKey(JDBC_USER))
{
ds.setUser(props.getProperty(JDBC_USER));
}
if (props.containsKey(JDBC_PASSWORD))
{
ds.setPassword(props.getProperty(JDBC_PASSWORD));
}
for (Entry