Skip to content

Commit

Permalink
Refactored code, fixed UpdateKnowledgeBase and FauxPropertiesUpdater.…
Browse files Browse the repository at this point in the history
… Added tests.
  • Loading branch information
litvinovg committed Dec 13, 2024
1 parent 54577a4 commit e441cb8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public static VitroHomeDirectory find(ServletContext ctx) {
private final Path path;
private final String discoveryMessage;
private Set<String> excludedHomeFiles = new HashSet<>(Arrays.asList("rdf"));
private String homeSourcePath;


public VitroHomeDirectory(ServletContext ctx, Path path,
String discoveryMessage) {
this.ctx = ctx;
this.path = path;
this.discoveryMessage = discoveryMessage;
setHomeSourcePath(ctx);
}

public ServletContext getCtx() {
Expand All @@ -60,6 +62,23 @@ public String getDiscoveryMessage() {
return discoveryMessage;
}

/**
* Get source home file directory path
*
* @return source home files directory path
*/
public String getSourcePath() {
return homeSourcePath;
}

private void setHomeSourcePath(ServletContext context) {
String location = "/WEB-INF/resources/home-files";
homeSourcePath = context.getRealPath(location);
if (homeSourcePath == null) {
throw new RuntimeException(String.format("Application home files not found in: %s", location));
}
}

/**
* Find something that specifies the location of the Vitro home directory.
* Look in the JDNI environment, the system properties, and the
Expand Down Expand Up @@ -221,75 +240,47 @@ public String toString() {
* Populates home directory with home files, excluding the rdf directory
*/
public void populate() {
File vhdDir = getPath().toFile();
File homeDestination = getPath().toFile();

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

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

log.info("Copied home files to " + homeDestination.toPath());
}

/**
* Create home directory
*/
private static void createHomeDirectory(Path vhdDir) {
private static void createHomeDirectory(Path homeDestination) {
try {
vhdDir.toFile().mkdirs();
homeDestination.toFile().mkdirs();
} catch (Exception e) {
log.error(e, e);
throw new RuntimeException("Failed to create home directory " + vhdDir);
throw new RuntimeException("Failed to create home directory " + homeDestination, e);
}
}

/**
* Copy file from home source to home destination
*/
private void copy(File homeDestination) throws IOException {
File homeSrcPath = new File(getHomeSrcPath(ctx));
File homeSrcPath = new File(getSourcePath());
File[] contents = homeSrcPath.listFiles();
for (File child : contents) {
if (!isExcluded(child)) {
FileUtils.copyDirectory(child, homeDestination);
if (!excludedHomeFiles.contains(child.getName())) {
if (child.isDirectory()) {
FileUtils.copyDirectoryToDirectory(child, homeDestination);
} else {
FileUtils.copyFileToDirectory(child, homeDestination);
}
}
}
}

/**
* Test if file name is excluded from copying
*
* @return true if file should be excluded
*/
private boolean isExcluded(File child) {
if (excludedHomeFiles.contains(child.getName())) {
return true;
}
return false;
}

/**
* Get source home file directory path
*
* @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 @@ -78,8 +78,7 @@ private boolean isAlreadyUpdated() {
}

private boolean locateFile() {
String homePath = ApplicationUtils.instance().getHomeDirectory()
.getPath().toString();
String homePath = ApplicationUtils.instance().getHomeDirectory().getSourcePath();
propertyConfigPath = Paths.get(homePath, PATH_TO_PROPERTY_CONFIG);
if (Files.exists(propertyConfigPath)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
import org.apache.jena.query.Dataset;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

import edu.cornell.mannlib.vitro.webapp.application.VitroHomeDirectory;
import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils;
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,7 +120,7 @@ public void contextInitialized(ServletContextEvent sce) {
private Set<Path> getFilegraphPaths(ServletContext ctx, String... strings) {
StartupStatus ss = StartupStatus.getBean(ctx);

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

Set<Path> paths = new TreeSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;

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

import javax.servlet.ServletContext;

Expand Down Expand Up @@ -221,7 +220,7 @@ else if (filename.endsWith("ttl"))
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void contextInitialized(ServletContextEvent sce) {
settings.setInferenceOntModelSelector(ModelAccess.on(ctx).getOntModelSelector(INFERENCES_ONLY));
settings.setUnionOntModelSelector(ModelAccess.on(ctx).getOntModelSelector());

Path homeDir = ApplicationUtils.instance().getHomeDirectory().getPath();
Path homeDir = Paths.get(ApplicationUtils.instance().getHomeDirectory().getSourcePath());
settings.setDisplayModel(ModelAccess.on(ctx).getOntModel(DISPLAY));
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(oldTBoxModelDir()));
settings.setOldTBoxModel(oldTBoxModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package edu.cornell.mannlib.vitro.webapp.application;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import stubs.javax.servlet.ServletContextStub;

public class VitroHomeDirectoryTest {

private static final String FILE = "file";
private static final String CONFIG = "config";
private static final String RDF = "rdf";
@Rule
public TemporaryFolder src = new TemporaryFolder();
@Rule
public TemporaryFolder dst = new TemporaryFolder();

@Test
public void testGetHomeSrcPath() {
ServletContextStub sc = new ServletContextStub();
String expectedPath = "/opt/tomcat/webapp/app/WEB-INF/resources/home-files";
sc.setRealPath("/WEB-INF/resources/home-files", expectedPath);
VitroHomeDirectory vhd = new VitroHomeDirectory(sc, dst.getRoot().toPath(), "");
String realPath = vhd.getSourcePath();
assertEquals(expectedPath, realPath);
}

@Test
public void testPopulate() throws Exception {
ServletContextStub sc = new ServletContextStub();
src.newFolder(RDF);
src.newFile(FILE);
src.newFolder(CONFIG);
sc.setRealPath("/WEB-INF/resources/home-files", src.getRoot().getAbsolutePath());
VitroHomeDirectory vhd = new VitroHomeDirectory(sc, dst.getRoot().toPath(), "");
vhd.populate();
Set<String> files = new HashSet<String>(Arrays.asList(dst.getRoot().list()));
assertTrue(files.contains(CONFIG));
assertTrue(files.contains(FILE));
assertFalse(files.contains(RDF));
}
}

0 comments on commit e441cb8

Please sign in to comment.