Skip to content

Commit

Permalink
Revert "close the dispatcher in the main method after running the las…
Browse files Browse the repository at this point in the history
…t check"

This reverts commit a51a9a3.
  • Loading branch information
jeanetteclark committed Oct 13, 2023
1 parent acf0062 commit cc42268
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 74 deletions.
116 changes: 43 additions & 73 deletions src/main/java/edu/ucsb/nceas/mdqengine/MDQEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.ucsb.nceas.mdqengine;

import edu.ucsb.nceas.mdqengine.dispatch.Dispatcher;
import edu.ucsb.nceas.mdqengine.dispatch.MDQCache;
import edu.ucsb.nceas.mdqengine.exception.MetadigException;
import edu.ucsb.nceas.mdqengine.model.*;
Expand Down Expand Up @@ -35,10 +34,9 @@
import java.util.concurrent.Future;

public class MDQEngine {

// private static final String RESOLVE_PREFIX =
// getConfiguration().getString("D1Client.CN_URL") + "/v2/resolve/";


//private static final String RESOLVE_PREFIX = getConfiguration().getString("D1Client.CN_URL") + "/v2/resolve/";

/**
* Default store uses the in-memory implementation
*/
Expand All @@ -47,17 +45,15 @@ public class MDQEngine {

protected Log log = LogFactory.getLog(this.getClass());
private static String metadigDataDir = null;

public MDQEngine() throws MetadigException, IOException, ConfigurationException {
store = new InMemoryStore();
// store = new MNStore();
MDQconfig cfg = new MDQconfig();
//store = new MNStore();
MDQconfig cfg = new MDQconfig ();
metadigDataDir = cfg.getString("metadig.data.dir");
MDQCache.initialize(null);
}

private Dispatcher dispatcher;

/**
* Executes the given suite for a given object
* @param suite
Expand Down Expand Up @@ -102,11 +98,8 @@ public Run runSuite(Suite suite, InputStream input, Map<String, Object> params,
run.setTimestamp(Calendar.getInstance().getTime());
List<Result> results = new ArrayList<Result>();

List<Check> check_list = suite.getCheck();
int lastIndex = check_list.size() - 1;
// run the checks in the suite to get results
for (int i = 0; i < check_list.size(); i++) {
Check check = check_list.get(i);
for (Check check: suite.getCheck()) {
// is this a reference to existing check?
if (check.getCode() == null && check.getId() != null) {
// then load it
Expand All @@ -130,18 +123,8 @@ public Run runSuite(Suite suite, InputStream input, Map<String, Object> params,
if(origCheck.getLevel() != null) check.setLevel(origCheck.getLevel());
if(origCheck.getType() != null) check.setType(origCheck.getType());
}
dispatcher = Dispatcher.getDispatcher(check.getEnvironment());
Result result = xml.runCheck(check, dispatcher);
Result result = xml.runCheck(check);
results.add(result);
// if its the last check, need to make sure to shut down the dispatcher
if (i == lastIndex){
try {
dispatcher.close();
dispatcher = null;
} catch (Exception e){
log.error("THE CLOSE EXCEPTION" + e);
}
}
}
run.setResult(results);

Expand All @@ -153,12 +136,11 @@ public Run runSuite(Suite suite, InputStream input, Map<String, Object> params,
return run;

}

/**
* Executes the given check for a given object
*
* @param check
* @param input the InputStream for the object to QC
* @param input the InputStream for the object to QC
* @param params optional additional parameters to make available for the check
* @return the Run results for this execution
* @throws MalformedURLException
Expand All @@ -168,53 +150,51 @@ public Run runSuite(Suite suite, InputStream input, Map<String, Object> params,
* @throws XPathExpressionException
* @throws ScriptException
*/
public Run runCheck(Check check, InputStream input, Map<String, Object> params, SystemMetadata sysMeta)
throws MalformedURLException, IOException, SAXException,
public Run runCheck(Check check, InputStream input, Map<String, Object> params, SystemMetadata sysMeta)
throws MalformedURLException, IOException, SAXException,
ParserConfigurationException, XPathExpressionException, ScriptException {


String content = IOUtils.toString(input, "UTF-8");
String metadataContent = content;

XMLDialect xml = new XMLDialect(IOUtils.toInputStream(metadataContent, "UTF-8"));
xml.setParams(params);
xml.setSystemMetadata(sysMeta);
Path tempDir = Files.createTempDirectory("mdq_run");
xml.setDirectory(tempDir.toFile().getAbsolutePath());

// make a run to capture results
Run run = new Run();
run.setId(UUID.randomUUID().toString());
run.setTimestamp(Calendar.getInstance().getTime());
List<Result> results = new ArrayList<Result>();

// run the check to get results
Result result = xml.runCheck(check, dispatcher);
Result result = xml.runCheck(check);
results.add(result);
run.setResult(results);

log.trace("Run results: " + JsonMarshaller.toJson(run));

// clean up
tempDir.toFile().delete();

return run;

}

/**
* To enable checks-by-id-reference, set the store so that checks can be
* retrieved

/**
* To enable checks-by-id-reference, set the store so that checks can be retrieved
* if not specified inline
*
* @param store The storage implementation to use for retrieving existing checks
*/
public void setStore(MDQStore store) {
this.store = store;
}

/**
* Run a suite on a given metadata document. Prints Run XML results.
*
* @param args first is the suite file path, second is the metadata file path
*
*/
Expand All @@ -226,32 +206,27 @@ public static void main(String args[]) {
try {
engine = new MDQEngine();
String xml = IOUtils.toString(new FileInputStream(args[0]), "UTF-8");
Suite suite = (Suite) XmlMarshaller.fromXml(xml, Suite.class);
Suite suite = (Suite) XmlMarshaller.fromXml(xml , Suite.class);
InputStream input = new FileInputStream(args[1]);
InputStream sysmetaInputStream = null;
SystemMetadata sysmeta = null;
Object tmpSysmeta = null;

// Read in the system metadata XML file if it is provided. Suites can be run
// without it.
// The SystemMetadata can be either version 1 or 2. The current type marshaller
// cannot handle version 1,
// so we have to convert v1 to v2 (seems like the marshalling call should do
// this for us).
// THe drawback to this approach is that it will be necessary to test for
// sysmeta v3 when it is released.
if (args.length >= 3) {
Class smClasses[] = { org.dataone.service.types.v2.SystemMetadata.class,
org.dataone.service.types.v1.SystemMetadata.class };
for (Class thisClass : smClasses) {
// Read in the system metadata XML file if it is provided. Suites can be run without it.
// The SystemMetadata can be either version 1 or 2. The current type marshaller cannot handle version 1,
// so we have to convert v1 to v2 (seems like the marshalling call should do this for us).
// THe drawback to this approach is that it will be necessary to test for sysmeta v3 when it is released.
if(args.length >= 3) {
Class smClasses[] = {org.dataone.service.types.v2.SystemMetadata.class, org.dataone.service.types.v1.SystemMetadata.class};
for (Class thisClass: smClasses) {
sysmetaInputStream = new FileInputStream(args[2]);
try {
tmpSysmeta = TypeMarshaller.unmarshalTypeFromStream(thisClass, sysmetaInputStream);
// Didn't get an error so proceed to convert to sysmeta v2, if needed.
break;
} catch (ClassCastException cce) {
cce.printStackTrace();
continue;
continue;
} catch (InstantiationException | IllegalAccessException | IOException | MarshallingException fis) {
fis.printStackTrace();
continue;
Expand All @@ -273,27 +248,23 @@ public static void main(String args[]) {
run.setRunStatus("SUCCESS");

// Add DataONE sysmeta, if it was provided.
if (sysmeta != null) {
if(sysmeta != null) {
SysmetaModel smm = new SysmetaModel();
// These sysmeta fields are always provided
smm.setOriginMemberNode(sysmeta.getOriginMemberNode().getValue());
smm.setRightsHolder(sysmeta.getRightsHolder().getValue());
smm.setDateUploaded(sysmeta.getDateUploaded());
smm.setFormatId(sysmeta.getFormatId().getValue());
// These fields aren't required.
if (sysmeta.getObsoletes() != null)
smm.setObsoletes(sysmeta.getObsoletes().getValue());
if (sysmeta.getObsoletedBy() != null)
smm.setObsoletedBy(sysmeta.getObsoletedBy().getValue());
if (sysmeta.getSeriesId() != null)
smm.setSeriesId(sysmeta.getSeriesId().getValue());
if (sysmeta.getObsoletes() != null) smm.setObsoletes(sysmeta.getObsoletes().getValue());
if (sysmeta.getObsoletedBy() != null) smm.setObsoletedBy(sysmeta.getObsoletedBy().getValue());
if (sysmeta.getSeriesId() != null) smm.setSeriesId(sysmeta.getSeriesId().getValue());

// Now make the call to DataONE to get the group information for this
// rightsHolder.
// Now make the call to DataONE to get the group information for this rightsHolder.
// Only wait for a certain amount of time before we will give up.
ExecutorService executorService = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newSingleThreadExecutor();

// Provide the rightsHolder to the DataONE group lookup.
// Provide the rightsHolder to the DataONE group lookup.
GroupLookupCheck glc = new GroupLookupCheck();
glc.setRightsHolder(sysmeta.getRightsHolder().getValue());
Future<List<String>> future = executorService.submit(glc);
Expand All @@ -307,13 +278,12 @@ public static void main(String args[]) {
}
// Sleep for 1 second

if (groups != null)
break;
System.out.println("Waiting 1 second for groups");
if(groups != null) break;
System.out.println("Waiting 1 second for groups");
Thread.sleep(1000);
}

if (groups != null) {
if(groups != null) {
System.out.println("Setting groups");
smm.setGroups(groups);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class XMLDialect {

private String directory;

private Dispatcher dispatcher;

public static Log log = LogFactory.getLog(XMLDialect.class);

public XMLDialect(InputStream input) throws SAXException, IOException, ParserConfigurationException {
Expand Down Expand Up @@ -116,7 +118,7 @@ public void mergeNamespaces(List<Namespace> namespaces) {
}
}

public Result runCheck(Check check, Dispatcher dispatcher) throws XPathExpressionException {
public Result runCheck(Check check) throws XPathExpressionException {

Result result = null;

Expand Down

0 comments on commit cc42268

Please sign in to comment.