-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quick Start Input flow and synching #37
Merged
paxtonhare
merged 15 commits into
Marklogic-retired:master
from
maeisabelle:Quick-Start-InputFlow
Feb 17, 2016
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4c8a799
Simplified display of flow directories by using abn_tree and using a …
maeisabelle08 e0aabbe
Added validation in Flow creation
maeisabelle08 d040102
Handling domain form validation and additional handling of flow form …
maeisabelle08 aabc6cd
fixed runtime exceptions
gmarintes f050a03
added initial implementation of MLCP client and MLCP transform
gmarintes 7c38894
changes for sync status update
gmarintes 135c21d
added initial support for running an input flow via MLCP
gmarintes 10d9796
Minor change to change the background of success message to dark green
maeisabelle08 aafa4af
Merge branch 'master' into Quick-Start-InputFlow
gmarintes 36014fc
Removed "required" form validation for input flow name and conform fl…
maeisabelle08 add1412
Form validation - Either supply the input or the conform flow name
maeisabelle08 c32576e
Use Scaffolding in creating a new domain and flow
maeisabelle08 57c1fff
mlcp path and connection parameters are now taken from EnvironmentCon…
gmarintes 806ea04
Updated source input path based on the user plugin directory and corr…
maeisabelle08 a9ae561
adjustments to make flows work
gmarintes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Copyright 2012-2016 MarkLogic Corporation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.marklogic.hub; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.marklogic.hub.util.IOUtil; | ||
import com.marklogic.hub.util.IOUtil.LogLevel; | ||
|
||
public class Mlcp { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(Mlcp.class); | ||
|
||
private List<MlcpSource> sources = new ArrayList<>(); | ||
|
||
private String mlcpPath; | ||
|
||
private String host; | ||
|
||
private String port; | ||
|
||
private String user; | ||
|
||
private String password; | ||
|
||
public Mlcp(String mlcpHome, String host, String port, String user, String password) { | ||
this.host = host; | ||
this.port = port; | ||
this.user = user; | ||
this.password = password; | ||
|
||
// set the mlcp executable path based on OS | ||
this.mlcpPath = mlcpHome; | ||
String osName = System.getProperty("os.name"); | ||
if (osName != null && osName.toLowerCase().startsWith("windows")) { | ||
mlcpPath += "/bin/mlcp.bat"; | ||
} | ||
else { | ||
mlcpPath += "/bin/mlcp.sh"; | ||
} | ||
} | ||
|
||
public void addSourceDirectory(String directoryPath, SourceOptions options) { | ||
MlcpSource source = new MlcpSource(directoryPath, options); | ||
sources.add(source); | ||
} | ||
|
||
public void loadContent() { | ||
for (MlcpSource source : sources) { | ||
Thread inputThread = null; | ||
Thread errorThread = null; | ||
try { | ||
List<String> arguments = new ArrayList<>(); | ||
|
||
arguments.add(mlcpPath); | ||
arguments.add("import"); | ||
arguments.add("-mode"); | ||
arguments.add("local"); | ||
arguments.add("-host"); | ||
arguments.add(host); | ||
arguments.add("-port"); | ||
arguments.add(port); | ||
arguments.add("-username"); | ||
arguments.add(user); | ||
arguments.add("-password"); | ||
arguments.add(password); | ||
|
||
// add arguments related to the source | ||
List<String> sourceArguments = source.getMlcpArguments(); | ||
arguments.addAll(sourceArguments); | ||
|
||
ProcessBuilder pb = new ProcessBuilder(arguments.toArray(new String[0])); | ||
Process process = pb.start(); | ||
|
||
inputThread = IOUtil.createInputStreamSink(process.getInputStream(), LOGGER, LogLevel.DEBUG); | ||
errorThread = IOUtil.createInputStreamSink(process.getErrorStream(), LOGGER, LogLevel.ERROR); | ||
|
||
inputThread.start(); | ||
errorThread.start(); | ||
|
||
process.waitFor(); | ||
} | ||
catch (Exception e) { | ||
LOGGER.error("Failed to load {}", source.getSourcePath(), e); | ||
} | ||
finally { | ||
if (inputThread != null) { | ||
inputThread.interrupt(); | ||
} | ||
if (errorThread != null) { | ||
errorThread.interrupt(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static class MlcpSource { | ||
private String sourcePath; | ||
private SourceOptions sourceOptions; | ||
|
||
public MlcpSource(String sourcePath, SourceOptions sourceOptions) { | ||
this.sourcePath = sourcePath; | ||
this.sourceOptions = sourceOptions; | ||
} | ||
|
||
public String getSourcePath() { | ||
return sourcePath; | ||
} | ||
|
||
public List<String> getMlcpArguments() throws IOException { | ||
File file = new File(sourcePath); | ||
String canonicalPath = file.getCanonicalPath(); | ||
|
||
List<String> arguments = new ArrayList<>(); | ||
arguments.add("-input_file_path"); | ||
arguments.add(canonicalPath); | ||
arguments.add("-input_file_type"); | ||
if (sourceOptions.getInputFileType() == null) { | ||
arguments.add("documents"); | ||
} | ||
else { | ||
arguments.add(sourceOptions.getInputFileType()); | ||
} | ||
|
||
if (sourceOptions.getInputFilePattern() != null) { | ||
arguments.add("-input_file_pattern"); | ||
arguments.add(sourceOptions.getInputFilePattern()); | ||
} | ||
|
||
// by default, cut the source directory path to make URIs shorter | ||
String uriReplace = "/" + canonicalPath + ",''"; | ||
uriReplace = uriReplace.replaceAll("\\\\", "/"); | ||
|
||
arguments.add("-output_uri_replace"); | ||
arguments.add(uriReplace); | ||
|
||
arguments.add("-transform_module"); | ||
arguments.add("/com.marklogic.hub/mlcp-flow-transform.xqy"); | ||
arguments.add("-transform_namespace"); | ||
arguments.add("http://marklogic.com/hub-in-a-box/mlcp-flow-transform"); | ||
arguments.add("-transform_param"); | ||
arguments.add("\"" + sourceOptions.getTransformParams() + "\""); | ||
|
||
return arguments; | ||
} | ||
} | ||
|
||
public static class SourceOptions { | ||
private String domainName; | ||
private String flowName; | ||
private String flowType; | ||
private String inputFileType; | ||
private String inputFilePattern; | ||
|
||
public SourceOptions(String domainName, String flowName, String flowType) { | ||
this.domainName = domainName; | ||
this.flowName = flowName; | ||
this.flowType = flowType; | ||
} | ||
|
||
public String getDomainName() { | ||
return domainName; | ||
} | ||
|
||
public String getFlowName() { | ||
return flowName; | ||
} | ||
|
||
public String getFlowType() { | ||
return flowType; | ||
} | ||
|
||
public String getInputFileType() { | ||
return inputFileType; | ||
} | ||
|
||
public void setInputFileType(String inputFileType) { | ||
this.inputFileType = inputFileType; | ||
} | ||
|
||
public String getInputFilePattern() { | ||
return inputFilePattern; | ||
} | ||
|
||
public void setInputFilePattern(String inputFilePattern) { | ||
this.inputFilePattern = inputFilePattern; | ||
} | ||
|
||
protected String getTransformParams() { | ||
return String.format("<params><domain-name>%s</domain-name><flow-name>%s</flow-name><flow-type>%s</flow-type></params>", domainName, flowName, flowType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2012-2016 MarkLogic Corporation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.marklogic.hub.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class IOUtil { | ||
|
||
public static Thread createInputStreamSink(InputStream inputStream) { | ||
return IOUtil.createInputStreamSink(inputStream, null, LogLevel.DEBUG); | ||
} | ||
|
||
public static Thread createInputStreamSink(InputStream inputStream, Logger logger, LogLevel logLevel) { | ||
return new InputStreamSinkThread(inputStream, logger, logLevel); | ||
} | ||
|
||
public static enum LogLevel { | ||
WARN | ||
,INFO | ||
,DEBUG | ||
,ERROR | ||
} | ||
|
||
private static class InputStreamSinkThread extends Thread { | ||
|
||
private InputStream inputStream; | ||
private Logger logger; | ||
private LogLevel logLevel; | ||
|
||
public InputStreamSinkThread(InputStream inputStream, Logger logger, LogLevel logLevel) { | ||
super("InputStreamSinkThread(" + inputStream + ")"); | ||
|
||
this.inputStream = inputStream; | ||
this.logger = logger; | ||
this.logLevel = logLevel; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); | ||
String line = null; | ||
try { | ||
while ((line = br.readLine()) != null) { | ||
if (logger != null) { | ||
if (logLevel == LogLevel.DEBUG) { | ||
logger.debug(line); | ||
} | ||
else if (logLevel == LogLevel.ERROR) { | ||
logger.error(line); | ||
} | ||
else if (logLevel == LogLevel.WARN) { | ||
logger.error(line); | ||
} | ||
else if (logLevel == LogLevel.INFO) { | ||
logger.info(line); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
if (logger != null) { | ||
logger.error("Error encountered while reading stream", e); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than this fn:empty check and the one above on line 408, I'd rather you check in the caller:
line 396:
Note the change: