From e9f2b152bc7c5b44189f6ea7fbfbc527427f2063 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 9 Oct 2024 17:00:31 +0200 Subject: [PATCH 1/7] removed home directory modifications by maven --- installer/home/pom.xml | 87 ++++------------------- installer/home/src/main/assembly/home.xml | 2 +- 2 files changed, 14 insertions(+), 75 deletions(-) diff --git a/installer/home/pom.xml b/installer/home/pom.xml index 17ccc4bb47..aeab1cd506 100644 --- a/installer/home/pom.xml +++ b/installer/home/pom.xml @@ -2,101 +2,40 @@ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 - org.vivoweb vitro-installer-home 1.15.1-SNAPSHOT pom - org.vivoweb vitro-installer 1.15.1-SNAPSHOT .. - Vitro Install Home - vitro - - - install - - vitro-dir - - - - - maven-assembly-plugin - - - src/main/assembly/home.xml - - false - - - - package - - single - - - - - - maven-antrun-plugin - - - remove-webapp - verify - - run - - - - - - - - - - - maven-resources-plugin - - - install - install - - copy-resources - - - ${vitro-dir} - - - ${project.build.directory}/${project.build.finalName} - - - - - - - - - - - - maven-install-plugin + maven-assembly-plugin - true + + src/main/assembly/home.xml + + false + + + package + + single + + + diff --git a/installer/home/src/main/assembly/home.xml b/installer/home/src/main/assembly/home.xml index 6de97e21d7..755b02745d 100644 --- a/installer/home/src/main/assembly/home.xml +++ b/installer/home/src/main/assembly/home.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd"> home - dir + tar false From 44a14607c69510bbaaaec41b5d40f0f884e2be15 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Wed, 9 Oct 2024 16:55:35 +0200 Subject: [PATCH 2/7] fixed war file deployment included vitro home into installer webapp --- installer/webapp/pom.xml | 74 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/installer/webapp/pom.xml b/installer/webapp/pom.xml index bac4187eec..1f400f639e 100644 --- a/installer/webapp/pom.xml +++ b/installer/webapp/pom.xml @@ -28,6 +28,34 @@ ${app-name} + + org.apache.maven.plugins + maven-dependency-plugin + + false + + + + include-home + process-resources + + unpack + + + + + org.vivoweb + vitro-installer-home + ${project.version} + tar + + target/${app-name}/WEB-INF/resources/home-files + + + + + + org.apache.maven.plugins maven-war-plugin @@ -57,53 +85,19 @@ - install + deploy tomcat-dir - - maven-antrun-plugin - - - remove-webapp - verify - - run - - - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - install - install - - unpack - - - - - ${project.groupId} - ${project.artifactId} - ${project.version} - war - true - ${tomcat-dir}/webapps/${project.build.finalName} - - - - - + maven-war-plugin + 3.2.1 + + ${tomcat-dir}/webapps/ + From e98711b32e741cf26fc80ce24d115835092c408c Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Thu, 10 Oct 2024 13:04:24 +0200 Subject: [PATCH 3/7] Updated home directory on startup, fixed rdf file loading --- .../webapp/application/ApplicationSetup.java | 2 + .../application/VitroHomeDirectory.java | 71 +++++++++++++++++++ .../webapp/servlet/setup/FileGraphSetup.java | 6 +- .../webapp/servlet/setup/RDFFilesLoader.java | 11 ++- 4 files changed, 81 insertions(+), 9 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/ApplicationSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/ApplicationSetup.java index a8fd8aa2fd..267ec77472 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/ApplicationSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/ApplicationSetup.java @@ -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(); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java index b55f9d87ed..49271f97f2 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java @@ -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; @@ -32,6 +38,8 @@ public static VitroHomeDirectory find(ServletContext ctx) { private final ServletContext ctx; private final Path path; private final String discoveryMessage; + private Set excludedHomeFiles = new HashSet<>(Arrays.asList("rdf")); + public VitroHomeDirectory(ServletContext ctx, Path path, String discoveryMessage) { @@ -210,4 +218,67 @@ 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()); + + } + + /** + * Copy file from home source to home destination + */ + 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); + } + } + } + + /** + * 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; + } + } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java index 80656403b9..eebac10b3b 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java @@ -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; @@ -121,8 +121,8 @@ public void contextInitialized(ServletContextEvent sce) { private Set 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 paths = new TreeSet<>(); if (Files.isDirectory(filegraphDir)) { diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java index b3791f3fb7..581ec30732 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java @@ -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; @@ -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 paths = getPaths(home, RDF, modelPath, FIRST_TIME); @@ -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 paths = getPaths(home, RDF, modelPath, EVERY_TIME); @@ -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); } /** From 54577a49605e1668a9344f80c6d697c3456641b1 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Thu, 10 Oct 2024 14:46:47 +0200 Subject: [PATCH 4/7] create home directory path if not exists --- .../webapp/application/VitroHomeDirectory.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java index 49271f97f2..c4dc636c99 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java @@ -176,8 +176,7 @@ private void confirmExactlyOneResult() { private void confirmValidDirectory() { Path vhd = getPath(); if (!Files.exists(vhd)) { - throw new IllegalStateException("Vitro home directory '" + vhd - + "' does not exist."); + createHomeDirectory(vhd); } if (!Files.isDirectory(vhd)) { throw new IllegalStateException("Vitro home directory '" + vhd @@ -241,6 +240,18 @@ public void populate() { } + /** + * Create home directory + */ + private static void createHomeDirectory(Path vhdDir) { + try { + vhdDir.toFile().mkdirs(); + } catch (Exception e) { + log.error(e, e); + throw new RuntimeException("Failed to create home directory " + vhdDir); + } + } + /** * Copy file from home source to home destination */ From e441cb883f202709cf5e452ffb2cbfa0e5f7bfae Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Fri, 13 Dec 2024 12:41:29 +0100 Subject: [PATCH 5/7] Refactored code, fixed UpdateKnowledgeBase and FauxPropertiesUpdater. Added tests. --- .../application/VitroHomeDirectory.java | 83 +++++++++---------- .../rel18/FauxPropertiesUpdater.java | 3 +- .../webapp/servlet/setup/FileGraphSetup.java | 5 +- .../webapp/servlet/setup/RDFFilesLoader.java | 5 +- .../servlet/setup/UpdateKnowledgeBase.java | 3 +- .../application/VitroHomeDirectoryTest.java | 50 +++++++++++ 6 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 api/src/test/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectoryTest.java diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java index c4dc636c99..6099f5100e 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java @@ -39,6 +39,7 @@ public static VitroHomeDirectory find(ServletContext ctx) { private final Path path; private final String discoveryMessage; private Set excludedHomeFiles = new HashSet<>(Arrays.asList("rdf")); + private String homeSourcePath; public VitroHomeDirectory(ServletContext ctx, Path path, @@ -46,6 +47,7 @@ public VitroHomeDirectory(ServletContext ctx, Path path, this.ctx = ctx; this.path = path; this.discoveryMessage = discoveryMessage; + setHomeSourcePath(ctx); } public ServletContext getCtx() { @@ -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 @@ -221,34 +240,30 @@ 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); } } @@ -256,40 +271,16 @@ private static void createHomeDirectory(Path vhdDir) { * 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; - } - } diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/migration/rel18/FauxPropertiesUpdater.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/migration/rel18/FauxPropertiesUpdater.java index fad60e9d06..28adf0b9e4 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/migration/rel18/FauxPropertiesUpdater.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/migration/rel18/FauxPropertiesUpdater.java @@ -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; diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java index eebac10b3b..41e858631b 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/FileGraphSetup.java @@ -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; @@ -121,7 +120,7 @@ public void contextInitialized(ServletContextEvent sce) { private Set 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 paths = new TreeSet<>(); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java index 581ec30732..d65f241b8b 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/RDFFilesLoader.java @@ -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; @@ -221,7 +220,7 @@ else if (filename.endsWith("ttl")) } private static String locateHomeDirectory(ServletContext ctx) { - return VitroHomeDirectory.getHomeSrcPath(ctx); + return ApplicationUtils.instance().getHomeDirectory().getSourcePath(); } /** diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java index 1bce3fbad6..b943ee48b7 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java @@ -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; @@ -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); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectoryTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectoryTest.java new file mode 100644 index 0000000000..d78071e794 --- /dev/null +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectoryTest.java @@ -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 files = new HashSet(Arrays.asList(dst.getRoot().list())); + assertTrue(files.contains(CONFIG)); + assertTrue(files.contains(FILE)); + assertFalse(files.contains(RDF)); + } +} From 3543dd5628e76cf6805278d75b71bda17255fd8a Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Tue, 17 Dec 2024 08:54:21 +0100 Subject: [PATCH 6/7] Reduced nesting, used more specific exception. --- .../application/VitroHomeDirectory.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java index 6099f5100e..d09b8f1a86 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java @@ -75,7 +75,7 @@ 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)); + throw new IllegalStateException(String.format("Application home files not found in: %s", location)); } } @@ -243,15 +243,15 @@ public void populate() { File homeDestination = getPath().toFile(); if (!homeDestination.isDirectory() || homeDestination.list() == null) { - throw new RuntimeException("Application home dir is not a directory! " + homeDestination); + throw new IllegalStateException("Application home dir is not a directory! " + homeDestination); } if (!homeDestination.canWrite()) { - throw new RuntimeException("Application home dir is not writable! " + homeDestination); + throw new IllegalStateException("Application home dir is not writable! " + homeDestination); } try { copy(homeDestination); } catch(Exception e) { - throw new RuntimeException("Failed to copy home files! " + homeDestination, e); + throw new IllegalStateException("Failed to copy home files! " + homeDestination, e); } log.info("Copied home files to " + homeDestination.toPath()); } @@ -263,7 +263,7 @@ private static void createHomeDirectory(Path homeDestination) { try { homeDestination.toFile().mkdirs(); } catch (Exception e) { - throw new RuntimeException("Failed to create home directory " + homeDestination, e); + throw new IllegalStateException("Failed to create home directory " + homeDestination, e); } } @@ -274,12 +274,13 @@ private void copy(File homeDestination) throws IOException { File homeSrcPath = new File(getSourcePath()); File[] contents = homeSrcPath.listFiles(); for (File child : contents) { - if (!excludedHomeFiles.contains(child.getName())) { - if (child.isDirectory()) { - FileUtils.copyDirectoryToDirectory(child, homeDestination); - } else { - FileUtils.copyFileToDirectory(child, homeDestination); - } + if (excludedHomeFiles.contains(child.getName())) { + continue; + } + if (child.isDirectory()) { + FileUtils.copyDirectoryToDirectory(child, homeDestination); + } else { + FileUtils.copyFileToDirectory(child, homeDestination); } } } From a1d7b2b9f34626a21dbe48bec3cad87be65d2d33 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Tue, 17 Dec 2024 08:59:03 +0100 Subject: [PATCH 7/7] Fixed idents and other code style in VitroHomeDirectory. Removed VitroHomeDirectory from checkstyle suppressions --- .../application/VitroHomeDirectory.java | 343 ++++++++---------- checkstyle-suppressions.xml | 1 - 2 files changed, 160 insertions(+), 184 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java index d09b8f1a86..be4a03c13b 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/application/VitroHomeDirectory.java @@ -27,40 +27,37 @@ * Encapsulates some of the info relating to the Vitro home directory. */ public class VitroHomeDirectory { - private static final Log log = LogFactory.getLog(VitroHomeDirectory.class); + private static final Log log = LogFactory.getLog(VitroHomeDirectory.class); - public static VitroHomeDirectory find(ServletContext ctx) { - HomeDirectoryFinder finder = new HomeDirectoryFinder(ctx); - return new VitroHomeDirectory(ctx, finder.getPath(), - finder.getMessage()); - } + public static VitroHomeDirectory find(ServletContext ctx) { + HomeDirectoryFinder finder = new HomeDirectoryFinder(ctx); + return new VitroHomeDirectory(ctx, finder.getPath(), finder.getMessage()); + } - private final ServletContext ctx; - private final Path path; - private final String discoveryMessage; + private final ServletContext ctx; + private final Path path; + private final String discoveryMessage; private Set 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 VitroHomeDirectory(ServletContext ctx, Path path, - String discoveryMessage) { - this.ctx = ctx; - this.path = path; - this.discoveryMessage = discoveryMessage; - setHomeSourcePath(ctx); - } - - public ServletContext getCtx() { - return ctx; - } + public ServletContext getCtx() { + return ctx; + } - public Path getPath() { - return path; - } + public Path getPath() { + return path; + } - public String getDiscoveryMessage() { - return discoveryMessage; - } + public String getDiscoveryMessage() { + return discoveryMessage; + } /** * Get source home file directory path @@ -79,162 +76,142 @@ private void setHomeSourcePath(ServletContext context) { } } - /** - * Find something that specifies the location of the Vitro home directory. - * Look in the JDNI environment, the system properties, and the - * build.properties file. - * - * If we don't find it, fail. If we find it more than once, use the first - * one (with a warning). If it is not an existing, readable directory, fail. - */ - private static class HomeDirectoryFinder { - /** JNDI path that defines the Vitro home directory */ - private static final String VHD_JNDI_PATH = "java:comp/env/vitro/home"; - - /** System property that defines the Vitro home directory */ - private static final String VHD_SYSTEM_PROPERTY = "vitro.home"; - - /** build.properties property that defines the Vitro home directory */ - private static final String VHD_BUILD_PROPERTY = "vitro.home"; - - private final ServletContext ctx; - private final List foundLocations = new ArrayList<>(); - - public HomeDirectoryFinder(ServletContext ctx) { - this.ctx = ctx; - - getVhdFromJndi(); - getVhdFromSystemProperties(); - getVhdFromBuildProperties(); - confirmExactlyOneResult(); - confirmValidDirectory(); - } - - public String getMessage() { - return foundLocations.get(0).getMessage(); - } - - public Path getPath() { - return foundLocations.get(0).getPath(); - } - - public void getVhdFromJndi() { - try { - String vhdPath = (String) new InitialContext() - .lookup(VHD_JNDI_PATH); - if (vhdPath == null) { - log.debug("Didn't find a JNDI value at '" + VHD_JNDI_PATH - + "'."); - } else { - log.debug("'" + VHD_JNDI_PATH + "' as specified by JNDI: " - + vhdPath); - String message = String.format( - "JNDI environment '%s' was set to '%s'", - VHD_JNDI_PATH, vhdPath); - foundLocations.add(new Found(Paths.get(vhdPath), message)); - } - } catch (Exception e) { - log.debug("JNDI lookup failed. " + e); - } - } - - private void getVhdFromSystemProperties() { - String vhdPath = System.getProperty(VHD_SYSTEM_PROPERTY); - if (vhdPath == null) { - log.debug("Didn't find a system property value at '" - + VHD_SYSTEM_PROPERTY + "'."); - } else { - log.debug("'" + VHD_SYSTEM_PROPERTY - + "' as specified by system property: " + vhdPath); - String message = String.format( - "System property '%s' was set to '%s'", - VHD_SYSTEM_PROPERTY, vhdPath); - foundLocations.add(new Found(Paths.get(vhdPath), message)); - } - } - - private void getVhdFromBuildProperties() { - try { - Map buildProps = new BuildProperties(ctx) - .getMap(); - String vhdPath = buildProps.get(VHD_BUILD_PROPERTY); - if (vhdPath == null) { - log.debug("build properties doesn't contain a value for '" - + VHD_BUILD_PROPERTY + "'."); - } else { - log.debug("'" + VHD_BUILD_PROPERTY - + "' as specified by build.properties: " + vhdPath); - String message = String.format( - "In resource '%s', '%s' was set to '%s'.", - WEBAPP_PATH_BUILD_PROPERTIES, VHD_BUILD_PROPERTY, - vhdPath); - foundLocations.add(new Found(Paths.get(vhdPath), message)); - } - } catch (Exception e) { - log.warn("Reading build properties failed. " + e); - } - } - - private void confirmExactlyOneResult() { - if (foundLocations.isEmpty()) { - String message = String.format("Can't find a value " - + "for the Vitro home directory. " - + "Looked in JNDI environment at '%s'. " - + "Looked for a system property named '%s'. " - + "Looked in 'WEB-INF/resources/build.properties' " - + "for '%s'.", VHD_JNDI_PATH, VHD_SYSTEM_PROPERTY, - VHD_BUILD_PROPERTY); - throw new IllegalStateException(message); - } else if (foundLocations.size() > 1) { - String message = "Found multiple values for the " - + "Vitro home directory: " + foundLocations; - log.warn(message); - } - } - - private void confirmValidDirectory() { - Path vhd = getPath(); - if (!Files.exists(vhd)) { - createHomeDirectory(vhd); - } - if (!Files.isDirectory(vhd)) { - throw new IllegalStateException("Vitro home directory '" + vhd - + "' is not a directory."); - } - if (!Files.isReadable(vhd)) { - throw new IllegalStateException( - "Cannot read Vitro home directory '" + vhd + "'."); - } - if (!Files.isWritable(vhd)) { - throw new IllegalStateException( - "Can't write to Vitro home directory: '" + vhd + "'."); - } - } - - /** We found it: where and how. */ - private static class Found { - private final Path path; - private final String message; - - public Found(Path path, String message) { - this.path = path; - this.message = message; - } - - public Path getPath() { - return path; - } - - public String getMessage() { - return message; - } - - @Override - public String toString() { - return "Found[path=" + path + ", message=" + message + "]"; - } - } - } + /** + * Find something that specifies the location of the Vitro home directory. Look in the JDNI environment, the system + * properties, and the build.properties file. + * + * If we don't find it, fail. If we find it more than once, use the first one (with a warning). If it is not an + * existing, readable directory, fail. + */ + private static class HomeDirectoryFinder { + /** JNDI path that defines the Vitro home directory */ + private static final String VHD_JNDI_PATH = "java:comp/env/vitro/home"; + + /** System property that defines the Vitro home directory */ + private static final String VHD_SYSTEM_PROPERTY = "vitro.home"; + + /** build.properties property that defines the Vitro home directory */ + private static final String VHD_BUILD_PROPERTY = "vitro.home"; + + private final ServletContext ctx; + private final List foundLocations = new ArrayList<>(); + + public HomeDirectoryFinder(ServletContext ctx) { + this.ctx = ctx; + + getVhdFromJndi(); + getVhdFromSystemProperties(); + getVhdFromBuildProperties(); + confirmExactlyOneResult(); + confirmValidDirectory(); + } + + public String getMessage() { + return foundLocations.get(0).getMessage(); + } + + public Path getPath() { + return foundLocations.get(0).getPath(); + } + + public void getVhdFromJndi() { + try { + String vhdPath = (String) new InitialContext().lookup(VHD_JNDI_PATH); + if (vhdPath == null) { + log.debug("Didn't find a JNDI value at '" + VHD_JNDI_PATH + "'."); + } else { + log.debug("'" + VHD_JNDI_PATH + "' as specified by JNDI: " + vhdPath); + String message = String.format("JNDI environment '%s' was set to '%s'", VHD_JNDI_PATH, vhdPath); + foundLocations.add(new Found(Paths.get(vhdPath), message)); + } + } catch (Exception e) { + log.debug("JNDI lookup failed. " + e); + } + } + + private void getVhdFromSystemProperties() { + String vhdPath = System.getProperty(VHD_SYSTEM_PROPERTY); + if (vhdPath == null) { + log.debug("Didn't find a system property value at '" + VHD_SYSTEM_PROPERTY + "'."); + } else { + log.debug("'" + VHD_SYSTEM_PROPERTY + "' as specified by system property: " + vhdPath); + String message = String.format("System property '%s' was set to '%s'", VHD_SYSTEM_PROPERTY, vhdPath); + foundLocations.add(new Found(Paths.get(vhdPath), message)); + } + } + + private void getVhdFromBuildProperties() { + try { + Map buildProps = new BuildProperties(ctx).getMap(); + String vhdPath = buildProps.get(VHD_BUILD_PROPERTY); + if (vhdPath == null) { + log.debug("build properties doesn't contain a value for '" + VHD_BUILD_PROPERTY + "'."); + } else { + log.debug("'" + VHD_BUILD_PROPERTY + "' as specified by build.properties: " + vhdPath); + String message = String.format("In resource '%s', '%s' was set to '%s'.", + WEBAPP_PATH_BUILD_PROPERTIES, VHD_BUILD_PROPERTY, vhdPath); + foundLocations.add(new Found(Paths.get(vhdPath), message)); + } + } catch (Exception e) { + log.warn("Reading build properties failed. " + e); + } + } + + private void confirmExactlyOneResult() { + if (foundLocations.isEmpty()) { + String message = String.format("Can't find a value " + + "for the Vitro home directory. " + + "Looked in JNDI environment at '%s'. " + + "Looked for a system property named '%s'. " + + "Looked in 'WEB-INF/resources/build.properties' " + + "for '%s'.", VHD_JNDI_PATH, VHD_SYSTEM_PROPERTY, VHD_BUILD_PROPERTY); + throw new IllegalStateException(message); + } else if (foundLocations.size() > 1) { + String message = "Found multiple values for the " + "Vitro home directory: " + foundLocations; + log.warn(message); + } + } + + private void confirmValidDirectory() { + Path vhd = getPath(); + if (!Files.exists(vhd)) { + createHomeDirectory(vhd); + } + if (!Files.isDirectory(vhd)) { + throw new IllegalStateException("Vitro home directory '" + vhd + "' is not a directory."); + } + if (!Files.isReadable(vhd)) { + throw new IllegalStateException("Cannot read Vitro home directory '" + vhd + "'."); + } + if (!Files.isWritable(vhd)) { + throw new IllegalStateException("Can't write to Vitro home directory: '" + vhd + "'."); + } + } + + /** We found it: where and how. */ + private static class Found { + private final Path path; + private final String message; + + public Found(Path path, String message) { + this.path = path; + this.message = message; + } + + public Path getPath() { + return path; + } + + public String getMessage() { + return message; + } + + @Override + public String toString() { + return "Found[path=" + path + ", message=" + message + "]"; + } + } + } /** * Populates home directory with home files, excluding the rdf directory @@ -250,7 +227,7 @@ public void populate() { } try { copy(homeDestination); - } catch(Exception e) { + } catch (Exception e) { throw new IllegalStateException("Failed to copy home files! " + homeDestination, e); } log.info("Copied home files to " + homeDestination.toPath()); diff --git a/checkstyle-suppressions.xml b/checkstyle-suppressions.xml index 5e4693a52b..89ca46a74d 100644 --- a/checkstyle-suppressions.xml +++ b/checkstyle-suppressions.xml @@ -48,7 +48,6 @@ -