VIVO-248 Move more RDF files into the home directory
Files for initializing the display, displayTbox and displayDisplay models. Also restructure the RDFFilesLoader.
This commit is contained in:
parent
290333a21e
commit
f2312a4067
12 changed files with 151 additions and 314 deletions
|
@ -3,12 +3,10 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.search.beans;
|
package edu.cornell.mannlib.vitro.webapp.search.beans;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
|
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
import edu.cornell.mannlib.vitro.webapp.servlet.setup.RDFFilesLoader;
|
||||||
|
|
||||||
public class FileBasedProhibitedFromSearch extends ProhibitedFromSearch {
|
public class FileBasedProhibitedFromSearch extends ProhibitedFromSearch {
|
||||||
|
|
||||||
|
@ -19,7 +17,7 @@ public class FileBasedProhibitedFromSearch extends ProhibitedFromSearch {
|
||||||
* @param dir to find N3 files in.
|
* @param dir to find N3 files in.
|
||||||
*/
|
*/
|
||||||
public FileBasedProhibitedFromSearch(String uri, File dir){
|
public FileBasedProhibitedFromSearch(String uri, File dir){
|
||||||
super( uri, JenaDataSourceSetupBase.getModelFromDir(dir));
|
super( uri, RDFFilesLoader.getModelFromDir(dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileBasedProhibitedFromSearch(String URI, OntModel model) {
|
public FileBasedProhibitedFromSearch(String URI, OntModel model) {
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletContextEvent;
|
import javax.servlet.ServletContextEvent;
|
||||||
import javax.servlet.ServletContextListener;
|
import javax.servlet.ServletContextListener;
|
||||||
|
@ -11,14 +8,8 @@ import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.joda.time.DateTime;
|
|
||||||
import org.joda.time.format.ISODateTimeFormat;
|
|
||||||
|
|
||||||
import com.hp.hpl.jena.ontology.OntModel;
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
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.rdf.model.Model;
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
|
||||||
|
@ -58,16 +49,15 @@ implements ServletContextListener {
|
||||||
try {
|
try {
|
||||||
Model displayDbModel = makeDBModel(bds,
|
Model displayDbModel = makeDBModel(bds,
|
||||||
JENA_DISPLAY_METADATA_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
JENA_DISPLAY_METADATA_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
||||||
if (displayDbModel.size() == 0) {
|
RDFFilesLoader.loadFirstTimeFiles(ctx, "display", displayDbModel, displayDbModel.isEmpty());
|
||||||
readOntologyFilesInPathSet(APPPATH, ctx,displayDbModel);
|
|
||||||
}
|
|
||||||
OntModel displayModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
OntModel displayModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||||
displayModel.add(displayDbModel);
|
displayModel.add(displayDbModel);
|
||||||
displayModel.getBaseModel().register(new ModelSynchronizer(displayDbModel));
|
displayModel.getBaseModel().register(new ModelSynchronizer(displayDbModel));
|
||||||
ModelAccess.on(ctx).setDisplayModel(displayModel);
|
ModelAccess.on(ctx).setDisplayModel(displayModel);
|
||||||
|
|
||||||
//at each startup load all RDF files from directory to sub-models of display model
|
//at each startup load all RDF files from directory to sub-models of display model
|
||||||
initializeDisplayLoadedAtStartup(ctx, displayModel);
|
RDFFilesLoader.loadEveryTimeFiles(ctx, "display", displayModel);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error("Unable to load user application configuration model", t);
|
log.error("Unable to load user application configuration model", t);
|
||||||
ss.fatal(this, "Unable to load user application configuration model", t);
|
ss.fatal(this, "Unable to load user application configuration model", t);
|
||||||
|
@ -78,159 +68,36 @@ implements ServletContextListener {
|
||||||
Model displayTboxModel = makeDBModel(bds,
|
Model displayTboxModel = makeDBModel(bds,
|
||||||
JENA_DISPLAY_TBOX_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
JENA_DISPLAY_TBOX_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
||||||
|
|
||||||
//Reading in single file every time, needs to be cleared/removed every time
|
OntModel appTBOXModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||||
readOntologyFileFromPath(APPPATH_LOAD + "displayTBOX.n3", displayTboxModel, ctx);
|
|
||||||
OntModel appTBOXModel = ModelFactory.createOntologyModel(
|
|
||||||
MEM_ONT_MODEL_SPEC);
|
|
||||||
appTBOXModel.add(displayTboxModel);
|
appTBOXModel.add(displayTboxModel);
|
||||||
appTBOXModel.getBaseModel().register(new ModelSynchronizer(displayTboxModel));
|
appTBOXModel.getBaseModel().register(new ModelSynchronizer(displayTboxModel));
|
||||||
ModelAccess.on(ctx).setOntModel(ModelID.DISPLAY_TBOX, appTBOXModel);
|
ModelAccess.on(ctx).setOntModel(ModelID.DISPLAY_TBOX, appTBOXModel);
|
||||||
log.debug("Loaded file " + APPPATH_LOAD + "displayTBOX.n3 into display tbox model");
|
|
||||||
|
//Reading in every time, needs to be cleared/removed every time
|
||||||
|
RDFFilesLoader.loadEveryTimeFiles(ctx, "displayTbox", appTBOXModel);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error("Unable to load user application configuration model TBOX", t);
|
log.error("Unable to load user application configuration model TBOX", t);
|
||||||
ss.fatal(this, "Unable to load user application configuration model TBOX", t);
|
ss.fatal(this, "Unable to load user application configuration model TBOX", t);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Display Display model, currently empty, create if doesn't exist but no files to load
|
//Display Display model, currently reading in every time
|
||||||
try {
|
try {
|
||||||
Model displayDisplayModel = makeDBModel(bds,
|
Model displayDisplayModel = makeDBModel(bds,
|
||||||
JENA_DISPLAY_DISPLAY_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
JENA_DISPLAY_DISPLAY_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
||||||
|
|
||||||
//Reading in single file every time, needs to be cleared/removed every
|
OntModel appDisplayDisplayModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||||
readOntologyFileFromPath(APPPATH_LOAD + "displayDisplay.n3", displayDisplayModel, ctx);
|
|
||||||
OntModel appDisplayDisplayModel = ModelFactory.createOntologyModel(
|
|
||||||
MEM_ONT_MODEL_SPEC);
|
|
||||||
appDisplayDisplayModel.add(displayDisplayModel);
|
appDisplayDisplayModel.add(displayDisplayModel);
|
||||||
appDisplayDisplayModel.getBaseModel().register(new ModelSynchronizer(displayDisplayModel));
|
appDisplayDisplayModel.getBaseModel().register(new ModelSynchronizer(displayDisplayModel));
|
||||||
ModelAccess.on(ctx).setOntModel(ModelID.DISPLAY_DISPLAY, appDisplayDisplayModel);
|
ModelAccess.on(ctx).setOntModel(ModelID.DISPLAY_DISPLAY, appDisplayDisplayModel);
|
||||||
log.debug("Loaded file " + APPPATH_LOAD + "displayDisplay.n3 into display display model");
|
|
||||||
|
//Reading in every time, needs to be cleared/removed every time
|
||||||
|
RDFFilesLoader.loadEveryTimeFiles(ctx, "displayDisplay", appDisplayDisplayModel);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
log.error("Unable to load user application configuration model Display Model", t);
|
log.error("Unable to load user application configuration model Display Model", t);
|
||||||
ss.fatal(this, "Unable to load user application configuration model Display Model", t);
|
ss.fatal(this, "Unable to load user application configuration model Display Model", t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the RDF found in the directory DISPLAY_MODEL_LOAD_AT_STARTUP_DIR
|
|
||||||
* a sub-models of displayModel. The RDF from thes files will not be saved
|
|
||||||
* in the database and it will be reloaded each time the system starts up.
|
|
||||||
*/
|
|
||||||
private void initializeDisplayLoadedAtStartup(ServletContext ctx, OntModel displayModel){
|
|
||||||
log.info("loading display model from files in " + ctx.getRealPath(DISPLAY_MODEL_LOAD_AT_STARTUP_DIR) );
|
|
||||||
Model displayLoadAtStartup = readInDisplayModelLoadAtStartup(ctx);
|
|
||||||
|
|
||||||
if( log.isDebugEnabled() ){
|
|
||||||
log.debug("loaded display model from files in " + ctx.getRealPath(DISPLAY_MODEL_LOAD_AT_STARTUP_DIR) );
|
|
||||||
displayLoadAtStartup.write(System.out, "N3-PP");
|
|
||||||
}
|
|
||||||
|
|
||||||
checkForOldListViews(ctx,displayModel,displayLoadAtStartup);
|
|
||||||
displayModel.addSubModel( displayLoadAtStartup );
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Model readInDisplayModelLoadAtStartup( ServletContext ctx ){
|
|
||||||
return getModelFromDir( new File( ctx.getRealPath( DISPLAY_MODEL_LOAD_AT_STARTUP_DIR )));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* All of the list views should now reside in files in DISPLAY_MODEL_LOAD_AT_STARTUP_DIR.
|
|
||||||
* This will check for custom list view annotation statements in the displayModel, check
|
|
||||||
* if they exist in the files in DISPLAY_MODEL_LOAD_AT_STARTUP_DIR, and write any that don't
|
|
||||||
* exist there to a file in DISPLAY_MODEL_LOAD_AT_STARTUP_DIR. After that the statements
|
|
||||||
* will be removed from the displayDBModel.
|
|
||||||
*
|
|
||||||
* returns true if there were old list view statements in the DB, returns false
|
|
||||||
* if there were none. displayLoadAlways should be reloaded from the file system
|
|
||||||
* if this returns true as this method may have changed the files.
|
|
||||||
*
|
|
||||||
* displayLoadAtStartup and displayModel may be modified.
|
|
||||||
*/
|
|
||||||
private void checkForOldListViews( ServletContext ctx, OntModel displayModel, Model displayLoadAtStartup){
|
|
||||||
// run construct for old custom list view statements from displayModel
|
|
||||||
Model oldListViewModel = getOldListViewStatements( displayModel );
|
|
||||||
if( log.isDebugEnabled() ){
|
|
||||||
log.debug("Printing the old list view statements from the display model to System.out.");
|
|
||||||
oldListViewModel.write(System.out,"N3-PP");
|
|
||||||
}
|
|
||||||
|
|
||||||
// find statements in old stmts that are not in loadedAtStartup and
|
|
||||||
// save them in a new file in DISPLAY_MODEL_LOAD_AT_STARTUP_DIR
|
|
||||||
// so that in the future they will be in loadedAtStartup
|
|
||||||
Model stmtsInOldAndFiles = displayLoadAtStartup.intersection( displayModel );
|
|
||||||
Model unhandledOldListViewStmts = oldListViewModel.difference( stmtsInOldAndFiles );
|
|
||||||
|
|
||||||
boolean saved = false;
|
|
||||||
boolean neededSave = false;
|
|
||||||
|
|
||||||
if( unhandledOldListViewStmts != null && !unhandledOldListViewStmts.isEmpty() ){
|
|
||||||
log.debug("need to deal with old list view statements from the display model");
|
|
||||||
neededSave = true;
|
|
||||||
try{
|
|
||||||
//create a file for the old statements in the loadAtStartup directory
|
|
||||||
String newFileName = ctx.getRealPath(
|
|
||||||
DISPLAY_MODEL_LOAD_AT_STARTUP_DIR + File.separator
|
|
||||||
+ new DateTime().toString(ISODateTimeFormat.basicDateTime()) + ".n3" );
|
|
||||||
File file = new File( newFileName );
|
|
||||||
file.createNewFile();
|
|
||||||
|
|
||||||
log.info("Relocating " + unhandledOldListViewStmts.size() + " custom list view statements from DB and saving to "
|
|
||||||
+ file.getAbsolutePath()+ File.separator + file.getName()
|
|
||||||
+ ". These will be loaded from this file when the system starts up.");
|
|
||||||
|
|
||||||
FileOutputStream fileOut = new FileOutputStream(file);
|
|
||||||
unhandledOldListViewStmts.write(fileOut, "N3-PP");
|
|
||||||
fileOut.close();
|
|
||||||
saved = true;
|
|
||||||
}catch(Throwable th){
|
|
||||||
log.warn("Could not save old list view statements. Leaving them in the DB",th);
|
|
||||||
}
|
|
||||||
|
|
||||||
//need to reload displayLoadAlways because DISPLAY_MODEL_LOAD_AT_STARTUP_DIR may have changed
|
|
||||||
displayLoadAtStartup.removeAll().add(readInDisplayModelLoadAtStartup(ctx));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if( oldListViewModel != null && ! oldListViewModel.isEmpty() ){
|
|
||||||
//At this point, there are old list view statements in the DB but they
|
|
||||||
//should are all redundant with ones in DISPLAY_MODEL_LOAD_AT_STARTUP_DIR
|
|
||||||
if( (neededSave && saved) || (!neededSave) ){
|
|
||||||
//if there was nothing to save, just remove the old stuff
|
|
||||||
//if there was stuff to save, only remove if it was saved.
|
|
||||||
log.debug("removing old statements from displayModel");
|
|
||||||
displayModel.remove(oldListViewModel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Model getOldListViewStatements(OntModel displayModel) {
|
|
||||||
//run a construct on displayModel to get all list view statements
|
|
||||||
Query query = QueryFactory.create ( listViewQuery );
|
|
||||||
QueryExecution qexec = QueryExecutionFactory.create(query, displayModel) ;
|
|
||||||
Model oldModel = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
oldModel = qexec.execConstruct();
|
|
||||||
} catch( Throwable th ){
|
|
||||||
log.error("could not check for old custom list views, query exception",th);
|
|
||||||
}finally {
|
|
||||||
qexec.close() ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( oldModel != null)
|
|
||||||
return oldModel;
|
|
||||||
else
|
|
||||||
return ModelFactory.createDefaultModel();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String listViewQuery = "" +
|
|
||||||
"PREFIX d: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#>\n" +
|
|
||||||
"CONSTRUCT { \n" +
|
|
||||||
" ?a d:listViewConfigFile ?b . \n" +
|
|
||||||
"} WHERE {\n" +
|
|
||||||
" ?a d:listViewConfigFile ?b . \n" +
|
|
||||||
"} ";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextDestroyed(ServletContextEvent arg0) {
|
public void contextDestroyed(ServletContextEvent arg0) {
|
||||||
// does nothing.
|
// does nothing.
|
||||||
|
|
|
@ -82,11 +82,10 @@ public class ContentModelSetup extends JenaDataSourceSetupBase
|
||||||
|
|
||||||
|
|
||||||
if (isFirstStartup()) {
|
if (isFirstStartup()) {
|
||||||
RDFFilesLoader loader = new RDFFilesLoader(ctx);
|
RDFFilesLoader.loadFirstTimeFiles(ctx, "abox", baseABoxModel, true);
|
||||||
loader.loadFirstTimeFiles("abox", baseABoxModel, true);
|
RDFFilesLoader.loadFirstTimeFiles(ctx, "tbox", baseTBoxModel, true);
|
||||||
loader.loadFirstTimeFiles("tbox", baseTBoxModel, true);
|
|
||||||
|
|
||||||
loader.loadFirstTimeFiles("applicationMetadata", applicationMetadataModel, true);
|
RDFFilesLoader.loadFirstTimeFiles(ctx, "applicationMetadata", applicationMetadataModel, true);
|
||||||
setPortalUriOnFirstTime(applicationMetadataModel, ctx);
|
setPortalUriOnFirstTime(applicationMetadataModel, ctx);
|
||||||
} else {
|
} else {
|
||||||
checkForNamespaceMismatch( applicationMetadataModel, ctx );
|
checkForNamespaceMismatch( applicationMetadataModel, ctx );
|
||||||
|
|
|
@ -3,11 +3,7 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
||||||
|
|
||||||
import java.beans.PropertyVetoException;
|
import java.beans.PropertyVetoException;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
@ -69,16 +65,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
|
||||||
protected final static boolean DEFAULT_TESTONBORROW = true,
|
protected final static boolean DEFAULT_TESTONBORROW = true,
|
||||||
DEFAULT_TESTONRETURN = true, DEFAULT_TESTWHILEIDLE = true;
|
DEFAULT_TESTONRETURN = true, DEFAULT_TESTWHILEIDLE = true;
|
||||||
|
|
||||||
protected static String BASE = "/WEB-INF/ontologies/";
|
|
||||||
public static String APPPATH = BASE+"app/";
|
|
||||||
//these files are loaded everytime the system starts up
|
|
||||||
public static String APPPATH_LOAD = APPPATH + "menuload/";
|
|
||||||
|
|
||||||
//All files in this directory will be reloaded every startup
|
|
||||||
//and attached as sub-models to the displayOntModel.
|
|
||||||
static final String DISPLAY_MODEL_LOAD_AT_STARTUP_DIR =
|
|
||||||
APPPATH + "loadedAtStartup";
|
|
||||||
|
|
||||||
protected static boolean firstStartup = false;
|
protected static boolean firstStartup = false;
|
||||||
|
|
||||||
String DB_USER = "jenatest"; // database user id
|
String DB_USER = "jenatest"; // database user id
|
||||||
|
@ -390,47 +376,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
|
||||||
return dbModel;
|
return dbModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO get rid of this?
|
|
||||||
public static void readOntologyFilesInPathSet(String path,
|
|
||||||
ServletContext ctx, Model model) {
|
|
||||||
log.debug("Reading ontology files from '" + path + "'");
|
|
||||||
Set<String> paths = ctx.getResourcePaths(path);
|
|
||||||
if (paths != null) {
|
|
||||||
for (String p : paths) {
|
|
||||||
readOntologyFileFromPath(p, model, ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO get rid of this?
|
|
||||||
public static void readOntologyFileFromPath(String p,
|
|
||||||
Model model,
|
|
||||||
ServletContext ctx) {
|
|
||||||
//Check that this is a file and not a directory
|
|
||||||
File f = new File(ctx.getRealPath(p));
|
|
||||||
if(f.exists() && f.isFile()){
|
|
||||||
String format = getRdfFormat(p);
|
|
||||||
log.info("Loading ontology file at " + p +
|
|
||||||
" as format " + format);
|
|
||||||
InputStream ontologyInputStream = ctx.getResourceAsStream(p);
|
|
||||||
try {
|
|
||||||
model.read(ontologyInputStream, null, format);
|
|
||||||
log.debug("...successful");
|
|
||||||
} catch (Throwable t) {
|
|
||||||
log.error("Failed to load ontology file at '" + p +
|
|
||||||
"' as format " + format, t);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(!f.exists()) {
|
|
||||||
log.info("File for path " + p + " does not exist");
|
|
||||||
}
|
|
||||||
else if(f.isDirectory()) {
|
|
||||||
log.info("Path " + p +
|
|
||||||
" corresponds to directory and not file so was not read in");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getRdfFormat(String filename){
|
private static String getRdfFormat(String filename){
|
||||||
String defaultformat = "RDF/XML";
|
String defaultformat = "RDF/XML";
|
||||||
if( filename == null )
|
if( filename == null )
|
||||||
|
@ -486,42 +431,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Read all the files in the directory as RDF files
|
|
||||||
* and return a model build from all RDF data found in those files.
|
|
||||||
* This will attempt to load formats supported by getRdfFormat().
|
|
||||||
*/
|
|
||||||
public static OntModel getModelFromDir( File dir){
|
|
||||||
if( dir == null )
|
|
||||||
throw new IllegalStateException("Must pass a File to getModelFromDir()");
|
|
||||||
if( !dir.isDirectory() )
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"Directory must be a File object for a directory");
|
|
||||||
if( !dir.canRead() )
|
|
||||||
throw new IllegalStateException("getModelFromDir(): Directory " +
|
|
||||||
" must be readable, check premissions on "
|
|
||||||
+ dir.getAbsolutePath());
|
|
||||||
|
|
||||||
OntModel model = ModelFactory.createOntologyModel();
|
|
||||||
for( File file : dir.listFiles()){
|
|
||||||
if( file.isFile()
|
|
||||||
&& file.canRead()
|
|
||||||
&& file.getName() != null ){
|
|
||||||
String format = getRdfFormat( file.getName() );
|
|
||||||
try{
|
|
||||||
model.read( new FileInputStream(file), null, format);
|
|
||||||
log.info("read in file " + file.getCanonicalPath() );
|
|
||||||
}catch( Throwable th){
|
|
||||||
log.warn("Could not load file " +
|
|
||||||
file.getAbsolutePath() + file.separator + file.getName() +
|
|
||||||
" check that it contains valid " + format + " data.",
|
|
||||||
th);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void setVitroJenaModelMaker(VitroJenaModelMaker vjmm,
|
public static void setVitroJenaModelMaker(VitroJenaModelMaker vjmm,
|
||||||
ServletContext ctx){
|
ServletContext ctx){
|
||||||
ctx.setAttribute(rdbModelMaker, vjmm);
|
ctx.setAttribute(rdbModelMaker, vjmm);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -17,7 +18,10 @@ import javax.servlet.ServletContext;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
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.Model;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ -30,8 +34,9 @@ public class RDFFilesLoader {
|
||||||
private static final String PROPERTY_VITRO_HOME = "vitro.home";
|
private static final String PROPERTY_VITRO_HOME = "vitro.home";
|
||||||
private static final String DEFAULT_RDF_FORMAT = "RDF/XML";
|
private static final String DEFAULT_RDF_FORMAT = "RDF/XML";
|
||||||
private static final String FIRST_TIME = "firsttime";
|
private static final String FIRST_TIME = "firsttime";
|
||||||
|
private static final String EVERY_TIME = "everytime";
|
||||||
|
|
||||||
/** Directory filter that ignores sub-directories and hidden files. */
|
/** Path filter that ignores sub-directories and hidden files. */
|
||||||
private static final DirectoryStream.Filter<Path> RDF_FILE_FILTER = new DirectoryStream.Filter<Path>() {
|
private static final DirectoryStream.Filter<Path> RDF_FILE_FILTER = new DirectoryStream.Filter<Path>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(Path p) throws IOException {
|
public boolean accept(Path p) throws IOException {
|
||||||
|
@ -47,33 +52,77 @@ public class RDFFilesLoader {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final String homeDirProperty;
|
|
||||||
|
|
||||||
public RDFFilesLoader(ServletContext ctx) {
|
|
||||||
ConfigurationProperties props = ConfigurationProperties.getBean(ctx);
|
|
||||||
this.homeDirProperty = props.getProperty(PROPERTY_VITRO_HOME);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load the "first time" files if the model is empty.
|
|
||||||
*/
|
|
||||||
public void loadFirstTimeFiles(String modelPath, Model model) {
|
|
||||||
loadFirstTimeFiles(modelPath, model, model.isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the "first time" files if we say it is the first time.
|
* Load the "first time" files if we say it is the first time.
|
||||||
|
*
|
||||||
|
* The location is based on the home directory and the model path: "abox",
|
||||||
|
* "display", etc.
|
||||||
|
*
|
||||||
|
* The files from the directory are added to the model.
|
||||||
*/
|
*/
|
||||||
public void loadFirstTimeFiles(String modelPath, Model model, boolean firstTime) {
|
public static void loadFirstTimeFiles(ServletContext ctx, String modelPath,
|
||||||
|
Model model, boolean firstTime) {
|
||||||
if (firstTime) {
|
if (firstTime) {
|
||||||
Set<Path> paths = getPaths(homeDirProperty, modelPath, FIRST_TIME);
|
Set<Path> paths = getPaths(locateHomeDirectory(ctx), modelPath,
|
||||||
|
FIRST_TIME);
|
||||||
for (Path p : paths) {
|
for (Path p : paths) {
|
||||||
readOntologyFileIntoModel(p, model);
|
readOntologyFileIntoModel(p, model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Path> getPaths(String parentDir, String... strings) {
|
/**
|
||||||
|
* Load the "every time" files.
|
||||||
|
*
|
||||||
|
* The location is based on the home directory and the model path: "abox",
|
||||||
|
* "display", etc.
|
||||||
|
*
|
||||||
|
* The files from the directory become a sub-model of the model.
|
||||||
|
*/
|
||||||
|
public static void loadEveryTimeFiles(ServletContext ctx, String modelPath,
|
||||||
|
OntModel model) {
|
||||||
|
OntModel everytimeModel = ModelFactory
|
||||||
|
.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||||
|
Set<Path> paths = getPaths(locateHomeDirectory(ctx), modelPath,
|
||||||
|
EVERY_TIME);
|
||||||
|
for (Path p : paths) {
|
||||||
|
readOntologyFileIntoModel(p, everytimeModel);
|
||||||
|
}
|
||||||
|
model.addSubModel(everytimeModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a model from all the RDF files in the specified directory.
|
||||||
|
*/
|
||||||
|
public static OntModel getModelFromDir(File dir) {
|
||||||
|
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
|
||||||
|
if (dir == null) {
|
||||||
|
log.warn("Must pass a File to getModelFromDir()");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
if (!dir.isDirectory()) {
|
||||||
|
log.warn("Directory must be a File object for a directory");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
if (!dir.canRead()) {
|
||||||
|
log.warn("getModelFromDir(): Directory "
|
||||||
|
+ " must be readable, check permissions on "
|
||||||
|
+ dir.getAbsolutePath());
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Path> paths = getPaths(dir.getPath());
|
||||||
|
for (Path p : paths) {
|
||||||
|
readOntologyFileIntoModel(p, model);
|
||||||
|
}
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the paths to RDF files in this directory. Sub-directories and hidden
|
||||||
|
* files are ignored.
|
||||||
|
*/
|
||||||
|
private static Set<Path> getPaths(String parentDir, String... strings) {
|
||||||
Path dir = Paths.get(parentDir, strings);
|
Path dir = Paths.get(parentDir, strings);
|
||||||
|
|
||||||
Set<Path> paths = new TreeSet<>();
|
Set<Path> paths = new TreeSet<>();
|
||||||
|
@ -87,7 +136,7 @@ public class RDFFilesLoader {
|
||||||
log.warn("Failed to read directory '" + dir + "'", e);
|
log.warn("Failed to read directory '" + dir + "'", e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.debug("Filegraph directory '" + dir + "' doesn't exist.");
|
log.debug("Directory '" + dir + "' doesn't exist.");
|
||||||
}
|
}
|
||||||
log.debug("Paths from '" + dir + "': " + paths);
|
log.debug("Paths from '" + dir + "': " + paths);
|
||||||
return paths;
|
return paths;
|
||||||
|
@ -99,9 +148,9 @@ public class RDFFilesLoader {
|
||||||
try (InputStream stream = new FileInputStream(p.toFile())) {
|
try (InputStream stream = new FileInputStream(p.toFile())) {
|
||||||
model.read(stream, null, format);
|
model.read(stream, null, format);
|
||||||
log.debug("...successful");
|
log.debug("...successful");
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to load ontology file at '" + p + "' as format "
|
log.warn("Could not load RDF file '" + p
|
||||||
+ format, e);
|
+ "'. Check that it contains valid " + format + " data.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,4 +164,16 @@ public class RDFFilesLoader {
|
||||||
return DEFAULT_RDF_FORMAT;
|
return DEFAULT_RDF_FORMAT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String locateHomeDirectory(ServletContext ctx) {
|
||||||
|
return ConfigurationProperties.getBean(ctx).getProperty(
|
||||||
|
PROPERTY_VITRO_HOME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No need to create an instance -- all methods are static.
|
||||||
|
*/
|
||||||
|
private RDFFilesLoader() {
|
||||||
|
// Nothing to initialize.
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,15 +17,14 @@ import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer;
|
||||||
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
|
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the user account model. If it does not exist in the
|
* Setup the user account model. If it does not exist in the database, create
|
||||||
* database, create and populate it.
|
* and populate it.
|
||||||
*/
|
*/
|
||||||
public class UserModelSetup extends JenaDataSourceSetupBase
|
public class UserModelSetup extends JenaDataSourceSetupBase implements
|
||||||
implements ServletContextListener {
|
ServletContextListener {
|
||||||
private static final Log log = LogFactory.getLog(
|
private static final Log log = LogFactory.getLog(UserModelSetup.class
|
||||||
UserModelSetup.class.getName());
|
.getName());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextInitialized(ServletContextEvent sce) {
|
public void contextInitialized(ServletContextEvent sce) {
|
||||||
|
@ -34,9 +33,11 @@ public class UserModelSetup extends JenaDataSourceSetupBase
|
||||||
|
|
||||||
DataSource bds = getApplicationDataSource(ctx);
|
DataSource bds = getApplicationDataSource(ctx);
|
||||||
if (bds == null) {
|
if (bds == null) {
|
||||||
ss.fatal(this, "A DataSource must be setup before ModelSetup "+
|
ss.fatal(
|
||||||
"is run. Make sure that JenaPersistentDataSourceSetup runs before "+
|
this,
|
||||||
"ModelSetup.");
|
"A DataSource must be setup before ModelSetup "
|
||||||
|
+ "is run. Make sure that JenaPersistentDataSourceSetup runs before "
|
||||||
|
+ "ModelSetup.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,15 +49,17 @@ public class UserModelSetup extends JenaDataSourceSetupBase
|
||||||
// Does nothing.
|
// Does nothing.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupUserAccountModel (DataSource bds, ServletContext ctx ,StartupStatus ss){
|
private void setupUserAccountModel(DataSource bds, ServletContext ctx,
|
||||||
|
StartupStatus ss) {
|
||||||
try {
|
try {
|
||||||
Model userAccountsDbModel = makeDBModel(bds,
|
Model userAccountsDbModel = makeDBModel(bds,
|
||||||
JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC, ctx);
|
||||||
OntModel userAccountsModel =
|
OntModel userAccountsModel = ModelFactory
|
||||||
ModelFactory.createOntologyModel( MEM_ONT_MODEL_SPEC);
|
.createOntologyModel(MEM_ONT_MODEL_SPEC);
|
||||||
|
|
||||||
// This is used in Selenium testing, when we load user accounts from a file.
|
// This is used in Selenium testing, to load accounts from a file.
|
||||||
new RDFFilesLoader(ctx).loadFirstTimeFiles("auth", userAccountsDbModel);
|
RDFFilesLoader.loadFirstTimeFiles(ctx, "auth", userAccountsDbModel,
|
||||||
|
userAccountsDbModel.isEmpty());
|
||||||
|
|
||||||
userAccountsModel.add(userAccountsDbModel);
|
userAccountsModel.add(userAccountsDbModel);
|
||||||
userAccountsModel.getBaseModel().register(
|
userAccountsModel.getBaseModel().register(
|
||||||
|
|
Loading…
Add table
Reference in a new issue