Skip to content

Commit

Permalink
Allows running Dataverse on Windows (#2557)
Browse files Browse the repository at this point in the history
Allows running Dataverse on Windows.
  • Loading branch information
lbownik authored Oct 2, 2024
1 parent 2eb0ad6 commit ab89826
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,71 +1,87 @@
package edu.harvard.iq.dataverse.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import static java.lang.System.getProperty;
import static java.lang.Thread.currentThread;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.copy;
import static java.nio.file.Files.newBufferedWriter;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.apache.commons.io.IOUtils.resourceToString;
import static org.apache.commons.lang3.StringUtils.replaceOnce;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
* Initializer of jhove configuration files.
*
* Classes that uses jhove should assume that
* configuration file for jhove is located in
* {@link #JHOVE_CONFIG_PATH}.
* Classes that uses jhove should assume that configuration file for jhove is
* located in {@link #getConfigPath}.
*/
@Startup
@Singleton
public class JhoveConfigurationInitializer {

static Path getConfigPath() {

return pathOf("jhove.conf");
}

/**
* Copies jhove configuration and configuration xsd files from classpath into
* temp directory. In case of configuration file - it also replaces xsd location
* placeholder to the path in temp directory where it was copied.
*
* Replacing of schema location is done, because jhove will validate given
* configuration file against it.
*/
@PostConstruct
public void initializeJhoveConfig() {

try {
final Path xsdPath = copyXsd();
copyConfigWith(xsdPath);
} catch (final IOException e) {
throw new RuntimeException("Unable to prepare jhove configuration files", e);
}
}

private static final String JHOVE_CONFIG_CLASSPATH = "jhove/jhove.conf";
private static final String JHOVE_CONFIG_XSD_CLASSPATH = "jhove/jhoveConfig.xsd";
private Path copyXsd() throws IOException {

public static final String JHOVE_CONFIG_PATH = System.getProperty("java.io.tmpdir") + "/jhove.conf";
public static final String JHOVE_CONFIG_XSD_PATH = System.getProperty("java.io.tmpdir") + "/jhoveConfig.xsd";
final Path target = pathOf("jhoveConfig.xsd");

try(final InputStream in = getResourceAsStream("jhove/jhoveConfig.xsd")) {
copy(in, target, REPLACE_EXISTING);
}
return target;
}

private void copyConfigWith(final Path xsd) throws IOException {

String jhoveConf = resourceToString("jhove/jhove.conf", UTF_8,
currentThread().getContextClassLoader());

jhoveConf = replaceOnce(jhoveConf, "{jhove.config.xsd.path}", "file://" + xsd.toString());

try(final Writer out = newBufferedWriter(getConfigPath())) {
out.write(jhoveConf);
}
}

private static Path pathOf(final String name) {

return Paths.get(getProperty("java.io.tmpdir"), name);
}

/**
* Copies jhove configuration and configuration xsd files from
* classpath into temp directory. In case of configuration
* file - it also replaces xsd location placeholder to
* the path in temp directory where it was copied.
*
* Replacing of schema location is done, because jhove will
* validate given configuration file against it.
*/
@PostConstruct
public void initializeJhoveConfig() {
try {
Files.copy(Paths.get(getJhoveConfigXsdFile()), Paths.get(JHOVE_CONFIG_XSD_PATH), StandardCopyOption.REPLACE_EXISTING);

String jhoveConf = FileUtils.readFileToString(new File(getJhoveConfigFile()), StandardCharsets.UTF_8);
jhoveConf = StringUtils.replaceOnce(jhoveConf, "{jhove.config.xsd.path}", "file://" + JHOVE_CONFIG_XSD_PATH);
FileUtils.writeStringToFile(new File(JHOVE_CONFIG_PATH), jhoveConf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Unable to prepare jhove configuration files", e);
}
}

// -------------------- PRIVATE --------------------

private String getJhoveConfigFile() {
return Thread.currentThread()
.getContextClassLoader()
.getResource(JHOVE_CONFIG_CLASSPATH).getPath();
}

private String getJhoveConfigXsdFile() {
return Thread.currentThread()
.getContextClassLoader()
.getResource(JHOVE_CONFIG_XSD_CLASSPATH).getPath();
}
private static InputStream getResourceAsStream(final String name) {

return currentThread().getContextClassLoader().getResourceAsStream(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private App createJhoveApp() {
}

private JhoveBase createJhoveBase() throws JhoveException {
String configFile = JhoveConfigurationInitializer.JHOVE_CONFIG_PATH;
String configFile = JhoveConfigurationInitializer.getConfigPath().toString();

// create an instance of jhove engine
JhoveBase jb = new JhoveBase();
Expand Down

0 comments on commit ab89826

Please sign in to comment.