Skip to content

Commit 6d26deb

Browse files
committed
added work around for detecting MLCP errors
- work around is to set System.out to our own OutputStream before running MLCP
1 parent 359978d commit 6d26deb

File tree

3 files changed

+89
-12
lines changed

3 files changed

+89
-12
lines changed

marklogic-data-hub/src/main/java/com/marklogic/hub/DataHubContentPump.java

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package com.marklogic.hub;
22

3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.io.PrintStream;
310
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
13+
import java.util.regex.PatternSyntaxException;
14+
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
417

518
import com.marklogic.contentpump.ContentPump;
619
import com.marklogic.contentpump.utilities.OptionsFileUtil;
720

821
public class DataHubContentPump extends ContentPump {
22+
private final static Logger LOGGER = LoggerFactory.getLogger(DataHubContentPump.class);
923

1024
private String[] arguments;
1125

@@ -21,19 +35,80 @@ public DataHubContentPump(String[] arguments) {
2135
* Run the Content Pump.
2236
*
2337
* @return true if the content pump executed successfully, false otherwise.
38+
* @throws IOException
2439
*/
25-
public boolean execute() {
40+
public void execute() throws IOException {
2641
String[] expandedArgs = null;
27-
int rc = 1;
42+
43+
PrintStream sysout = System.out;
44+
PrintStream mlcpOutputStream = null;
45+
BufferedReader mlcpBufferedReader = null;
46+
File mlcpOutputFile = null;
2847
try {
48+
// redirect standard output
49+
mlcpOutputFile = File.createTempFile("mlcp", ".txt");
50+
mlcpOutputStream = new PrintStream(mlcpOutputFile);
51+
System.setOut(mlcpOutputStream);
52+
53+
// run mlcp
2954
expandedArgs = OptionsFileUtil.expandArguments(arguments);
30-
rc = runCommand(expandedArgs);
55+
runCommand(expandedArgs);
3156
} catch (Exception ex) {
3257
LOG.error("Error while expanding arguments", ex);
3358
System.err.println(ex.getMessage());
3459
System.err.println("Try 'mlcp help' for usage.");
60+
} finally {
61+
// close the mlcp output stream
62+
if (mlcpOutputStream != null) {
63+
mlcpOutputStream.close();
64+
}
65+
66+
// revert to the original standard output
67+
System.setOut(sysout);
3568
}
69+
70+
// read the mlcp output and get any error message
71+
StringBuilder errorMessage = new StringBuilder();
72+
try {
73+
String regex = "([^\\s]*) \\s (\\[ [^ \\] ]*? \\]) \\s ([^\\s]*) \\s ([^\\s]*) \\s - \\s (.*)";
74+
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
3675

37-
return rc == 0;
76+
mlcpBufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(mlcpOutputFile)));
77+
String line = null;
78+
while ((line = mlcpBufferedReader.readLine()) != null) {
79+
Matcher matcher = pattern.matcher(line);
80+
if (matcher.matches()) {
81+
String logLevel = matcher.groupCount() >= 3 ? matcher.group(3) : "";
82+
String message = matcher.groupCount() >= 5 ? matcher.group(5) : "";
83+
if (logLevel.toLowerCase().equals("error")) {
84+
if (errorMessage.length() > 0) {
85+
errorMessage.append("\r\n");
86+
}
87+
errorMessage.append(message);
88+
}
89+
}
90+
}
91+
} catch (PatternSyntaxException e) {
92+
LOGGER.error("Unexpected error", e);
93+
} catch (FileNotFoundException e) {
94+
LOGGER.error("Unexpected error", e);
95+
} catch (IOException e) {
96+
LOGGER.error("Unexpected error", e);
97+
} finally {
98+
if (mlcpBufferedReader != null) {
99+
try {
100+
mlcpBufferedReader.close();
101+
} catch (IOException e) {
102+
// intentionally empty
103+
}
104+
}
105+
106+
// delete the temporary file
107+
mlcpOutputFile.delete();
108+
}
109+
110+
if (errorMessage.length() > 0) {
111+
throw new IOException("Load data failed with:\r\n" + errorMessage.toString());
112+
}
38113
}
39114
}

marklogic-data-hub/src/main/java/com/marklogic/hub/Mlcp.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public void addSourceDirectory(String directoryPath, SourceOptions options) {
5252
sources.add(source);
5353
}
5454

55-
public boolean loadContent() {
56-
boolean success = true;
55+
public void loadContent() throws IOException {
5756
for (MlcpSource source : sources) {
5857
try {
5958
List<String> arguments = new ArrayList<>();
@@ -75,13 +74,11 @@ public boolean loadContent() {
7574
arguments.addAll(sourceArguments);
7675

7776
DataHubContentPump contentPump = new DataHubContentPump(arguments);
78-
success = success && contentPump.execute();
79-
}
80-
catch (Exception e) {
81-
LOGGER.error("Failed to load {}", source.getSourcePath(), e);
77+
contentPump.execute();
78+
} catch (IOException e) {
79+
throw new IOException("Cannot load data from: " + source.getSourcePath() + " due to: " + e.getMessage());
8280
}
8381
}
84-
return success;
8582
}
8683

8784
protected void setHadoopHomeDir() throws IOException {

quick-start/src/main/resources/static/top/topController.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@
6565
TaskManager.waitForTask(flow.inputFlowTaskId)
6666
.success(function (result) {
6767
if (!flow.inputFlowCancelled) {
68-
DataHub.displayMessage('Load data successful.', 'success', 'notification', false);
68+
if (result.success) {
69+
DataHub.displayMessage('Load data successful.', 'success', 'notification', false);
70+
}
71+
else {
72+
DataHub.displayMessage(result.errorMessage, 'error', 'notification', false);
73+
}
6974
}
7075
})
7176
.error(function () {

0 commit comments

Comments
 (0)