Skip to content

Commit

Permalink
further changes per review
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmyers committed May 10, 2023
1 parent 12e5e96 commit 89ec230
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 79 deletions.
37 changes: 15 additions & 22 deletions modules/dataverse-spi-export-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,42 @@
<groupId>edu.harvard.iq</groupId>
<artifactId>dataverse-parent</artifactId>
<version>${revision}</version>
<relativePath>../../modules/dataverse-parent</relativePath>
<relativePath>../dataverse-parent</relativePath>
</parent>

<artifactId>dataverse-example-exporter</artifactId>
<version>0.0.1</version>
<groupId>io.gdcc</groupId>
<artifactId>dataverse-spi-export-examples</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>edu.harvard.iq</groupId>
<artifactId>dataverse-exporter-spi</artifactId>
<version>0.0.1</version>
<groupId>io.gdcc</groupId>
<artifactId>dataverse-spi</artifactId>
<version>1.0.0</version>
</dependency>
<!--
https://mvnrepository.com/artifact/com.google.auto.service/auto-service -->
<dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0.1</version>
</dependency>

<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>
jakarta.jakartaee-api</artifactId>
<version>${jakartaee-api.version}</version>
<scope>
provided</scope>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<scope>provided</scope>
<!-- no version here as managed by parent -->
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>
jakarta.json</artifactId>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
<!-- no version here as managed by Payara BOM above! -->
<!-- no version here as managed by parent -->
</dependency>
</dependencies>
<build>
<sourceDirectory>
src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.OutputStream;
import java.util.Locale;
import javax.ws.rs.core.MediaType;

public interface Exporter {

Expand All @@ -16,7 +15,7 @@ public interface Exporter {

void exportDataset(ExportDataProvider dataProvider, OutputStream outputStream) throws ExportException;

String getProviderName();
String getFormatName();

String getDisplayName(Locale locale);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DCTermsExporter implements XMLExporter {


@Override
public String getProviderName() {
public String getFormatName() {
return "dcterms";
}

Expand All @@ -39,7 +39,7 @@ public void exportDataset(ExportDataProvider dataProvider, OutputStream outputSt
try {
DublinCoreExportUtil.datasetJson2dublincore(dataProvider.getDatasetJson(), outputStream, DublinCoreExportUtil.DC_FLAVOR_DCTERMS);
} catch (XMLStreamException xse) {
throw new ExportException("Caught XMLStreamException performing DCTERMS export");
throw new ExportException("Caught XMLStreamException performing DCTERMS export", xse);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DDIExporter implements XMLExporter {
public static final String PROVIDER_NAME = "ddi";

@Override
public String getProviderName() {
public String getFormatName() {
return PROVIDER_NAME;
}

Expand All @@ -51,7 +51,7 @@ public void exportDataset(ExportDataProvider dataProvider, OutputStream outputSt
DdiExportUtil.datasetJson2ddi(dataProvider.getDatasetJson(), dataProvider.getDatasetFileDetails(),
outputStream);
} catch (XMLStreamException xse) {
throw new ExportException("Caught XMLStreamException performing DDI export");
throw new ExportException("Caught XMLStreamException performing DDI export", xse);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import edu.harvard.iq.dataverse.util.BundleUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

/**
Expand All @@ -27,7 +27,7 @@ public class DataCiteExporter implements XMLExporter {
public static final String NAME = "Datacite";

@Override
public String getProviderName() {
public String getFormatName() {
return NAME;
}

Expand All @@ -41,9 +41,9 @@ public String getDisplayName(Locale locale) {
public void exportDataset(ExportDataProvider dataProvider, OutputStream outputStream) throws ExportException {
try {
String xml = dataProvider.getDataCiteXml();
outputStream.write(xml.getBytes(Charset.forName("utf-8")));
outputStream.write(xml.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new ExportException("Caught IOException performing DataCite export");
throw new ExportException("Caught IOException performing DataCite export", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class DublinCoreExporter implements XMLExporter {

@Override
public String getProviderName() {
public String getFormatName() {
return "oai_dc";
}

Expand All @@ -41,7 +41,7 @@ public void exportDataset(ExportDataProvider dataProvider, OutputStream outputSt
DublinCoreExportUtil.datasetJson2dublincore(dataProvider.getDatasetJson(), outputStream,
DublinCoreExportUtil.DC_FLAVOR_OAI);
} catch (XMLStreamException xse) {
throw new ExportException("Caught XMLStreamException performing DC export");
throw new ExportException("Caught XMLStreamException performing DC export", xse);
}
}

Expand Down
56 changes: 30 additions & 26 deletions src/main/java/edu/harvard/iq/dataverse/export/ExportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
Expand All @@ -66,17 +67,21 @@ private ExportService() {
* Step 1 - find the EXPORTERS dir and add all jar files there to a class loader
*/
List<URL> jarUrls = new ArrayList<URL>();
Path exporterDir = Paths.get(JvmSettings.EXPORTERS_DIRECTORY.lookup());
try (DirectoryStream<Path> stream = Files.newDirectoryStream(exporterDir)) {
for (Path path : stream) {
if (!Files.isDirectory(path)) {
logger.fine("Adding " + path.toUri().toURL());
//This is the syntax required to indicate a jar file from which classes should be loaded (versus a class file).
Optional<String> exportPathSetting = JvmSettings.EXPORTERS_DIRECTORY.lookupOptional(String.class);
if (exportPathSetting.isPresent()) {
Path exporterDir = Paths.get(exportPathSetting.get());
// Get all JAR files from the configured directory
try (DirectoryStream<Path> stream = Files.newDirectoryStream(exporterDir, "*.jar")) {
// Using the foreach loop here to enable catching the URI/URL exceptions
for (Path path : stream) {
logger.log(Level.FINE, "Adding {0}", path.toUri().toURL());
// This is the syntax required to indicate a jar file from which classes should
// be loaded (versus a class file).
jarUrls.add(new URL("jar:" + path.toUri().toURL() + "!/"));
}
} catch (IOException e) {
logger.warning("Problem accessing external Exporters: " + e.getLocalizedMessage());
}
} catch (IOException e) {
logger.warning("Problem accessing external Exporters: " + e.getLocalizedMessage());
}
URLClassLoader cl = URLClassLoader.newInstance(jarUrls.toArray(new URL[0]), this.getClass().getClassLoader());

Expand All @@ -93,12 +98,12 @@ private ExportService() {
* providerName may be processed before or after external ones.
*/
loader.forEach(exp -> {
String formatName = exp.getProviderName();
String formatName = exp.getFormatName();
// If no entry for this providerName yet or if it is an external exporter
if (!exporterMap.containsKey(formatName) || exp.getClass().getClassLoader().equals(cl)) {
exporterMap.put(formatName, exp);
}
logger.fine("SL: " + exp.getProviderName() + " from " + exp.getClass().getCanonicalName()
logger.log(Level.FINE, "SL: " + exp.getFormatName() + " from " + exp.getClass().getCanonicalName()
+ " and classloader: " + exp.getClass().getClassLoader().getClass().getCanonicalName());
});
}
Expand All @@ -116,7 +121,7 @@ public List<String[]> getExportersLabels() {
exporterMap.values().forEach(exp -> {
String[] temp = new String[2];
temp[0] = exp.getDisplayName(BundleUtil.getCurrentLocale());
temp[1] = exp.getProviderName();
temp[1] = exp.getFormatName();
retList.add(temp);
});
return retList;
Expand Down Expand Up @@ -248,10 +253,10 @@ public void exportAllFormats(Dataset dataset) throws ExportException {
throw new ExportException("No released version for dataset " + dataset.getGlobalId().toString());
}
ExportDataProvider dataProvider = new InternalExportDataProvider(releasedVersion);

for (Exporter e : exporterMap.values()) {
String formatName = e.getProviderName();
String formatName = e.getFormatName();

cacheExport(dataset, dataProvider, formatName, e);
}
// Finally, if we have been able to successfully export in all available
Expand All @@ -272,7 +277,7 @@ public void clearAllCachedFormats(Dataset dataset) throws IOException {
try {

for (Exporter e : exporterMap.values()) {
String formatName = e.getProviderName();
String formatName = e.getFormatName();
clearCachedExport(dataset, formatName);
}

Expand All @@ -293,7 +298,8 @@ public void exportFormat(Dataset dataset, String formatName) throws ExportExcept
if (e != null) {
DatasetVersion releasedVersion = dataset.getReleasedVersion();
if (releasedVersion == null) {
throw new ExportException("No published version found during export. " + dataset.getGlobalId().toString());
throw new ExportException(
"No published version found during export. " + dataset.getGlobalId().toString());
}
InternalExportDataProvider dataProvider = new InternalExportDataProvider(releasedVersion);
cacheExport(dataset, dataProvider, formatName, e);
Expand All @@ -303,15 +309,13 @@ public void exportFormat(Dataset dataset, String formatName) throws ExportExcept
throw new ExportException("Exporter not found");
}
} catch (IllegalStateException e) {
// IllegalStateException can potentially mean very different, and
// IllegalStateException can potentially mean very different, and
// unexpected things. An exporter attempting to get a single primitive
// value from a fieldDTO that is in fact a Multiple and contains a
// value from a fieldDTO that is in fact a Multiple and contains a
// json vector (this has happened, for example, when the code in the
// DDI exporter was not updated following a metadata fieldtype change),
// DDI exporter was not updated following a metadata fieldtype change),
// will result in IllegalStateException.
throw new ExportException("IllegalStateException caught when exporting "
+ formatName
+ " for dataset "
throw new ExportException("IllegalStateException caught when exporting " + formatName + " for dataset "
+ dataset.getGlobalId().toString()
+ "; may or may not be due to a mismatch between an exporter code and a metadata block update. "
+ e.getMessage());
Expand All @@ -331,12 +335,12 @@ public Exporter getExporter(String formatName) throws ExportException {
// in a file in the dataset directory / container based on its DOI:
private void cacheExport(Dataset dataset, ExportDataProvider dataProvider, String format, Exporter exporter)
throws ExportException {
boolean tempFileUsed = false;
File tempFile = null;
OutputStream outputStream = null;

StorageIO<Dataset> storageIO = null;
try {
boolean tempFileUsed = false;
File tempFile = null;
StorageIO<Dataset> storageIO = null;

// With some storage drivers, we can open a WritableChannel, or OutputStream
// to directly write the generated metadata export that we want to cache;
// Some drivers (like Swift) do not support that, and will give us an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class HtmlCodeBookExporter implements Exporter {

@Override
public String getProviderName() {
public String getFormatName() {
return "html";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public JsonObject getDatasetORE() {

@Override
public String getDataCiteXml() {
// TODO Auto-generated method stub
return DOIDataCiteRegisterService.getMetadataFromDvObject(
dv.getDataset().getGlobalId().asString(), new DataCitation(dv).getDataCiteMetadata(), dv.getDataset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class JSONExporter implements Exporter {

@Override
public String getProviderName() {
public String getFormatName() {
return "dataverse_json";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class OAI_DDIExporter implements XMLExporter {

@Override
public String getProviderName() {
public String getFormatName() {
// TODO: Consider adding this "short form" to the "Export Metadata" dropdown in the GUI.
return "oai_ddi";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void exportDataset(ExportDataProvider dataProvider, OutputStream outputSt


@Override
public String getProviderName() {
public String getFormatName() {
return NAME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public OpenAireExporter() {
}

@Override
public String getProviderName() {
public String getFormatName() {
return "oai_datacite";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* and available as an export format.
* <p>
* Do not make any backward incompatible changes unless it's absolutely
* necessary and list them in the API Guide. The existing list is in the
* "Native API" section.
* necessary and list them in the API Guide. The existing list is in the "Native
* API" section.
* <p>
* {@link SchemaDotOrgExporterTest} has most of the tests but
* {@link DatasetVersionTest} has some as well. See
Expand Down Expand Up @@ -73,9 +73,8 @@ public class SchemaDotOrgExporter implements Exporter {
public static final String NAME = "schema.org";

@Override
public void exportDataset(ExportDataProvider dataProvider, OutputStream outputStream)
throws ExportException {
try {
public void exportDataset(ExportDataProvider dataProvider, OutputStream outputStream) throws ExportException {
try {
outputStream.write(dataProvider.getDatasetSchemaDotOrg().toString().getBytes("UTF8"));
} catch (IOException ex) {
logger.info("IOException calling outputStream.write: " + ex);
Expand All @@ -88,7 +87,7 @@ public void exportDataset(ExportDataProvider dataProvider, OutputStream outputSt
}

@Override
public String getProviderName() {
public String getFormatName() {
return NAME;
}

Expand All @@ -99,7 +98,8 @@ public String getDisplayName(Locale locale) {

@Override
public Boolean isHarvestable() {
// Defer harvesting because the current effort was estimated as a "2": https://github.com/IQSS/dataverse/issues/3700
// Defer harvesting because the current effort was estimated as a "2":
// https://github.com/IQSS/dataverse/issues/3700
return false;
}

Expand Down
Loading

0 comments on commit 89ec230

Please sign in to comment.