some improvements to data source setup

This commit is contained in:
bjl23 2011-01-07 21:16:45 +00:00
parent 5944ca0a32
commit a201cd2c31
2 changed files with 190 additions and 103 deletions

View file

@ -35,109 +35,160 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils; import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
public class JenaDataSourceSetupBase extends JenaBaseDaoCon { public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
private static final Log log = LogFactory.getLog(JenaDataSourceSetupBase.class);
private static final Log log = LogFactory.getLog(
JenaDataSourceSetupBase.class);
protected final static int DEFAULT_MAXWAIT = 10000, // ms protected final static int DEFAULT_MAXWAIT = 10000, // ms
DEFAULT_INITIALSIZE = 8, DEFAULT_MAXACTIVE = 32,
DEFAULT_MAXACTIVE = 300, DEFAULT_MAXIDLE = 10,
DEFAULT_MAXIDLE = 30,
DEFAULT_MINIDLE = 8,
DEFAULT_TIMEBETWEENEVICTIONS = 30 * 60 * 1000, // ms DEFAULT_TIMEBETWEENEVICTIONS = 30 * 60 * 1000, // ms
DEFAULT_TESTSPEREVICTION = 3, DEFAULT_TESTSPEREVICTION = 3,
DEFAULT_MINEVICTIONIDLETIME = 1000 * 60 * 30; // ms DEFAULT_MINEVICTIONIDLETIME = 1000 * 60 * 30; // ms
protected final static String DEFAULT_VALIDATIONQUERY = "SELECT 1"; protected final static String VALIDATIONQUERY =
ConfigurationProperties.getProperty(
"VitroConnection.DataSource.validationQuery", "SELECT 1");
protected final static boolean DEFAULT_TESTONBORROW = true, protected final static boolean DEFAULT_TESTONBORROW = true,
DEFAULT_TESTONRETURN = true, DEFAULT_TESTWHILEIDLE = true; DEFAULT_TESTONRETURN = true, DEFAULT_TESTWHILEIDLE = true;
protected static String BASE = "/WEB-INF/ontologies/"; protected static String BASE = "/WEB-INF/ontologies/";
protected static String USERPATH = BASE+"user/"; protected static String USERPATH = BASE+"user/";
protected static String SYSTEMPATH = BASE+"system/"; protected static String SYSTEMPATH = BASE+"system/";
protected static String AUTHPATH = BASE+"auth/"; protected static String AUTHPATH = BASE+"auth/";
protected static String APPPATH = BASE+"app/"; protected static String APPPATH = BASE+"app/";
protected static String SUBMODELS = "/WEB-INF/submodels/"; protected static String SUBMODELS = "/WEB-INF/submodels/";
String DB_USER = "jenatest"; // database user id String DB_USER = "jenatest"; // database user id
String DB_PASSWD = "jenatest"; // database password String DB_PASSWD = "jenatest"; // database password
String DB = "MySQL"; // database type String DB = ConfigurationProperties.getProperty( // database type
String DB_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; "VitroConnection.DataSource.dbtype","MySQL");
String DB_DRIVER_CLASS_NAME = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.driver",
"com.mysql.jdbc.Driver");
// ABox assertions. These are stored in a database (Jena SDB) and the application works // ABox assertions. These are stored in a database (Jena SDB) and the
// (queries and updates) with the ABox data from the DB - this model is not maintained // application works (queries and updates) with the ABox data from the DB -
// in memory. For query performance reasons, there won't be any submodels for the ABox data. // this model is not maintained in memory. For query performance reasons,
// there won't be any submodels for the ABox data.
public static final String JENA_DB_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-2"; public static final String JENA_DB_MODEL =
"http://vitro.mannlib.cornell.edu/default/vitro-kb-2";
// ABox inferences. This is ABox data that is inferred, using VIVO's native simple, specific- // ABox inferences. This is ABox data that is inferred, using VIVO's native
// purpose reasoning based on the combination of the Abox (assertion and inferences) data // simple, specific-purpose reasoning based on the combination of the ABox
// and the TBox (assertions and inferences) data. // (assertion and inferences) data and the TBox (assertions and inferences)
public static final String JENA_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf"; // data.
public static final String JENA_INF_MODEL =
"http://vitro.mannlib.cornell.edu/default/vitro-kb-inf";
// TBox assertions. // TBox assertions.
// Some of these (the local extensions) are stored and maintained in a Jena database and // Some of these (the local extensions) are stored and maintained in a Jena
// are also maintained in memory while the application is running. // database and are also maintained in memory while the application is
// Other parts of the TBox, the 'VIVO Core', are also backed by a Jena DB, but they are // running. Other parts of the TBox, the 'VIVO Core,' are also backed by a
// read fresh from files each time the application starts. While the application is running, // Jena DB, but they are read fresh from files each time the application
// they are kept in memory, as submodels of the in memory copy of this named graph. // starts. While the application is running, they are kept in memory, as
public static final String JENA_TBOX_ASSERTIONS_MODEL = "http://vitro.mannlib.cornell.edu/default/asserted-tbox"; // submodels of the in memory copy of this named graph.
public static final String JENA_TBOX_ASSERTIONS_MODEL =
"http://vitro.mannlib.cornell.edu/default/asserted-tbox";
// Inferred TBox. This is TBox data that is inferred from the combination of VIVO core TBox // Inferred TBox. This is TBox data that is inferred from the combination of
// and any local extension TBox assertions. Pellet computes these inferences. // VIVO core TBox and any local extension TBox assertions. Pellet computes
// These are stored in the DB. // these inferences. These are stored in the DB.
public static final String JENA_TBOX_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/inferred-tbox"; public static final String JENA_TBOX_INF_MODEL =
"http://vitro.mannlib.cornell.edu/default/inferred-tbox";
static final String JENA_AUDIT_MODEL = "http://vitro.mannlib.cornell.edu/ns/db/experimental/audit"; // Model for tracking edit changes. Obsolete.
static final String JENA_AUDIT_MODEL =
"http://vitro.mannlib.cornell.edu/ns/db/experimental/audit";
public static final String JENA_USER_ACCOUNTS_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts"; // User accounts data
public static final String JENA_USER_ACCOUNTS_MODEL =
"http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts";
// This model doesn't exist yet. It's a placeholder for the application ontology. // This model doesn't exist yet. It's a placeholder for the application
// ontology.
static final String JENA_APPLICATION_METADATA_MODEL =
"http://vitro.mannlib.cornell.edu/default/vitro-kb-applicationMetadata";
static final String JENA_APPLICATION_METADATA_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-applicationMetadata"; // This is Brian C's application.owl file. We may not have to be concerned
// with this for release 1.2.
static final String JENA_DISPLAY_METADATA_MODEL =
"http://vitro.mannlib.cornell.edu/default/vitro-kb-displayMetadata";
// This is Brian C's application.owl file. We may not have to be concerned with this for static final String DEFAULT_DEFAULT_NAMESPACE =
// release 1.2. "http://vitro.mannlib.cornell.edu/ns/default#";
static final String JENA_DISPLAY_METADATA_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-displayMetadata";
static final String DEFAULT_DEFAULT_NAMESPACE = "http://vitro.mannlib.cornell.edu/ns/default#"; static String defaultNamespace = DEFAULT_DEFAULT_NAMESPACE; // FIXME
static String defaultNamespace = DEFAULT_DEFAULT_NAMESPACE; // TODO: improve this // use OWL models with no reasoning
static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
// use OWL models with no reasoning private String getJdbcUrl() {
static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; String jdbcUrl = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.url");
// Ensure that MySQL handles unicode properly, else all kinds of
// horrible nastiness ensues.
if ("MySQL".equals(DB) && !jdbcUrl.contains("?")) {
jdbcUrl += "?useUnicode=yes&characterEncoding=utf8";
}
return jdbcUrl;
}
/** /**
* Sets up a Model and DB connection using values from * Sets up a Model and DB connection using values from
* a properties file. * a properties file.
*/ */
public final Model makeDBModelFromConfigurationProperties(String jenaDbModelName, OntModelSpec jenaDbOntModelSpec){ public final Model makeDBModelFromConfigurationProperties(
String dbDriverClassname = ConfigurationProperties.getProperty("VitroConnection.DataSource.driver", DB_DRIVER_CLASS_NAME); String jenaDbModelName,
String jdbcUrl = ConfigurationProperties.getProperty("VitroConnection.DataSource.url") + "?useUnicode=yes&characterEncoding=utf8"; OntModelSpec jenaDbOntModelSpec) {
String username = ConfigurationProperties.getProperty("VitroConnection.DataSource.username");
String password = ConfigurationProperties.getProperty("VitroConnection.DataSource.password");
BasicDataSource ds = makeBasicDataSource(dbDriverClassname, jdbcUrl, username, password);
String dns = ConfigurationProperties.getProperty("Vitro.defaultNamespace"); String jdbcUrl = getJdbcUrl();
defaultNamespace = (dns != null && dns.length()>0) ? dns : null;
String username = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.username");
String password = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.password");
BasicDataSource ds = makeBasicDataSource(
DB_DRIVER_CLASS_NAME, jdbcUrl, username, password);
String dns = ConfigurationProperties.getProperty(
"Vitro.defaultNamespace");
defaultNamespace = (dns != null && dns.length() > 0) ? dns : null;
jenaDbOntModelSpec = (jenaDbOntModelSpec != null)
? jenaDbOntModelSpec
: DB_ONT_MODEL_SPEC;
jenaDbOntModelSpec = (jenaDbOntModelSpec != null) ? jenaDbOntModelSpec : DB_ONT_MODEL_SPEC;
return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec); return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec);
} }
/** /**
* Sets up a BasicDataSource using values from * Sets up a BasicDataSource using values from
* a properties file. * a properties file.
*/ */
public final BasicDataSource makeDataSourceFromConfigurationProperties(){ public final BasicDataSource makeDataSourceFromConfigurationProperties(){
String dbDriverClassname = ConfigurationProperties.getProperty("VitroConnection.DataSource.driver", DB_DRIVER_CLASS_NAME); String dbDriverClassname = ConfigurationProperties.getProperty(
String jdbcUrl = ConfigurationProperties.getProperty("VitroConnection.DataSource.url") + "?useUnicode=yes&characterEncoding=utf8"; "VitroConnection.DataSource.driver", DB_DRIVER_CLASS_NAME);
String username = ConfigurationProperties.getProperty("VitroConnection.DataSource.username"); String jdbcUrl = getJdbcUrl();
String password = ConfigurationProperties.getProperty("VitroConnection.DataSource.password"); String username = ConfigurationProperties.getProperty(
return makeBasicDataSource(dbDriverClassname, jdbcUrl, username, password); "VitroConnection.DataSource.username");
} String password = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.password");
return makeBasicDataSource(
dbDriverClassname, jdbcUrl, username, password);
}
public void setApplicationDataSource(BasicDataSource bds, ServletContext ctx) { public void setApplicationDataSource(BasicDataSource bds,
ServletContext ctx) {
ctx.setAttribute(getDataSourceAttributeName(), bds); ctx.setAttribute(getDataSourceAttributeName(), bds);
} }
@ -149,9 +200,12 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
return JenaDataSourceSetupBase.class.getName() + ".dataSource"; return JenaDataSourceSetupBase.class.getName() + ".dataSource";
} }
public static BasicDataSource makeBasicDataSource(String dbDriverClassname, String jdbcUrl, String username, String password) { public static BasicDataSource makeBasicDataSource(String dbDriverClassname,
String jdbcUrl,
String username,
String password) {
log.debug("makeBasicDataSource('" + dbDriverClassname + "', '" log.debug("makeBasicDataSource('" + dbDriverClassname + "', '"
+ jdbcUrl + "', '" + username + "', '" + password + "')"); + jdbcUrl + "', '" + username + "', '" + password + "')");
BasicDataSource ds = new BasicDataSource(); BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(dbDriverClassname); ds.setDriverClassName(dbDriverClassname);
ds.setUrl(jdbcUrl); ds.setUrl(jdbcUrl);
@ -166,14 +220,12 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
log.error("Unable to parse connection pool maxActive setting " log.error("Unable to parse connection pool maxActive setting "
+ maxActiveStr + " as an integer"); + maxActiveStr + " as an integer");
} }
} }
ds.setMaxActive(maxActiveInt); ds.setMaxActive(maxActiveInt);
ds.setInitialSize(DEFAULT_INITIALSIZE);
ds.setMinIdle(DEFAULT_MINIDLE);
ds.setMaxIdle(DEFAULT_MAXIDLE); ds.setMaxIdle(DEFAULT_MAXIDLE);
ds.setMaxWait(DEFAULT_MAXWAIT); ds.setMaxWait(DEFAULT_MAXWAIT);
ds.setValidationQuery(DEFAULT_VALIDATIONQUERY); ds.setValidationQuery(VALIDATIONQUERY);
ds.setTestOnBorrow(DEFAULT_TESTONBORROW); ds.setTestOnBorrow(DEFAULT_TESTONBORROW);
ds.setTestOnReturn(DEFAULT_TESTONRETURN); ds.setTestOnReturn(DEFAULT_TESTONRETURN);
ds.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTIONIDLETIME); ds.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTIONIDLETIME);
@ -193,26 +245,46 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
RDB, SDB RDB, SDB
} }
protected Model makeDBModel(BasicDataSource ds, String jenaDbModelname, OntModelSpec jenaDbOntModelSpec) { protected Model makeDBModel(BasicDataSource ds,
return makeDBModel(ds, jenaDbModelname, jenaDbOntModelSpec, TripleStoreType.RDB); String jenaDbModelname,
OntModelSpec jenaDbOntModelSpec) {
return makeDBModel(
ds, jenaDbModelname, jenaDbOntModelSpec, TripleStoreType.RDB);
} }
protected Model makeDBModel(BasicDataSource ds, String jenaDbModelName, OntModelSpec jenaDbOntModelSpec, TripleStoreType storeType) { protected Model makeDBModel(BasicDataSource ds,
String jenaDbModelName,
OntModelSpec jenaDbOntModelSpec,
TripleStoreType storeType) {
Model dbModel = null; Model dbModel = null;
try { try {
// open the db model // open the db model
try { try {
Graph g = null; Graph g = null;
switch (storeType) { switch (storeType) {
case RDB: case RDB:
g = new RegeneratingGraph(new RDBGraphGenerator(ds, DB, jenaDbModelName)); break; g = new RegeneratingGraph(
case SDB: new RDBGraphGenerator(
StoreDesc desc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL); ds, DB, jenaDbModelName));
g = new RegeneratingGraph(new SDBGraphGenerator(ds, desc, jenaDbModelName)); break; break;
default: throw new RuntimeException ("Unsupported store type " + storeType); case SDB:
} String layoutStr = ConfigurationProperties.getProperty(
dbModel = ModelFactory.createModelForGraph(g); "VitroConnection.DataSource.sdb.layout",
log.debug("Using database at "+ds.getUrl()); "layout2/hash");
String dbtypeStr = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.dbtype", "MySQL");
StoreDesc desc = new StoreDesc(
LayoutType.fetch(layoutStr),
DatabaseType.fetch(dbtypeStr) );
g = new RegeneratingGraph(
new SDBGraphGenerator(
ds, desc, jenaDbModelName));
break;
default: throw new RuntimeException (
"Unsupported store type " + storeType);
}
dbModel = ModelFactory.createModelForGraph(g);
log.debug("Using database at "+ds.getUrl());
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
@ -229,13 +301,15 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
if (paths != null) { if (paths != null) {
for (String p : paths) { for (String p : paths) {
String format = getRdfFormat(p); String format = getRdfFormat(p);
log.info("Loading ontology file at " + p + " as format " + format); log.info("Loading ontology file at " + p +
" as format " + format);
InputStream ontologyInputStream = ctx.getResourceAsStream(p); InputStream ontologyInputStream = ctx.getResourceAsStream(p);
try { try {
model.read(ontologyInputStream, null, format); model.read(ontologyInputStream, null, format);
log.debug("...successful"); log.debug("...successful");
} catch (Throwable t) { } catch (Throwable t) {
log.error("Failed to load ontology file at '" + p + "' as format " + format, t); log.error("Failed to load ontology file at '" + p +
"' as format " + format, t);
} }
} }
} }
@ -266,7 +340,7 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
// A hard-coded MD5 encryption of "defaultAdmin" // A hard-coded MD5 encryption of "defaultAdmin"
String initialAdminPassword = "22BA075EC8951A70960A0A95C0BC2294"; String initialAdminPassword = "22BA075EC8951A70960A0A95C0BC2294";
String vitroDefaultNs = "http://vitro.mannlib.cornell.edu/ns/vitro/default#"; String vitroDefaultNs = DEFAULT_DEFAULT_NAMESPACE;
Resource user = model.createResource(vitroDefaultNs Resource user = model.createResource(vitroDefaultNs
+ "defaultAdminUser"); + "defaultAdminUser");
@ -291,29 +365,37 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
private static final String rdbModelMaker = "vitroJenaModelMaker"; private static final String rdbModelMaker = "vitroJenaModelMaker";
protected void makeModelMakerFromConnectionProperties(TripleStoreType type){ protected void makeModelMakerFromConnectionProperties(TripleStoreType type){
String jdbcUrl = ConfigurationProperties.getProperty("VitroConnection.DataSource.url") String jdbcUrl = getJdbcUrl();
+ "?useUnicode=yes&characterEncoding=utf8"; String username = ConfigurationProperties.getProperty(
String username = ConfigurationProperties.getProperty("VitroConnection.DataSource.username"); "VitroConnection.DataSource.username");
String password = ConfigurationProperties.getProperty("VitroConnection.DataSource.password"); String password = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.password");
if (TripleStoreType.RDB.equals(type)){ if (TripleStoreType.RDB.equals(type)){
vjmm = new VitroJenaModelMaker(jdbcUrl, username, password, DB_TYPE); vjmm = new VitroJenaModelMaker(
jdbcUrl, username, password, DB_TYPE);
} }
else if(TripleStoreType.SDB.equals(type)){ else if(TripleStoreType.SDB.equals(type)){
StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash,DatabaseType.MySQL); StoreDesc storeDesc = new StoreDesc(
SDBConnection sdbConn = new SDBConnection(jdbcUrl,username,password); LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL);
SDBConnection sdbConn = new SDBConnection(
jdbcUrl, username, password);
Store store = SDBFactory.connectStore(sdbConn, storeDesc); Store store = SDBFactory.connectStore(sdbConn, storeDesc);
vsmm = new VitroJenaSDBModelMaker(store); vsmm = new VitroJenaSDBModelMaker(store);
} }
return; return;
} }
public static void setVitroJenaModelMaker(VitroJenaModelMaker vjmm, ServletContextEvent sce){ public static void setVitroJenaModelMaker(VitroJenaModelMaker vjmm,
ServletContextEvent sce){
sce.getServletContext().setAttribute(rdbModelMaker, vjmm); sce.getServletContext().setAttribute(rdbModelMaker, vjmm);
} }
public static void setVitroJenaSDBModelMaker(VitroJenaSDBModelMaker vsmm, ServletContextEvent sce){ public static void setVitroJenaSDBModelMaker(VitroJenaSDBModelMaker vsmm,
ServletContextEvent sce){
sce.getServletContext().setAttribute(sdbModelMaker, vsmm); sce.getServletContext().setAttribute(sdbModelMaker, vsmm);
} }
@ -325,5 +407,4 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
return vsmm; return vsmm;
} }
} }

View file

@ -108,7 +108,13 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
checkForNamespaceMismatch( memModel, defaultNamespace ); checkForNamespaceMismatch( memModel, defaultNamespace );
// SDB initialization // SDB initialization
StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL) ; String layoutStr = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.sdb.layout","layout2/hash");
String dbtypeStr = ConfigurationProperties.getProperty(
"VitroConnection.DataSource.dbtype", "MySQL");
StoreDesc storeDesc = new StoreDesc(
LayoutType.fetch(layoutStr),
DatabaseType.fetch(dbtypeStr) );
sce.getServletContext().setAttribute("storeDesc", storeDesc); sce.getServletContext().setAttribute("storeDesc", storeDesc);
BasicDataSource bds = makeDataSourceFromConfigurationProperties(); BasicDataSource bds = makeDataSourceFromConfigurationProperties();
this.setApplicationDataSource(bds, sce.getServletContext()); this.setApplicationDataSource(bds, sce.getServletContext());