Nixed database stuff & Depot depend.

I wasn't thinking clearly when I introduced that Depot depend. ooo-util is
*just* supposed to depend on Guava and samskivert and the fact that it was able
to do DB stuff because of samskivert's (unfortunate) database stuff was an
unhappy accident. ooo-util is really for only the most generic utility stuff.

I'm creating a new library, ooo-db which extends ooo-util with database stuff,
and OOO projects that do database things (which is not Narya, Nenya, Vilya &c)
can depend on ooo-db instead of ooo-util and they'll get a sensible Depot in
addition to a sensible Guava and samskivert.

 don't want to all of a sudden foist a Depot on all the non-Depot needing
libraries that happen to already depend on ooo-util.
This commit is contained in:
Michael Bayne
2015-03-12 11:23:29 -07:00
parent b7bceac7cb
commit 2bd94e6dde
3 changed files with 0 additions and 189 deletions
-11
View File
@@ -47,11 +47,6 @@
<artifactId>samskivert</artifactId> <artifactId>samskivert</artifactId>
<version>1.9</version> <version>1.9</version>
</dependency> </dependency>
<dependency>
<groupId>com.samskivert</groupId>
<artifactId>depot</artifactId>
<version>1.7</version>
</dependency>
<dependency> <dependency>
<groupId>com.google.guava</groupId> <groupId>com.google.guava</groupId>
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
@@ -63,12 +58,6 @@
<version>2.5</version> <version>2.5</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.3-606.jdbc4</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@@ -1,73 +0,0 @@
//
// ooo-util - a place for OOO utilities
// Copyright (C) 2011 Three Rings Design, Inc., All Rights Reserved
// http://github.com/threerings/ooo-util/blob/master/LICENSE
package com.threerings.util;
import java.util.Properties;
import com.samskivert.util.Config;
import com.samskivert.depot.ConnectionProvider;
import com.samskivert.depot.StaticConnectionProvider;
public class ConnectionProviderUtil
{
public enum DB {
postgres, hsqldb;
}
/**
* Creates a ConnectionProvider for the type of databases specified by <code>db.type</code> in
* <code>config</code>.
* <h2>Types</h2>
* <dl>
* <dt>postgres</dt>
* <dd>Uses {@link PostgresUtil#createPoolingProvider(Config, String)} with the given config
* and ident.</dd>
* <dt>hsqldb</dt>
* <dd>a {@link StaticConnectionProvider} is created from
* the sub-properties under <code>db</code>. It defaults to an in-memory database if
* <code>db.default.url</code> isn't defined.
* </dd>
* </dl>
*
* @throws IllegalArgumentException - if <code>db.type</code> doesn't match a known type.
*/
public static ConnectionProvider createProvider (Config config, String ident)
{
return createProvider(config, ident, "db");
}
/**
* Creates a ConnectionProvider as in {@link #createProvider(Config,String)}, but uses
* the supplied {@code prefix} in place of {@code db}.
*/
public static ConnectionProvider createProvider (Config config, String ident, String prefix)
{
DB type = DB.valueOf(config.getValue(prefix + ".type", "postgres"));
switch(type) {
case postgres:
return PostgresUtil.createPoolingProvider(config, ident, prefix);
case hsqldb:
Properties sub = config.getSubProperties(prefix);
putIfNotPresent(sub, "default.driver", "org.hsqldb.jdbcDriver");
putIfNotPresent(sub, "default.url", "jdbc:hsqldb:mem:.");
putIfNotPresent(sub, "default.username", "sa");
putIfNotPresent(sub, "default.password", "");
return new StaticConnectionProvider(sub);
default:
throw new IllegalArgumentException("Unknown db type " +
config.getValue(prefix + ".type", (String)null));
}
}
protected static void putIfNotPresent (Properties props, String key, String value)
{
if (!props.containsKey(key)) {
props.put(key, value);
}
}
}
@@ -1,105 +0,0 @@
//
// ooo-util - a place for OOO utilities
// Copyright (C) 2011 Three Rings Design, Inc., All Rights Reserved
// http://github.com/threerings/ooo-util/blob/master/LICENSE
package com.threerings.util;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.sql.DataSource;
import org.postgresql.jdbc2.optional.PoolingDataSource;
import com.samskivert.depot.ConnectionProvider;
import com.samskivert.depot.DataSourceConnectionProvider;
import com.samskivert.util.Config;
import com.samskivert.util.MissingPropertyException;
import static com.samskivert.util.PropertiesUtil.requireProperty;
/**
* Postgres related utility methods.
*/
public class PostgresUtil
{
/**
* Creates a ConnectionProvider that communicates with a Postgres database server using
* read-only and read-write connections via a connection pooled data source. The supplied
* config must provide the following configuration:
* <pre>
* db.default.server = DBHOST
* db.default.port = 5432
* db.default.database = DATABASE
* db.default.username = USERNAME
* db.default.password = PASSWORD
* db.readonly.maxconns = 1
* db.readwrite.maxconns = 1
* </pre>
* It can optionally provide overrides for the server, port, database, username and password
* fields for the readonly and readwrite conncetions:
* <pre>
* db.readonly.server = READSLAVE
* db.readonly.username = READUSER
* db.readonly.password = READPASS
* db.readwrite.server = MASTER
* ...
* </pre>
*
* @param config used to obtain the configuration data.
* @param projectId a unique identifier for the system creating this provider which will be
* used to prefix the identifiers of the data source names (which are JVM global).
*/
public static ConnectionProvider createPoolingProvider (Config config, String projectId)
{
return createPoolingProvider(config, projectId, "db");
}
/**
* Creates a ConnectionProvider as in {@link #createPoolingProvider(Config,String)}, but uses
* the supplied {@code prefix} in place of {@code db}.
*/
public static ConnectionProvider createPoolingProvider (
Config config, String projectId, String prefix)
{
final DataSource[] sources = new DataSource[2];
String[] modes = new String[] { "readonly", "readwrite" };
String defPrefix = prefix + ".default";
for (int ii = 0; ii < sources.length; ii++) {
String modePrefix = prefix + "." + modes[ii];
try {
// start with defaults, then apply overrides
Properties props = config.getSubProperties(defPrefix);
config.getSubProperties(modePrefix, props);
PoolingDataSource source = new PoolingDataSource();
source.setDataSourceName(projectId + "." + modes[ii]);
source.setServerName(requireProperty(props, "server"));
source.setDatabaseName(requireProperty(props, "database"));
source.setPortNumber(Integer.parseInt(requireProperty(props, "port")));
source.setUser(requireProperty(props, "username"));
source.setPassword(requireProperty(props, "password"));
source.setMaxConnections(Integer.parseInt(props.getProperty("maxconns", "1")));
sources[ii] = source;
} catch (MissingPropertyException mpe) {
String key = mpe.getKey();
throw new MissingPropertyException(
key, "Unable to locate required property '" + key + "' as '" +
defPrefix + "." + key + "' or '" + modePrefix + "." + key + "'.");
}
}
return new DataSourceConnectionProvider("jdbc:postgresql", sources[0], sources[1]) {
@Override public void shutdown () {
if (_shutdown.getAndSet(true)) {
// Only shutdown the sources once; Postgres' pooling data source doesn't like
// multiple shutdown calls
return;
}
for (DataSource source : sources) {
((PoolingDataSource)source).close();
}
}
protected AtomicBoolean _shutdown = new AtomicBoolean();
};
}
}