From 2d714c54b832f09aad15b9e7cf4a4ef34c13cc3c Mon Sep 17 00:00:00 2001 From: jeb228 Date: Thu, 24 Feb 2011 18:35:55 +0000 Subject: [PATCH] NIHVIVO-1261 Pass the ServletContext down the chain to methods in JenaDataSourceSetupBase, to prepare for the change in ConfigurationProperties. --- .../freemarker/SDBSetupController.java | 2 +- .../controller/jena/JenaExportController.java | 2 +- .../controller/jena/JenaIngestController.java | 21 ++-- .../webapp/dao/jena/VitroJenaModelMaker.java | 20 +++- .../filters/WebappDaoFactorySDBPrep.java | 5 +- .../servlet/setup/JenaDataSourceSetup.java | 56 +++++------ .../setup/JenaDataSourceSetupBase.java | 97 +++++++++++-------- .../servlet/setup/JenaDataSourceSetupSDB.java | 84 ++++++++-------- .../JenaPersistentDBOnlyDataSourceSetup.java | 24 ++--- .../setup/JenaPersistentDataSourceSetup.java | 28 +++--- .../servlet/setup/ModelAuditorSetup.java | 13 ++- .../servlet/setup/SimpleReasonerSetup.java | 20 +--- 12 files changed, 199 insertions(+), 173 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SDBSetupController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SDBSetupController.java index e8abe3755..64812836c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SDBSetupController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SDBSetupController.java @@ -90,7 +90,7 @@ public class SDBSetupController extends FreemarkerHttpServlet { Boolean done = true; getServletContext().setAttribute("done",done); StoreDesc storeDesc = jenaDataSourceSetupSDB.makeStoreDesc(); - BasicDataSource bds = jenaDataSourceSetupSDB.makeDataSourceFromConfigurationProperties(); + BasicDataSource bds = jenaDataSourceSetupSDB.makeDataSourceFromConfigurationProperties(getServletContext()); Store store = null; try { store = JenaDataSourceSetupSDB.connectStore(bds, storeDesc); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaExportController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaExportController.java index c3b5c09f4..16b295faa 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaExportController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/JenaExportController.java @@ -97,7 +97,7 @@ public class JenaExportController extends BaseEditController { } - String mode = (JenaDataSourceSetupBase.isSDBActive()) ? "SDB" : "RDB"; + String mode = (JenaDataSourceSetupBase.isSDBActive(vreq)) ? "SDB" : "RDB"; if( "abox".equals(subgraphParam)){ model = ModelFactory.createDefaultModel(); if("inferred".equals(assertedOrInferredParam)){ 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 32622d76d..4c012efe5 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 @@ -13,18 +13,15 @@ import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -55,12 +52,8 @@ 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.SDBFactory; -import com.hp.hpl.jena.sdb.Store; import com.hp.hpl.jena.sdb.StoreDesc; import com.hp.hpl.jena.sdb.sql.JDBC; -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.shared.Lock; @@ -68,7 +61,6 @@ import com.hp.hpl.jena.util.ResourceUtils; import com.hp.hpl.jena.util.iterator.ClosableIterator; import edu.cornell.mannlib.vedit.controller.BaseEditController; -import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.beans.Ontology; import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.controller.Controllers; @@ -82,7 +74,6 @@ 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.JenaDataSourceSetup; import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase; -import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupSDB; import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestUtils; import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestWorkflowProcessor; import edu.cornell.mannlib.vitro.webapp.utils.jena.WorkflowOntology; @@ -117,6 +108,7 @@ public class JenaIngestController extends BaseEditController { private static final String RENAME_RESOURCE = "/jenaIngest/renameResource.jsp"; private static final String RENAME_RESULT = "/jenaIngest/renameResult.jsp"; + @Override public void doGet (HttpServletRequest request, HttpServletResponse response) { if (!checkLoginStatus(request,response)) { @@ -134,7 +126,7 @@ public class JenaIngestController extends BaseEditController { String modelT = (String)getServletContext().getAttribute("modelT"); String info = (String)getServletContext().getAttribute("info"); if(modelT == null){ - boolean initialSwitch = new JenaDataSourceSetupBase().isSDBActive(); + boolean initialSwitch = JenaDataSourceSetupBase.isSDBActive(vreq); if(initialSwitch){ VitroJenaSDBModelMaker vsmm = (VitroJenaSDBModelMaker) getServletContext().getAttribute("vitroJenaSDBModelMaker"); vreq.getSession().setAttribute("vitroJenaModelMaker", vsmm); @@ -909,11 +901,12 @@ public class JenaIngestController extends BaseEditController { String driver = loadDriver(dbTypeObj); System.out.println("Connecting to DB at "+jdbcUrl); StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash,dbTypeObj) ; - BasicDataSource bds = JenaDataSourceSetup.makeBasicDataSource( - driver, jdbcUrl, username, password); + ServletContext ctx = vreq.getSession().getServletContext(); + BasicDataSource bds = JenaDataSourceSetup.makeBasicDataSource( + driver, jdbcUrl, username, password, ctx); try { VitroJenaSDBModelMaker vsmm = new VitroJenaSDBModelMaker(storeDesc, bds); - VitroJenaModelMaker vjmm = new VitroJenaModelMaker(jdbcUrl, username, password, dbType); + VitroJenaModelMaker vjmm = new VitroJenaModelMaker(jdbcUrl, username, password, dbType, ctx); getServletContext().setAttribute("vitroJenaSDBModelMaker", vsmm); getServletContext().setAttribute("vitroJenaModelMaker", vjmm); if("SDB".equals(tripleStore)) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VitroJenaModelMaker.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VitroJenaModelMaker.java index 2ae071466..1a40088fb 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VitroJenaModelMaker.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/VitroJenaModelMaker.java @@ -6,6 +6,7 @@ import java.sql.SQLException; import java.util.HashMap; import java.util.List; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.commons.dbcp.BasicDataSource; @@ -23,7 +24,6 @@ import com.hp.hpl.jena.rdf.model.ModelMaker; import com.hp.hpl.jena.rdf.model.ModelReader; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.util.iterator.ExtendedIterator; -import com.hp.hpl.jena.util.iterator.NiceIterator; import com.hp.hpl.jena.util.iterator.WrappedIterator; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; @@ -51,7 +51,7 @@ public class VitroJenaModelMaker implements ModelMaker { private HashMap modelCache; private HttpServletRequest request = null; - public VitroJenaModelMaker(String jdbcUrl, String username, String password, String dbTypeStr) { + public VitroJenaModelMaker(String jdbcUrl, String username, String password, String dbTypeStr, ServletContext ctx) { this.jdbcUrl = jdbcUrl; this.username = username; this.password = password; @@ -65,7 +65,7 @@ public class VitroJenaModelMaker implements ModelMaker { } this.dataSource = JenaDataSourceSetupBase.makeBasicDataSource( driverName, - jdbcUrl, username, password); + jdbcUrl, username, password, ctx); modelCache = new HashMap(); } @@ -89,12 +89,14 @@ public class VitroJenaModelMaker implements ModelMaker { return this.modelCache; } + @Override public void close() { // TODO Auto-generated method stub // So, in theory, this should close database connections and drop references // to in-memory models and all that kind of stuff. } + @Override public Model createModel(String arg0) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -110,6 +112,7 @@ public class VitroJenaModelMaker implements ModelMaker { } } + @Override public Model createModel(String arg0, boolean arg1) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -123,11 +126,13 @@ public class VitroJenaModelMaker implements ModelMaker { } } + @Override public GraphMaker getGraphMaker() { throw new UnsupportedOperationException(this.getClass().getName() + " does not support getGraphMaker()"); } + @Override public boolean hasModel(String arg0) { DBConnection conn = new DBConnection(jdbcUrl, username, password, dbTypeStr); try { @@ -141,6 +146,7 @@ public class VitroJenaModelMaker implements ModelMaker { } } + @Override public ExtendedIterator listModels() { DBConnection conn = new DBConnection(jdbcUrl, username, password, dbTypeStr); try { @@ -155,6 +161,7 @@ public class VitroJenaModelMaker implements ModelMaker { } } + @Override public Model openModel(String arg0, boolean arg1) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -168,6 +175,7 @@ public class VitroJenaModelMaker implements ModelMaker { } } + @Override public void removeModel(String arg0) { Model m = modelCache.get(arg0); if (m != null) { @@ -210,12 +218,14 @@ public class VitroJenaModelMaker implements ModelMaker { return null; } + @Override public Model createDefaultModel() { throw new UnsupportedOperationException(this.getClass().getName() + " does not support createDefaultModel()"); } + @Override public Model createFreshModel() { throw new UnsupportedOperationException(this.getClass().getName() + " does not support createFreshModel()"); } @@ -233,6 +243,7 @@ public class VitroJenaModelMaker implements ModelMaker { } + @Override public Model openModel(String arg0) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -247,6 +258,7 @@ public class VitroJenaModelMaker implements ModelMaker { } + @Override public Model openModelIfPresent(String arg0) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -261,6 +273,7 @@ public class VitroJenaModelMaker implements ModelMaker { } + @Override public Model getModel(String arg0) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } @@ -275,6 +288,7 @@ public class VitroJenaModelMaker implements ModelMaker { } + @Override public Model getModel(String arg0, ModelReader arg1) { Model specialModel = null; if ( (specialModel = getSpecialModel(arg0)) != null ) { return specialModel; } 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 bf1fc7070..8d0d67fee 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/filters/WebappDaoFactorySDBPrep.java @@ -57,10 +57,11 @@ public class WebappDaoFactorySDBPrep implements Filter { Pattern.compile("/.*/images/.*") }; + @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { - if ( (!(JenaDataSourceSetupBase.isSDBActive())) || + if ( (!(JenaDataSourceSetupBase.isSDBActive(request))) || (request.getAttribute( "WebappDaoFactorySDBPrep.setup") != null) ) { // don't run multiple times or if SDB is not active @@ -134,6 +135,7 @@ public class WebappDaoFactorySDBPrep implements Filter { } + @Override public void init(FilterConfig filterConfig) throws ServletException { try { _ctx = filterConfig.getServletContext(); @@ -142,6 +144,7 @@ public class WebappDaoFactorySDBPrep implements Filter { } } + @Override public void destroy() { // no destroy actions } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java index 5b9958e7f..44db80764 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetup.java @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup; import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -24,18 +23,15 @@ import com.hp.hpl.jena.util.iterator.ClosableIterator; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean; -import edu.cornell.mannlib.vitro.webapp.beans.Portal; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDaoCon; 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.SearchReindexingListener; import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector; 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.WebappDaoFactoryJena; -import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase.TripleStoreType; import edu.cornell.mannlib.vitro.webapp.utils.NamespaceMapper; import edu.cornell.mannlib.vitro.webapp.utils.jena.InitialJenaModelUtils; import edu.cornell.mannlib.vitro.webapp.utils.jena.NamespaceMapperJena; @@ -44,7 +40,9 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class.getName()); - public void contextInitialized(ServletContextEvent sce) { + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); String tripleStoreTypeStr = ConfigurationProperties.getProperty( @@ -56,23 +54,23 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java } - if (AbortStartup.isStartupAborted(sce.getServletContext())) { + if (AbortStartup.isStartupAborted(ctx)) { return; } try { - OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); + OntModel memModel = (OntModel) ctx.getAttribute("jenaOntModel"); if (memModel == null) { memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); log.warn("WARNING: no database connected. Changes will disappear after context restart."); - sce.getServletContext().setAttribute("jenaOntModel",memModel); + ctx.setAttribute("jenaOntModel",memModel); } memModel.addSubModel((new JenaBaseDaoCon()).getConstModel()); // add the vitro tbox to the model - OntModel inferenceModel = ontModelFromContextAttribute(sce.getServletContext(), "inferenceOntModel"); + OntModel inferenceModel = ontModelFromContextAttribute(ctx, "inferenceOntModel"); - OntModel userAccountsModel = ontModelFromContextAttribute(sce.getServletContext(), "userAccountsOntModel"); + OntModel userAccountsModel = ontModelFromContextAttribute(ctx, "userAccountsOntModel"); if (userAccountsModel.size() == 0) { checkMainModelForUserAccounts(memModel, userAccountsModel); } @@ -86,57 +84,57 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java inferenceOms.setUserAccountsModel(userAccountsModel); unionOms.setUserAccountsModel(userAccountsModel); - OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel"); + OntModel displayModel = ontModelFromContextAttribute(ctx,"displayOntModel"); baseOms.setDisplayModel(displayModel); inferenceOms.setDisplayModel(displayModel); unionOms.setDisplayModel(displayModel); checkForNamespaceMismatch( memModel, defaultNamespace ); - sce.getServletContext().setAttribute("baseOntModel", memModel); + ctx.setAttribute("baseOntModel", memModel); WebappDaoFactory baseWadf = new WebappDaoFactoryJena( baseOms, defaultNamespace, null, null); - sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf); - ModelContext.setBaseOntModelSelector(baseOms, sce.getServletContext()); + ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); + ModelContext.setBaseOntModelSelector(baseOms, ctx); - sce.getServletContext().setAttribute("inferenceOntModel", inferenceModel); + ctx.setAttribute("inferenceOntModel", inferenceModel); WebappDaoFactory infWadf = new WebappDaoFactoryJena( inferenceOms, defaultNamespace, null, null); - sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf); - ModelContext.setInferenceOntModelSelector(inferenceOms, sce.getServletContext()); + ctx.setAttribute("deductionsWebappDaoFactory", infWadf); + ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); - sce.getServletContext().setAttribute("jenaOntModel", unionModel); + ctx.setAttribute("jenaOntModel", unionModel); WebappDaoFactory wadf = new WebappDaoFactoryJena( unionOms, baseOms, inferenceOms, defaultNamespace, null, null); - sce.getServletContext().setAttribute("webappDaoFactory",wadf); - ModelContext.setUnionOntModelSelector(unionOms, sce.getServletContext()); + ctx.setAttribute("webappDaoFactory",wadf); + ModelContext.setUnionOntModelSelector(unionOms, ctx); ApplicationBean appBean = getApplicationBeanFromOntModel(memModel,wadf); if (appBean != null) { - sce.getServletContext().setAttribute("applicationBean", appBean); + ctx.setAttribute("applicationBean", appBean); } if (isEmpty(memModel)) { - loadDataFromFilesystem(memModel, sce.getServletContext()); + loadDataFromFilesystem(memModel, ctx); } if (userAccountsModel.size() == 0) { - readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel); + readOntologyFilesInPathSet(AUTHPATH, ctx, userAccountsModel); if (userAccountsModel.size() == 0) { - createInitialAdminUser(userAccountsModel); + createInitialAdminUser(userAccountsModel, ctx); } } ensureEssentialInterfaceData(memModel, sce, wadf); NamespaceMapper namespaceMapper = new NamespaceMapperJena(unionModel, unionModel, defaultNamespace); - sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper); + ctx.setAttribute("NamespaceMapper", namespaceMapper); memModel.getBaseModel().register(namespaceMapper); - makeModelMakerFromConnectionProperties(TripleStoreType.RDB); + makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); setVitroJenaModelMaker(vjmm,sce); - makeModelMakerFromConnectionProperties(TripleStoreType.SDB); + makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker(); setVitroJenaSDBModelMaker(vsmm,sce); @@ -185,7 +183,9 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java /* ====================================================================== */ - public void contextDestroyed(ServletContextEvent sce) { + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Nothing to do } private ApplicationBean getApplicationBeanFromOntModel(OntModel ontModel,WebappDaoFactory wadf) { 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 383b2456d..a26609a60 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 @@ -8,6 +8,8 @@ import java.util.Set; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.logging.Log; @@ -18,10 +20,7 @@ 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.Resource; -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; @@ -47,10 +46,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { DEFAULT_TESTSPEREVICTION = 3, DEFAULT_MINEVICTIONIDLETIME = 1000 * 60 * 30; // ms - protected final static String VALIDATIONQUERY = - ConfigurationProperties.getProperty( - "VitroConnection.DataSource.validationQuery", "SELECT 1"); - protected final static boolean DEFAULT_TESTONBORROW = true, DEFAULT_TESTONRETURN = true, DEFAULT_TESTWHILEIDLE = true; @@ -63,11 +58,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { String DB_USER = "jenatest"; // database user id String DB_PASSWD = "jenatest"; // database password - String DB = ConfigurationProperties.getProperty( // database type - "VitroConnection.DataSource.dbtype","MySQL"); - String DB_DRIVER_CLASS_NAME = ConfigurationProperties.getProperty( - "VitroConnection.DataSource.driver", - "com.mysql.jdbc.Driver"); // ABox assertions. These are stored in a database (Jena SDB) and the // application works (queries and updates) with the ABox data from the DB - @@ -128,14 +118,13 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; - private String getJdbcUrl() { - + private String getJdbcUrl(ServletContext ctx) { String jdbcUrl = ConfigurationProperties.getProperty( "VitroConnection.DataSource.url"); // Ensure that MySQL handles unicode properly, else all kinds of // horrible nastiness ensues. - if ("MySQL".equals(DB) && !jdbcUrl.contains("?")) { + if ("MySQL".equals(getDbType(ctx)) && !jdbcUrl.contains("?")) { jdbcUrl += "?useUnicode=yes&characterEncoding=utf8"; } @@ -149,16 +138,16 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { */ public final Model makeDBModelFromConfigurationProperties( String jenaDbModelName, - OntModelSpec jenaDbOntModelSpec) { + OntModelSpec jenaDbOntModelSpec, ServletContext ctx) { - String jdbcUrl = getJdbcUrl(); + String jdbcUrl = getJdbcUrl(ctx); String username = ConfigurationProperties.getProperty( "VitroConnection.DataSource.username"); String password = ConfigurationProperties.getProperty( "VitroConnection.DataSource.password"); BasicDataSource ds = makeBasicDataSource( - DB_DRIVER_CLASS_NAME, jdbcUrl, username, password); + getDbDriverClassName(ctx), jdbcUrl, username, password, ctx); String dns = ConfigurationProperties.getProperty( "Vitro.defaultNamespace"); @@ -168,7 +157,7 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { ? jenaDbOntModelSpec : DB_ONT_MODEL_SPEC; - return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec); + return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec, ctx); } @@ -176,16 +165,16 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { * Sets up a BasicDataSource using values from * a properties file. */ - public final BasicDataSource makeDataSourceFromConfigurationProperties(){ + public final BasicDataSource makeDataSourceFromConfigurationProperties(ServletContext ctx){ String dbDriverClassname = ConfigurationProperties.getProperty( - "VitroConnection.DataSource.driver", DB_DRIVER_CLASS_NAME); - String jdbcUrl = getJdbcUrl(); + "VitroConnection.DataSource.driver", getDbDriverClassName(ctx)); + String jdbcUrl = getJdbcUrl(ctx); String username = ConfigurationProperties.getProperty( "VitroConnection.DataSource.username"); String password = ConfigurationProperties.getProperty( "VitroConnection.DataSource.password"); return makeBasicDataSource( - dbDriverClassname, jdbcUrl, username, password); + dbDriverClassname, jdbcUrl, username, password, ctx); } public void setApplicationDataSource(BasicDataSource bds, @@ -204,7 +193,8 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { public static BasicDataSource makeBasicDataSource(String dbDriverClassname, String jdbcUrl, String username, - String password) { + String password, + ServletContext ctx) { log.debug("makeBasicDataSource('" + dbDriverClassname + "', '" + jdbcUrl + "', '" + username + "', '" + password + "')"); BasicDataSource ds = new BasicDataSource(); @@ -239,7 +229,7 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { ds.setMaxActive(maxActiveInt); ds.setMaxIdle(maxIdleInt); ds.setMaxWait(DEFAULT_MAXWAIT); - ds.setValidationQuery(VALIDATIONQUERY); + ds.setValidationQuery(getValidationQuery(ctx)); ds.setTestOnBorrow(DEFAULT_TESTONBORROW); ds.setTestOnReturn(DEFAULT_TESTONRETURN); ds.setMinEvictableIdleTimeMillis(DEFAULT_MINEVICTIONIDLETIME); @@ -261,23 +251,25 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { protected Model makeDBModel(BasicDataSource ds, String jenaDbModelname, - OntModelSpec jenaDbOntModelSpec) { + OntModelSpec jenaDbOntModelSpec, + ServletContext ctx) { return makeDBModel( - ds, jenaDbModelname, jenaDbOntModelSpec, TripleStoreType.RDB); + ds, jenaDbModelname, jenaDbOntModelSpec, TripleStoreType.RDB, ctx); } protected Model makeDBModel(BasicDataSource ds, String jenaDbModelName, OntModelSpec jenaDbOntModelSpec, - TripleStoreType storeType) { + TripleStoreType storeType, ServletContext ctx) { return makeDBModel ( - ds, jenaDbModelName, jenaDbOntModelSpec, storeType, DB); + ds, jenaDbModelName, jenaDbOntModelSpec, storeType, getDbType(ctx), ctx); } public static Model makeDBModel(BasicDataSource ds, String jenaDbModelName, OntModelSpec jenaDbOntModelSpec, - TripleStoreType storeType, String dbType) { + TripleStoreType storeType, String dbType, + ServletContext ctx) { Model dbModel = null; try { // open the db model @@ -352,7 +344,7 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { * If the {@link ConfigurationProperties} has a name for the initial admin * user, create the user and add it to the model. */ - protected void createInitialAdminUser(Model model) { + protected void createInitialAdminUser(Model model, ServletContext ctx) { String initialAdminUsername = ConfigurationProperties .getProperty("initialAdminUser"); if (initialAdminUsername == null) { @@ -386,8 +378,8 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { private static final String sdbModelMaker = "vitroJenaSDBModelMaker"; private static final String rdbModelMaker = "vitroJenaModelMaker"; - protected void makeModelMakerFromConnectionProperties(TripleStoreType type){ - String jdbcUrl = getJdbcUrl(); + protected void makeModelMakerFromConnectionProperties(TripleStoreType type, ServletContext ctx){ + String jdbcUrl = getJdbcUrl(ctx); String username = ConfigurationProperties.getProperty( "VitroConnection.DataSource.username"); String password = ConfigurationProperties.getProperty( @@ -395,14 +387,14 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { if (TripleStoreType.RDB.equals(type)){ vjmm = new VitroJenaModelMaker( - jdbcUrl, username, password, DB_TYPE); + jdbcUrl, username, password, DB_TYPE, ctx); } else if(TripleStoreType.SDB.equals(type)){ StoreDesc storeDesc = new StoreDesc( LayoutType.LayoutTripleNodesHash, DatabaseType.MySQL); BasicDataSource bds = JenaDataSourceSetup.makeBasicDataSource( - DB_DRIVER_CLASS_NAME, jdbcUrl, username, password); + getDbDriverClassName(ctx), jdbcUrl, username, password, ctx); try { vsmm = new VitroJenaSDBModelMaker(storeDesc, bds); } catch (SQLException sqle) { @@ -424,11 +416,19 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { sce.getServletContext().setAttribute(sdbModelMaker, vsmm); } - public static boolean isSDBActive() { - String tripleStoreTypeStr = - ConfigurationProperties.getProperty( - "VitroConnection.DataSource.tripleStoreType", "RDB"); - return ("SDB".equals(tripleStoreTypeStr)); + public static boolean isSDBActive(ServletRequest req) { + if (!(req instanceof HttpServletRequest)) { + return false; + } + HttpServletRequest hreq = (HttpServletRequest) req; + return isSDBActive(hreq.getSession().getServletContext()); + } + + public static boolean isSDBActive(ServletContext ctx) { + String tripleStoreTypeStr = + ConfigurationProperties.getProperty( + "VitroConnection.DataSource.tripleStoreType", "RDB"); + return ("SDB".equals(tripleStoreTypeStr)); } protected VitroJenaModelMaker getVitroJenaModelMaker(){ @@ -439,4 +439,21 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { return vsmm; } + private static String getDbType(ServletContext ctx) { + return ConfigurationProperties.getProperty( // database type + "VitroConnection.DataSource.dbtype","MySQL"); + } + + private static String getDbDriverClassName(ServletContext ctx) { + return ConfigurationProperties.getProperty( + "VitroConnection.DataSource.driver", + "com.mysql.jdbc.Driver"); + + } + private static String getValidationQuery(ServletContext ctx) { + return ConfigurationProperties.getProperty( + "VitroConnection.DataSource.validationQuery", "SELECT 1"); + } + + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupSDB.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupSDB.java index 4a2bf7c6d..1c5628dd5 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupSDB.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaDataSourceSetupSDB.java @@ -62,9 +62,11 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j private static final Log log = LogFactory.getLog(JenaDataSourceSetupSDB.class); - public void contextInitialized(ServletContextEvent sce) { + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); - if (AbortStartup.isStartupAborted(sce.getServletContext())) { + if (AbortStartup.isStartupAborted(ctx)) { return; } @@ -74,28 +76,28 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j // up things related to the DB. // TODO: I would like to make this code (before the sdb try/catch conditional so // that it is not executed in a post-sdb-conversion environment. - OntModel memModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); + OntModel memModel = (OntModel) ctx.getAttribute("jenaOntModel"); //memModel.writeAll(System.out,"N3",null); - if ( updateRequired(sce.getServletContext(), memModel)) { + if ( updateRequired(ctx, memModel)) { log.error(getMigrationErrString()); System.out.println(getMigrationErrString()); // The rest of the application should not // start if this condition is encountered - AbortStartup.abortStartup(sce.getServletContext()); + AbortStartup.abortStartup(ctx); throw new MigrationRequiredError(getMigrationErrString()); } if (memModel == null) { memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); log.warn("WARNING: no database connected. Changes will disappear after context restart."); - sce.getServletContext().setAttribute("jenaOntModel",memModel); + ctx.setAttribute("jenaOntModel",memModel); } - OntModel inferenceModel = ontModelFromContextAttribute(sce.getServletContext(), "inferenceOntModel"); + OntModel inferenceModel = ontModelFromContextAttribute(ctx, "inferenceOntModel"); - OntModel userAccountsModel = ontModelFromContextAttribute(sce.getServletContext(), "userAccountsOntModel"); + OntModel userAccountsModel = ontModelFromContextAttribute(ctx, "userAccountsOntModel"); if (userAccountsModel.size() == 0) { checkMainModelForUserAccounts(memModel, userAccountsModel); } @@ -110,7 +112,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j inferenceOms.setUserAccountsModel(userAccountsModel); unionOms.setUserAccountsModel(userAccountsModel); - OntModel displayModel = ontModelFromContextAttribute(sce.getServletContext(),"displayOntModel"); + OntModel displayModel = ontModelFromContextAttribute(ctx,"displayOntModel"); baseOms.setDisplayModel(displayModel); inferenceOms.setDisplayModel(displayModel); unionOms.setDisplayModel(displayModel); @@ -123,17 +125,17 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j SDB.getContext().set(SDB.unionDefaultGraph, true) ; StoreDesc storeDesc = makeStoreDesc(); - setApplicationStoreDesc(storeDesc, sce.getServletContext()); + setApplicationStoreDesc(storeDesc, ctx); - BasicDataSource bds = makeDataSourceFromConfigurationProperties(); - this.setApplicationDataSource(bds, sce.getServletContext()); + BasicDataSource bds = makeDataSourceFromConfigurationProperties(ctx); + this.setApplicationDataSource(bds, ctx); Store store = connectStore(bds, storeDesc); - setApplicationStore(store, sce.getServletContext()); + setApplicationStore(store, ctx); if (!isSetUp(store)) { log.info("Non-SDB system detected. Setting up SDB store"); - setupSDB(sce.getServletContext(), store, memModel, inferenceModel); + setupSDB(ctx, store, memModel, inferenceModel); } // The code below, which sets up the OntModelSelectors, controls whether each @@ -144,12 +146,12 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j // 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); + 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); + 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)); @@ -158,7 +160,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j // 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); + 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) { @@ -176,7 +178,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j // TBox inferences try { - Model tboxInferencesDB = makeDBModel(bds, JENA_TBOX_INF_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); + 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) { @@ -204,7 +206,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j // Application metadata model is cached in memory. try { - Model applicationMetadataModelDB = makeDBModel(bds, JENA_APPLICATION_METADATA_MODEL, DB_ONT_MODEL_SPEC, TripleStoreType.SDB); + 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(); @@ -239,7 +241,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j OntModel baseUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, ModelFactory.createUnion(baseOms.getABoxModel(), baseOms.getTBoxModel())); baseOms.setFullModel(baseUnion); - ModelContext.setBaseOntModel(baseOms.getFullModel(), sce.getServletContext()); + ModelContext.setBaseOntModel(baseOms.getFullModel(), ctx); WebappDaoFactory baseWadf = new WebappDaoFactorySDB( baseOms, bds, @@ -248,12 +250,12 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j null, null, WebappDaoFactorySDB.SDBDatasetMode.ASSERTIONS_ONLY); - sce.getServletContext().setAttribute("assertionsWebappDaoFactory",baseWadf); + ctx.setAttribute("assertionsWebappDaoFactory",baseWadf); OntModel inferenceUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, ModelFactory.createUnion(inferenceOms.getABoxModel(), inferenceOms.getTBoxModel())); inferenceOms.setFullModel(inferenceUnion); - ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), sce.getServletContext()); + ModelContext.setInferenceOntModel(inferenceOms.getFullModel(), ctx); WebappDaoFactory infWadf = new WebappDaoFactorySDB( inferenceOms, bds, @@ -262,34 +264,34 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j null, null, WebappDaoFactorySDB.SDBDatasetMode.INFERENCES_ONLY); - sce.getServletContext().setAttribute("deductionsWebappDaoFactory", infWadf); + ctx.setAttribute("deductionsWebappDaoFactory", infWadf); OntModel masterUnion = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, ModelFactory.createUnion(unionABoxModel, unionTBoxModel)); unionOms.setFullModel(masterUnion); - sce.getServletContext().setAttribute("jenaOntModel", masterUnion); + ctx.setAttribute("jenaOntModel", masterUnion); WebappDaoFactory wadf = new WebappDaoFactorySDB(unionOms, bds, storeDesc, defaultNamespace, null, null); - sce.getServletContext().setAttribute("webappDaoFactory",wadf); + ctx.setAttribute("webappDaoFactory",wadf); - ModelContext.setUnionOntModelSelector(unionOms, sce.getServletContext()); // assertions and inferences - ModelContext.setBaseOntModelSelector(baseOms, sce.getServletContext()); // assertions - ModelContext.setInferenceOntModelSelector(inferenceOms, sce.getServletContext()); // inferences + ModelContext.setUnionOntModelSelector(unionOms, ctx); // assertions and inferences + ModelContext.setBaseOntModelSelector(baseOms, ctx); // assertions + ModelContext.setInferenceOntModelSelector(inferenceOms, ctx); // inferences ApplicationBean appBean = getApplicationBeanFromOntModel(unionOms.getFullModel(),wadf); if (appBean != null) { - sce.getServletContext().setAttribute("applicationBean", appBean); + ctx.setAttribute("applicationBean", appBean); } //if (isEmpty(unionOms.getFullModel())) { - // loadDataFromFilesystem(unionOms.getFullModel(), sce.getServletContext()); + // loadDataFromFilesystem(unionOms.getFullModel(), ctx); //} log.info("Checking for user account data"); if (userAccountsModel.size() == 0) { - readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsModel); + readOntologyFilesInPathSet(AUTHPATH, ctx, userAccountsModel); if (userAccountsModel.size() == 0) { - createInitialAdminUser(userAccountsModel); + createInitialAdminUser(userAccountsModel, ctx); } } @@ -300,17 +302,17 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j log.info("Setting up namespace mapper"); NamespaceMapper namespaceMapper = new NamespaceMapperJena(masterUnion, masterUnion, defaultNamespace); - sce.getServletContext().setAttribute("NamespaceMapper", namespaceMapper); + ctx.setAttribute("NamespaceMapper", namespaceMapper); unionOms.getFullModel().getBaseModel().register(namespaceMapper); - sce.getServletContext().setAttribute("defaultNamespace", defaultNamespace); + ctx.setAttribute("defaultNamespace", defaultNamespace); log.info("SDB store ready for use"); - makeModelMakerFromConnectionProperties(TripleStoreType.RDB); + makeModelMakerFromConnectionProperties(TripleStoreType.RDB, ctx); VitroJenaModelMaker vjmm = getVitroJenaModelMaker(); setVitroJenaModelMaker(vjmm,sce); - makeModelMakerFromConnectionProperties(TripleStoreType.SDB); + makeModelMakerFromConnectionProperties(TripleStoreType.SDB, ctx); VitroJenaSDBModelMaker vsmm = getVitroJenaSDBModelMaker(); setVitroJenaSDBModelMaker(vsmm,sce); @@ -321,7 +323,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j } catch (SQLException sqle) { // SQL exceptions are fatal and should halt startup - AbortStartup.abortStartup(sce.getServletContext()); + AbortStartup.abortStartup(ctx); log.error("Error using SQL database; startup aborted.", sqle); @@ -375,7 +377,9 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j /* ====================================================================== */ - public void contextDestroyed(ServletContextEvent sce) { + @Override + public void contextDestroyed(ServletContextEvent sce) { + // Nothing to do. } private ApplicationBean getApplicationBeanFromOntModel(OntModel ontModel,WebappDaoFactory wadf) { @@ -383,7 +387,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j ResourceFactory.createResource(VitroVocabulary.APPLICATION)); try { if (appIt.hasNext()) { - Individual appInd = (Individual) appIt.next(); + Individual appInd = appIt.next(); ApplicationBean appBean = new ApplicationBean(); try { appBean.setMaxPortalId(Integer.decode( ((Literal)appInd.getPropertyValue(ResourceFactory.createProperty(VitroVocabulary.APPLICATION_MAXPORTALID))).getLexicalForm())); @@ -424,7 +428,7 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j //Set the default namespace to the namespace of the first portal object we find. //This will keep existing applications from dying when the default namespace //config option is missing. - Individual portal = (Individual) portalIt.next(); + Individual portal = portalIt.next(); if (portal.getNameSpace() != null) { defaultNamespace = portal.getNameSpace(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDBOnlyDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDBOnlyDataSourceSetup.java index b39dc548d..a05e47d41 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDBOnlyDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDBOnlyDataSourceSetup.java @@ -23,11 +23,14 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase private static final Log log = LogFactory.getLog(JenaPersistentDataSourceSetup.class.getName()); + @Override public void contextInitialized(ServletContextEvent sce) { - OntModel memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); + ServletContext ctx = sce.getServletContext(); + + OntModel memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); OntModel dbModel = null; try { - Model dbPlainModel = makeDBModelFromConfigurationProperties(JENA_DB_MODEL, DB_ONT_MODEL_SPEC); + Model dbPlainModel = makeDBModelFromConfigurationProperties(JENA_DB_MODEL, DB_ONT_MODEL_SPEC, ctx); dbModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,dbPlainModel); boolean isEmpty = true; ClosableIterator stmtIt = dbModel.listStatements(); @@ -42,15 +45,14 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase if (isEmpty) { long startTime = System.currentTimeMillis(); System.out.println("Reading ontology files into database"); - ServletContext ctx = sce.getServletContext(); readOntologyFilesInPathSet(USERPATH, ctx, dbModel); readOntologyFilesInPathSet(AUTHPATH, ctx, dbModel); readOntologyFilesInPathSet(SYSTEMPATH, ctx, dbModel); System.out.println((System.currentTimeMillis()-startTime)/1000+" seconds to populate DB"); } - //readOntologyFilesInPathSet(sce.getServletContext().getResourcePaths(AUTHPATH), sce, dbModel); - //readOntologyFilesInPathSet(sce.getServletContext().getResourcePaths(SYSTEMPATH), sce, dbModel); + //readOntologyFilesInPathSet(ctx.getResourcePaths(AUTHPATH), sce, dbModel); + //readOntologyFilesInPathSet(ctx.getResourcePaths(SYSTEMPATH), sce, dbModel); memModel = dbModel; @@ -65,7 +67,6 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase try { if (dbModel==null) { System.out.println("Reading ontology files"); - ServletContext ctx = sce.getServletContext(); readOntologyFilesInPathSet(USERPATH, ctx, dbModel); readOntologyFilesInPathSet(AUTHPATH, ctx, dbModel); readOntologyFilesInPathSet(SYSTEMPATH, ctx, dbModel); @@ -77,20 +78,21 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase // default inference graph try { - Model infDbPlainModel = makeDBModelFromConfigurationProperties(JENA_INF_MODEL, DB_ONT_MODEL_SPEC); + Model infDbPlainModel = makeDBModelFromConfigurationProperties(JENA_INF_MODEL, DB_ONT_MODEL_SPEC, ctx); OntModel infDbModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,infDbPlainModel); - sce.getServletContext().setAttribute("inferenceOntModel",infDbModel); + ctx.setAttribute("inferenceOntModel",infDbModel); } catch (Throwable e) { log.error(e, e); } - sce.getServletContext().setAttribute("jenaOntModel", memModel); - sce.getServletContext().setAttribute("persistentOntModel", dbModel); + ctx.setAttribute("jenaOntModel", memModel); + ctx.setAttribute("persistentOntModel", dbModel); // BJL23: This is a funky hack until I completely rework how the models get set up in a more sane fashion - sce.getServletContext().setAttribute("useModelSynchronizers", "false"); + ctx.setAttribute("useModelSynchronizers", "false"); } + @Override public void contextDestroyed(ServletContextEvent sce) { //Close the database connection //try { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java index 86f3074ea..1238aa2b4 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JenaPersistentDataSourceSetup.java @@ -6,7 +6,6 @@ import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; -import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -26,9 +25,11 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase private static final Log log = LogFactory.getLog( JenaPersistentDataSourceSetup.class.getName()); + @Override public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); - if (AbortStartup.isStartupAborted(sce.getServletContext())) { + if (AbortStartup.isStartupAborted(ctx)) { return; } @@ -39,14 +40,13 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase try { dbModel = makeDBModelFromConfigurationProperties( - JENA_DB_MODEL, DB_ONT_MODEL_SPEC); + JENA_DB_MODEL, DB_ONT_MODEL_SPEC, ctx); boolean firstStartup = isFirstStartup(dbModel); if (firstStartup) { long startTime = System.currentTimeMillis(); System.out.println("Reading ontology files into database"); - ServletContext ctx = sce.getServletContext(); readOntologyFilesInPathSet(USERPATH, ctx, dbModel); readOntologyFilesInPathSet(SYSTEMPATH, ctx, dbModel); System.out.println( @@ -55,7 +55,7 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase } - if (isSDBActive()) { + if (isSDBActive(ctx)) { memModel = ModelFactory.createOntologyModel( this.DB_ONT_MODEL_SPEC, dbModel); // no in-memory copying @@ -94,10 +94,10 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase // default inference graph try { Model infDbModel = makeDBModelFromConfigurationProperties( - JENA_INF_MODEL, DB_ONT_MODEL_SPEC); + JENA_INF_MODEL, DB_ONT_MODEL_SPEC, ctx); OntModel infModel = null; if (infDbModel != null) { - if (isSDBActive()) { + if (isSDBActive(ctx)) { infModel = ModelFactory.createOntologyModel( MEM_ONT_MODEL_SPEC, infDbModel); } else { @@ -112,7 +112,7 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase } } infModel.getBaseModel().register(new ModelSynchronizer(infDbModel)); - sce.getServletContext().setAttribute("inferenceOntModel",infModel); + ctx.setAttribute("inferenceOntModel",infModel); } catch (Throwable e) { log.error("Unable to load inference cache from DB", e); } @@ -120,12 +120,12 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase // user accounts Model try { Model userAccountsDbModel = makeDBModelFromConfigurationProperties( - JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC); + JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC, ctx); if (userAccountsDbModel.size() == 0) { readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsDbModel); if (userAccountsDbModel.size() == 0) { - createInitialAdminUser(userAccountsDbModel); + createInitialAdminUser(userAccountsDbModel, ctx); } } OntModel userAccountsModel = ModelFactory.createOntologyModel( @@ -142,7 +142,7 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase // display, editing and navigation Model try { Model appDbModel = makeDBModelFromConfigurationProperties( - JENA_DISPLAY_METADATA_MODEL, DB_ONT_MODEL_SPEC); + JENA_DISPLAY_METADATA_MODEL, DB_ONT_MODEL_SPEC, ctx); if (appDbModel.size() == 0) readOntologyFilesInPathSet( APPPATH, sce.getServletContext(),appDbModel); @@ -150,12 +150,12 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase MEM_ONT_MODEL_SPEC); appModel.add(appDbModel); appModel.getBaseModel().register(new ModelSynchronizer(appDbModel)); - sce.getServletContext().setAttribute("displayOntModel", appModel); + ctx.setAttribute("displayOntModel", appModel); } catch (Throwable t) { log.error("Unable to load user application configuration model from DB", t); } - sce.getServletContext().setAttribute("jenaOntModel", memModel); + ctx.setAttribute("jenaOntModel", memModel); } @@ -171,7 +171,9 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase } } + @Override public void contextDestroyed(ServletContextEvent sce) { + // Nothing to do. } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelAuditorSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelAuditorSetup.java index ed3278b03..cdb141bb1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelAuditorSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ModelAuditorSetup.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup; +import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -12,28 +13,30 @@ import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; -import edu.cornell.mannlib.vitro.webapp.auth.policy.JenaNetidPolicy.ContextSetup; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelAuditor; public class ModelAuditorSetup extends JenaDataSourceSetupBase implements ServletContextListener { private static final Log log = LogFactory.getLog(ModelAuditorSetup.class.getName()); + @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } + @Override public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); try { // the events don't seem to filter down to the writable model of a dynamic union. Bummer. For now we have the ugly workaround of registering twice and recording some duplication. - OntModel ontModel = (OntModel) sce.getServletContext().getAttribute("baseOntModel"); + OntModel ontModel = (OntModel) ctx.getAttribute("baseOntModel"); Model baseModel = ontModel.getBaseModel(); - OntModel dynamicUnionModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); + OntModel dynamicUnionModel = (OntModel) ctx.getAttribute("jenaOntModel"); log.debug("Setting model auditor database..."); - OntModel auditModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,makeDBModelFromConfigurationProperties(JENA_AUDIT_MODEL, DB_ONT_MODEL_SPEC)); - sce.getServletContext().setAttribute("jenaAuditModel", auditModel); + OntModel auditModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,makeDBModelFromConfigurationProperties(JENA_AUDIT_MODEL, DB_ONT_MODEL_SPEC, ctx)); + ctx.setAttribute("jenaAuditModel", auditModel); ModelAuditor ma = new ModelAuditor(auditModel,ontModel); baseModel.register(ma); dynamicUnionModel.getBaseModel().register(ma); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java index 62ee5f160..988567432 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/SimpleReasonerSetup.java @@ -2,8 +2,6 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup; -import java.sql.Connection; - import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -13,23 +11,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mindswap.pellet.PelletOptions; -import com.hp.hpl.jena.graph.Graph; -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.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.vocabulary.OWL; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; -import edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator; -import edu.cornell.mannlib.vitro.webapp.dao.jena.RegeneratingGraph; -import edu.cornell.mannlib.vitro.webapp.dao.jena.SDBGraphGenerator; import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena; import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener; import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.ReasonerConfiguration; @@ -45,6 +31,7 @@ public class SimpleReasonerSetup implements ServletContextListener { static final String JENA_INF_MODEL_REBUILD = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf-rebuild"; static final String JENA_INF_MODEL_SCRATCHPAD = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf-scratchpad"; + @Override public void contextInitialized(ServletContextEvent sce) { if (AbortStartup.isStartupAborted(sce.getServletContext())) { @@ -97,13 +84,13 @@ public class SimpleReasonerSetup implements ServletContextListener { JENA_INF_MODEL_REBUILD, JenaDataSourceSetupBase.DB_ONT_MODEL_SPEC, TripleStoreType.SDB, - dbType); + dbType, ctx); Model scratchModel = JenaDataSourceSetupBase.makeDBModel( bds, JENA_INF_MODEL_SCRATCHPAD, JenaDataSourceSetupBase.DB_ONT_MODEL_SPEC, TripleStoreType.SDB, - dbType); + dbType, ctx); // the simple reasoner will register itself as a listener to the ABox assertions @@ -137,6 +124,7 @@ public class SimpleReasonerSetup implements ServletContextListener { } } + @Override public void contextDestroyed(ServletContextEvent arg0) { // nothing to do }