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-holo Show documentation
Show all versions of postgresql-holo Show documentation
PostgreSQL JDBC Driver Postgresql
The newest version!
/*
* Copyright (c) 2003, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.osgi;
import static org.postgresql.util.internal.Nullness.castNonNull;
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;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.osgi.service.jdbc.DataSourceFactory;
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;
/**
* 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;
@SuppressWarnings("method.invocation.invalid")
SingleUseProperties(Properties initialProperties) {
super();
if (initialProperties != null) {
putAll(initialProperties);
}
}
@Override
public @Nullable 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(castNonNull(props.getProperty(JDBC_URL)));
}
if (props.containsKey(JDBC_SERVER_NAME)) {
ds.setServerName(castNonNull(props.getProperty(JDBC_SERVER_NAME)));
}
if (props.containsKey(JDBC_PORT_NUMBER)) {
ds.setPortNumber(Integer.parseInt(castNonNull(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