
fluximpl.MercurialTriggerImpl Maven / Gradle / Ivy
package fluximpl;
import flux.*;
import flux.dev.NotTriggeredException;
import fluximpl.audittrail.AuditTrailConstants;
import fluximpl.xml.five_zero_converter.ElementNames;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Set;
/**
* Implementation for MercurialTrigger. Uses "polling" based changeset consumption from Mercurial.
*/
public class MercurialTriggerImpl extends TriggerImpl implements MercurialTrigger {
public static final String TRIGGER_VARIABLE = "MERCURIAL_TRIGGER";
public static final String HG_CHANGESET = "HG_CHANGESET";
/**
* Hg CLI Command that returns the latest changeset.
*/
public static final String EXEC = "hg id -i -r tip ";
public MercurialTriggerImpl() {
super(new FlowChartImpl(), "Mercurial Trigger");
}
public MercurialTriggerImpl(FlowChartImpl flowChart, String s) {
super(flowChart, s);
}
public Set getHiddenVariableNames() {
// The returned raw type is a Set of String.
@SuppressWarnings("unchecked")
Set set = super.getHiddenVariableNames();
set.add(TRIGGER_VARIABLE);
return set;
}
public String getRepositoryUrl() {
return getVariable().getRepositoryUrl();
}
@Override
public void setRepositoryUrl(String url) {
MercurialTriggerVariable var = getVariable();
var.setRepositoryUrl(url);
setVariable(var);
}
public String getPath() {
return getVariable().getPath();
}
@Override
public void setPath(String path) {
MercurialTriggerVariable var = getVariable();
var.setPath(path);
setVariable(var);
}
public String getPollingDelay() {
return getVariable().getPollingDelay();
}
@Override
public void setPollingDelay(String timeExpression) {
MercurialTriggerVariable var = getVariable();
var.setPollingDelay(timeExpression);
setVariable(var);
}
@Override
public Date getNextPollingDate() {
try {
Factory fluxFactory = Factory.makeInstance();
EngineHelper engineHelper = fluxFactory.makeEngineHelper();
return engineHelper.applyTimeExpression(getPollingDelay(), null, null);
} // try
catch (EngineException e) {
// Should not occur.
} // catch
return null;
}
@Override
public Object execute(FlowContext flowContext) throws Exception {
MercurialTriggerResult result = new MercurialTriggerResult();
String command;
if (!StringUtil.isNullOrEmpty(getPath())) {
command = getPath() + EXEC + getRepositoryUrl();
} else {
command = EXEC + getRepositoryUrl();
}
String lastSeenChangeSet = (String) flowContext.getFlowChart().getVariableManager().get(HG_CHANGESET);
if (StringUtil.isNullOrEmpty(lastSeenChangeSet)) {
Process process = Runtime.getRuntime().exec(command);
byte[] buf = new byte[4096];
InputStream in = process.getInputStream();
int exitCode = -1;
try {
exitCode = process.waitFor();
}
catch (InterruptedException e) {
// ignore
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (int bytesRead = in.read(buf); bytesRead != -1; bytesRead = in.read(buf)) {
outputStream.write(buf, 0, bytesRead);
}
lastSeenChangeSet = outputStream.toString();
//replace newline character
lastSeenChangeSet = lastSeenChangeSet.replaceAll("\\n", "");
if (exitCode == 0 && !StringUtil.isNullOrEmpty(lastSeenChangeSet))
flowContext.getFlowChart().getVariableManager().put(HG_CHANGESET, lastSeenChangeSet);
try {
outputStream.close();
} catch (Exception e) {
// ignore
}
throw new NotTriggeredException();
} else {
Process process = Runtime.getRuntime().exec(command);
byte[] buf = new byte[4096];
InputStream in = process.getInputStream();
int exitCode = -1;
try {
exitCode = process.waitFor();
} catch (InterruptedException e) {
// ignore
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (int bytesRead = in.read(buf); bytesRead != -1; bytesRead = in.read(buf)) {
outputStream.write(buf, 0, bytesRead);
}
String latestChangeSet = outputStream.toString();
//replace newline character
latestChangeSet = latestChangeSet.replaceAll("\\n", "");
try {
outputStream.close();
} catch (Exception e) {
// ignore
}
if (exitCode == 0 && !StringUtil.isNullOrEmpty(latestChangeSet)) {
flowContext.getFlowChart().getVariableManager().put(HG_CHANGESET, latestChangeSet);
}
if (lastSeenChangeSet.equalsIgnoreCase(latestChangeSet)) {
throw new NotTriggeredException();
} else {
result.changeSet = latestChangeSet;
flowContext.sendToAuditTrail("flux.audittrail.server.ExecutingActionEvent", getDetails(result, flowContext.getActionName()));
return result;
}
}
}
public Object expedite(FlowContext flowContext) throws Exception, NotTriggeredException {
flowContext.getLogger().info("Expedite called for " + flowContext.getActionName());
return null;
}
/**
* This delegation is required for pre 7.11 trigger expedite implementation.
*/
public Object a(FlowContext flowContext) throws Exception, NotTriggeredException {
return expedite(flowContext);
}
@Override
public void verify() throws EngineException {
if (StringUtil.isNullOrEmpty(getRepositoryUrl())) {
throw new EngineException("Expected \"Repository URL\" to be non-null or non-empty, but it was null or empty.");
}
}
@Override
public Class getResultInfo() {
return MercurialTriggerResult.class;
}
public String getDetails(Object result, String actionName) {
MercurialTriggerResult mtr = (MercurialTriggerResult) result;
StringBuffer sb = new StringBuffer();
String name = AuditTrailConstants.RESULT;
String value = mtr.changeSet;
if (!StringUtil.isNullOrEmpty(value)) {
sb.append("<" + ElementNames.PROPERTY + ">");
sb.append("<" + ElementNames.NAME + ">");
sb.append("");
sb.append("" + ElementNames.NAME + ">");
sb.append("<" + ElementNames.VALUE + ">");
sb.append("");
sb.append("" + ElementNames.VALUE + ">");
sb.append("" + ElementNames.PROPERTY + ">");
}
return ElementNames.open(ElementNames.EVENT_MESSAGE_ENTRY)
+ ElementNames.open(ElementNames.EVENT_DESCRIPTION) + "Exiting " + actionName + " \"" + getName() + "\""
+ ElementNames.close(ElementNames.EVENT_DESCRIPTION)
+ ElementNames.open(ElementNames.MESSAGE_DETAIL)
+ ElementNames.open(ElementNames.DETAIL) + sb.toString()
+ ElementNames.close(ElementNames.DETAIL)
+ ElementNames.close(ElementNames.MESSAGE_DETAIL)
+ ElementNames.close(ElementNames.EVENT_MESSAGE_ENTRY);
}
private MercurialTriggerVariable getVariable() {
if (!getVariableManager().contains(TRIGGER_VARIABLE)) {
getVariableManager().put(TRIGGER_VARIABLE, new MercurialTriggerVariable());
}
return (MercurialTriggerVariable) getVariableManager().get(TRIGGER_VARIABLE);
}
private void setVariable(MercurialTriggerVariable variable) {
getVariableManager().put(TRIGGER_VARIABLE, variable);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy