diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroRequest.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroRequest.java index 7f071e2d8..33d9a57ef 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroRequest.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/VitroRequest.java @@ -114,15 +114,15 @@ public class VitroRequest extends HttpServletRequestWrapper { public WebappDaoFactory getAssertionsWebappDaoFactory() { Object webappDaoFactoryAttr = _req.getSession().getAttribute("assertionsWebappDaoFactory"); if (webappDaoFactoryAttr instanceof WebappDaoFactory) { - log.info("Returning assertionsWebappDaoFactory from session"); + log.debug("Returning assertionsWebappDaoFactory from session"); return (WebappDaoFactory) webappDaoFactoryAttr; } else { webappDaoFactoryAttr = getAttribute("assertionsWebappDaoFactory"); if (webappDaoFactoryAttr instanceof WebappDaoFactory) { - log.info("returning assertionsWebappDaoFactory from request attribute"); + log.debug("returning assertionsWebappDaoFactory from request attribute"); return (WebappDaoFactory) webappDaoFactoryAttr; } else { - log.info("Returning assertionsWebappDaoFactory from context"); + log.debug("Returning assertionsWebappDaoFactory from context"); return (WebappDaoFactory) _req.getSession().getServletContext().getAttribute("assertionsWebappDaoFactory"); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaIngestController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaIngestController.java index 8cb1e0c27..149d64af5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaIngestController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaIngestController.java @@ -74,7 +74,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.VitroJenaSpecialModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent; -import edu.cornell.mannlib.vitro.webapp.servlet.setup.WebappDaoSDBSetup; +import edu.cornell.mannlib.vitro.webapp.servlet.setup.WebappDaoSetup; import edu.cornell.mannlib.vitro.webapp.utils.SparqlQueryUtils; import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestUtils; import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestUtils.MergeResult; @@ -943,7 +943,7 @@ public class JenaIngestController extends BaseEditController { log.debug("Connecting to DB at "+jdbcUrl); StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash,dbTypeObj) ; ServletContext ctx = vreq.getSession().getServletContext(); - BasicDataSource bds = WebappDaoSDBSetup.makeBasicDataSource( + BasicDataSource bds = WebappDaoSetup.makeBasicDataSource( driver, jdbcUrl, username, password, ctx); try { VitroJenaSDBModelMaker vsmm = new VitroJenaSDBModelMaker(storeDesc, bds); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ModelContext.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ModelContext.java index 4d2a84f98..57d835a4c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ModelContext.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ModelContext.java @@ -111,16 +111,6 @@ public class ModelContext { log.error(e,e); } -// ModelContext.getJenaOntModel(ctx).register(ml); -// ModelContext.getBaseOntModel(ctx).register(ml); -// ModelContext.getInferenceOntModel(ctx).register(ml); -// ModelContext.getUnionOntModelSelector(ctx).getABoxModel().register(ml); -// ModelContext.getBaseOntModelSelector(ctx).getABoxModel().register(ml); -// ModelContext.getBaseOntModelSelector(ctx).getApplicationMetadataModel().register(ml); -// ModelContext.getInferenceOntModelSelector(ctx).getABoxModel().register(ml); - -// ModelContext.getBaseOntModelSelector(ctx).getTBoxModel().register(ml); - } public static OntModel getDisplayModel(ServletContext ctx){ diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/StandardModelSelector.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/StandardModelSelector.java index 4c6665172..47c0d19d2 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/StandardModelSelector.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/StandardModelSelector.java @@ -29,13 +29,13 @@ public class StandardModelSelector implements ModelSelector { } } if(sessionOntModel != null && sessionOntModel instanceof OntModel ) { - log.info("++++++++++ using OntModelSelector from session"); + log.debug("using OntModelSelector from session"); return (OntModel)sessionOntModel; } else if (vreq.getOntModelSelector() != null) { - log.info("++++++++++ using OntModelSelector from request"); + log.debug("using OntModelSelector from request"); return vreq.getOntModelSelector().getABoxModel(); } else { - log.info("++++++++++ using OntModelSelector from context"); + log.debug("using OntModelSelector from context"); return ((OntModelSelector) context .getAttribute("unionOntModelSelector")).getABoxModel(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java index 48d9dd37c..95cab9ee5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java @@ -41,6 +41,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlDataset; import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlDatasetGraph; import edu.cornell.mannlib.vitro.webapp.dao.jena.SparqlGraphMultilingual; import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB; +import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB.SDBDatasetMode; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils; @@ -110,9 +111,14 @@ public class WebappDaoFactorySDBPrep implements Filter { RDFService rdfService = factory.getRDFService(); Dataset dataset = new RDFServiceDataset(rdfService); wadf = new WebappDaoFactorySDB(rdfService, oms, config); + WebappDaoFactory assertions = new WebappDaoFactorySDB( + rdfService, oms, config, SDBDatasetMode.ASSERTIONS_ONLY); vreq.setWebappDaoFactory(wadf); + vreq.setAssertionsWebappDaoFactory(assertions); vreq.setFullWebappDaoFactory(wadf); vreq.setDataset(dataset); + vreq.setOntModelSelector(oms); + vreq.setJenaOntModel(ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, dataset.getDefaultModel())); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sdb/RDFServiceSDB.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sdb/RDFServiceSDB.java index ea49d5982..1c60855ee 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sdb/RDFServiceSDB.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/rdfservice/impl/sdb/RDFServiceSDB.java @@ -99,6 +99,10 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService { try { if (transaction) { conn.getTransactionHandler().begin(); + } else { + for (Object o : changeSet.getPreChangeEvents()) { + this.notifyListenersOfEvent(o); + } } while (csIt.hasNext()) { ModelChange modelChange = csIt.next(); @@ -124,10 +128,10 @@ public class RDFServiceSDB extends RDFServiceImpl implements RDFService { dataset.getLock().leaveCriticalSection(); } } - for (Object o : changeSet.getPreChangeEvents()) { - this.notifyListenersOfEvent(o); - } if (transaction) { + for (Object o : changeSet.getPreChangeEvents()) { + this.notifyListenersOfEvent(o); + } conn.getTransactionHandler().commit(); } for (Object o : changeSet.getPostChangeEvents()) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java index 49f895e01..4d45e537f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java @@ -77,9 +77,7 @@ public class FileGraphSetup implements ServletContextListener { log.error(errMsg); throw new ClassCastException(errMsg); } catch (Throwable t) { - System.out.println("Throwable in listener " + this.getClass().getName()); - log.error(t); - t.printStackTrace(); + log.error(t, t); } finally { OntDocumentManager.getInstance().setProcessImports(false); } 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 deleted file mode 100644 index 8350e24c8..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java +++ /dev/null @@ -1,673 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.servlet.setup; - -import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.ontology.OntModelSpec; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.ModelMaker; -import com.hp.hpl.jena.rdf.model.ResIterator; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; -import com.hp.hpl.jena.sdb.SDB; -import com.hp.hpl.jena.sdb.SDBFactory; -import com.hp.hpl.jena.sdb.Store; -import com.hp.hpl.jena.sdb.StoreDesc; -import com.hp.hpl.jena.sdb.sql.SDBConnection; -import com.hp.hpl.jena.sdb.store.DatabaseType; -import com.hp.hpl.jena.sdb.store.LayoutType; -import com.hp.hpl.jena.sdb.util.StoreUtils; -import com.hp.hpl.jena.shared.Lock; -import com.hp.hpl.jena.util.ResourceUtils; -import com.hp.hpl.jena.util.iterator.ClosableIterator; -import com.hp.hpl.jena.vocabulary.RDF; - -import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; -import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig; -import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource; -import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; -import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; - -public class JenaDataSourceSetup extends JenaDataSourceSetupBase - implements javax.servlet.ServletContextListener { - - private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class); - - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - - try { - long startTime = System.currentTimeMillis(); - setUpJenaDataSource(ctx); - log.info((System.currentTimeMillis() - startTime) / 1000 + - " seconds to set up SDB store"); - } catch (SQLException sqle) { - // SQL exceptions are fatal and should halt startup - log.error("Error using SQL database; startup aborted.", sqle); - ss.fatal(this, "Error using SQL database; startup aborted.", sqle); - } catch (Throwable t) { - log.error("Throwable in " + this.getClass().getName(), t); - ss.fatal(this, "Throwable in " + this.getClass().getName(), t); - } - - } - - private void setUpJenaDataSource(ServletContext ctx) throws SQLException { - - OntModelSelectorImpl baseOms = new OntModelSelectorImpl(); - OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl(); - OntModelSelectorImpl unionOms = new OntModelSelectorImpl(); - - OntModel userAccountsModel = ontModelFromContextAttribute( - ctx, "userAccountsOntModel"); - baseOms.setUserAccountsModel(userAccountsModel); - inferenceOms.setUserAccountsModel(userAccountsModel); - unionOms.setUserAccountsModel(userAccountsModel); - - OntModel displayModel = ontModelFromContextAttribute( - ctx,DISPLAY_ONT_MODEL); - baseOms.setDisplayModel(displayModel); - inferenceOms.setDisplayModel(displayModel); - unionOms.setDisplayModel(displayModel); - - // SDB setup - - // union default graph - SDB.getContext().set(SDB.unionDefaultGraph, true) ; - - StoreDesc storeDesc = makeStoreDesc(ctx); - setApplicationStoreDesc(storeDesc, ctx); - - BasicDataSource bds = getApplicationDataSource(ctx); - if (bds == null) { - bds = makeDataSourceFromConfigurationProperties(ctx); - setApplicationDataSource(bds, ctx); - } - - Store store = connectStore(bds, storeDesc); - setApplicationStore(store, ctx); - setStartupDataset(SDBFactory.connectDataset(store), ctx); - - if (!isSetUp(store)) { - log.info("Initializing SDB store"); - if (isFirstStartup()) { - setupSDB(ctx, store); - } else { - migrateToSDBFromExistingRDBStore(ctx, store); - } - } - - // The code below, which sets up the OntModelSelectors, controls whether - // each model is maintained in memory, in the DB, or both while the - // application is running. - - // Populate the three OntModelSelectors (BaseOntModel = assertions, - // InferenceOntModel = inferences and JenaOntModel = union of assertions - // and inferences) with the post-SDB-conversion models. - - // ABox assertions - Model aboxAssertions = makeDBModel( - bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - Model listenableAboxAssertions = ModelFactory.createUnion( - aboxAssertions, ModelFactory.createDefaultModel()); - baseOms.setABoxModel( - ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, listenableAboxAssertions)); - - // ABox inferences - Model aboxInferences = makeDBModel( - bds, JenaDataSourceSetupBase.JENA_INF_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - Model listenableAboxInferences = ModelFactory.createUnion( - aboxInferences, ModelFactory.createDefaultModel()); - inferenceOms.setABoxModel(ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, listenableAboxInferences)); - - - // Since the TBox models are in memory, they do not have timeout issues - // like the like the ABox models do (and so don't need the extra step - // to make them listenable.) - - // TBox assertions - try { - Model tboxAssertionsDB = makeDBModel( - bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel tboxAssertions = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxAssertionsDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox assertions into memory"); - tboxAssertions.add(tboxAssertionsDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox assertions"); - } - - tboxAssertions.getBaseModel().register(new ModelSynchronizer( - tboxAssertionsDB)); - baseOms.setTBoxModel(tboxAssertions); - } catch (Throwable e) { - log.error("Unable to load tbox assertion cache from DB", e); - } - - // TBox inferences - try { - Model tboxInferencesDB = makeDBModel( - bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel tboxInferences = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxInferencesDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox inferences into memory"); - tboxInferences.add(tboxInferencesDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox inferences"); - } - - tboxInferences.getBaseModel().register(new ModelSynchronizer( - tboxInferencesDB)); - inferenceOms.setTBoxModel(tboxInferences); - } catch (Throwable e) { - log.error("Unable to load tbox inference cache from DB", e); - } - - // union ABox - OntModel unionABoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getABoxModel(), inferenceOms.getABoxModel())); - unionOms.setABoxModel(unionABoxModel); - - // union TBox - OntModel unionTBoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getTBoxModel(), inferenceOms.getTBoxModel())); - unionOms.setTBoxModel(unionTBoxModel); - - - // Application metadata model is cached in memory. - try { - - Model applicationMetadataModelDB = makeDBModel( - bds, JENA_APPLICATION_METADATA_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel applicationMetadataModel = - ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); - - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached application metadata model into memory"); - applicationMetadataModel.add(applicationMetadataModelDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load application metadata model " + - "assertions of size " + applicationMetadataModel.size()); - applicationMetadataModel.getBaseModel().register( - new ModelSynchronizer(applicationMetadataModelDB)); - - if (isFirstStartup()) { - applicationMetadataModel.add( - InitialJenaModelUtils.loadInitialModel( - ctx, getDefaultNamespace(ctx))); - - } else if (applicationMetadataModelDB.size() == 0) { - repairAppMetadataModel( - applicationMetadataModel, aboxAssertions, - aboxInferences); - } - - baseOms.setApplicationMetadataModel(applicationMetadataModel); - inferenceOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - unionOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - - } catch (Throwable e) { - log.error("Unable to load application metadata model cache from DB" - , e); - } - - checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx ); - - if (isFirstStartup()) { - loadDataFromFilesystem(baseOms, ctx); - } - - log.info("Setting up union models and DAO factories"); - - // create TBox + ABox union models and set up webapp DAO factories - OntModel baseUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion(baseOms.getABoxModel(), - baseOms.getTBoxModel())); - baseOms.setFullModel(baseUnion); - ModelContext.setBaseOntModel(baseOms.getFullModel(), ctx); - WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); - config.setDefaultNamespace(getDefaultNamespace(ctx)); -// WebappDaoFactory baseWadf = new WebappDaoFactorySDB( -// baseOms, bds, storeDesc, config, -// WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); -// ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); - - OntModel inferenceUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion( - inferenceOms.getABoxModel(), - inferenceOms.getTBoxModel())); - inferenceOms.setFullModel(inferenceUnion); - ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), ctx); -// WebappDaoFactory infWadf = new WebappDaoFactorySDB( -// inferenceOms, bds, storeDesc, config, -// WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); -// ctx.setAttribute("deductionsWebappDaoFactory", infWadf); - -// OntModel masterUnion = ModelFactory.createOntologyModel( -// DB_ONT_MODEL_SPEC, makeDBModel( -// bds, WebappDaoFactorySDB.UNION_GRAPH, -// DB_ONT_MODEL_SPEC, TripleStoreType.SDB, ctx)); -// unionOms.setFullModel(masterUnion); -// ctx.setAttribute("jenaOntModel", masterUnion); -// WebappDaoFactory wadf = new WebappDaoFactorySDB( -// unionOms, bds, storeDesc, config); -// ctx.setAttribute("webappDaoFactory",wadf); - - ModelContext.setOntModelSelector(unionOms, ctx); - ModelContext.setUnionOntModelSelector(unionOms, ctx); - // assertions and inferences - ModelContext.setBaseOntModelSelector(baseOms, ctx); - // assertions - ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); - // inferences - - ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx)); - - log.info("SDB store ready for use"); - - makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); - VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); - setVitroJenaModelMaker(vjmm, ctx); - makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); - ModelMaker vsmm = getVitroJenaSDBModelMaker(); - setVitroJenaSDBModelMaker(vsmm, ctx); - - //bdc34: I have no reason for vsmm vs vjmm. - //I don't know what are the implications of this choice. - setVitroModelSource( new VitroModelSource(vsmm,ctx), ctx); - - log.info("Model makers set up"); - } - - - /** - * 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 portals = getPortal1s(model); - - if(!portals.isEmpty() && noPortalForNamespace( - portals, expectedNamespace)) { - // There really should be only one portal 1, but if there happen to - // be multiple, just arbitrarily pick the first in the list. - Resource portal = portals.get(0); - String oldNamespace = portal.getNameSpace(); - renamePortal(portal, expectedNamespace, model); - StartupStatus ss = StartupStatus.getBean(ctx); - ss.warning(this, "\nThe default namespace has been changed \n" + - "from " + oldNamespace + - "\nto " + expectedNamespace + ".\n" + - "The application will function normally, but " + - "any individuals in the \n" + oldNamespace + " " + - "namespace will need to have their URIs \n" + - "changed in order to be served as linked data. " + - "You can use the Ingest Tools \nto change the " + - "URIs for a batch of resources."); - } - } - - private List getPortal1s(Model model) { - List portals = new ArrayList(); - try { - model.enterCriticalSection(Lock.READ); - ResIterator portalIt = model.listResourcesWithProperty( - RDF.type, PORTAL); - while (portalIt.hasNext()) { - Resource portal = portalIt.nextResource(); - if ("portal1".equals(portal.getLocalName())) { - portals.add(portal); - } - } - } finally { - model.leaveCriticalSection(); - } - return portals; - } - - private boolean noPortalForNamespace(List portals, - String expectedNamespace) { - for (Resource portal : portals) { - if(expectedNamespace.equals(portal.getNameSpace())) { - return false; - } - } - return true; - } - - private void renamePortal(Resource portal, String namespace, Model model) { - model.enterCriticalSection(Lock.WRITE); - try { - ResourceUtils.renameResource( - portal, namespace + portal.getLocalName()); - } finally { - model.leaveCriticalSection(); - } - } - - - /* ===================================================================== */ - - - @Override - public void contextDestroyed(ServletContextEvent sce) { - // Nothing to do. - } - - protected OntModel ontModelFromContextAttribute(ServletContext ctx, - String attribute) { - OntModel ontModel; - Object attributeValue = ctx.getAttribute(attribute); - if (attributeValue != null && attributeValue instanceof OntModel) { - ontModel = (OntModel) attributeValue; - } else { - ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); - ctx.setAttribute(attribute, ontModel); - } - return ontModel; - } - - private boolean isEmpty(Model model) { - ClosableIterator closeIt = model.listStatements( - null, RDF.type, ResourceFactory.createResource( - VitroVocabulary.PORTAL)); - try { - if (closeIt.hasNext()) { - return false; - } else { - return true; - } - } finally { - closeIt.close(); - } - } - - private void loadDataFromFilesystem(OntModelSelector baseOms, - ServletContext ctx) { - Long startTime = System.currentTimeMillis(); - log.debug("Initializing models from RDF files"); - readOntologyFilesInPathSet(USER_ABOX_PATH, ctx, baseOms.getABoxModel()); - readOntologyFilesInPathSet(USER_TBOX_PATH, ctx, baseOms.getTBoxModel()); - readOntologyFilesInPathSet( - USER_APPMETA_PATH, ctx, baseOms.getApplicationMetadataModel()); - log.debug(((System.currentTimeMillis() - startTime) / 1000) - + " seconds to read RDF files "); - } - - private static void getTBoxModel(Model fullModel, - Model submodels, - Model tboxModel) { - - JenaModelUtils modelUtils = new JenaModelUtils(); - - Model tempModel = ModelFactory.createUnion(fullModel, submodels); - Model tempTBoxModel = modelUtils.extractTBox(tempModel); - - // copy intersection of tempTBoxModel and fullModel to tboxModel. - StmtIterator iter = tempTBoxModel.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (fullModel.contains(stmt)) { - tboxModel.add(stmt); - } - } - - return; - } - - /* - * Copy all statements from model 1 that are not in model 2 to model 3. - */ - private static void copyDifference(Model model1, - Model model2, - Model model3) { - - StmtIterator iter = model1.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (!model2.contains(stmt)) { - model3.add(stmt); - } - } - - return; - } - - protected static void getAppMetadata(Model source, Model target) { - - String amdQuery = "DESCRIBE ?x WHERE { " + - "{?x a <" + VitroVocabulary.PORTAL +"> } UNION " + - "{?x a <" + VitroVocabulary.PROPERTYGROUP +"> } UNION " + - "{?x a <" + VitroVocabulary.CLASSGROUP +"> } } "; - - try { - Query q = QueryFactory.create(amdQuery, Syntax.syntaxARQ); - QueryExecution qe = QueryExecutionFactory.create(q, source); - qe.execDescribe(target); - } catch (Exception e) { - log.error("unable to create the application metadata model",e); - } - - return; - } - - protected static void repairAppMetadataModel(Model applicationMetadataModel, - Model aboxAssertions, - Model aboxInferences) { - - log.info("Moving application metadata from ABox to dedicated model"); - getAppMetadata(aboxAssertions, applicationMetadataModel); - getAppMetadata(aboxInferences, applicationMetadataModel); - aboxAssertions.remove(applicationMetadataModel); - aboxInferences.remove(applicationMetadataModel); - - return; - } - - public static StoreDesc makeStoreDesc(ServletContext ctx) { - String layoutStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.sdb.layout", "layout2/hash"); - String dbtypeStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.dbtype", "MySQL"); - return new StoreDesc( - LayoutType.fetch(layoutStr), - DatabaseType.fetch(dbtypeStr) ); - } - - public static Store connectStore(BasicDataSource bds, StoreDesc storeDesc) - throws SQLException { - SDBConnection conn = new SDBConnection(bds.getConnection()) ; - return SDBFactory.connectStore(conn, storeDesc); - } - - public static void setupSDB(ServletContext ctx, Store store) { - setupSDB(ctx, - store, - ModelFactory.createDefaultModel(), - ModelFactory.createDefaultModel()); - } - - public static void setupSDB(ServletContext ctx, - Store store, - Model memModel, - Model inferenceModel) { - - store.getTableFormatter().create(); - store.getTableFormatter().truncate(); - - store.getTableFormatter().dropIndexes(); // improve load performance - - try { - - // This is a one-time copy of stored KB data - from a Jena RDB store - // to a Jena SDB store. In the process, we will also separate out - // the TBox from the Abox; these are in the same graph in pre-1.2 - // VIVO versions and will now be stored and maintained in separate - // models. Access to the Jena RDB data is through the - // OntModelSelectors that have been set up earlier in the current - // session by JenaPersistentDataSourceSetup.java. In the code - // below, note that the current getABoxModel() methods on the - // OntModelSelectors return a graph with both ABox and TBox data. - - OntModel submodels = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - readOntologyFilesInPathSet(SUBMODELS, ctx, submodels); - - Model tboxAssertions = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxAssertions = ModelFactory.createDefaultModel(); - getTBoxModel(memModel, submodels, memTboxAssertions); - tboxAssertions.add(memTboxAssertions); - - Model tboxInferences = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxInferences = ModelFactory.createDefaultModel(); - getTBoxModel(inferenceModel, submodels, memTboxInferences); - tboxInferences.add(memTboxInferences); - - Model aboxAssertions = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_DB_MODEL); - copyDifference(memModel, memTboxAssertions, aboxAssertions); - - Model aboxInferences = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_INF_MODEL); - copyDifference(inferenceModel, memTboxInferences, aboxInferences); - - // Set up the application metadata model - Model applicationMetadataModel = SDBFactory.connectNamedModel( - store, - JenaDataSourceSetupBase.JENA_APPLICATION_METADATA_MODEL); - getAppMetadata(memModel, applicationMetadataModel); - log.info("During initial SDB setup, created an application " + - "metadata model of size " + - applicationMetadataModel.size()); - - // remove application metadata from ABox model - aboxAssertions.remove(applicationMetadataModel); - aboxInferences.remove(applicationMetadataModel); - - // Make sure the reasoner takes into account the newly-set-up data. - SimpleReasonerSetup.setRecomputeRequired(ctx); - - } finally { - log.info("Adding indexes to SDB database tables."); - store.getTableFormatter().addIndexes(); - log.info("Indexes created."); - } - } - - private void migrateToSDBFromExistingRDBStore(ServletContext ctx, - Store store) { - Model rdbAssertionsModel = makeDBModelFromConfigurationProperties( - JENA_DB_MODEL, DB_ONT_MODEL_SPEC, ctx); - Model rdbInferencesModel = makeDBModelFromConfigurationProperties( - JENA_INF_MODEL, DB_ONT_MODEL_SPEC, ctx); - setupSDB(ctx, store, rdbAssertionsModel, rdbInferencesModel); - } - - - - /** - * Tests whether an SDB store has been formatted and populated for use. - * @param store - * @return - */ - private boolean isSetUp(Store store) throws SQLException { - if (!(StoreUtils.isFormatted(store))) { - return false; - } - - // even if the store exists, it may be empty - - try { - return (SDBFactory.connectNamedModel( - store, - JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL)) - .size() > 0; - } catch (Exception e) { - return false; - } - } - - private static final String STOREDESC_ATTR = "storeDesc"; - private static final String STORE_ATTR = "kbStore"; - - public static void setApplicationStoreDesc(StoreDesc storeDesc, - ServletContext ctx) { - ctx.setAttribute(STOREDESC_ATTR, storeDesc); - } - - public static StoreDesc getApplicationStoreDesc(ServletContext ctx) { - return (StoreDesc) ctx.getAttribute(STOREDESC_ATTR); - } - - public static void setApplicationStore(Store store, - ServletContext ctx) { - ctx.setAttribute(STORE_ATTR, store); - } - - public static Store getApplicationStore(ServletContext ctx) { - return (Store) ctx.getAttribute(STORE_ATTR); - } - } - 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 695efac70..eddee1b45 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 @@ -75,7 +75,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { public static String APPPATH = BASE+"app/"; //these files are loaded everytime the system starts up public static String APPPATH_LOAD = APPPATH + "menuload/"; - protected static String SUBMODELS = "/WEB-INF/submodels/"; //All files in this directory will be reloaded every startup //and attached as sub-models to the displayOntModel. @@ -430,7 +429,7 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { } else if (TripleStoreType.SDB.equals(type)) { StoreDesc storeDesc = new StoreDesc( LayoutType.LayoutTripleNodesHash, DatabaseType.fetch(dbtypeStr)); - BasicDataSource bds = WebappDaoSDBSetup.makeBasicDataSource( + BasicDataSource bds = WebappDaoSetup.makeBasicDataSource( getDbDriverClassName(ctx), jdbcUrl, username, password, ctx); bds.setMaxActive(4); // for now, the SDB model makers should not use more // than a small handful of connections diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelSetup.java deleted file mode 100644 index 7de262fc4..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelSetup.java +++ /dev/null @@ -1,239 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.servlet.setup; - -import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.ontology.OntModelSpec; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; - -import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; -import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase.TripleStoreType; -import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; -import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; - -/** - * Setup the ABox, TBox, inference and Union models. - * Also setup the OntModelSelectors. - */ -public class ModelSetup extends JenaDataSourceSetupBase -implements javax.servlet.ServletContextListener { - private static final Log log = LogFactory.getLog(ModelSetup.class); - - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - - BasicDataSource bds = getApplicationDataSource(ctx); - if( bds == null ){ - ss.fatal(this, "A DataSource must be setup before ModelSetup "+ - "is run. Make sure that JenaPersistentDataSourceSetup runs before "+ - "ModelSetup."); - return; - } - - setupModels(ctx,ss,bds); - } - - private void setupModels(ServletContext ctx, StartupStatus ss, BasicDataSource bds){ - log.info("Setting up model makers and union models"); - - /////////////////////////////////////////////////////////////// - //set up the OntModelSelectors - - OntModelSelectorImpl baseOms = new OntModelSelectorImpl(); - OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl(); - OntModelSelectorImpl unionOms = new OntModelSelectorImpl(); - - //Put OntModelSelectorImpl objs into the context - ModelContext.setOntModelSelector(unionOms, ctx); - ModelContext.setUnionOntModelSelector(unionOms, ctx); - // assertions and inferences - ModelContext.setBaseOntModelSelector(baseOms, ctx); - // assertions - ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); - // inferences - - //add userAccountsModel to OntModelSelectors - OntModel userAccountsModel = ontModelFromContextAttribute( - ctx, "userAccountsOntModel"); - baseOms.setUserAccountsModel(userAccountsModel); - inferenceOms.setUserAccountsModel(userAccountsModel); - unionOms.setUserAccountsModel(userAccountsModel); - - //add display to OntModelSelectors - OntModel displayModel = ontModelFromContextAttribute( - ctx,DISPLAY_ONT_MODEL); - baseOms.setDisplayModel(displayModel); - inferenceOms.setDisplayModel(displayModel); - unionOms.setDisplayModel(displayModel); - - // The code below, which sets up the OntModelSelectors, controls whether - // each model is maintained in memory, in the DB, or both while the - // application is running. - - // Populate the three OntModelSelectors (BaseOntModel = assertions, - // InferenceOntModel = inferences and JenaOntModel = union of assertions - // and inferences) with the post-SDB-conversion models. - - // ABox assertions - Model aboxAssertions = makeDBModel( - bds, JenaDataSourceSetupBase.JENA_DB_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - Model listenableAboxAssertions = ModelFactory.createUnion( - aboxAssertions, ModelFactory.createDefaultModel()); - baseOms.setABoxModel( - ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, listenableAboxAssertions)); - - // ABox inferences - Model aboxInferences = makeDBModel( - bds, JenaDataSourceSetupBase.JENA_INF_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - Model listenableAboxInferences = ModelFactory.createUnion( - aboxInferences, ModelFactory.createDefaultModel()); - inferenceOms.setABoxModel(ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, listenableAboxInferences)); - - - // Since the TBox models are in memory, they do not have timeout issues - // like the like the ABox models do (and so don't need the extra step - // to make them listenable.) - - // TBox assertions - try { - Model tboxAssertionsDB = makeDBModel( - bds, JENA_TBOX_ASSERTIONS_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel tboxAssertions = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxAssertionsDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox assertions into memory"); - tboxAssertions.add(tboxAssertionsDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox assertions"); - } - - tboxAssertions.getBaseModel().register(new ModelSynchronizer( - tboxAssertionsDB)); - baseOms.setTBoxModel(tboxAssertions); - } catch (Throwable e) { - log.error("Unable to load tbox assertion cache from DB", e); - } - - // TBox inferences - try { - Model tboxInferencesDB = makeDBModel( - bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel tboxInferences = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxInferencesDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox inferences into memory"); - tboxInferences.add(tboxInferencesDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox inferences"); - } - - tboxInferences.getBaseModel().register(new ModelSynchronizer( - tboxInferencesDB)); - inferenceOms.setTBoxModel(tboxInferences); - } catch (Throwable e) { - log.error("Unable to load tbox inference cache from DB", e); - } - - // union ABox - OntModel unionABoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getABoxModel(), inferenceOms.getABoxModel())); - unionOms.setABoxModel(unionABoxModel); - - // union TBox - OntModel unionTBoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getTBoxModel(), inferenceOms.getTBoxModel())); - unionOms.setTBoxModel(unionTBoxModel); - - - // Application metadata model is cached in memory. - try { - - Model applicationMetadataModelDB = makeDBModel( - bds, JENA_APPLICATION_METADATA_MODEL, DB_ONT_MODEL_SPEC, - TripleStoreType.SDB, ctx); - OntModel applicationMetadataModel = - ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); - - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached application metadata model into memory"); - applicationMetadataModel.add(applicationMetadataModelDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load application metadata model " + - "assertions of size " + applicationMetadataModel.size()); - applicationMetadataModel.getBaseModel().register( - new ModelSynchronizer(applicationMetadataModelDB)); - - if (isFirstStartup()) { - applicationMetadataModel.add( - InitialJenaModelUtils.loadInitialModel( - ctx, getDefaultNamespace(ctx))); - - } else if (applicationMetadataModelDB.size() == 0) { - repairAppMetadataModel( - applicationMetadataModel, aboxAssertions, - aboxInferences); - } - - baseOms.setApplicationMetadataModel(applicationMetadataModel); - inferenceOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - unionOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - - } catch (Throwable e) { - log.error("Unable to load application metadata model cache from DB" - , e); - } - - // create TBox + ABox union models - OntModel baseUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion(baseOms.getABoxModel(), - baseOms.getTBoxModel())); - baseOms.setFullModel(baseUnion); - ModelContext.setBaseOntModel(baseOms.getFullModel(), ctx); - - log.info("Model makers and union set up"); - } - - @Override - public void contextDestroyed(ServletContextEvent arg0) { - // nothing to do. - - } - -} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFServiceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFServiceSetup.java index 31de85553..63a0f3a18 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFServiceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFServiceSetup.java @@ -1,12 +1,7 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ - package edu.cornell.mannlib.vitro.webapp.servlet.setup; -import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL; - import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; @@ -15,21 +10,6 @@ import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.ontology.OntModelSpec; -import com.hp.hpl.jena.query.Dataset; -import com.hp.hpl.jena.query.Query; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; -import com.hp.hpl.jena.query.QueryFactory; -import com.hp.hpl.jena.query.Syntax; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.ResIterator; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.sdb.SDB; import com.hp.hpl.jena.sdb.SDBFactory; import com.hp.hpl.jena.sdb.Store; @@ -38,601 +18,79 @@ import com.hp.hpl.jena.sdb.sql.SDBConnection; import com.hp.hpl.jena.sdb.store.DatabaseType; import com.hp.hpl.jena.sdb.store.LayoutType; import com.hp.hpl.jena.sdb.util.StoreUtils; -import com.hp.hpl.jena.shared.Lock; -import com.hp.hpl.jena.util.ResourceUtils; -import com.hp.hpl.jena.util.iterator.ClosableIterator; -import com.hp.hpl.jena.vocabulary.RDF; import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; -import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig; -import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; -import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset; -import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceModelMaker; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource; -import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceFactorySingle; import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils; import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sdb.RDFServiceSDB; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.sparql.RDFServiceSparql; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; -import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; public class RDFServiceSetup extends JenaDataSourceSetupBase - implements javax.servlet.ServletContextListener { - - private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class); - - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - - try { - long startTime = System.currentTimeMillis(); - setUpJenaDataSource(ctx); - log.info((System.currentTimeMillis() - startTime) / 1000 + - " seconds to set up SDB store"); - } catch (SQLException sqle) { - // SQL exceptions are fatal and should halt startup - log.error("Error using SQL database; startup aborted.", sqle); - ss.fatal(this, "Error using SQL database; startup aborted.", sqle); - } catch (Throwable t) { - log.error("Throwable in " + this.getClass().getName(), t); - ss.fatal(this, "Throwable in " + this.getClass().getName(), t); - } - - } +implements javax.servlet.ServletContextListener { + private static final Log log = LogFactory.getLog(RDFServiceSetup.class); - private void setUpJenaDataSource(ServletContext ctx) throws SQLException { - - OntModelSelectorImpl baseOms = new OntModelSelectorImpl(); - OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl(); - OntModelSelectorImpl unionOms = new OntModelSelectorImpl(); - - OntModel userAccountsModel = ontModelFromContextAttribute( - ctx, "userAccountsOntModel"); - baseOms.setUserAccountsModel(userAccountsModel); - inferenceOms.setUserAccountsModel(userAccountsModel); - unionOms.setUserAccountsModel(userAccountsModel); - - OntModel displayModel = ontModelFromContextAttribute( - ctx,DISPLAY_ONT_MODEL); - baseOms.setDisplayModel(displayModel); - inferenceOms.setDisplayModel(displayModel); - unionOms.setDisplayModel(displayModel); - - // SDB setup + @Override + public void contextDestroyed(ServletContextEvent arg0) { + // nothing to do + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + StartupStatus ss = StartupStatus.getBean(ctx); + try { + String endpointURI = ConfigurationProperties.getBean(sce).getProperty( + "VitroConnection.DataSource.endpointURI"); + if (endpointURI != null) { + useEndpoint(endpointURI, ctx); + } else { + useSDB(ctx, ss); + } + } catch (SQLException e) { + ss.fatal(this, "Exception in RDFServiceSetup", e); + } + } + + private void useEndpoint(String endpointURI, ServletContext ctx) { + RDFService rdfService = new RDFServiceSparql(endpointURI); + RDFServiceFactory rdfServiceFactory = new RDFServiceFactorySingle(rdfService); + RDFServiceUtils.setRDFServiceFactory(ctx, rdfServiceFactory); + } + + private void useSDB(ServletContext ctx, StartupStatus ss) throws SQLException { + BasicDataSource bds = getApplicationDataSource(ctx); + if( bds == null ){ + ss.fatal(this, "A DataSource must be setup before SDBSetup "+ + "is run. Make sure that JenaPersistentDataSourceSetup runs before "+ + "SDBSetup."); + return; + } // union default graph SDB.getContext().set(SDB.unionDefaultGraph, true) ; -// - StoreDesc storeDesc = makeStoreDesc(ctx); -// setApplicationStoreDesc(storeDesc, ctx); -// - BasicDataSource bds = getApplicationDataSource(ctx); -// if (bds == null) { -// bds = makeDataSourceFromConfigurationProperties(ctx); -// setApplicationDataSource(bds, ctx); -// } + + StoreDesc storeDesc = makeStoreDesc(ctx); + setApplicationStoreDesc(storeDesc, ctx); + + Store store = connectStore(bds, storeDesc); + setApplicationStore(store, ctx); + + if (!isSetUp(store)) { + JenaPersistentDataSourceSetup.thisIsFirstStartup(); + setupSDB(ctx, store); + } -// Store store = connectStore(bds, storeDesc); -// setApplicationStore(store, ctx); - - //okay let's make a graph-backed model - RDFService rdfService = new RDFServiceSDB(bds, storeDesc); RDFServiceFactory rdfServiceFactory = new RDFServiceFactorySingle(rdfService); RDFServiceUtils.setRDFServiceFactory(ctx, rdfServiceFactory); -// Graph g = new RDFServiceGraph(rdfService); - - Dataset dataset = new RDFServiceDataset(rdfService); - setStartupDataset(dataset, ctx); - -// setStartupDataset(SDBFactory.connectDataset(store), ctx); - -// if (!isSetUp(store)) { -// log.info("Initializing SDB store"); -// if (isFirstStartup()) { -// setupSDB(ctx, store); -// } else { -// migrateToSDBFromExistingRDBStore(ctx, store); -// } -// } - - // The code below, which sets up the OntModelSelectors, controls whether - // each model is maintained in memory, in the DB, or both while the - // application is running. - - // Populate the three OntModelSelectors (BaseOntModel = assertions, - // InferenceOntModel = inferences and JenaOntModel = union of assertions - // and inferences) with the post-SDB-conversion models. - - // ABox assertions - Model aboxAssertions = dataset.getNamedModel( - JenaDataSourceSetupBase.JENA_DB_MODEL); - baseOms.setABoxModel( - ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, aboxAssertions)); - - // ABox inferences - Model aboxInferences = dataset.getNamedModel( - JenaDataSourceSetupBase.JENA_INF_MODEL); - inferenceOms.setABoxModel(ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, aboxInferences)); - - // TBox assertions - try { - Model tboxAssertionsDB = dataset.getNamedModel( - JENA_TBOX_ASSERTIONS_MODEL); - OntModel tboxAssertions = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxAssertionsDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox assertions into memory"); - tboxAssertions.add(tboxAssertionsDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox assertions"); - } - - tboxAssertions.getBaseModel().register(new ModelSynchronizer( - tboxAssertionsDB)); - - baseOms.setTBoxModel(tboxAssertions); - } catch (Throwable e) { - log.error("Unable to load tbox assertion cache from DB", e); - } - - // TBox inferences - try { - Model tboxInferencesDB = dataset.getNamedModel(JENA_TBOX_INF_MODEL); - OntModel tboxInferences = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - - if (tboxInferencesDB != null) { - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached tbox inferences into memory"); - tboxInferences.add(tboxInferencesDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load tbox inferences"); - } - - tboxInferences.getBaseModel().register(new ModelSynchronizer( - tboxInferencesDB)); - inferenceOms.setTBoxModel(tboxInferences); - } catch (Throwable e) { - log.error("Unable to load tbox inference cache from DB", e); - } - - // union ABox - OntModel unionABoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getABoxModel(), inferenceOms.getABoxModel())); - unionOms.setABoxModel(unionABoxModel); - - // union TBox - OntModel unionTBoxModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( - baseOms.getTBoxModel(), inferenceOms.getTBoxModel())); - unionOms.setTBoxModel(unionTBoxModel); - - - // Application metadata model is cached in memory. - try { - - Model applicationMetadataModelDB = dataset.getNamedModel( - JENA_APPLICATION_METADATA_MODEL); - OntModel applicationMetadataModel = - ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); - - long startTime = System.currentTimeMillis(); - System.out.println( - "Copying cached application metadata model into memory"); - applicationMetadataModel.add(applicationMetadataModelDB); - System.out.println((System.currentTimeMillis() - startTime) - / 1000 + " seconds to load application metadata model " + - "assertions of size " + applicationMetadataModel.size()); - applicationMetadataModel.getBaseModel().register( - new ModelSynchronizer(applicationMetadataModelDB)); - - if (applicationMetadataModel.size()== 0 /* isFirstStartup() */) { - applicationMetadataModel.add( - InitialJenaModelUtils.loadInitialModel( - ctx, getDefaultNamespace(ctx))); - - } -// else if (applicationMetadataModelDB.size() == 0) { -// repairAppMetadataModel( -// applicationMetadataModel, aboxAssertions, -// aboxInferences); -// } - - baseOms.setApplicationMetadataModel(applicationMetadataModel); - inferenceOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - unionOms.setApplicationMetadataModel( - baseOms.getApplicationMetadataModel()); - - } catch (Throwable e) { - log.error("Unable to load application metadata model cache from DB" - , e); - } - - checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx ); - - if (isFirstStartup()) { - loadDataFromFilesystem(baseOms, ctx); - } - - log.info("Setting up union models and DAO factories"); - - // create TBox + ABox union models and set up webapp DAO factories - OntModel baseUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion(baseOms.getABoxModel(), - baseOms.getTBoxModel())); - baseOms.setFullModel(baseUnion); - ModelContext.setBaseOntModel(baseOms.getFullModel(), ctx); - WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); - config.setDefaultNamespace(getDefaultNamespace(ctx)); - WebappDaoFactory baseWadf = new WebappDaoFactorySDB( - rdfService, baseOms, config, - WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); - ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); - - OntModel inferenceUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion( - inferenceOms.getABoxModel(), - inferenceOms.getTBoxModel())); - inferenceOms.setFullModel(inferenceUnion); - ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), ctx); - WebappDaoFactory infWadf = new WebappDaoFactorySDB( - rdfService, inferenceOms, config, - WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); - ctx.setAttribute("deductionsWebappDaoFactory", infWadf); - - OntModel masterUnion = ModelFactory.createOntologyModel( - DB_ONT_MODEL_SPEC, dataset.getDefaultModel()); - unionOms.setFullModel(masterUnion); - ctx.setAttribute("jenaOntModel", masterUnion); - WebappDaoFactory wadf = new WebappDaoFactorySDB( - rdfService, unionOms, config); - ctx.setAttribute("webappDaoFactory",wadf); - - ModelContext.setOntModelSelector(unionOms, ctx); - ModelContext.setUnionOntModelSelector(unionOms, ctx); - // assertions and inferences - ModelContext.setBaseOntModelSelector(baseOms, ctx); - // assertions - ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); - // inferences - - ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx)); - log.info("SDB store ready for use"); - makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); - VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); - setVitroJenaModelMaker(vjmm, ctx); - makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); - RDFServiceModelMaker vsmm = new RDFServiceModelMaker(rdfServiceFactory); - setVitroJenaSDBModelMaker(vsmm, ctx); - - //bdc34: I have no reason for vsmm vs vjmm. - //I don't know what are the implications of this choice. - setVitroModelSource( new VitroModelSource(vsmm,ctx), ctx); - - log.info("Model makers set up"); - } - - - /** - * 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 portals = getPortal1s(model); - - if(!portals.isEmpty() && noPortalForNamespace( - portals, expectedNamespace)) { - // There really should be only one portal 1, but if there happen to - // be multiple, just arbitrarily pick the first in the list. - Resource portal = portals.get(0); - String oldNamespace = portal.getNameSpace(); - renamePortal(portal, expectedNamespace, model); - StartupStatus ss = StartupStatus.getBean(ctx); - ss.warning(this, "\nThe default namespace has been changed \n" + - "from " + oldNamespace + - "\nto " + expectedNamespace + ".\n" + - "The application will function normally, but " + - "any individuals in the \n" + oldNamespace + " " + - "namespace will need to have their URIs \n" + - "changed in order to be served as linked data. " + - "You can use the Ingest Tools \nto change the " + - "URIs for a batch of resources."); - } } - private List getPortal1s(Model model) { - List portals = new ArrayList(); - try { - model.enterCriticalSection(Lock.READ); - ResIterator portalIt = model.listResourcesWithProperty( - RDF.type, PORTAL); - while (portalIt.hasNext()) { - Resource portal = portalIt.nextResource(); - if ("portal1".equals(portal.getLocalName())) { - portals.add(portal); - } - } - } finally { - model.leaveCriticalSection(); - } - return portals; - } - - private boolean noPortalForNamespace(List portals, - String expectedNamespace) { - for (Resource portal : portals) { - if(expectedNamespace.equals(portal.getNameSpace())) { - return false; - } - } - return true; - } - - private void renamePortal(Resource portal, String namespace, Model model) { - model.enterCriticalSection(Lock.WRITE); - try { - ResourceUtils.renameResource( - portal, namespace + portal.getLocalName()); - } finally { - model.leaveCriticalSection(); - } - } - - - /* ===================================================================== */ - - - @Override - public void contextDestroyed(ServletContextEvent sce) { - // Nothing to do. - } - - @Override - protected OntModel ontModelFromContextAttribute(ServletContext ctx, - String attribute) { - OntModel ontModel; - Object attributeValue = ctx.getAttribute(attribute); - if (attributeValue != null && attributeValue instanceof OntModel) { - ontModel = (OntModel) attributeValue; - } else { - ontModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); - ctx.setAttribute(attribute, ontModel); - } - return ontModel; - } - - private boolean isEmpty(Model model) { - ClosableIterator closeIt = model.listStatements( - null, RDF.type, ResourceFactory.createResource( - VitroVocabulary.PORTAL)); - try { - if (closeIt.hasNext()) { - return false; - } else { - return true; - } - } finally { - closeIt.close(); - } - } - - private void loadDataFromFilesystem(OntModelSelector baseOms, - ServletContext ctx) { - Long startTime = System.currentTimeMillis(); - log.debug("Initializing models from RDF files"); - readOntologyFilesInPathSet(USER_ABOX_PATH, ctx, baseOms.getABoxModel()); - readOntologyFilesInPathSet(USER_TBOX_PATH, ctx, baseOms.getTBoxModel()); - readOntologyFilesInPathSet( - USER_APPMETA_PATH, ctx, baseOms.getApplicationMetadataModel()); - log.debug(((System.currentTimeMillis() - startTime) / 1000) - + " seconds to read RDF files "); - } - - private static void getTBoxModel(Model fullModel, - Model submodels, - Model tboxModel) { - - JenaModelUtils modelUtils = new JenaModelUtils(); - - Model tempModel = ModelFactory.createUnion(fullModel, submodels); - Model tempTBoxModel = modelUtils.extractTBox(tempModel); - - // copy intersection of tempTBoxModel and fullModel to tboxModel. - StmtIterator iter = tempTBoxModel.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (fullModel.contains(stmt)) { - tboxModel.add(stmt); - } - } - - return; - } - - /* - * Copy all statements from model 1 that are not in model 2 to model 3. - */ - private static void copyDifference(Model model1, - Model model2, - Model model3) { - - StmtIterator iter = model1.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (!model2.contains(stmt)) { - model3.add(stmt); - } - } - - return; - } - - protected static void getAppMetadata(Model source, Model target) { - - String amdQuery = "DESCRIBE ?x WHERE { " + - "{?x a <" + VitroVocabulary.PORTAL +"> } UNION " + - "{?x a <" + VitroVocabulary.PROPERTYGROUP +"> } UNION " + - "{?x a <" + VitroVocabulary.CLASSGROUP +"> } } "; - - try { - Query q = QueryFactory.create(amdQuery, Syntax.syntaxARQ); - QueryExecution qe = QueryExecutionFactory.create(q, source); - qe.execDescribe(target); - } catch (Exception e) { - log.error("unable to create the application metadata model",e); - } - - return; - } - - protected static void repairAppMetadataModel(Model applicationMetadataModel, - Model aboxAssertions, - Model aboxInferences) { - - log.info("Moving application metadata from ABox to dedicated model"); - getAppMetadata(aboxAssertions, applicationMetadataModel); - getAppMetadata(aboxInferences, applicationMetadataModel); - aboxAssertions.remove(applicationMetadataModel); - aboxInferences.remove(applicationMetadataModel); - - return; - } - - public static StoreDesc makeStoreDesc(ServletContext ctx) { - String layoutStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.sdb.layout", "layout2/hash"); - String dbtypeStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.dbtype", "MySQL"); - return new StoreDesc( - LayoutType.fetch(layoutStr), - DatabaseType.fetch(dbtypeStr) ); - } - - public static Store connectStore(BasicDataSource bds, StoreDesc storeDesc) - throws SQLException { - SDBConnection conn = new SDBConnection(bds.getConnection()) ; - return SDBFactory.connectStore(conn, storeDesc); - } - - public static void setupSDB(ServletContext ctx, Store store) { - setupSDB(ctx, - store, - ModelFactory.createDefaultModel(), - ModelFactory.createDefaultModel()); - } - - public static void setupSDB(ServletContext ctx, - Store store, - Model memModel, - Model inferenceModel) { - - store.getTableFormatter().create(); - store.getTableFormatter().truncate(); - - store.getTableFormatter().dropIndexes(); // improve load performance - - try { - - // This is a one-time copy of stored KB data - from a Jena RDB store - // to a Jena SDB store. In the process, we will also separate out - // the TBox from the Abox; these are in the same graph in pre-1.2 - // VIVO versions and will now be stored and maintained in separate - // models. Access to the Jena RDB data is through the - // OntModelSelectors that have been set up earlier in the current - // session by JenaPersistentDataSourceSetup.java. In the code - // below, note that the current getABoxModel() methods on the - // OntModelSelectors return a graph with both ABox and TBox data. - - OntModel submodels = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); - readOntologyFilesInPathSet(SUBMODELS, ctx, submodels); - - Model tboxAssertions = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxAssertions = ModelFactory.createDefaultModel(); - getTBoxModel(memModel, submodels, memTboxAssertions); - tboxAssertions.add(memTboxAssertions); - - Model tboxInferences = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxInferences = ModelFactory.createDefaultModel(); - getTBoxModel(inferenceModel, submodels, memTboxInferences); - tboxInferences.add(memTboxInferences); - - Model aboxAssertions = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_DB_MODEL); - copyDifference(memModel, memTboxAssertions, aboxAssertions); - - Model aboxInferences = SDBFactory.connectNamedModel( - store, JenaDataSourceSetupBase.JENA_INF_MODEL); - copyDifference(inferenceModel, memTboxInferences, aboxInferences); - - // Set up the application metadata model - Model applicationMetadataModel = SDBFactory.connectNamedModel( - store, - JenaDataSourceSetupBase.JENA_APPLICATION_METADATA_MODEL); - getAppMetadata(memModel, applicationMetadataModel); - log.info("During initial SDB setup, created an application " + - "metadata model of size " + - applicationMetadataModel.size()); - - // remove application metadata from ABox model - aboxAssertions.remove(applicationMetadataModel); - aboxInferences.remove(applicationMetadataModel); - - // Make sure the reasoner takes into account the newly-set-up data. - SimpleReasonerSetup.setRecomputeRequired(ctx); - - } finally { - log.info("Adding indexes to SDB database tables."); - store.getTableFormatter().addIndexes(); - log.info("Indexes created."); - } - } - - private void migrateToSDBFromExistingRDBStore(ServletContext ctx, - Store store) { - Model rdbAssertionsModel = makeDBModelFromConfigurationProperties( - JENA_DB_MODEL, DB_ONT_MODEL_SPEC, ctx); - Model rdbInferencesModel = makeDBModelFromConfigurationProperties( - JENA_INF_MODEL, DB_ONT_MODEL_SPEC, ctx); - setupSDB(ctx, store, rdbAssertionsModel, rdbInferencesModel); - } - - /** * Tests whether an SDB store has been formatted and populated for use. @@ -656,25 +114,26 @@ public class RDFServiceSetup extends JenaDataSourceSetupBase } } - private static final String STOREDESC_ATTR = "storeDesc"; - private static final String STORE_ATTR = "kbStore"; - - public static void setApplicationStoreDesc(StoreDesc storeDesc, - ServletContext ctx) { - ctx.setAttribute(STOREDESC_ATTR, storeDesc); + public static StoreDesc makeStoreDesc(ServletContext ctx) { + String layoutStr = ConfigurationProperties.getBean(ctx).getProperty( + "VitroConnection.DataSource.sdb.layout", "layout2/hash"); + String dbtypeStr = ConfigurationProperties.getBean(ctx).getProperty( + "VitroConnection.DataSource.dbtype", "MySQL"); + return new StoreDesc( + LayoutType.fetch(layoutStr), + DatabaseType.fetch(dbtypeStr) ); } - - public static StoreDesc getApplicationStoreDesc(ServletContext ctx) { - return (StoreDesc) ctx.getAttribute(STOREDESC_ATTR); - } - - public static void setApplicationStore(Store store, - ServletContext ctx) { - ctx.setAttribute(STORE_ATTR, store); - } - - public static Store getApplicationStore(ServletContext ctx) { - return (Store) ctx.getAttribute(STORE_ATTR); - } - } + public static Store connectStore(BasicDataSource bds, StoreDesc storeDesc) + throws SQLException { + SDBConnection conn = new SDBConnection(bds.getConnection()); + return SDBFactory.connectStore(conn, storeDesc); + } + + protected static void setupSDB(ServletContext ctx, Store store) { + log.info("Initializing SDB store"); + store.getTableFormatter().create(); + store.getTableFormatter().truncate(); + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SDBSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SDBSetup.java deleted file mode 100644 index 03e1ecf47..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SDBSetup.java +++ /dev/null @@ -1,231 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ -package edu.cornell.mannlib.vitro.webapp.servlet.setup; - -import java.sql.SQLException; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.rdf.model.StmtIterator; -import com.hp.hpl.jena.sdb.SDB; -import com.hp.hpl.jena.sdb.SDBFactory; -import com.hp.hpl.jena.sdb.Store; -import com.hp.hpl.jena.sdb.StoreDesc; -import com.hp.hpl.jena.sdb.sql.SDBConnection; -import com.hp.hpl.jena.sdb.store.DatabaseType; -import com.hp.hpl.jena.sdb.store.LayoutType; -import com.hp.hpl.jena.sdb.util.StoreUtils; - -import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; -import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils; -import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; - -public class SDBSetup extends JenaDataSourceSetupBase -implements javax.servlet.ServletContextListener { - private static final Log log = LogFactory.getLog(SDBSetup.class); - - @Override - public void contextDestroyed(ServletContextEvent arg0) { - // nothing to do - - } - - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - try { - setupSDB(ctx, ss); - log.info("SDB store ready for use"); - } catch (SQLException e) { - ss.fatal(this, "Exception in setupSDB", e); - } - } - - private void setupSDB(ServletContext ctx, StartupStatus ss) throws SQLException { - BasicDataSource bds = getApplicationDataSource(ctx); - if( bds == null ){ - ss.fatal(this, "A DataSource must be setup before SDBSetup "+ - "is run. Make sure that JenaPersistentDataSourceSetup runs before "+ - "SDBSetup."); - return; - } - - // union default graph - SDB.getContext().set(SDB.unionDefaultGraph, true) ; - - StoreDesc storeDesc = makeStoreDesc(ctx); - setApplicationStoreDesc(storeDesc, ctx); - - Store store = connectStore(bds, storeDesc); - setApplicationStore(store, ctx); - - if (!isSetUp(store)) { - JenaPersistentDataSourceSetup.thisIsFirstStartup(); - setupSDB(ctx, store); - } - } - - - /** - * Tests whether an SDB store has been formatted and populated for use. - * @param store - * @return - */ - private boolean isSetUp(Store store) throws SQLException { - if (!(StoreUtils.isFormatted(store))) { - return false; - } - - // even if the store exists, it may be empty - - try { - return (SDBFactory.connectNamedModel( - store, - JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL)) - .size() > 0; - } catch (Exception e) { - return false; - } - } - - public static StoreDesc makeStoreDesc(ServletContext ctx) { - String layoutStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.sdb.layout", "layout2/hash"); - String dbtypeStr = ConfigurationProperties.getBean(ctx).getProperty( - "VitroConnection.DataSource.dbtype", "MySQL"); - return new StoreDesc( - LayoutType.fetch(layoutStr), - DatabaseType.fetch(dbtypeStr) ); - } - - public static Store connectStore(BasicDataSource bds, StoreDesc storeDesc) - throws SQLException { - SDBConnection conn = new SDBConnection(bds.getConnection()); - return SDBFactory.connectStore(conn, storeDesc); - } - - protected static void setupSDB(ServletContext ctx, Store store) { - setupSDB(ctx, store, ModelFactory.createDefaultModel(), - ModelFactory.createDefaultModel()); - } - - protected static void setupSDB(ServletContext ctx, Store store, - Model memModel, Model inferenceModel) { - log.info("Initializing SDB store"); - - store.getTableFormatter().create(); - store.getTableFormatter().truncate(); - - store.getTableFormatter().dropIndexes(); // improve load performance - - try { - - // This is a one-time copy of stored KB data - from a Jena RDB store - // to a Jena SDB store. In the process, we will also separate out - // the TBox from the Abox; these are in the same graph in pre-1.2 - // VIVO versions and will now be stored and maintained in separate - // models. Access to the Jena RDB data is through the - // OntModelSelectors that have been set up earlier in the current - // session by JenaPersistentDataSourceSetup.java. In the code - // below, note that the current getABoxModel() methods on the - // OntModelSelectors return a graph with both ABox and TBox data. - - OntModel submodels = ModelFactory - .createOntologyModel(MEM_ONT_MODEL_SPEC); - readOntologyFilesInPathSet(SUBMODELS, ctx, submodels); - - Model tboxAssertions = SDBFactory.connectNamedModel(store, - JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxAssertions = ModelFactory.createDefaultModel(); - getTBoxModel(memModel, submodels, memTboxAssertions); - tboxAssertions.add(memTboxAssertions); - - Model tboxInferences = SDBFactory.connectNamedModel(store, - JenaDataSourceSetupBase.JENA_TBOX_INF_MODEL); - // initially putting the results in memory so we have a - // cheaper way of computing the difference when we copy the ABox - Model memTboxInferences = ModelFactory.createDefaultModel(); - getTBoxModel(inferenceModel, submodels, memTboxInferences); - tboxInferences.add(memTboxInferences); - - Model aboxAssertions = SDBFactory.connectNamedModel(store, - JenaDataSourceSetupBase.JENA_DB_MODEL); - copyDifference(memModel, memTboxAssertions, aboxAssertions); - - Model aboxInferences = SDBFactory.connectNamedModel(store, - JenaDataSourceSetupBase.JENA_INF_MODEL); - copyDifference(inferenceModel, memTboxInferences, aboxInferences); - - // Set up the application metadata model - Model applicationMetadataModel = SDBFactory.connectNamedModel( - store, - JenaDataSourceSetupBase.JENA_APPLICATION_METADATA_MODEL); - getAppMetadata(memModel, applicationMetadataModel); - log.info("During initial SDB setup, created an application " - + "metadata model of size " - + applicationMetadataModel.size()); - - // remove application metadata from ABox model - aboxAssertions.remove(applicationMetadataModel); - aboxInferences.remove(applicationMetadataModel); - - // Make sure the reasoner takes into account the newly-set-up data. - SimpleReasonerSetup.setRecomputeRequired(ctx); - - } finally { - log.info("Adding indexes to SDB database tables."); - store.getTableFormatter().addIndexes(); - log.info("Indexes created."); - } - } - - /* - * Copy all statements from model 1 that are not in model 2 to model 3. - */ - private static void copyDifference(Model model1, Model model2, Model model3) { - - StmtIterator iter = model1.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (!model2.contains(stmt)) { - model3.add(stmt); - } - } - - return; - } - - private static void getTBoxModel(Model fullModel, Model submodels, - Model tboxModel) { - - JenaModelUtils modelUtils = new JenaModelUtils(); - - Model tempModel = ModelFactory.createUnion(fullModel, submodels); - Model tempTBoxModel = modelUtils.extractTBox(tempModel); - - // copy intersection of tempTBoxModel and fullModel to tboxModel. - StmtIterator iter = tempTBoxModel.listStatements(); - - while (iter.hasNext()) { - Statement stmt = iter.next(); - if (fullModel.contains(stmt)) { - tboxModel.add(stmt); - } - } - - return; - } - -} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSDBSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSDBSetup.java deleted file mode 100644 index 24620a0d1..000000000 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSDBSetup.java +++ /dev/null @@ -1,273 +0,0 @@ -/* $This file is distributed under the terms of the license in /doc/license.txt$ */ - -package edu.cornell.mannlib.vitro.webapp.servlet.setup; - -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.ontology.OntModelSpec; -import com.hp.hpl.jena.rdf.model.Model; -import com.hp.hpl.jena.rdf.model.ModelFactory; -import com.hp.hpl.jena.rdf.model.ModelMaker; -import com.hp.hpl.jena.rdf.model.ResIterator; -import com.hp.hpl.jena.rdf.model.Resource; -import com.hp.hpl.jena.rdf.model.ResourceFactory; -import com.hp.hpl.jena.rdf.model.Statement; -import com.hp.hpl.jena.sdb.StoreDesc; -import com.hp.hpl.jena.shared.Lock; -import com.hp.hpl.jena.util.ResourceUtils; -import com.hp.hpl.jena.util.iterator.ClosableIterator; -import com.hp.hpl.jena.vocabulary.RDF; - -import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; -import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig; -import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; -import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; -import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource; -import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; - -/** - * Primarily sets up webapp DAO factories. - */ -public class WebappDaoSDBSetup extends JenaDataSourceSetupBase - implements javax.servlet.ServletContextListener { - - private static final Log log = LogFactory.getLog(WebappDaoSDBSetup.class); - - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - - try { - long startTime = System.currentTimeMillis(); - setUpJenaDataSource(ctx, ss); - log.info((System.currentTimeMillis() - startTime) / 1000 + - " seconds to set up SDB store"); - - //TODO remove this temporary development scaffolding - if (true || ConfigurationProperties.getBean(sce).getProperty( - "VitroConnection.DataSource.endpointURI") != null) { - (new RDFServiceSetup()).contextInitialized(sce); - } - - } catch (SQLException sqle) { - // SQL exceptions are fatal and should halt startup - log.error("Error using SQL database; startup aborted.", sqle); - ss.fatal(this, "Error using SQL database; startup aborted.", sqle); - } catch (Throwable t) { - log.error("Throwable in " + this.getClass().getName(), t); - ss.fatal(this, "Throwable in " + this.getClass().getName(), t); - } - - } - - private void setUpJenaDataSource(ServletContext ctx, StartupStatus ss) throws SQLException { - - BasicDataSource bds = getApplicationDataSource(ctx); - if( bds == null ){ - ss.fatal(this, "A DataSource must be setup before "+ WebappDaoSDBSetup.class.getName() + - "is run. Make sure that JenaPersistentDataSourceSetup runs before "+ - WebappDaoSDBSetup.class.getName() ); - return; - } - - //Get the OntModelSelectors - OntModelSelectorImpl baseOms = - (OntModelSelectorImpl) ModelContext.getBaseOntModelSelector(ctx); - OntModelSelectorImpl inferenceOms = - (OntModelSelectorImpl) ModelContext.getInferenceOntModelSelector(ctx); - OntModelSelectorImpl unionOms = - (OntModelSelectorImpl) ModelContext.getUnionOntModelSelector(ctx); - - /////////////////////////////////////////////////////////////// - // Check for namespace mismatch - - checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx ); - ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx)); - - /////////////////////////////////////////////////////////////// - // first startup? - - if (isFirstStartup()) { - loadDataFromFilesystem(baseOms, ctx); - } - - log.info("Setting up DAO factories"); - - /////////////////////////////////////////////////////////////// - //create assertions webapp DAO factory - - StoreDesc storeDesc = getApplicationStoreDesc(ctx); - WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); - config.setDefaultNamespace(getDefaultNamespace(ctx)); -// WebappDaoFactory baseWadf = new WebappDaoFactorySDB( -// baseOms, bds, storeDesc, config, -// WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); -// ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); - - /////////////////////////////////////////////////////////////// - //create inference webapp DAO factory - - OntModel inferenceUnion = ModelFactory.createOntologyModel( - OntModelSpec.OWL_MEM, - ModelFactory.createUnion( - inferenceOms.getABoxModel(), - inferenceOms.getTBoxModel())); - inferenceOms.setFullModel(inferenceUnion); - ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), ctx); -// WebappDaoFactory infWadf = new WebappDaoFactorySDB( -// inferenceOms, bds, storeDesc, config, -// WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); -// ctx.setAttribute("deductionsWebappDaoFactory", infWadf); - - /////////////////////////////////////////////////////////////// - //create default union webapp DAO factory - -// OntModel masterUnion = ModelFactory.createOntologyModel( -// DB_ONT_MODEL_SPEC, makeDBModel( -// bds, WebappDaoFactorySDB.UNION_GRAPH, -// DB_ONT_MODEL_SPEC, TripleStoreType.SDB, ctx)); -// unionOms.setFullModel(masterUnion); -// ctx.setAttribute("jenaOntModel", masterUnion); -// WebappDaoFactory wadf = new WebappDaoFactorySDB( -// unionOms, bds, storeDesc, config); -// ctx.setAttribute("webappDaoFactory",wadf); - - makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); - VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); - setVitroJenaModelMaker(vjmm, ctx); - makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); - ModelMaker vsmm = getVitroJenaSDBModelMaker(); - setVitroJenaSDBModelMaker(vsmm, ctx); - - //bdc34: I have no reason for vsmm vs vjmm. - //I don't know what are the implications of this choice. - setVitroModelSource( new VitroModelSource(vsmm,ctx), ctx); - - log.info("DAOs set up"); - } - - - /** - * 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 portals = getPortal1s(model); - - if(!portals.isEmpty() && noPortalForNamespace( - portals, expectedNamespace)) { - // There really should be only one portal 1, but if there happen to - // be multiple, just arbitrarily pick the first in the list. - Resource portal = portals.get(0); - String oldNamespace = portal.getNameSpace(); - renamePortal(portal, expectedNamespace, model); - StartupStatus ss = StartupStatus.getBean(ctx); - ss.warning(this, "\nThe default namespace has been changed \n" + - "from " + oldNamespace + - "\nto " + expectedNamespace + ".\n" + - "The application will function normally, but " + - "any individuals in the \n" + oldNamespace + " " + - "namespace will need to have their URIs \n" + - "changed in order to be served as linked data. " + - "You can use the Ingest Tools \nto change the " + - "URIs for a batch of resources."); - } - } - - private List getPortal1s(Model model) { - List portals = new ArrayList(); - try { - model.enterCriticalSection(Lock.READ); - ResIterator portalIt = model.listResourcesWithProperty( - RDF.type, PORTAL); - while (portalIt.hasNext()) { - Resource portal = portalIt.nextResource(); - if ("portal1".equals(portal.getLocalName())) { - portals.add(portal); - } - } - } finally { - model.leaveCriticalSection(); - } - return portals; - } - - private boolean noPortalForNamespace(List portals, - String expectedNamespace) { - for (Resource portal : portals) { - if(expectedNamespace.equals(portal.getNameSpace())) { - return false; - } - } - return true; - } - - private void renamePortal(Resource portal, String namespace, Model model) { - model.enterCriticalSection(Lock.WRITE); - try { - ResourceUtils.renameResource( - portal, namespace + portal.getLocalName()); - } finally { - model.leaveCriticalSection(); - } - } - - - /* ===================================================================== */ - - - @Override - public void contextDestroyed(ServletContextEvent sce) { - // Nothing to do. - } - - private boolean isEmpty(Model model) { - ClosableIterator closeIt = model.listStatements( - null, RDF.type, ResourceFactory.createResource( - VitroVocabulary.PORTAL)); - try { - if (closeIt.hasNext()) { - return false; - } else { - return true; - } - } finally { - closeIt.close(); - } - } - - private void loadDataFromFilesystem(OntModelSelector baseOms, - ServletContext ctx) { - Long startTime = System.currentTimeMillis(); - log.info("Initializing models from RDF files"); - - readOntologyFilesInPathSet(USER_ABOX_PATH, ctx, baseOms.getABoxModel()); - readOntologyFilesInPathSet(USER_TBOX_PATH, ctx, baseOms.getTBoxModel()); - readOntologyFilesInPathSet( - USER_APPMETA_PATH, ctx, baseOms.getApplicationMetadataModel()); - - log.debug(((System.currentTimeMillis() - startTime) / 1000) - + " seconds to read RDF files "); - } - - - - - } - diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSetup.java new file mode 100644 index 000000000..cb87bed44 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/WebappDaoSetup.java @@ -0,0 +1,385 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.servlet.setup; + +import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.DISPLAY_ONT_MODEL; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.ontology.OntModelSpec; +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.ModelMaker; +import com.hp.hpl.jena.rdf.model.ResIterator; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.sdb.StoreDesc; +import com.hp.hpl.jena.shared.Lock; +import com.hp.hpl.jena.util.ResourceUtils; +import com.hp.hpl.jena.util.iterator.ClosableIterator; +import com.hp.hpl.jena.vocabulary.RDF; + +import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; +import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; +import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl; +import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset; +import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceModelMaker; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroModelSource; +import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactorySDB; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; +import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceFactory; +import edu.cornell.mannlib.vitro.webapp.rdfservice.impl.RDFServiceUtils; +import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase.TripleStoreType; +import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; +import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; + +/** + * Primarily sets up webapp DAO factories. + */ +public class WebappDaoSetup extends JenaDataSourceSetupBase + implements javax.servlet.ServletContextListener { + + private static final Log log = LogFactory.getLog(WebappDaoSetup.class); + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + StartupStatus ss = StartupStatus.getBean(ctx); + + try { + long startTime = System.currentTimeMillis(); + setUpJenaDataSource(ctx, ss); + log.info((System.currentTimeMillis() - startTime) / 1000 + + " seconds to set up models and DAO factories"); + } catch (Throwable t) { + log.error("Throwable in " + this.getClass().getName(), t); + ss.fatal(this, "Throwable in " + this.getClass().getName(), t); + } + + } + + private void setUpJenaDataSource(ServletContext ctx, StartupStatus ss) { + OntModelSelectorImpl baseOms = new OntModelSelectorImpl(); + OntModelSelectorImpl inferenceOms = new OntModelSelectorImpl(); + OntModelSelectorImpl unionOms = new OntModelSelectorImpl(); + + OntModel userAccountsModel = ontModelFromContextAttribute( + ctx, "userAccountsOntModel"); + baseOms.setUserAccountsModel(userAccountsModel); + inferenceOms.setUserAccountsModel(userAccountsModel); + unionOms.setUserAccountsModel(userAccountsModel); + + OntModel displayModel = ontModelFromContextAttribute( + ctx,DISPLAY_ONT_MODEL); + baseOms.setDisplayModel(displayModel); + inferenceOms.setDisplayModel(displayModel); + unionOms.setDisplayModel(displayModel); + + RDFServiceFactory rdfServiceFactory = RDFServiceUtils.getRDFServiceFactory(ctx); + RDFService rdfService = rdfServiceFactory.getRDFService(); + Dataset dataset = new RDFServiceDataset(rdfService); + setStartupDataset(dataset, ctx); + + // ABox assertions + Model aboxAssertions = dataset.getNamedModel( + JenaDataSourceSetupBase.JENA_DB_MODEL); + baseOms.setABoxModel( + ModelFactory.createOntologyModel( + OntModelSpec.OWL_MEM, aboxAssertions)); + + // ABox inferences + Model aboxInferences = dataset.getNamedModel( + JenaDataSourceSetupBase.JENA_INF_MODEL); + inferenceOms.setABoxModel(ModelFactory.createOntologyModel( + OntModelSpec.OWL_MEM, aboxInferences)); + + // TBox assertions + try { + Model tboxAssertionsDB = dataset.getNamedModel( + JENA_TBOX_ASSERTIONS_MODEL); + OntModel tboxAssertions = ModelFactory.createOntologyModel( + MEM_ONT_MODEL_SPEC); + + if (tboxAssertionsDB != null) { + long startTime = System.currentTimeMillis(); + System.out.println( + "Copying cached tbox assertions into memory"); + tboxAssertions.add(tboxAssertionsDB); + System.out.println((System.currentTimeMillis() - startTime) + / 1000 + " seconds to load tbox assertions"); + } + + tboxAssertions.getBaseModel().register(new ModelSynchronizer( + tboxAssertionsDB)); + + baseOms.setTBoxModel(tboxAssertions); + } catch (Throwable e) { + log.error("Unable to load tbox assertion cache from DB", e); + } + + // TBox inferences + try { + Model tboxInferencesDB = dataset.getNamedModel(JENA_TBOX_INF_MODEL); + OntModel tboxInferences = ModelFactory.createOntologyModel( + MEM_ONT_MODEL_SPEC); + + if (tboxInferencesDB != null) { + long startTime = System.currentTimeMillis(); + System.out.println( + "Copying cached tbox inferences into memory"); + tboxInferences.add(tboxInferencesDB); + System.out.println((System.currentTimeMillis() - startTime) + / 1000 + " seconds to load tbox inferences"); + } + + tboxInferences.getBaseModel().register(new ModelSynchronizer( + tboxInferencesDB)); + inferenceOms.setTBoxModel(tboxInferences); + } catch (Throwable e) { + log.error("Unable to load tbox inference cache from DB", e); + } + + // union ABox + OntModel unionABoxModel = ModelFactory.createOntologyModel( + MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( + baseOms.getABoxModel(), inferenceOms.getABoxModel())); + unionOms.setABoxModel(unionABoxModel); + + // union TBox + OntModel unionTBoxModel = ModelFactory.createOntologyModel( + MEM_ONT_MODEL_SPEC,ModelFactory.createUnion( + baseOms.getTBoxModel(), inferenceOms.getTBoxModel())); + unionOms.setTBoxModel(unionTBoxModel); + + + // Application metadata model is cached in memory. + try { + + Model applicationMetadataModelDB = dataset.getNamedModel( + JENA_APPLICATION_METADATA_MODEL); + OntModel applicationMetadataModel = + ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); + + long startTime = System.currentTimeMillis(); + System.out.println( + "Copying cached application metadata model into memory"); + applicationMetadataModel.add(applicationMetadataModelDB); + System.out.println((System.currentTimeMillis() - startTime) + / 1000 + " seconds to load application metadata model " + + "assertions of size " + applicationMetadataModel.size()); + applicationMetadataModel.getBaseModel().register( + new ModelSynchronizer(applicationMetadataModelDB)); + + if (applicationMetadataModel.size()== 0 /* isFirstStartup() */) { + applicationMetadataModel.add( + InitialJenaModelUtils.loadInitialModel( + ctx, getDefaultNamespace(ctx))); + + } + + baseOms.setApplicationMetadataModel(applicationMetadataModel); + inferenceOms.setApplicationMetadataModel( + baseOms.getApplicationMetadataModel()); + unionOms.setApplicationMetadataModel( + baseOms.getApplicationMetadataModel()); + + } catch (Throwable e) { + log.error("Unable to load application metadata model cache from DB" + , e); + } + + checkForNamespaceMismatch( baseOms.getApplicationMetadataModel(), ctx ); + + if (isFirstStartup()) { + loadDataFromFilesystem(baseOms, ctx); + } + + log.info("Setting up union models and DAO factories"); + + // create TBox + ABox union models and set up webapp DAO factories + OntModel baseUnion = ModelFactory.createOntologyModel( + OntModelSpec.OWL_MEM, + ModelFactory.createUnion(baseOms.getABoxModel(), + baseOms.getTBoxModel())); + baseOms.setFullModel(baseUnion); + ModelContext.setBaseOntModel(baseOms.getFullModel(), ctx); + WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); + config.setDefaultNamespace(getDefaultNamespace(ctx)); + WebappDaoFactory baseWadf = new WebappDaoFactorySDB( + rdfService, baseOms, config, + WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); + ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); + + OntModel inferenceUnion = ModelFactory.createOntologyModel( + OntModelSpec.OWL_MEM, + ModelFactory.createUnion( + inferenceOms.getABoxModel(), + inferenceOms.getTBoxModel())); + inferenceOms.setFullModel(inferenceUnion); + ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), ctx); + WebappDaoFactory infWadf = new WebappDaoFactorySDB( + rdfService, inferenceOms, config, + WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); + ctx.setAttribute("deductionsWebappDaoFactory", infWadf); + + OntModel masterUnion = ModelFactory.createOntologyModel( + DB_ONT_MODEL_SPEC, dataset.getDefaultModel()); + unionOms.setFullModel(masterUnion); + ctx.setAttribute("jenaOntModel", masterUnion); + WebappDaoFactory wadf = new WebappDaoFactorySDB( + rdfService, unionOms, config); + ctx.setAttribute("webappDaoFactory",wadf); + + ModelContext.setOntModelSelector(unionOms, ctx); + ModelContext.setUnionOntModelSelector(unionOms, ctx); + // assertions and inferences + ModelContext.setBaseOntModelSelector(baseOms, ctx); + // assertions + ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); + // inferences + + ctx.setAttribute("defaultNamespace", getDefaultNamespace(ctx)); + + makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); + VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); + setVitroJenaModelMaker(vjmm, ctx); + makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); + RDFServiceModelMaker vsmm = new RDFServiceModelMaker(rdfServiceFactory); + setVitroJenaSDBModelMaker(vsmm, ctx); + + //bdc34: I have no reason for vsmm vs vjmm. + //I don't know what are the implications of this choice. + setVitroModelSource( new VitroModelSource(vsmm,ctx), ctx); + + log.info("Model makers set up"); + } + + + /** + * 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 portals = getPortal1s(model); + + if(!portals.isEmpty() && noPortalForNamespace( + portals, expectedNamespace)) { + // There really should be only one portal 1, but if there happen to + // be multiple, just arbitrarily pick the first in the list. + Resource portal = portals.get(0); + String oldNamespace = portal.getNameSpace(); + renamePortal(portal, expectedNamespace, model); + StartupStatus ss = StartupStatus.getBean(ctx); + ss.warning(this, "\nThe default namespace has been changed \n" + + "from " + oldNamespace + + "\nto " + expectedNamespace + ".\n" + + "The application will function normally, but " + + "any individuals in the \n" + oldNamespace + " " + + "namespace will need to have their URIs \n" + + "changed in order to be served as linked data. " + + "You can use the Ingest Tools \nto change the " + + "URIs for a batch of resources."); + } + } + + private List getPortal1s(Model model) { + List portals = new ArrayList(); + try { + model.enterCriticalSection(Lock.READ); + ResIterator portalIt = model.listResourcesWithProperty( + RDF.type, PORTAL); + while (portalIt.hasNext()) { + Resource portal = portalIt.nextResource(); + if ("portal1".equals(portal.getLocalName())) { + portals.add(portal); + } + } + } finally { + model.leaveCriticalSection(); + } + return portals; + } + + private boolean noPortalForNamespace(List portals, + String expectedNamespace) { + for (Resource portal : portals) { + if(expectedNamespace.equals(portal.getNameSpace())) { + return false; + } + } + return true; + } + + private void renamePortal(Resource portal, String namespace, Model model) { + model.enterCriticalSection(Lock.WRITE); + try { + ResourceUtils.renameResource( + portal, namespace + portal.getLocalName()); + } finally { + model.leaveCriticalSection(); + } + } + + + /* ===================================================================== */ + + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Nothing to do. + } + + private boolean isEmpty(Model model) { + ClosableIterator closeIt = model.listStatements( + null, RDF.type, ResourceFactory.createResource( + VitroVocabulary.PORTAL)); + try { + if (closeIt.hasNext()) { + return false; + } else { + return true; + } + } finally { + closeIt.close(); + } + } + + private void loadDataFromFilesystem(OntModelSelector baseOms, + ServletContext ctx) { + Long startTime = System.currentTimeMillis(); + log.info("Initializing models from RDF files"); + + readOntologyFilesInPathSet(USER_ABOX_PATH, ctx, baseOms.getABoxModel()); + readOntologyFilesInPathSet(USER_TBOX_PATH, ctx, baseOms.getTBoxModel()); + readOntologyFilesInPathSet( + USER_APPMETA_PATH, ctx, baseOms.getApplicationMetadataModel()); + + log.debug(((System.currentTimeMillis() - startTime) / 1000) + + " seconds to read RDF files "); + } + + + + + } + diff --git a/webapp/web/WEB-INF/resources/startup_listeners.txt b/webapp/web/WEB-INF/resources/startup_listeners.txt index da63256dc..5ab45b8b0 100644 --- a/webapp/web/WEB-INF/resources/startup_listeners.txt +++ b/webapp/web/WEB-INF/resources/startup_listeners.txt @@ -16,13 +16,12 @@ edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailFactory$Setup ### this listener must be run before SDBSetup, all models setups and WebappDaoSetup ### edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaPersistentDataSourceSetup -edu.cornell.mannlib.vitro.webapp.servlet.setup.SDBSetup +edu.cornell.mannlib.vitro.webapp.servlet.setup.RDFServiceSetup edu.cornell.mannlib.vitro.webapp.servlet.setup.ApplicationModelSetup edu.cornell.mannlib.vitro.webapp.servlet.setup.UserModelSetup -edu.cornell.mannlib.vitro.webapp.servlet.setup.ModelSetup -edu.cornell.mannlib.vitro.webapp.servlet.setup.WebappDaoSDBSetup +edu.cornell.mannlib.vitro.webapp.servlet.setup.WebappDaoSetup edu.cornell.mannlib.vitro.webapp.servlet.setup.UpdateKnowledgeBase