forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows running Dataverse on Windows (#2557)
Allows running Dataverse on Windows.
- Loading branch information
Showing
2 changed files
with
70 additions
and
54 deletions.
There are no files selected for viewing
122 changes: 69 additions & 53 deletions
122
...rse-webapp/src/main/java/edu/harvard/iq/dataverse/util/JhoveConfigurationInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters