Skip to content

Commit

Permalink
Updated home directory on startup, fixed rdf file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
litvinovg committed Oct 10, 2024
1 parent 73438ab commit d25ee81
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void contextInitialized(ServletContextEvent sce) {
this.vitroHomeDir = VitroHomeDirectory.find(ctx);
ss.info(this, vitroHomeDir.getDiscoveryMessage());

this.vitroHomeDir.populate();

locateApplicationConfigFile();
loadApplicationConfigFile();
createConfigurationBeanLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

import static edu.cornell.mannlib.vitro.webapp.application.BuildProperties.WEBAPP_PATH_BUILD_PROPERTIES;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.naming.InitialContext;
import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -32,6 +38,8 @@ public static VitroHomeDirectory find(ServletContext ctx) {
private final ServletContext ctx;
private final Path path;
private final String discoveryMessage;
private Set<String> excludedHomeFiles = new HashSet<>(Arrays.asList("rdf"));


public VitroHomeDirectory(ServletContext ctx, Path path,
String discoveryMessage) {
Expand Down Expand Up @@ -210,4 +218,59 @@ public String toString() {
}
}

/**
* Populates home directory with home files, excluding the rdf directory
*/
public void populate() {
File vhdDir = getPath().toFile();

if (!vhdDir.isDirectory() || vhdDir.list() == null) {
throw new RuntimeException("Application home dir is not a directory! " + vhdDir);
}

if (!vhdDir.canWrite()) {
throw new RuntimeException("Application home dir is not writable! " + vhdDir);
}
try {
copy(vhdDir);
} catch(Exception e) {
log.error(e, e);
throw new RuntimeException("Failed to copy home files! " + vhdDir);
}
log.info("Copied home files to " + vhdDir.toPath());

}

private void copy(File homeDestination) throws IOException {
File homeSrcPath = new File(getHomeSrcPath(ctx));
File[] contents = homeSrcPath.listFiles();
for (File child : contents) {
if (!isExcluded(child)) {
FileUtils.copyDirectory(child, homeDestination);
}
}
}

private boolean isExcluded(File child) {
if (excludedHomeFiles.contains(child.getName())) {
return true;
}
return false;
}

/**
* Get source home files.
*
* @return source home files directory path
*/
public static String getHomeSrcPath(ServletContext context) {
String location = "/WEB-INF/resources/home-files";
String realPath = context.getRealPath(location);
if (realPath == null) {
log.error("Application home files not found in: " + location);
throw new RuntimeException("Application home files not found in: " + location);
}
return realPath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.application.VitroHomeDirectory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDFServiceDataset;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames;
Expand Down Expand Up @@ -121,8 +121,8 @@ public void contextInitialized(ServletContextEvent sce) {
private Set<Path> getFilegraphPaths(ServletContext ctx, String... strings) {
StartupStatus ss = StartupStatus.getBean(ctx);

String homeDirProperty = ApplicationUtils.instance().getHomeDirectory().getPath().toString();
Path filegraphDir = Paths.get(homeDirProperty, strings);
String homeDirPath = VitroHomeDirectory.getHomeSrcPath(ctx);
Path filegraphDir = Paths.get(homeDirPath, strings);

Set<Path> paths = new TreeSet<>();
if (Files.isDirectory(filegraphDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;

import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
import edu.cornell.mannlib.vitro.webapp.application.VitroHomeDirectory;

import javax.servlet.ServletContext;

Expand Down Expand Up @@ -81,7 +81,7 @@ public boolean accept(Path p) throws IOException {
public static void loadFirstTimeFiles(ServletContext ctx, String modelPath, Model model,
boolean firstTime) {
if (firstTime) {
String home = locateHomeDirectory();
String home = locateHomeDirectory(ctx);

// Load common files
Set<Path> paths = getPaths(home, RDF, modelPath, FIRST_TIME);
Expand Down Expand Up @@ -113,7 +113,7 @@ public static void loadFirstTimeFiles(ServletContext ctx, String modelPath, Mode
public static void loadEveryTimeFiles(ServletContext ctx, String modelPath, OntModel model) {
OntModel everytimeModel = ModelFactory
.createOntologyModel(OntModelSpec.OWL_MEM);
String home = locateHomeDirectory();
String home = locateHomeDirectory(ctx);

// Load common files
Set<Path> paths = getPaths(home, RDF, modelPath, EVERY_TIME);
Expand Down Expand Up @@ -220,9 +220,8 @@ else if (filename.endsWith("ttl"))
return DEFAULT_RDF_FORMAT;
}

private static String locateHomeDirectory() {
return ApplicationUtils.instance().getHomeDirectory().getPath()
.toString();
private static String locateHomeDirectory(ServletContext ctx) {
return VitroHomeDirectory.getHomeSrcPath(ctx);
}

/**
Expand Down

0 comments on commit d25ee81

Please sign in to comment.