From dc450cd25c0448923d6e7ac44ca557135e465964 Mon Sep 17 00:00:00 2001 From: j2blake Date: Mon, 3 Oct 2011 17:03:51 +0000 Subject: [PATCH] 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. --- .../servlet/setup/JenaDataSourceSetup.java | 84 ++++++++++--------- .../setup/JenaDataSourceSetupBase.java | 25 +++--- 2 files changed, 60 insertions(+), 49 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java index 4659e6f4b..fbd9eafc7 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java @@ -197,7 +197,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java applicationMetadataModel.getBaseModel().register(new ModelSynchronizer(applicationMetadataModelDB)); if (isFirstStartup()) { - applicationMetadataModel.add(InitialJenaModelUtils.loadInitialModel(ctx, defaultNamespace)); + applicationMetadataModel.add(InitialJenaModelUtils.loadInitialModel(ctx, getDefaultNamespace(ctx))); } else if (applicationMetadataModelDB.size() == 0) { 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); } - checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), defaultNamespace, ctx ); + checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx ); if (isFirstStartup()) { loadDataFromFilesystem(baseOms, ctx); @@ -236,7 +236,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java baseOms, bds, storeDesc, - defaultNamespace, + getDefaultNamespace(ctx), null, null, WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); @@ -250,7 +250,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java inferenceOms, bds, storeDesc, - defaultNamespace, + getDefaultNamespace(ctx), null, null, WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); @@ -264,7 +264,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java // ModelFactory.createUnion(unionABoxModel, unionTBoxModel)); unionOms.setFullModel(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); ModelContext.setUnionOntModelSelector(unionOms, ctx); // assertions and inferences @@ -277,7 +277,7 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java //ctx.setAttribute("NamespaceMapper", namespaceMapper); //unionOms.getFullModel().getBaseModel().register(namespaceMapper); - ctx.setAttribute("defaultNamespace", defaultNamespace); + ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx)); log.info("SDB store ready for use"); @@ -290,38 +290,46 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java log.info("Model makers set up"); } - - private void checkForNamespaceMismatch(OntModel model, String defaultNamespace, ServletContext ctx) { - String defaultNamespaceFromDeployProperites = ConfigurationProperties - .getBean(ctx).getProperty("Vitro.defaultNamespace"); - if( defaultNamespaceFromDeployProperites == null ){ - log.error("Could not get namespace from deploy.properties."); - } - - List portalURIs = new ArrayList(); - try { - model.enterCriticalSection(Lock.READ); - Iterator portalIt = model.listIndividuals(PORTAL); - while (portalIt.hasNext()) { - portalURIs.add( portalIt.next().getURI() ); - } - } finally { - model.leaveCriticalSection(); - } - if( portalURIs.size() > 0 ){ - for( String portalUri : portalURIs){ - if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){ - StartupStatus ss = StartupStatus.getBean(ctx); - 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."); - } - } - } - } + + /** + * If we find a "portal1" portal (and we should), its URI should use the + * default namespace. + */ + private void checkForNamespaceMismatch(OntModel model, ServletContext ctx) { + String expectedNamespace = getDefaultNamespace(ctx); + + List portalURIs = new ArrayList(); + try { + model.enterCriticalSection(Lock.READ); + Iterator portalIt = model.listIndividuals(PORTAL); + while (portalIt.hasNext()) { + portalURIs.add(portalIt.next().getURI()); + } + } finally { + model.leaveCriticalSection(); + } + + for (String portalUri : portalURIs) { + if (portalUri == null) { + continue; + } + + 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."); + } + } + } /* ====================================================================== */ diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java index 03b63a2fd..4e0dd7807 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupBase.java @@ -34,8 +34,9 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSDBModelMaker; public class JenaDataSourceSetupBase extends JenaBaseDaoCon { - - private static final Log log = LogFactory.getLog( + private static final String VITRO_DEFAULT_NAMESPACE = "Vitro.defaultNamespace"; + + private static final Log log = LogFactory.getLog( JenaDataSourceSetupBase.class); protected final static int DEFAULT_MAXWAIT = 10000, // ms @@ -126,11 +127,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { static final String JENA_DISPLAY_DISPLAY_MODEL = 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 static final OntModelSpec DB_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( 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 : DB_ONT_MODEL_SPEC; @@ -457,6 +449,17 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { 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(){ return vjmm; }