This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MlcpProducer.java
81 lines (69 loc) · 2.63 KB
/
MlcpProducer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.marklogic.camel.component.mlcp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.impl.DefaultProducer;
import com.marklogic.contentpump.ContentPump;
import com.marklogic.contentpump.utilities.OptionsFileUtil;
/**
* This does the actual work of building a list of arguments and then invoking Content Pump with them.
* <p>
* Since the input to Content Pump is a file or a directory, this producer assumes that the consumer at the other end of
* the route is a Camel File component.
*/
public class MlcpProducer extends DefaultProducer {
private MlcpEndpoint endpoint;
public MlcpProducer(MlcpEndpoint endpoint) {
super(endpoint);
this.endpoint = endpoint;
}
@Override
public void process(Exchange exchange) throws Exception {
List<String> argList = buildListOfCommonArgs();
addQuerystringParamsToArgList(argList);
addInputFilePathToArgList(argList, exchange);
invokeContentPump(argList);
}
protected List<String> buildListOfCommonArgs() {
List<String> l = new ArrayList<String>();
l.add("IMPORT");
l.add("-host");
l.add(endpoint.getHost());
l.add("-port");
l.add(endpoint.getPort() + "");
return l;
}
protected void addQuerystringParamsToArgList(List<String> argList) {
Map<String, Object> params = endpoint.getMlcpParams();
for (String key : params.keySet()) {
argList.add("-" + key);
argList.add(params.get(key).toString());
}
}
protected void addInputFilePathToArgList(List<String> argList, Exchange exchange) {
Message msg = exchange.getIn();
argList.add("-input_file_path");
argList.add(msg.getHeader("CamelFilePath").toString());
}
protected void invokeContentPump(List<String> argList) throws Exception {
String[] args = argList.toArray(new String[] {});
String[] expandedArgs = OptionsFileUtil.expandArguments(args);
if (log.isDebugEnabled()) {
debugContentPumpArgs(expandedArgs);
}
ContentPump.runCommand(expandedArgs);
}
protected void debugContentPumpArgs(String[] args) {
List<String> argList = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
if ("-password".equals(args[i]) && (i + 1 <= args.length)) {
i++;
continue;
}
argList.add(args[i]);
}
log.debug("Invoking Content Pump with args (not showing password): " + argList);
}
}