NIHVIVO-3135 Clean up the use of defaultNamespace in JenaDataSourceSetup (and sub-classes), and improve the error message if it doesn't match the one in the database.

This commit is contained in:
j2blake 2011-10-03 17:03:51 +00:00
parent 3f6b6a7d28
commit dc450cd25c
2 changed files with 60 additions and 49 deletions

View file

@ -197,7 +197,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
applicationMetadataModel.getBaseModel().register(new ModelSynchronizer(applicationMetadataModelDB)); applicationMetadataModel.getBaseModel().register(new ModelSynchronizer(applicationMetadataModelDB));
if (isFirstStartup()) { if (isFirstStartup()) {
applicationMetadataModel.add(InitialJenaModelUtils.loadInitialModel(ctx, defaultNamespace)); applicationMetadataModel.add(InitialJenaModelUtils.loadInitialModel(ctx, getDefaultNamespace(ctx)));
} else if (applicationMetadataModelDB.size() == 0) { } else if (applicationMetadataModelDB.size() == 0) {
repairAppMetadataModel(applicationMetadataModel, aboxAssertions, aboxInferences); repairAppMetadataModel(applicationMetadataModel, aboxAssertions, aboxInferences);
@ -211,7 +211,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
log.error("Unable to load application metadata model cache from DB", e); log.error("Unable to load application metadata model cache from DB", e);
} }
checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), defaultNamespace, ctx ); checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx );
if (isFirstStartup()) { if (isFirstStartup()) {
loadDataFromFilesystem(baseOms, ctx); loadDataFromFilesystem(baseOms, ctx);
@ -236,7 +236,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
baseOms, baseOms,
bds, bds,
storeDesc, storeDesc,
defaultNamespace, getDefaultNamespace(ctx),
null, null,
null, null,
WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY);
@ -250,7 +250,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
inferenceOms, inferenceOms,
bds, bds,
storeDesc, storeDesc,
defaultNamespace, getDefaultNamespace(ctx),
null, null,
null, null,
WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY);
@ -264,7 +264,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
// ModelFactory.createUnion(unionABoxModel, unionTBoxModel)); // ModelFactory.createUnion(unionABoxModel, unionTBoxModel));
unionOms.setFullModel(masterUnion); unionOms.setFullModel(masterUnion);
ctx.setAttribute("jenaOntModel", masterUnion); ctx.setAttribute("jenaOntModel", masterUnion);
WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null); WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, getDefaultNamespace(ctx), null, null);
ctx.setAttribute("webappDaoFactory",wadf); ctx.setAttribute("webappDaoFactory",wadf);
ModelContext.setUnionOntModelSelector(unionOms, ctx); // assertions and inferences ModelContext.setUnionOntModelSelector(unionOms, ctx); // assertions and inferences
@ -277,7 +277,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
//ctx.setAttribute("NamespaceMapper", namespaceMapper); //ctx.setAttribute("NamespaceMapper", namespaceMapper);
//unionOms.getFullModel().getBaseModel().register(namespaceMapper); //unionOms.getFullModel().getBaseModel().register(namespaceMapper);
ctx.setAttribute("defaultNamespace", defaultNamespace); ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx));
log.info("SDB store ready for use"); log.info("SDB store ready for use");
@ -291,12 +291,12 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
log.info("Model makers set up"); log.info("Model makers set up");
} }
private void checkForNamespaceMismatch(OntModel model, String defaultNamespace, ServletContext ctx) { /**
String defaultNamespaceFromDeployProperites = ConfigurationProperties * If we find a "portal1" portal (and we should), its URI should use the
.getBean(ctx).getProperty("Vitro.defaultNamespace"); * default namespace.
if( defaultNamespaceFromDeployProperites == null ){ */
log.error("Could not get namespace from deploy.properties."); private void checkForNamespaceMismatch(OntModel model, ServletContext ctx) {
} String expectedNamespace = getDefaultNamespace(ctx);
List<String> portalURIs = new ArrayList<String>(); List<String> portalURIs = new ArrayList<String>();
try { try {
@ -308,17 +308,25 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
} finally { } finally {
model.leaveCriticalSection(); model.leaveCriticalSection();
} }
if( portalURIs.size() > 0 ){
for (String portalUri : portalURIs) { for (String portalUri : portalURIs) {
if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){ if (portalUri == null) {
StartupStatus ss = StartupStatus.getBean(ctx); continue;
ss.warning(this, "Namespace mismatch between db and deploy.properties.");
ss.warning(this, "Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " +
"a portal in the database. Namespace from deploy.properties: \"" + defaultNamespaceFromDeployProperites +
"\" Namespace from an existing portal: \"" + portalUri + "\" To get the application to start with this " +
"database change the default namespace in deploy.properties " + portalUri.substring(0, portalUri.lastIndexOf("/")+1) +
" Another possibility is that deploy.properties does not specify the intended database.");
} }
int splitHere = portalUri.lastIndexOf("/") + 1;
String actualNamespace = portalUri.substring(0, splitHere);
String portalName = portalUri.substring(splitHere);
if ("portal1".equals(portalName)
&& (!expectedNamespace.equals(actualNamespace))) {
StartupStatus ss = StartupStatus.getBean(ctx);
ss.fatal(this, "Default namespace mis-match. "
+ "The default namespace specified in deploy.properties ('" + expectedNamespace + "') "
+ "does not match the namespace used in the database ('" + actualNamespace + "'). "
+ "Perhaps the 'Vitro.defaultNamespace' property in deploy.properties is incorrect, "
+ "or perhaps the 'VitroConnection.DataSource.url' property in deploy.properties "
+ "specifies the wrong database.");
} }
} }
} }

View file

@ -34,6 +34,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker;
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker;
public class JenaDataSourceSetupBase extends JenaBaseDaoCon { public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
private static final String VITRO_DEFAULT_NAMESPACE = "Vitro.defaultNamespace";
private static final Log log = LogFactory.getLog( private static final Log log = LogFactory.getLog(
JenaDataSourceSetupBase.class); JenaDataSourceSetupBase.class);
@ -126,11 +127,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
static final String JENA_DISPLAY_DISPLAY_MODEL = static final String JENA_DISPLAY_DISPLAY_MODEL =
DisplayVocabulary.DISPLAY_DISPLAY_MODEL_URI; DisplayVocabulary.DISPLAY_DISPLAY_MODEL_URI;
static final String DEFAULT_DEFAULT_NAMESPACE =
"http://vitro.mannlib.cornell.edu/ns/default#";
static String defaultNamespace = DEFAULT_DEFAULT_NAMESPACE; // FIXME
// use OWL models with no reasoning // use OWL models with no reasoning
static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
@ -166,10 +162,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
BasicDataSource ds = makeBasicDataSource( BasicDataSource ds = makeBasicDataSource(
getDbDriverClassName(ctx), jdbcUrl, username, password, ctx); getDbDriverClassName(ctx), jdbcUrl, username, password, ctx);
String dns = ConfigurationProperties.getBean(ctx).getProperty(
"Vitro.defaultNamespace");
defaultNamespace = (dns != null && dns.length() > 0) ? dns : null;
jenaDbOntModelSpec = (jenaDbOntModelSpec != null) jenaDbOntModelSpec = (jenaDbOntModelSpec != null)
? jenaDbOntModelSpec ? jenaDbOntModelSpec
: DB_ONT_MODEL_SPEC; : DB_ONT_MODEL_SPEC;
@ -457,6 +449,17 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
ctx.setAttribute(sdbModelMaker, vsmm); ctx.setAttribute(sdbModelMaker, vsmm);
} }
protected String getDefaultNamespace(ServletContext ctx) {
String dns = ConfigurationProperties.getBean(ctx).getProperty(
VITRO_DEFAULT_NAMESPACE);
if ((dns != null) && (!dns.isEmpty())) {
return dns;
} else {
throw new IllegalStateException("deploy.properties does not "
+ "contain a value for '" + VITRO_DEFAULT_NAMESPACE + "'");
}
}
protected VitroJenaModelMaker getVitroJenaModelMaker(){ protected VitroJenaModelMaker getVitroJenaModelMaker(){
return vjmm; return vjmm;
} }