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..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 @@ -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) { @@ -168,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 @@ -210,4 +217,79 @@ 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()); + + } + + /** + * 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 + */ + 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); } /** 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 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/ +