diff --git a/webapp/web/WEB-INF/ontologies/app/loadedAtStartup/displayModelListViews.rdf b/webapp/rdffiles/display/everytime/displayModelListViews.rdf similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/loadedAtStartup/displayModelListViews.rdf rename to webapp/rdffiles/display/everytime/displayModelListViews.rdf diff --git a/webapp/web/WEB-INF/ontologies/app/application.owl b/webapp/rdffiles/display/firsttime/application.owl similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/application.owl rename to webapp/rdffiles/display/firsttime/application.owl diff --git a/webapp/web/WEB-INF/ontologies/app/menu.n3 b/webapp/rdffiles/display/firsttime/menu.n3 similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/menu.n3 rename to webapp/rdffiles/display/firsttime/menu.n3 diff --git a/webapp/web/WEB-INF/ontologies/app/profilePageType.n3 b/webapp/rdffiles/display/firsttime/profilePageType.n3 similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/profilePageType.n3 rename to webapp/rdffiles/display/firsttime/profilePageType.n3 diff --git a/webapp/web/WEB-INF/ontologies/app/menuload/displayDisplay.n3 b/webapp/rdffiles/displayDisplay/everytime/displayDisplay.n3 similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/menuload/displayDisplay.n3 rename to webapp/rdffiles/displayDisplay/everytime/displayDisplay.n3 diff --git a/webapp/web/WEB-INF/ontologies/app/menuload/displayTBOX.n3 b/webapp/rdffiles/displayTbox/everytime/displayTBOX.n3 similarity index 100% rename from webapp/web/WEB-INF/ontologies/app/menuload/displayTBOX.n3 rename to webapp/rdffiles/displayTbox/everytime/displayTBOX.n3 diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java index 45549f9f7..3f7502a64 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/beans/FileBasedProhibitedFromSearch.java @@ -3,12 +3,10 @@ package edu.cornell.mannlib.vitro.webapp.search.beans; import java.io.File; -import java.io.FileInputStream; 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 { @@ -19,7 +17,7 @@ public class FileBasedProhibitedFromSearch extends ProhibitedFromSearch { * @param dir to find N3 files in. */ public FileBasedProhibitedFromSearch(String uri, File dir){ - super( uri, JenaDataSourceSetupBase.getModelFromDir(dir)); + super( uri, RDFFilesLoader.getModelFromDir(dir)); } public FileBasedProhibitedFromSearch(String URI, OntModel model) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ApplicationModelSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ApplicationModelSetup.java index 366cbc5d6..7bb26c752 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ApplicationModelSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ApplicationModelSetup.java @@ -1,9 +1,6 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ package edu.cornell.mannlib.vitro.webapp.servlet.setup; -import java.io.File; -import java.io.FileOutputStream; - import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -11,14 +8,8 @@ import javax.sql.DataSource; import org.apache.commons.logging.Log; 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.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.ModelFactory; @@ -58,16 +49,15 @@ implements ServletContextListener { try { Model displayDbModel = makeDBModel(bds, JENA_DISPLAY_METADATA_MODEL, DB_ONT_MODEL_SPEC, ctx); - if (displayDbModel.size() == 0) { - readOntologyFilesInPathSet(APPPATH, ctx,displayDbModel); - } + RDFFilesLoader.loadFirstTimeFiles(ctx, "display", displayDbModel, displayDbModel.isEmpty()); + OntModel displayModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); displayModel.add(displayDbModel); displayModel.getBaseModel().register(new ModelSynchronizer(displayDbModel)); ModelAccess.on(ctx).setDisplayModel(displayModel); - //at each startup load all RDF files from directory to sub-models of display model - initializeDisplayLoadedAtStartup(ctx, displayModel); + //at each startup load all RDF files from directory to sub-models of display model + RDFFilesLoader.loadEveryTimeFiles(ctx, "display", displayModel); } catch (Throwable t) { log.error("Unable to load user application configuration model", t); ss.fatal(this, "Unable to load user application configuration model", t); @@ -78,158 +68,35 @@ implements ServletContextListener { Model displayTboxModel = makeDBModel(bds, JENA_DISPLAY_TBOX_MODEL, DB_ONT_MODEL_SPEC, ctx); - //Reading in single file every time, needs to be cleared/removed every time - readOntologyFileFromPath(APPPATH_LOAD + "displayTBOX.n3", displayTboxModel, ctx); - OntModel appTBOXModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); + OntModel appTBOXModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); appTBOXModel.add(displayTboxModel); appTBOXModel.getBaseModel().register(new ModelSynchronizer(displayTboxModel)); 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) { log.error("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 { Model displayDisplayModel = makeDBModel(bds, JENA_DISPLAY_DISPLAY_MODEL, DB_ONT_MODEL_SPEC, ctx); - //Reading in single file every time, needs to be cleared/removed every - readOntologyFileFromPath(APPPATH_LOAD + "displayDisplay.n3", displayDisplayModel, ctx); - OntModel appDisplayDisplayModel = ModelFactory.createOntologyModel( - MEM_ONT_MODEL_SPEC); + OntModel appDisplayDisplayModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC); appDisplayDisplayModel.add(displayDisplayModel); appDisplayDisplayModel.getBaseModel().register(new ModelSynchronizer(displayDisplayModel)); 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) { 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); } } - - /** - * 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: \n" + - "CONSTRUCT { \n" + - " ?a d:listViewConfigFile ?b . \n" + - "} WHERE {\n" + - " ?a d:listViewConfigFile ?b . \n" + - "} "; @Override public void contextDestroyed(ServletContextEvent arg0) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java index ab983464f..02ec7bf8d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/ContentModelSetup.java @@ -82,11 +82,10 @@ public class ContentModelSetup extends JenaDataSourceSetupBase if (isFirstStartup()) { - RDFFilesLoader loader = new RDFFilesLoader(ctx); - loader.loadFirstTimeFiles("abox", baseABoxModel, true); - loader.loadFirstTimeFiles("tbox", baseTBoxModel, true); + RDFFilesLoader.loadFirstTimeFiles(ctx, "abox", baseABoxModel, true); + RDFFilesLoader.loadFirstTimeFiles(ctx, "tbox", baseTBoxModel, true); - loader.loadFirstTimeFiles("applicationMetadata", applicationMetadataModel, true); + RDFFilesLoader.loadFirstTimeFiles(ctx, "applicationMetadata", applicationMetadataModel, true); setPortalUriOnFirstTime(applicationMetadataModel, ctx); } else { checkForNamespaceMismatch( applicationMetadataModel, ctx ); 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 4e89c46bf..b79b71fa2 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 @@ -3,11 +3,7 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup; import java.beans.PropertyVetoException; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; import java.sql.SQLException; -import java.util.Set; import javax.servlet.ServletContext; import javax.sql.DataSource; @@ -69,16 +65,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { protected final static boolean DEFAULT_TESTONBORROW = 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; String DB_USER = "jenatest"; // database user id @@ -390,47 +376,6 @@ public class JenaDataSourceSetupBase extends JenaBaseDaoCon { 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 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){ String defaultformat = "RDF/XML"; 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, ServletContext ctx){ ctx.setAttribute(rdbModelMaker, vjmm); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java index d0b151013..0fc145d7b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -17,7 +18,10 @@ import javax.servlet.ServletContext; 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 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 DEFAULT_RDF_FORMAT = "RDF/XML"; 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 RDF_FILE_FILTER = new DirectoryStream.Filter() { @Override 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. + * + * 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) { - Set paths = getPaths(homeDirProperty, modelPath, FIRST_TIME); + Set paths = getPaths(locateHomeDirectory(ctx), modelPath, + FIRST_TIME); for (Path p : paths) { readOntologyFileIntoModel(p, model); } } } - private Set 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 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 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 getPaths(String parentDir, String... strings) { Path dir = Paths.get(parentDir, strings); Set paths = new TreeSet<>(); @@ -87,7 +136,7 @@ public class RDFFilesLoader { log.warn("Failed to read directory '" + dir + "'", e); } } else { - log.debug("Filegraph directory '" + dir + "' doesn't exist."); + log.debug("Directory '" + dir + "' doesn't exist."); } log.debug("Paths from '" + dir + "': " + paths); return paths; @@ -99,9 +148,9 @@ public class RDFFilesLoader { try (InputStream stream = new FileInputStream(p.toFile())) { model.read(stream, null, format); log.debug("...successful"); - } catch (IOException e) { - log.error("Failed to load ontology file at '" + p + "' as format " - + format, e); + } catch (Exception e) { + log.warn("Could not load RDF file '" + p + + "'. Check that it contains valid " + format + " data.", e); } } @@ -115,4 +164,16 @@ public class RDFFilesLoader { 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. + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UserModelSetup.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UserModelSetup.java index dcae1a549..121537b76 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UserModelSetup.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UserModelSetup.java @@ -17,55 +17,58 @@ import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; - -/** - * Setup the user account model. If it does not exist in the - * database, create and populate it. +/** + * Setup the user account model. If it does not exist in the database, create + * and populate it. */ -public class UserModelSetup extends JenaDataSourceSetupBase - implements ServletContextListener { - private static final Log log = LogFactory.getLog( - UserModelSetup.class.getName()); +public class UserModelSetup extends JenaDataSourceSetupBase implements + ServletContextListener { + private static final Log log = LogFactory.getLog(UserModelSetup.class + .getName()); - @Override - public void contextInitialized(ServletContextEvent sce) { - ServletContext ctx = sce.getServletContext(); - StartupStatus ss = StartupStatus.getBean(ctx); - - DataSource 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; - } - - setupUserAccountModel(bds, ctx, ss); - } - - @Override - public void contextDestroyed(ServletContextEvent arg0) { - // Does nothing. - } + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + StartupStatus ss = StartupStatus.getBean(ctx); - private void setupUserAccountModel (DataSource bds, ServletContext ctx ,StartupStatus ss){ - try { - Model userAccountsDbModel = makeDBModel(bds, - JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC, ctx); - OntModel userAccountsModel = - ModelFactory.createOntologyModel( MEM_ONT_MODEL_SPEC); - - // This is used in Selenium testing, when we load user accounts from a file. - new RDFFilesLoader(ctx).loadFirstTimeFiles("auth", userAccountsDbModel); + DataSource 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; + } - userAccountsModel.add(userAccountsDbModel); - userAccountsModel.getBaseModel().register( - new ModelSynchronizer(userAccountsDbModel)); - ModelAccess.on(ctx).setUserAccountsModel(userAccountsModel); - - } catch (Throwable t) { - log.error("Unable to load user accounts model from DB", t); - ss.fatal(this, "Unable to load user accounts model from DB", t); - } - } + setupUserAccountModel(bds, ctx, ss); + } + + @Override + public void contextDestroyed(ServletContextEvent arg0) { + // Does nothing. + } + + private void setupUserAccountModel(DataSource bds, ServletContext ctx, + StartupStatus ss) { + try { + Model userAccountsDbModel = makeDBModel(bds, + JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC, ctx); + OntModel userAccountsModel = ModelFactory + .createOntologyModel(MEM_ONT_MODEL_SPEC); + + // This is used in Selenium testing, to load accounts from a file. + RDFFilesLoader.loadFirstTimeFiles(ctx, "auth", userAccountsDbModel, + userAccountsDbModel.isEmpty()); + + userAccountsModel.add(userAccountsDbModel); + userAccountsModel.getBaseModel().register( + new ModelSynchronizer(userAccountsDbModel)); + ModelAccess.on(ctx).setUserAccountsModel(userAccountsModel); + + } catch (Throwable t) { + log.error("Unable to load user accounts model from DB", t); + ss.fatal(this, "Unable to load user accounts model from DB", t); + } + } }