11package 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 ;
310import 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
518import com .marklogic .contentpump .ContentPump ;
619import com .marklogic .contentpump .utilities .OptionsFileUtil ;
720
821public 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}
0 commit comments